Add Arithmetic commands for VAR

New Arithmetic commands to be used with VARs:

ADD1 to ADD5: Add a value to VAR[x]
Usage: ADD1 15
Result: VAR1 = VAR1 + 15

SUB1 to SUB5: Substract a value from VAR[x]
Usage: SUB1 15
Result: VAR1 = VAR1 - 15

MULT1 to MULT5: Multiply a value to VAR[x]
Usage: MULT1 15
Result: VAR1 = VAR1 * 15

SCALE1 to SCALE5: Scale a value from a low and high limit to another low and high limit and store it in VAR[x] (directly equivalent to MAP arduino command)

Syntax: 
SCALEx value, fromLow, fromHigh, toLow, toHigh

Parameters
value: the number to scale

fromLow: the lower bound of the value’s current range

fromHigh: the upper bound of the value’s current range

toLow: the lower bound of the value’s target range

toHigh: the upper bound of the value’s target range

(omitted values are taken as zero)

Usage: SCALE1 15, 0, 100, 0, 1000
Result: VAR1 = 150
This commit is contained in:
Adrian Scillato 2018-06-10 02:09:11 -03:00 committed by GitHub
parent 09df088e83
commit f45dd53352
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 60 additions and 2 deletions

View File

@ -74,11 +74,15 @@
#define D_CMND_EVENT "Event" #define D_CMND_EVENT "Event"
#define D_CMND_VAR "Var" #define D_CMND_VAR "Var"
#define D_CMND_MEM "Mem" #define D_CMND_MEM "Mem"
#define D_CMND_ADD "Add"
#define D_CMND_SUB "Sub"
#define D_CMND_MULT "Mult"
#define D_CMND_SCALE "Scale"
#define D_JSON_INITIATED "Initiated" #define D_JSON_INITIATED "Initiated"
enum RulesCommands { CMND_RULE, CMND_RULETIMER, CMND_EVENT, CMND_VAR, CMND_MEM }; enum RulesCommands { CMND_RULE, CMND_RULETIMER, CMND_EVENT, CMND_VAR, CMND_MEM, CMND_ADD, CMND_SUB, CMND_MULT, CMND_SCALE };
const char kRulesCommands[] PROGMEM = D_CMND_RULE "|" D_CMND_RULETIMER "|" D_CMND_EVENT "|" D_CMND_VAR "|" D_CMND_MEM ; const char kRulesCommands[] PROGMEM = D_CMND_RULE "|" D_CMND_RULETIMER "|" D_CMND_EVENT "|" D_CMND_VAR "|" D_CMND_MEM "|" D_CMND_ADD "|" D_CMND_SUB "|" D_CMND_MULT "|" D_CMND_SCALE ;
String rules_event_value; String rules_event_value;
unsigned long rules_timer[MAX_RULE_TIMERS] = { 0 }; unsigned long rules_timer[MAX_RULE_TIMERS] = { 0 };
@ -500,6 +504,60 @@ boolean RulesCommand()
} }
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, Settings.mems[index -1]); snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, Settings.mems[index -1]);
} }
else if ((CMND_ADD == command_code) && (index > 0) && (index <= RULES_MAX_VARS)) {
if ( (XdrvMailbox.data_len > 0) && (XdrvMailbox.payload >= 0) ){
int16_t tempvar = atol(vars[index -1]) + XdrvMailbox.payload;
snprintf_P(vars[index -1], sizeof(vars[index -1]), PSTR("%d"), tempvar );
}
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, vars[index -1]);
}
else if ((CMND_SUB == command_code) && (index > 0) && (index <= RULES_MAX_VARS)) {
if ( (XdrvMailbox.data_len > 0) && (XdrvMailbox.payload >= 0) ){
int16_t tempvar = atol(vars[index -1]) - XdrvMailbox.payload;
snprintf_P(vars[index -1], sizeof(vars[index -1]), PSTR("%d"), tempvar );
}
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, vars[index -1]);
}
else if ((CMND_MULT == command_code) && (index > 0) && (index <= RULES_MAX_VARS)) {
if ( (XdrvMailbox.data_len > 0) && (XdrvMailbox.payload >= 0) ){
int16_t tempvar = atol(vars[index -1]) * XdrvMailbox.payload;
snprintf_P(vars[index -1], sizeof(vars[index -1]), PSTR("%d"), tempvar );
}
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, vars[index -1]);
}
else if ((CMND_SCALE == command_code) && (index > 0) && (index <= RULES_MAX_VARS)) {
if ( XdrvMailbox.data_len > 0 ) {
if (strstr(XdrvMailbox.data, ",")) { // Process parameter entry
uint8_t tpos = 0; // Parameter index
int16_t value = 0;
int16_t valueIN = 0;
int16_t fromLow = 0;
int16_t fromHigh = 0;
int16_t toLow = 0;
int16_t toHigh = 0;
char *p = XdrvMailbox.data; // Parameters like "1, 2, 3, 4"
char *q = p; // Value entered flag
while (p && (tpos < 6)) {
if (p > q) { // Any value entered
if (1 == tpos) { valueIN = value; }
if (2 == tpos) { fromLow = value; }
if (3 == tpos) { fromHigh = value; }
if (4 == tpos) { toLow = value; }
if (5 == tpos) { toHigh = value; }
}
p = LTrim(p); // Skip spaces
if (tpos && (*p == ',')) { p++; } // Skip separator
p = LTrim(p); // Skip spaces
q = p; // Reset any value entered flag
value = strtol(p, &p, 10);
tpos++; // Next parameter
}
value = map(valueIN, fromLow, fromHigh, toLow, toHigh);
snprintf_P(vars[index -1], sizeof(vars[index -1]), PSTR("%d"), value );
}
}
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, vars[index -1]);
}
else serviced = false; // Unknown command else serviced = false; // Unknown command
return serviced; return serviced;