From cd14bde1f709224802ab18f25e32f8381dfc48f7 Mon Sep 17 00:00:00 2001 From: Alexander Schliebner Date: Fri, 6 Mar 2020 21:13:12 +0100 Subject: [PATCH] Update xdrv_10_scripter.ino MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added mapping function `mp` ´´´ mp(x str1 str2 ... str) ´´´ It addresses a standard task with less code and much flexibility: mapping an arbitrary incoming numeric value into the allowed range. The numeric value `x` passed as the first parameter is compared to the rules in the order they are provided as subsequent sting parameters. If the value matches the criteria, the defined value is returned. Subsequent rules are skipped. If `x` matches none of the rules, `x` is returned unchanged. Rules consist of one of the comparison operators `< > =` followed by a numeric value `v1`, optionally followed by a colon and another numeric value `v2`. ``` <|>|=v1[:v2] ``` Example 1: `"<8:0"` - this rule reads: If x is less than 8, return 0. Example 2: `">100"` - this rule reads: If x is greater than 100, return 100. Example 3: ``` y=mp(x "<8:0" ">100") ``` Assigns 0 to y if x is less than 8. Assigns 100 to y if x is greater than 100. Assigns x to y for all values of x that do not meet the above criteria (8 to 100). The above code of example 3 does the same as the following code - with just one line of code and 15 characters less: ``` y=x if x<8 { y=0 } if x>100 { y=100 } ``` --- tasmota/xdrv_10_scripter.ino | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tasmota/xdrv_10_scripter.ino b/tasmota/xdrv_10_scripter.ino index d850dfaea..074e32baa 100755 --- a/tasmota/xdrv_10_scripter.ino +++ b/tasmota/xdrv_10_scripter.ino @@ -1496,6 +1496,40 @@ chknext: fvar=!global_state.mqtt_down; goto exit; } + if (!strncmp(vname,"mp(",3)) { + lp+=3; + float fvar1; + lp=GetNumericResult(lp,OPER_EQU,&fvar1,0); + SCRIPT_SKIP_SPACES + while (*lp!=')') { + char str[SCRIPT_MAXSSIZE]; + lp=GetStringResult(lp,OPER_EQU,str,0); + SCRIPT_SKIP_SPACES + char *pstr=str; + pstr++; + float fvar2; + pstr=GetNumericResult(pstr,OPER_EQU,&fvar2,0); + while (*pstr==' ') pstr++; + fvar=fvar1; + if ((str[0]=='<' && fvar1' && fvar1>fvar2) || + (str[0]=='=' && fvar1==fvar2)) + { + if (*pstr==':') { + pstr++; + while (*pstr==' ') pstr++; + float fvar3; + pstr=GetNumericResult(pstr,OPER_EQU,&fvar3,0); + fvar=fvar3; + } else { + fvar=fvar2; + } + break; + } + } + len=0; + goto exit; + } break; case 'p': if (!strncmp(vname,"pin[",4)) {