Skip to content

Commit

Permalink
Merge pull request arendst#4466 from ascillato/patch-1
Browse files Browse the repository at this point in the history
Add Command CalcRes
  • Loading branch information
arendst authored Nov 27, 2018
2 parents a92752c + b289cca commit 9e407f0
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
5 changes: 4 additions & 1 deletion sonoff/_changelog.ino
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* 6.3.0.13 20181126
/* 6.3.0.14 20181127
* Add Command CalcRes to set number of decimals (0 - 7) used in commands ADD, SUB, MULT and SCALE (#4420)
*
* 6.3.0.13 20181126
* Add command SetOption59 0/1 to change state topic from tele/STATE to stat/RESULT (#4450)
* Fix WifiManager functionality on initial installation (#4433)
*
Expand Down
1 change: 1 addition & 0 deletions sonoff/my_user_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
#define SWITCH_DEBOUNCE_TIME 50 // [SwitchDebounce] Number of mSeconds switch press debounce time
#define SWITCH_MODE TOGGLE // [SwitchMode] TOGGLE, FOLLOW, FOLLOW_INV, PUSHBUTTON, PUSHBUTTON_INV, PUSHBUTTONHOLD, PUSHBUTTONHOLD_INV, PUSHBUTTON_TOGGLE (the wall switch state)
#define WS2812_LEDS 30 // [Pixels] Number of WS2812 LEDs to start with (max is 512)
#define CALC_RESOLUTION 3 // [CalcRes] Maximum number of decimals (0 - 7) used in commands ADD, SUB, MULT and SCALE

#define TEMP_CONVERSION 0 // [SetOption8] Return temperature in (0 = Celsius or 1 = Fahrenheit)
#define PRESSURE_CONVERSION 0 // [SetOption24] Return pressure in (0 = hPa or 1 = mmHg)
Expand Down
4 changes: 1 addition & 3 deletions sonoff/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ typedef union {
uint32_t spare03 : 1;
uint32_t spare04 : 1;
uint32_t spare05 : 1;
uint32_t spare06 : 1;
uint32_t spare07 : 1;
uint32_t spare08 : 1;
uint32_t calc_resolution : 3;
uint32_t weight_resolution : 2;
uint32_t frequency_resolution : 2;
uint32_t axis_resolution : 2;
Expand Down
4 changes: 4 additions & 0 deletions sonoff/settings.ino
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ void SettingsDefaultSet2(void)
// Settings.rule_enabled = 0;
// Settings.rule_once = 0;
// for (byte i = 1; i < MAX_RULE_SETS; i++) { Settings.rules[i][0] = '\0'; }
Settings.flag2.calc_resolution = CALC_RESOLUTION;

// Home Assistant
Settings.flag.hass_discovery = HOME_ASSISTANT_DISCOVERY_ENABLE;
Expand Down Expand Up @@ -857,6 +858,9 @@ void SettingsDelta(void)
if (Settings.version < 0x0603000A) {
Settings.param[P_LOOP_SLEEP_DELAY] = LOOP_SLEEP_DELAY;
}
if (Settings.version < 0x0603000E) {
Settings.flag2.calc_resolution = CALC_RESOLUTION;
}

Settings.version = VERSION;
SettingsSave(1);
Expand Down
2 changes: 1 addition & 1 deletion sonoff/sonoff_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#ifndef _SONOFF_VERSION_H_
#define _SONOFF_VERSION_H_

#define VERSION 0x0603000D
#define VERSION 0x0603000E

#define D_PROGRAMNAME "Sonoff-Tasmota"
#define D_AUTHOR "Theo Arends"
Expand Down
19 changes: 13 additions & 6 deletions sonoff/xdrv_10_rules.ino
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@
#define D_CMND_SUB "Sub"
#define D_CMND_MULT "Mult"
#define D_CMND_SCALE "Scale"
#define D_CMND_CALC_RESOLUTION "CalcRes"

#define D_JSON_INITIATED "Initiated"

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 ;
enum RulesCommands { CMND_RULE, CMND_RULETIMER, CMND_EVENT, CMND_VAR, CMND_MEM, CMND_ADD, CMND_SUB, CMND_MULT, CMND_SCALE, CMND_CALC_RESOLUTION };
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 "|" D_CMND_CALC_RESOLUTION ;

String rules_event_value;
unsigned long rules_timer[MAX_RULE_TIMERS] = { 0 };
Expand Down Expand Up @@ -593,24 +594,30 @@ boolean RulesCommand(void)
}
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, Settings.mems[index -1]);
}
else if (CMND_CALC_RESOLUTION == command_code) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 7)) {
Settings.flag2.calc_resolution = XdrvMailbox.payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.flag2.calc_resolution);
}
else if ((CMND_ADD == command_code) && (index > 0) && (index <= MAX_RULE_VARS)) {
if (XdrvMailbox.data_len > 0) {
double tempvar = CharToDouble(vars[index -1]) + CharToDouble(XdrvMailbox.data);
dtostrfd(tempvar, 2, vars[index -1]);
dtostrfd(tempvar, Settings.flag2.calc_resolution, vars[index -1]);
}
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 <= MAX_RULE_VARS)) {
if (XdrvMailbox.data_len > 0) {
double tempvar = CharToDouble(vars[index -1]) - CharToDouble(XdrvMailbox.data);
dtostrfd(tempvar, 2, vars[index -1]);
dtostrfd(tempvar, Settings.flag2.calc_resolution, vars[index -1]);
}
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 <= MAX_RULE_VARS)) {
if (XdrvMailbox.data_len > 0) {
double tempvar = CharToDouble(vars[index -1]) * CharToDouble(XdrvMailbox.data);
dtostrfd(tempvar, 2, vars[index -1]);
dtostrfd(tempvar, Settings.flag2.calc_resolution, vars[index -1]);
}
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, vars[index -1]);
}
Expand All @@ -625,7 +632,7 @@ boolean RulesCommand(void)
double toLow = CharToDouble(subStr(sub_string, XdrvMailbox.data, ",", 4));
double toHigh = CharToDouble(subStr(sub_string, XdrvMailbox.data, ",", 5));
double value = map_double(valueIN, fromLow, fromHigh, toLow, toHigh);
dtostrfd(value, 2, vars[index -1]);
dtostrfd(value, Settings.flag2.calc_resolution, vars[index -1]);
}
}
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, vars[index -1]);
Expand Down

0 comments on commit 9e407f0

Please sign in to comment.