From 3707d4b239b71ed0fc9bc550c217f22d87423546 Mon Sep 17 00:00:00 2001 From: gemu Date: Sun, 22 Sep 2024 12:20:02 +0200 Subject: [PATCH] Sml update (#22185) * some refactoring * refactoring, save some ram --- .../tasmota_xdrv_driver/xdrv_10_scripter.ino | 221 ++++++++++++------ tasmota/tasmota_xsns_sensor/xsns_53_sml.ino | 112 +++++---- 2 files changed, 215 insertions(+), 118 deletions(-) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_10_scripter.ino b/tasmota/tasmota_xdrv_driver/xdrv_10_scripter.ino index 5be2d0cfc..0f6dbccb2 100755 --- a/tasmota/tasmota_xdrv_driver/xdrv_10_scripter.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_10_scripter.ino @@ -61,7 +61,7 @@ const uint8_t SCRIPT_VERS[2] = {5, 3}; #endif #define MAXNVARS MAXVARS-MAXSVARS -#ifdef USE_SML_M +#if (defined(USE_SML_M) || defined(USE_BINPLUGINS)) #ifndef NO_USE_SML_SCRIPT_CMD // allows several sml cmds from scripts, as well as access to sml registers #undef USE_SML_SCRIPT_CMD @@ -293,9 +293,9 @@ void Script_ticker4_end(void) { #endif #endif -#if defined(USE_SML_M) && defined (USE_SML_SCRIPT_CMD) -extern uint8_t sml_options; -#endif +//#if defined(USE_SML_M) && defined (USE_SML_SCRIPT_CMD) +//extern uint8_t sml_options; +//#endif #if defined(EEP_SCRIPT_SIZE) && !defined(ESP32) @@ -531,6 +531,36 @@ struct SCRIPT_SPI { #define FLT_MAX 99999999 #endif +uint32_t SML_SetBaud(uint32_t meter, uint32_t br); +uint32_t sml_status(uint32_t meter); +uint32_t SML_Write(int32_t meter, char *hstr); +uint32_t SML_Read(int32_t meter, char *str, uint32_t slen); +uint32_t sml_getv(uint32_t sel); +uint32_t SML_Shift_Num(uint32_t meter, uint32_t shift); +double SML_GetVal(uint32_t index); +char *SML_GetSVal(uint32_t index); +int32_t SML_Set_WStr(uint32_t meter, char *hstr); +void SML_Decode(uint8_t index); +uint32_t SML_SetOptions(uint32_t in); + +typedef struct { + uint32_t (*SML_SetBaud)(uint32_t,uint32_t); + uint32_t (*sml_status)(uint32_t); + uint32_t (*SML_Write)(int32_t,char*); + uint32_t (*SML_Read)(int32_t,char*,uint32_t); + uint32_t (*sml_getv)(uint32_t); + uint32_t (*SML_Shift_Num)(uint32_t,uint32_t); + double (*SML_GetVal)(uint32_t); + char * (*SML_GetSVal)(uint32_t); + int32_t (*SML_Set_WStr)(uint32_t,char*); + void (*SML_Decode)(uint8_t); + uint32_t (*SML_SetOptions)(uint32_t); +} SML_TABLE; + +#ifdef USE_SML_M +SML_TABLE smltab PROGMEM = {&SML_SetBaud,&sml_status,&SML_Write,&SML_Read,&sml_getv,&SML_Shift_Num,&SML_GetVal,&SML_GetSVal,&SML_Set_WStr,&SML_Decode,&SML_SetOptions}; +#endif + #ifdef USE_SCRIPT_ONEWIRE #include #include @@ -628,7 +658,7 @@ typedef struct { uint32_t from_time; uint32_t to_time; #endif -#if defined(USE_SML_M) && defined(USE_SML_SCRIPT_CMD) && defined(USE_SCRIPT_SERIAL) +#if (defined(USE_SML_M) || defined(USE_BINPLUGINS)) && defined(USE_SML_SCRIPT_CMD) && defined(USE_SCRIPT_SERIAL) char *hstr; #endif @@ -734,11 +764,16 @@ typedef struct { #endif #endif +#if defined(USE_SML_M) || defined (USE_BINPLUGINS) + SML_TABLE *smlptr; +#endif + } SCRIPT_MEM; SCRIPT_MEM glob_script_mem; +uint32_t Plugin_Query(uint16_t, uint8_t); void script_setaflg(uint8_t flg) { glob_script_mem.tasm_cmd_activ = flg; @@ -807,6 +842,24 @@ void script_sort_array(TS_FLOAT *array, uint16_t size); uint32_t Touch_Status(int32_t sel); int32_t play_wave(char *path); + +#if defined(USE_BINPLUGINS) && !defined(USE_SML_M) +SML_TABLE *get_sml_table(void) { + if (Plugin_Query(53, 0)) { + return (SML_TABLE*)Plugin_Query(53, 1); + } else { + return 0; + } +} +#endif + +#ifdef USE_SML_M +SML_TABLE *get_sml_table(void) { + return glob_script_mem.smlptr; +} +#endif + + void ScriptEverySecond(void) { if (bitRead(Settings->rule_enabled, 0)) { @@ -916,6 +969,10 @@ char *script; uint16_t maxvars = maxsvars + maxnvars; //AddLog(LOG_LEVEL_INFO, PSTR("SCR: svar = %d, nvars = %d"), maxsvars, maxnvars); +#ifdef USE_SML_M + glob_script_mem.smlptr = &smltab; +#endif + // scan lines for >DEF uint16_t lines = 0; uint16_t nvars = 0; @@ -2866,7 +2923,14 @@ char *isvar(char *lp, uint8_t *vtype, struct T_INDEX *tind, TS_FLOAT *fp, char * char str_value[SCRIPT_MAX_SBSIZE]; str_value[0]=0; TS_FLOAT fv; +#if 0 uint32_t res = JsonParsePath(gv->jo, vname, '#', &fv, str_value, sizeof(str_value)); +#else + // replace vars allows for looped queries + char vname_buff[64]; + Replace_Cmd_Vars(lp, sizeof(vname), vname_buff , sizeof(vname_buff)); + uint32_t res = JsonParsePath(gv->jo, vname_buff, '#', &fv, str_value, sizeof(str_value)); +#endif if (!res) { goto chknext; } @@ -2881,10 +2945,10 @@ nexit: } else { // string if (!strncmp_XP(str_value, XPSTR("ON"), 2)) { - if (fp) *fp = 1; + fv = 1; goto nexit; } else if (!strncmp_XP(str_value, XPSTR("OFF"), 3)) { - if (fp) *fp = 0; + fv = 0; goto nexit; } else { *vtype = STR_RES; @@ -2896,6 +2960,7 @@ nexit: } #else + // normal json parser JsonParserObject *jpo = gv->jo; char jvname[64]; strcpy(jvname, vname); @@ -4782,15 +4847,12 @@ extern void W8960_SetGain(uint8_t sel, uint16_t value); goto exit; } #ifdef USE_BINPLUGINS -char *Plugin_Query(uint8_t, uint8_t); if (!strncmp_XP(lp, XPSTR("mo("), 3)) { TS_FLOAT fvar1; lp = GetNumericArgument(lp + 3, OPER_EQU, &fvar1, gv); SCRIPT_SKIP_SPACES - TS_FLOAT fvar2; - lp = GetNumericArgument(lp, OPER_EQU, &fvar2, gv); - SCRIPT_SKIP_SPACES - char *rbuff = Plugin_Query(fvar1, fvar2); + + char *rbuff = (char*)Plugin_Query(126, fvar1); if (rbuff) { if (sp) strlcpy(sp, rbuff, glob_script_mem.max_ssize); free (rbuff); @@ -4966,7 +5028,7 @@ char *Plugin_Query(uint8_t, uint8_t); goto exit; } -#ifdef USE_PLAY_WAVE +#if defined(USE_PLAY_WAVE) && defined(USE_UFILESYS) if (!strncmp_XP(lp, XPSTR("pwav("), 5)) { char str[SCRIPT_MAX_SBSIZE]; lp = GetStringArgument(lp + 5, OPER_EQU, str, 0); @@ -5300,14 +5362,17 @@ char *Plugin_Query(uint8_t, uint8_t); #endif //USE_ANGLE_FUNC -#if defined(USE_SML_M) && defined (USE_SML_SCRIPT_CMD) -uint32_t sml_status(uint32_t meter); -extern char *SML_GetSVal(uint32_t index); +#if (defined(USE_SML_M) || defined(USE_BINPLUGINS)) && defined(USE_SML_SCRIPT_CMD) if (!strncmp_XP(lp, XPSTR("sml["), 4)) { lp = GetNumericArgument(lp + 4, OPER_EQU, &fvar, gv); SCRIPT_SKIP_SPACES - fvar = SML_GetVal(fvar); + SML_TABLE *smlp = get_sml_table(); + if (smlp) { + fvar = smlp->SML_GetVal(fvar); + } else { + fvar = 0; + } goto nfuncexit; } if (!strncmp_XP(lp, XPSTR("smls["), 5)) { @@ -5315,16 +5380,19 @@ extern char *SML_GetSVal(uint32_t index); SCRIPT_SKIP_SPACES lp++; len = 0; - if (fvar > 0) { - if (sp) strlcpy(sp, SML_GetSVal(fvar), glob_script_mem.max_ssize); - } else { - char sbuff[SCRIPT_MAX_SBSIZE]; - fvar = fabs(fvar); - if (fvar < 1) { - fvar = 1; + SML_TABLE *smlp = get_sml_table(); + if (smlp) { + if (fvar > 0) { + if (sp) strlcpy(sp, smlp->SML_GetSVal(fvar), glob_script_mem.max_ssize); + } else { + char sbuff[SCRIPT_MAX_SBSIZE]; + fvar = fabs(fvar); + if (fvar < 1) { + fvar = 1; + } + dtostrfd(smlp->SML_GetVal(fvar), glob_script_mem.script_dprec, sbuff); + if (sp) strlcpy(sp, sbuff, glob_script_mem.max_ssize); } - dtostrfd(SML_GetVal(fvar), glob_script_mem.script_dprec, sbuff); - if (sp) strlcpy(sp, sbuff, glob_script_mem.max_ssize); } goto strexit; } @@ -5335,54 +5403,63 @@ extern char *SML_GetSVal(uint32_t index); TS_FLOAT fvar2; lp = GetNumericArgument(lp, OPER_EQU, &fvar2, gv); SCRIPT_SKIP_SPACES - if (fvar2 == 0) { - TS_FLOAT fvar3; - lp = GetNumericArgument(lp, OPER_EQU, &fvar3, gv); - fvar = SML_SetBaud(fvar1, fvar3); - } else if (fvar2 == 1) { - char str[SCRIPT_MAX_SBSIZE]; - lp = GetStringArgument(lp, OPER_EQU, str, 0); - fvar = SML_Write(fvar1, str); - } else if (fvar2 == 2) { - char str[SCRIPT_MAX_SBSIZE]; - str[0] = 0; - fvar = SML_Read(fvar1, str, SCRIPT_MAX_SBSIZE); - if (sp) strlcpy(sp, str, glob_script_mem.max_ssize); - lp++; - len = 0; - goto strexit; - } else if (fvar2 == 3) { - uint8_t vtype; - struct T_INDEX ind; - lp = isvar(lp, &vtype, &ind, 0, 0, 0); - if (vtype != VAR_NV) { - // found variable as result - if (vtype == NUM_RES || (vtype & STYPE) == 0) { - // numeric result - fvar = -1; + SML_TABLE *smlp = get_sml_table(); + if (smlp) { + if (fvar2 == 0) { + TS_FLOAT fvar3; + lp = GetNumericArgument(lp, OPER_EQU, &fvar3, gv); + fvar = smlp->SML_SetBaud(fvar1, fvar3); + } else if (fvar2 == 1) { + char str[SCRIPT_MAX_SBSIZE]; + lp = GetStringArgument(lp, OPER_EQU, str, 0); + fvar = smlp->SML_Write(fvar1, str); + } else if (fvar2 == 2) { + char str[SCRIPT_MAX_SBSIZE]; + str[0] = 0; + fvar = smlp->SML_Read(fvar1, str, SCRIPT_MAX_SBSIZE); + if (sp) strlcpy(sp, str, glob_script_mem.max_ssize); + lp++; + len = 0; + goto strexit; + } else if (fvar2 == 3) { + uint8_t vtype; + struct T_INDEX ind; + lp = isvar(lp, &vtype, &ind, 0, 0, 0); + if (vtype != VAR_NV) { + // found variable as result + if (vtype == NUM_RES || (vtype & STYPE) == 0) { + // numeric result + fvar = -1; + } else { + // string result + uint16_t sindex = glob_script_mem.type[ind.index].index; + char *cp = glob_script_mem.glob_snp + (sindex * glob_script_mem.max_ssize); + fvar = smlp->SML_Set_WStr(fvar1, cp); + } } else { - // string result - uint16_t sindex = glob_script_mem.type[ind.index].index; - char *cp = glob_script_mem.glob_snp + (sindex * glob_script_mem.max_ssize); - fvar = SML_Set_WStr(fvar1, cp); + fvar = -99; } } else { - fvar = -99; + fvar = smlp->sml_status(fvar1); } - } else { - fvar = sml_status(fvar1); } goto nfuncexit; } if (!strncmp_XP(vname, XPSTR("smlj"), 4)) { - fvar = sml_options; + SML_TABLE *smlp = get_sml_table(); + if (smlp) { + fvar = smlp->SML_SetOptions(0); // sml_options; + } tind->index = SML_JSON_ENABLE; goto exit_settable; } if (!strncmp_XP(lp, XPSTR("smld("), 5)) { lp = GetNumericArgument(lp + 5, OPER_EQU, &fvar, gv); if (fvar < 1) fvar = 1; - SML_Decode(fvar - 1); + SML_TABLE *smlp = get_sml_table(); + if (smlp) { + smlp->SML_Decode(fvar - 1); + } goto nfuncexit; } if (!strncmp_XP(lp, XPSTR("smls("), 5)) { @@ -5390,12 +5467,18 @@ extern char *SML_GetSVal(uint32_t index); lp = GetNumericArgument(lp + 5, OPER_EQU, &meter, gv); if (meter < 1) meter = 1; lp = GetNumericArgument(lp, OPER_EQU, &fvar, gv); - SML_Shift_Num(meter - 1, fvar); + SML_TABLE *smlp = get_sml_table(); + if (smlp) { + smlp->SML_Shift_Num(meter - 1, fvar); + } goto nfuncexit; } if (!strncmp_XP(lp, XPSTR("smlv["), 5)) { lp = GetNumericArgument(lp + 5, OPER_EQU, &fvar, gv); - fvar = sml_getv(fvar); + SML_TABLE *smlp = get_sml_table(); + if (smlp) { + fvar = smlp->sml_getv(fvar); + } goto nfuncexit; } #endif //USE_SML_M @@ -6645,7 +6728,7 @@ char *getop(char *lp, uint8_t *operand) { -#ifdef USE_PLAY_WAVE +#if defined(USE_PLAY_WAVE) && defined(USE_UFILESYS) #ifdef ESP8266 #include @@ -8480,9 +8563,13 @@ getnext: } glob_script_mem.cmdbuffer_size = *dfvar; break; -#if defined(USE_SML_M) && defined (USE_SML_SCRIPT_CMD) +#if (defined(USE_SML_M) || defined(USE_BINPLUGINS)) && defined(USE_SML_SCRIPT_CMD) case SML_JSON_ENABLE: - sml_options = *dfvar; + //sml_options = *dfvar; + SML_TABLE *smlp = get_sml_table(); + if (smlp) { + fvar = smlp->SML_SetOptions(0x100 | (uint8_t) *dfvar); // sml_options; + } break; #endif } @@ -9906,7 +9993,7 @@ void Script_Handle_Hue(String path) { uint16_t args = Webserver->args(); -#ifdef ESP8266 +#ifdef ESP82666 char *json = (char*)Webserver->arg(args - 1).c_str(); #else String request_arg = Webserver->arg(args - 1); @@ -13764,7 +13851,7 @@ bool Xdrv10(uint32_t function) { Script_Check_Hue(0); #endif //USE_SCRIPT_HUE -#if defined(USE_SML_M) && defined(USE_SML_SCRIPT_CMD) && defined(USE_SCRIPT_SERIAL) +#if (defined(USE_SML_M) || defined(USE_BINPLUGINS)) && defined(USE_SML_SCRIPT_CMD) && defined(USE_SCRIPT_SERIAL) glob_script_mem.hstr = 0; #endif } diff --git a/tasmota/tasmota_xsns_sensor/xsns_53_sml.ino b/tasmota/tasmota_xsns_sensor/xsns_53_sml.ino index 4984733c4..f010803c1 100755 --- a/tasmota/tasmota_xsns_sensor/xsns_53_sml.ino +++ b/tasmota/tasmota_xsns_sensor/xsns_53_sml.ino @@ -596,6 +596,7 @@ double buffer[MEDIAN_SIZE]; int8_t index; }; +#define SML_OPTIONS_JSON_ENABLE 1 struct SML_GLOBS { uint8_t sml_send_blocks; @@ -629,13 +630,11 @@ struct SML_GLOBS { #ifdef USE_SML_CANBUS uint8_t twai_installed; #endif // USE_SML_CANBUS + uint8_t sml_options = SML_OPTIONS_JSON_ENABLE; } sml_globs; -#define SML_OPTIONS_JSON_ENABLE 1 -uint8_t sml_options = SML_OPTIONS_JSON_ENABLE; - #ifdef USE_SML_MEDIAN_FILTER #ifndef FLT_MAX @@ -794,7 +793,7 @@ void dump2log(void) { while (logsiz) { sml_dump_start('>'); for (uint16_t cnt = 0; cnt < 16; cnt++) { - sprintf(&sml_globs.log_data[sml_globs.sml_logindex], "%02x ", mp->sbuff[index++]); + sprintf_P(&sml_globs.log_data[sml_globs.sml_logindex], PSTR("%02x "), mp->sbuff[index++]); if (sml_globs.sml_logindex < sml_globs.logsize - 7) { sml_globs.sml_logindex += 3; } @@ -809,7 +808,7 @@ void dump2log(void) { // dump serial buffer sml_dump_start(' '); while (index < mp->spos) { - sprintf(&sml_globs.log_data[sml_globs.sml_logindex], "%02x ", mp->sbuff[index++]); + sprintf_P(&sml_globs.log_data[sml_globs.sml_logindex], PSTR("%02x "), mp->sbuff[index++]); if (sml_globs.sml_logindex >= 32*3+2) { AddLogData(LOG_LEVEL_INFO, sml_globs.log_data); sml_dump_start(' '); @@ -838,7 +837,7 @@ void dump2log(void) { while ((millis() - d_lastms) < 40) { if (SML_SAVAILABLE) { uint8_t c = SML_SREAD; - sprintf(&sml_globs.log_data[index], "%02x ", c); + sprintf_P(&sml_globs.log_data[index], PSTR("%02x "), c); dchars[hcnt] = c; index += 3; hcnt++; @@ -896,7 +895,7 @@ void dump2log(void) { AddLogData(LOG_LEVEL_INFO, sml_globs.log_data); sml_dump_start(' '); } - sprintf(&sml_globs.log_data[sml_globs.sml_logindex], "%02x ", c); + sprintf_P(&sml_globs.log_data[sml_globs.sml_logindex], PSTR("%02x "), c); if (sml_globs.sml_logindex < sml_globs.logsize - 7) { sml_globs.sml_logindex += 3; } @@ -919,7 +918,7 @@ void dump2log(void) { } continue; } - sprintf(&sml_globs.log_data[sml_globs.sml_logindex], "%02x ", c); + sprintf_P(&sml_globs.log_data[sml_globs.sml_logindex], PSTR("%02x "), c); if (sml_globs.sml_logindex < sml_globs.logsize - 7) { sml_globs.sml_logindex += 3; } @@ -936,7 +935,7 @@ void dump2log(void) { AddLogData(LOG_LEVEL_INFO, sml_globs.log_data); sml_dump_start(' '); } - sprintf(&sml_globs.log_data[sml_globs.sml_logindex], "%02x ", c); + sprintf_P(&sml_globs.log_data[sml_globs.sml_logindex], PSTR("%02x "), c); if (sml_globs.sml_logindex < sml_globs.logsize - 7) { sml_globs.sml_logindex += 3; } @@ -960,7 +959,7 @@ void dump2log(void) { } sml_dump_start(' '); for (uint8_t index = 0; index < canFrame.can_dlc + 5; index++) { - sprintf(&sml_globs.log_data[sml_globs.sml_logindex], "%02x", mp->sbuff[index]); + sprintf_P(&sml_globs.log_data[sml_globs.sml_logindex], PSTR("%02x"), mp->sbuff[index]); sml_globs.sml_logindex += 2; if (index == 3) { sml_globs.log_data[sml_globs.sml_logindex] = ':'; @@ -1001,7 +1000,7 @@ void dump2log(void) { } sml_dump_start(' '); for (uint8_t index = 0; index < message.data_length_code + 5; index++) { - sprintf(&sml_globs.log_data[sml_globs.sml_logindex], "%02x", mp->sbuff[index]); + sprintf_P(&sml_globs.log_data[sml_globs.sml_logindex], PSTR("%02x"), mp->sbuff[index]); sml_globs.sml_logindex += 2; if (index == 3) { sml_globs.log_data[sml_globs.sml_logindex] = ':'; @@ -1026,7 +1025,7 @@ void dump2log(void) { while (SML_SAVAILABLE) { d_lastms = millis(); yield(); - sprintf(&sml_globs.log_data[sml_globs.sml_logindex], "%02x ", SML_SREAD); + sprintf_P(&sml_globs.log_data[sml_globs.sml_logindex], PSTR("%02x "), SML_SREAD); if (sml_globs.sml_logindex < sml_globs.logsize - 7) { sml_globs.sml_logindex += 3; } @@ -1052,7 +1051,7 @@ void Hexdump(uint8_t *sbuff, uint32_t slen) { *cp++ = '>'; *cp++ = ' '; for (uint32_t cnt = 0; cnt < slen; cnt ++) { - sprintf(cp, "%02x ", sbuff[cnt]); + sprintf_P(cp, PSTR("%02x "), sbuff[cnt]); cp += 3; } AddLogData(LOG_LEVEL_INFO, cbuff); @@ -1229,12 +1228,12 @@ double dval; s1 = *cp << 16 | *(cp + 1) <<8 | *(cp + 2); cp += 4; s2 = *cp << 16 | *(cp + 1) <<8 | *(cp + 2); - sprintf(&meter_desc[index].meter_id[0], "%u-%u", s1, s2); + sprintf_P(&meter_desc[index].meter_id[0], PSTR("%u-%u"), s1, s2); } else { // server id on hager char *str = &meter_desc[index].meter_id[0]; for (type = 0; type < len - 1; type++) { - sprintf(str,"%02x", *cp++); + sprintf_P(str, PSTR("%02x"), *cp++); str += 2; } } @@ -1964,7 +1963,7 @@ void SML_Decode(uint8_t index) { } } else { // ebus modbus pzem vbus or raw - if (!strncmp(mp, "pm(", 3)) { + if (!strncmp_P(mp, PSTR("pm("), 3)) { // pattern match uint8_t dp = 0; mp += 3; @@ -2042,7 +2041,7 @@ void SML_Decode(uint8_t index) { cp += skip; } } - } else if (!strncmp(mp, "U64", 3)) { + } else if (!strncmp_P(mp, PSTR("U64"), 3)) { uint32_t valh = (cp[0]<<24) | (cp[1]<<16) | (cp[2]<<8) | (cp[3]<<0); uint32_t vall = (cp[4]<<24) | (cp[5]<<16) | (cp[6]<<8) | (cp[7]<<0); uint64_t val = ((uint64_t)valh<<32) | vall; @@ -2050,7 +2049,7 @@ void SML_Decode(uint8_t index) { cp += 8; ebus_dval = val; mbus_dval = val; - } else if (!strncmp(mp, "u64", 3)) { + } else if (!strncmp_P(mp, PSTR("u64"), 3)) { uint64_t valh = (cp[1]<<24) | (cp[0]<<16) | (cp[3]<<8) | (cp[2]<<0); uint64_t vall = (cp[5]<<24) | (cp[4]<<16) | (cp[7]<<8) | (cp[6]<<0); uint64_t val = ((uint64_t)valh<<32) | vall; @@ -2058,10 +2057,10 @@ void SML_Decode(uint8_t index) { cp += 8; ebus_dval = val; mbus_dval = val; - } else if (!strncmp(mp, "U32", 3)) { + } else if (!strncmp_P(mp, PSTR("U32"), 3)) { mp += 3; goto U32_do; - } else if (!strncmp(mp, "UUuuUUuu", 8)) { + } else if (!strncmp_P(mp, PSTR("UUuuUUuu"), 8)) { mp += 8; U32_do: uint32_t val = (cp[0]<<24) | (cp[1]<<16) | (cp[2]<<8) | (cp[3]<<0); @@ -2073,10 +2072,10 @@ void SML_Decode(uint8_t index) { } ebus_dval = val; mbus_dval = val; - } else if (!strncmp(mp, "u32", 3)) { + } else if (!strncmp_P(mp, PSTR("u32"), 3)) { mp += 3; goto u32_do; - } else if (!strncmp(mp, "uuUUuuUU", 8)) { + } else if (!strncmp_P(mp, PSTR("uuUUuuUU"), 8)) { mp += 8; u32_do: uint32_t val = (cp[1]<<24) | (cp[0]<<16) | (cp[3]<<8) | (cp[2]<<0); @@ -2088,16 +2087,16 @@ void SML_Decode(uint8_t index) { } ebus_dval = val; mbus_dval = val; - } else if (!strncmp(mp, "UUuu", 4)) { + } else if (!strncmp_P(mp, PSTR("UUuu"), 4)) { uint16_t val = cp[1] | (cp[0]<<8); mbus_dval = val; ebus_dval = val; mp += 4; cp += 2; - } else if (!strncmp(mp, "S32", 3)) { + } else if (!strncmp_P(mp, PSTR("S32"), 3)) { mp += 3; goto S32_do; - } else if (!strncmp(mp, "SSssSSss", 8)) { + } else if (!strncmp_P(mp, PSTR("SSssSSss"), 8)) { mp += 8; S32_do: int32_t val = (cp[0]<<24) | (cp[1]<<16) | (cp[2]<<8) | (cp[3]<<0); @@ -2109,10 +2108,10 @@ void SML_Decode(uint8_t index) { } ebus_dval = val; mbus_dval = val; - } else if (!strncmp(mp, "s32", 3)) { + } else if (!strncmp_P(mp, PSTR("s32"), 3)) { mp += 3; goto s32_do; - } else if (!strncmp(mp, "ssSSssSS", 8)) { + } else if (!strncmp_P(mp, PSTR("ssSSssSS"), 8)) { mp += 8; s32_do: int32_t val = (cp[1]<<24) | (cp[0]<<16) | (cp[3]<<8) | (cp[2]<<0); @@ -2124,42 +2123,42 @@ void SML_Decode(uint8_t index) { } ebus_dval = val; mbus_dval = val; - } else if (!strncmp(mp, "uuUU", 4)) { + } else if (!strncmp_P(mp, PSTR("uuUU"), 4)) { uint16_t val = cp[0] | (cp[1]<<8); mbus_dval = val; ebus_dval = val; mp += 4; cp += 2; - } else if (!strncmp(mp, "uu", 2)) { + } else if (!strncmp_P(mp, PSTR("uu"), 2)) { uint8_t val = *cp++; mbus_dval = val; ebus_dval = val; mp += 2; - } else if (!strncmp(mp, "ssSS", 4)) { + } else if (!strncmp_P(mp, PSTR("ssSS"), 4)) { int16_t val = *cp | (*(cp+1)<<8); mbus_dval = val; ebus_dval = val; mp += 4; cp += 2; - } else if (!strncmp(mp, "SSss", 4)) { + } else if (!strncmp_P(mp, PSTR("SSss"), 4)) { int16_t val = cp[1] | (cp[0]<<8); mbus_dval = val; ebus_dval = val; mp += 4; cp += 2; - } else if (!strncmp(mp,"ss", 2)) { + } else if (!strncmp_P(mp, PSTR("ss"), 2)) { int8_t val = *cp++; mbus_dval = val; ebus_dval = val; mp += 2; - } else if (!strncmp(mp, "ffffffff", 8)) { + } else if (!strncmp_P(mp, PSTR("ffffffff"), 8)) { uint32_t val = (cp[0]<<24) | (cp[1]<<16) | (cp[2]<<8) | (cp[3]<<0); float *fp = (float*)&val; ebus_dval = *fp; mbus_dval = *fp; mp += 8; cp += 4; - } else if (!strncmp(mp, "FFffFFff", 8)) { + } else if (!strncmp_P(mp, PSTR("FFffFFff"), 8)) { // reverse word float uint32_t val = (cp[1]<<0) | (cp[0]<<8) | (cp[3]<<16) | (cp[2]<<24); float *fp = (float*)&val; @@ -2167,24 +2166,24 @@ void SML_Decode(uint8_t index) { mbus_dval = *fp; mp += 8; cp += 4; - } else if (!strncmp(mp, "eeeeee", 6)) { + } else if (!strncmp_P(mp, PSTR("eeeeee"), 6)) { uint32_t val = (cp[0]<<16) | (cp[1]<<8) | (cp[2]<<0); mbus_dval = val; mp += 6; cp += 3; - } else if (!strncmp(mp, "vvvvvv", 6)) { + } else if (!strncmp_P(mp, PSTR("vvvvvv"), 6)) { mbus_dval = (float)((cp[0]<<8) | (cp[1])) + ((float)cp[2]/10.0); mp += 6; cp += 3; - } else if (!strncmp(mp, "cccccc", 6)) { + } else if (!strncmp_P(mp, PSTR("cccccc"), 6)) { mbus_dval = (float)((cp[0]<<8) | (cp[1])) + ((float)cp[2]/100.0); mp += 6; cp += 3; - } else if (!strncmp(mp, "pppp", 4)) { + } else if (!strncmp_P(mp, PSTR("pppp"), 4)) { mbus_dval = (float)((cp[0]<<8) | cp[1]); mp += 4; cp += 2; - } else if (!strncmp(mp, "kstr", 4)) { + } else if (!strncmp_P(mp, PSTR("kstr"), 4)) { mp += 4; // decode the mantissa uint32_t x = 0; @@ -2207,7 +2206,7 @@ void SML_Decode(uint8_t index) { } mbus_dval = (double )(x * ifl); - } else if (!strncmp(mp, "bcd", 3)) { + } else if (!strncmp_P(mp, PSTR("bcd"), 3)) { mp += 3; uint8_t digits = strtol((char*)mp, (char**)&mp, 10); if (digits < 2) digits = 2; @@ -2310,7 +2309,7 @@ void SML_Decode(uint8_t index) { } else { time = vbus_get_septet(cp) & 0xffff; } - sprintf(&meter_desc[index].meter_id[0], "%02d:%02d", time / 60, time % 60); + sprintf_P(&meter_desc[index].meter_id[0], PSTR("%02d:%02d"), time / 60, time % 60); } break; } @@ -2354,7 +2353,7 @@ void SML_Decode(uint8_t index) { date -= year * 10000; uint8_t month = date / 100; // = 09 uint8_t day = date % 100; // = 01 - sprintf(&meter_desc[mindex].meter_id[0],"%02d.%02d.%02d",day, month, year); + sprintf_P(&meter_desc[mindex].meter_id[0], PSTR("%02d.%02d.%02d"),day, month, year); } else { sml_getvalue(cp, mindex); } @@ -2596,7 +2595,7 @@ void SML_Show(boolean json) { // convert hex to asci sml_hex_asci(mindex, tpowstr); } else { - sprintf(tpowstr,"\"%s\"", &meter_desc[mindex].meter_id[0]); + sprintf_P(tpowstr, PSTR("\"%s\""), &meter_desc[mindex].meter_id[0]); } mid = 1; } else if (*cp == '(') { @@ -2701,8 +2700,12 @@ void SML_Show(boolean json) { } else { // web ui export //snprintf_P(b_mqtt_data, sizeof(b_mqtt_data), "%s{s}%s %s: {m}%s %s{e}", b_mqtt_data,meter_desc[mindex].prefix,name,tpowstr,unit); - if (strcmp(name, "*")) { - WSContentSend_P(PSTR("{s}%s %s{m}"), sml_globs.mp[mindex].prefix, name); // Do not replace decimal separator in label + if (strncmp_P(name, PSTR("*"), 1)) { + if (sml_globs.mp[mindex].prefix[0] == '*') { + WSContentSend_P(PSTR("{s}%s{m}"), name); + } else { + WSContentSend_P(PSTR("{s}%s %s{m}"), sml_globs.mp[mindex].prefix, name); // Do not replace decimal separator in label + } WSContentSend_PD(PSTR("%s %s{e}"), tpowstr, unit); // Replace decimal separator in value } } @@ -3364,8 +3367,8 @@ dddef_exit: goto next_line; } // 1,=h————————————— - if (!strncmp(lp1 + 1, ",=h", 3) || !strncmp(lp1 + 1, ",=so", 4)) { - if (!strncmp(lp1 + 1, ",=so", 4)) { + if (!strncmp_P(lp1 + 1, PSTR(",=h"), 3) || !strncmp_P(lp1 + 1, PSTR(",=so"), 4)) { + if (!strncmp_P(lp1 + 1, PSTR(",=so"), 4)) { SpecOptions(lp1 + 5, mnum - 1); } } else { @@ -3707,6 +3710,13 @@ next_line: #ifdef USE_SML_SCRIPT_CMD +uint32_t SML_SetOptions(uint32_t in) { + if (in & 0x100) { + sml_globs.sml_options = in; + } + return sml_globs.sml_options; +} + uint32_t SML_SetBaud(uint32_t meter, uint32_t br) { if (sml_globs.ready == false) return 0; if (meter < 1 || meter > sml_globs.meters_used) return 0; @@ -3813,7 +3823,7 @@ uint32_t SML_Read(int32_t meter, char *str, uint32_t slen) { } else { uint32_t index = 0; for (uint32_t cnt = 0; cnt < mp->spos; cnt++) { - sprintf(str,"%02x", mp->sbuff[cnt]); + sprintf_P(str, PSTR("%02x"), mp->sbuff[cnt]); str += 2; index += 2; if (index >= slen - 2) break; @@ -4594,7 +4604,7 @@ bool XSNS_53_cmd(void) { void InjektCounterValue(uint8_t meter, uint32_t counter, float rate) { - snprintf((char*)&meter_desc[meter].sbuff[0], meter_desc[meter].sbsiz, "1-0:1.8.0*255(%d)", counter); + snprintf_P((char*)&meter_desc[meter].sbuff[0], meter_desc[meter].sbsiz, PSTR("1-0:1.8.0*255(%d)"), counter); SML_Decode(meter); char freq[16]; @@ -4602,7 +4612,7 @@ void InjektCounterValue(uint8_t meter, uint32_t counter, float rate) { if (rate) { DOUBLE2CHAR(rate, 4, freq); } - snprintf((char*)&meter_desc[meter].sbuff[0], meter_desc[meter].sbsiz, "1-0:1.7.0*255(%s)", freq); + snprintf_P((char*)&meter_desc[meter].sbuff[0], meter_desc[meter].sbsiz, PSTR("1-0:1.7.0*255(%s)"), freq); SML_Decode(meter); } @@ -4657,7 +4667,7 @@ bool Xsns53(uint32_t function) { break; case FUNC_JSON_APPEND: if (sml_globs.ready) { - if (sml_options & SML_OPTIONS_JSON_ENABLE) { + if (sml_globs.sml_options & SML_OPTIONS_JSON_ENABLE) { SML_Show(1); } }