Skip to content

Commit

Permalink
Sml update (#22185)
Browse files Browse the repository at this point in the history
* some refactoring

* refactoring, save some ram
  • Loading branch information
gemu2015 authored Sep 22, 2024
1 parent 7ef93b2 commit 3707d4b
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 118 deletions.
221 changes: 154 additions & 67 deletions tasmota/tasmota_xdrv_driver/xdrv_10_scripter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 <OneWire.h>
#include <DS2480B.h>
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -2896,6 +2960,7 @@ nexit:
}

#else
// normal json parser
JsonParserObject *jpo = gv->jo;
char jvname[64];
strcpy(jvname, vname);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -5300,31 +5362,37 @@ 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)) {
lp = GetNumericArgument(lp + 5, OPER_EQU, &fvar, gv);
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;
}
Expand All @@ -5335,67 +5403,82 @@ 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)) {
TS_FLOAT meter;
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
Expand Down Expand Up @@ -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 <i2s.h>
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit 3707d4b

Please sign in to comment.