mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-25 11:46:31 +00:00
Merge pull request #3089 from ascillato/patch-2
Add decimal values support for commands ADD, SUB, MULT and SCALE
This commit is contained in:
commit
598c76bc9e
@ -557,55 +557,44 @@ 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)) {
|
else if ((CMND_ADD == command_code) && (index > 0) && (index <= RULES_MAX_VARS)) {
|
||||||
if ( (XdrvMailbox.data_len > 0) && (XdrvMailbox.payload >= 0) ){
|
if ( XdrvMailbox.data_len > 0 ) {
|
||||||
int16_t tempvar = atol(vars[index -1]) + XdrvMailbox.payload;
|
double tempvar = CharToDouble(vars[index -1]) + CharToDouble(XdrvMailbox.data);
|
||||||
snprintf_P(vars[index -1], sizeof(vars[index -1]), PSTR("%d"), tempvar );
|
dtostrfd(tempvar,2,vars[index -1]);
|
||||||
}
|
}
|
||||||
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, 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 <= RULES_MAX_VARS)) {
|
else if ((CMND_SUB == command_code) && (index > 0) && (index <= RULES_MAX_VARS)) {
|
||||||
if ( (XdrvMailbox.data_len > 0) && (XdrvMailbox.payload >= 0) ){
|
if ( XdrvMailbox.data_len > 0 ){
|
||||||
int16_t tempvar = atol(vars[index -1]) - XdrvMailbox.payload;
|
double tempvar = CharToDouble(vars[index -1]) - CharToDouble(XdrvMailbox.data);
|
||||||
snprintf_P(vars[index -1], sizeof(vars[index -1]), PSTR("%d"), tempvar );
|
dtostrfd(tempvar,2,vars[index -1]);
|
||||||
}
|
}
|
||||||
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, 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 <= RULES_MAX_VARS)) {
|
else if ((CMND_MULT == command_code) && (index > 0) && (index <= RULES_MAX_VARS)) {
|
||||||
if ( (XdrvMailbox.data_len > 0) && (XdrvMailbox.payload >= 0) ){
|
if ( XdrvMailbox.data_len > 0 ){
|
||||||
int16_t tempvar = atol(vars[index -1]) * XdrvMailbox.payload;
|
double tempvar = CharToDouble(vars[index -1]) * CharToDouble(XdrvMailbox.data);
|
||||||
snprintf_P(vars[index -1], sizeof(vars[index -1]), PSTR("%d"), tempvar );
|
dtostrfd(tempvar,2,vars[index -1]);
|
||||||
}
|
}
|
||||||
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, vars[index -1]);
|
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)) {
|
else if ((CMND_SCALE == command_code) && (index > 0) && (index <= RULES_MAX_VARS)) {
|
||||||
if ( XdrvMailbox.data_len > 0 ) {
|
if ( XdrvMailbox.data_len > 0 ) {
|
||||||
if (strstr(XdrvMailbox.data, ",")) { // Process parameter entry
|
if (strstr(XdrvMailbox.data, ",")) { // Process parameter entry
|
||||||
uint8_t tpos = 0; // Parameter index
|
double value = 0;
|
||||||
int16_t value = 0;
|
double valueIN = 0;
|
||||||
int16_t valueIN = 0;
|
double fromLow = 0;
|
||||||
int16_t fromLow = 0;
|
double fromHigh = 0;
|
||||||
int16_t fromHigh = 0;
|
double toLow = 0;
|
||||||
int16_t toLow = 0;
|
double toHigh = 0;
|
||||||
int16_t toHigh = 0;
|
|
||||||
char *p = XdrvMailbox.data; // Parameters like "1, 2, 3, 4"
|
valueIN = CharToDouble(subStr(XdrvMailbox.data, ",", 1));
|
||||||
char *q = p; // Value entered flag
|
fromLow = CharToDouble(subStr(XdrvMailbox.data, ",", 2));
|
||||||
while (p && (tpos < 6)) {
|
fromHigh = CharToDouble(subStr(XdrvMailbox.data, ",", 3));
|
||||||
if (p > q) { // Any value entered
|
toLow = CharToDouble(subStr(XdrvMailbox.data, ",", 4));
|
||||||
if (1 == tpos) { valueIN = value; }
|
toHigh = CharToDouble(subStr(XdrvMailbox.data, ",", 5));
|
||||||
if (2 == tpos) { fromLow = value; }
|
|
||||||
if (3 == tpos) { fromHigh = value; }
|
value = map_double(valueIN, fromLow, fromHigh, toLow, toHigh);
|
||||||
if (4 == tpos) { toLow = value; }
|
dtostrfd(value,2,vars[index -1]);
|
||||||
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]);
|
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, vars[index -1]);
|
||||||
@ -615,6 +604,30 @@ boolean RulesCommand()
|
|||||||
return serviced;
|
return serviced;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double map_double(double x, double in_min, double in_max, double out_min, double out_max)
|
||||||
|
{
|
||||||
|
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to return a substring defined by a delimiter at an index
|
||||||
|
char* subStr (char* str, const char *delim, int index) {
|
||||||
|
char *act, *sub, *ptr;
|
||||||
|
static char copy[10];
|
||||||
|
int i;
|
||||||
|
|
||||||
|
// Since strtok consumes the first arg, make a copy
|
||||||
|
strcpy(copy, str);
|
||||||
|
|
||||||
|
for (i = 1, act = copy; i <= index; i++, act = NULL) {
|
||||||
|
sub = strtok_r(act, delim, &ptr);
|
||||||
|
if (sub == NULL) break;
|
||||||
|
}
|
||||||
|
sub = LTrim(sub);
|
||||||
|
sub = RTrim(sub);
|
||||||
|
return sub;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*********************************************************************************************\
|
/*********************************************************************************************\
|
||||||
* Interface
|
* Interface
|
||||||
\*********************************************************************************************/
|
\*********************************************************************************************/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user