From f45dd53352563878c3911ee50be3bc0a89ccda5b Mon Sep 17 00:00:00 2001 From: Adrian Scillato <35405447+ascillato@users.noreply.github.com> Date: Sun, 10 Jun 2018 02:09:11 -0300 Subject: [PATCH] Add Arithmetic commands for VAR MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- sonoff/xdrv_10_rules.ino | 62 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/sonoff/xdrv_10_rules.ino b/sonoff/xdrv_10_rules.ino index 65e8df730..dd46e32d5 100644 --- a/sonoff/xdrv_10_rules.ino +++ b/sonoff/xdrv_10_rules.ino @@ -74,11 +74,15 @@ #define D_CMND_EVENT "Event" #define D_CMND_VAR "Var" #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" -enum RulesCommands { CMND_RULE, CMND_RULETIMER, CMND_EVENT, CMND_VAR, CMND_MEM }; -const char kRulesCommands[] PROGMEM = D_CMND_RULE "|" D_CMND_RULETIMER "|" D_CMND_EVENT "|" D_CMND_VAR "|" D_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 "|" D_CMND_ADD "|" D_CMND_SUB "|" D_CMND_MULT "|" D_CMND_SCALE ; String rules_event_value; 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]); } + 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 return serviced;