fix upd[x] chg[x] (#23477)

This commit is contained in:
gemu 2025-05-26 08:05:41 +02:00 committed by GitHub
parent 429537ce4c
commit 6aca98673d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -42,14 +42,12 @@ keywords if then else endif, or, and are better readable for beginners (others m
#define XDRV_10 10 #define XDRV_10 10
#ifndef TS_FLOAT #ifndef TS_FLOAT
#define TS_FLOAT float #define TS_FLOAT float
#endif #endif
// float = 4, double = 8 bytes // float = 4, double = 8 bytes
const uint8_t SCRIPT_VERS[2] = {5, 5};
const uint8_t SCRIPT_VERS[2] = {5, 4};
#define SCRIPT_DEBUG 0 #define SCRIPT_DEBUG 0
@ -78,7 +76,7 @@ const uint8_t SCRIPT_VERS[2] = {5, 4};
#endif #endif
#ifndef SCRIPT_MAXSSIZE #ifndef SCRIPT_MAXSSIZE
#define SCRIPT_MAXSSIZE 48 #define SCRIPT_MAXSSIZE 255
#endif #endif
//#define SCRIPT_MAX_SBSIZE SCRIPT_MAXSSIZE //#define SCRIPT_MAX_SBSIZE SCRIPT_MAXSSIZE
@ -191,6 +189,8 @@ char *Get_esc_char(char *cp, char *esc_chr);
#ifdef ESP32 #ifdef ESP32
#include "driver/i2s_std.h" #include "driver/i2s_std.h"
#include "driver/i2s_pdm.h" #include "driver/i2s_pdm.h"
#include "driver/rtc_io.h"
#include "esp_sleep.h"
#endif #endif
@ -256,6 +256,7 @@ char *Get_esc_char(char *cp, char *esc_chr);
#define USE_SCRIPT_FATFS_EXT #define USE_SCRIPT_FATFS_EXT
#endif #endif
#ifdef USE_SCRIPT_TIMER #ifdef USE_SCRIPT_TIMER
#include <Ticker.h> #include <Ticker.h>
Ticker Script_ticker1; Ticker Script_ticker1;
@ -285,6 +286,13 @@ void Script_ticker4_end(void) {
#define HARDWARE_FALLBACK 2 #define HARDWARE_FALLBACK 2
#endif #endif
#ifdef USE_SCRIPT_GLOBVARS
#ifndef SCRIPT_UDP_BUFFER_SIZE
#define SCRIPT_UDP_BUFFER_SIZE 128
#endif
#define SCRIPT_UDP_PORT 1999
// EEPROM MACROS // EEPROM MACROS
// i2c eeprom // i2c eeprom
#define EEP_WRITE(A,B,C) eeprom_writeBytes(A, B, (uint8_t*)C); #define EEP_WRITE(A,B,C) eeprom_writeBytes(A, B, (uint8_t*)C);
@ -300,9 +308,6 @@ void Script_ticker4_end(void) {
#endif #endif
#endif #endif
//#if defined(USE_SML_M) && defined (USE_SML_SCRIPT_CMD)
//extern uint8_t sml_options;
//#endif
#if defined(EEP_SCRIPT_SIZE) && !defined(ESP32) #if defined(EEP_SCRIPT_SIZE) && !defined(ESP32)
@ -311,6 +316,7 @@ void Script_ticker4_end(void) {
uint32_t eeprom_block; uint32_t eeprom_block;
// these support only one 4 k block below EEPROM (eeprom @0x402FB000) this steals 4k of application area // these support only one 4 k block below EEPROM (eeprom @0x402FB000) this steals 4k of application area
uint32_t alt_eeprom_init(uint32_t size) { uint32_t alt_eeprom_init(uint32_t size) {
//EEPROM.begin(size); //EEPROM.begin(size);
@ -374,7 +380,7 @@ extern Renderer *renderer;
#endif #endif
enum {OPER_EQU=1,OPER_PLS,OPER_MIN,OPER_MUL,OPER_DIV,OPER_PLSEQU,OPER_MINEQU,OPER_MULEQU,OPER_DIVEQU,OPER_EQUEQU,OPER_NOTEQU,OPER_GRTEQU,OPER_LOWEQU,OPER_GRT,OPER_LOW,OPER_PERC,OPER_XOR,OPER_AND,OPER_OR,OPER_ANDEQU,OPER_OREQU,OPER_XOREQU,OPER_PERCEQU,OPER_SHLEQU,OPER_SHREQU,OPER_SHL,OPER_SHR}; enum {OPER_EQU=1,OPER_PLS,OPER_MIN,OPER_MUL,OPER_DIV,OPER_PLSEQU,OPER_MINEQU,OPER_MULEQU,OPER_DIVEQU,OPER_EQUEQU,OPER_NOTEQU,OPER_GRTEQU,OPER_LOWEQU,OPER_GRT,OPER_LOW,OPER_PERC,OPER_XOR,OPER_AND,OPER_OR,OPER_ANDEQU,OPER_OREQU,OPER_XOREQU,OPER_PERCEQU,OPER_SHLEQU,OPER_SHREQU,OPER_SHL,OPER_SHR};
enum {SCRIPT_LOGLEVEL=1,SCRIPT_TELEPERIOD,SCRIPT_EVENT_HANDLED,SML_JSON_ENABLE,SCRIPT_EPOFFS,SCRIPT_CBSIZE}; enum {SCRIPT_LOGLEVEL=1,SCRIPT_TELEPERIOD,SCRIPT_EVENT_HANDLED,SML_JSON_ENABLE,SCRIPT_EPOFFS,SCRIPT_CBSIZE,SCRIPT_UDP_PBS,SCRIPT_UDP_MOD};
#ifdef USE_UFILESYS #ifdef USE_UFILESYS
@ -489,7 +495,7 @@ typedef union {
uint8_t nutu6 : 1; uint8_t nutu6 : 1;
uint8_t nutu5 : 1; uint8_t nutu5 : 1;
uint8_t nutu4 : 1; uint8_t nutu4 : 1;
uint8_t nutu3 : 1; uint8_t udp_binary_payload : 1;
uint8_t udp_connected : 1; uint8_t udp_connected : 1;
uint8_t udp_used : 1; uint8_t udp_used : 1;
}; };
@ -503,7 +509,7 @@ typedef union {
uint8_t nutu6 : 1; uint8_t nutu6 : 1;
uint8_t nutu5 : 1; uint8_t nutu5 : 1;
uint8_t nutu4 : 1; uint8_t nutu4 : 1;
uint8_t nutu3 : 1; uint8_t ignore_line : 1;
bool fsys : 1; bool fsys : 1;
bool eeprom : 1; bool eeprom : 1;
}; };
@ -527,9 +533,9 @@ struct SCRIPT_SPI {
}; };
#endif #endif
#define NUM_RES 0xfe #define NUM_RES 0xfe
#define STR_RES 0xfd #define STR_RES 0xfd
#define NUM_ARRAY_RES 0xfc
#define VAR_NV 0xff #define VAR_NV 0xff
#define NTYPE 0 #define NTYPE 0
@ -635,6 +641,8 @@ typedef struct {
IPAddress last_udp_ip; IPAddress last_udp_ip;
WiFiUDP Script_PortUdp; WiFiUDP Script_PortUdp;
IPAddress script_udp_remote_ip; IPAddress script_udp_remote_ip;
char *packet_buffer;
uint16_t pb_size = SCRIPT_UDP_BUFFER_SIZE;
#endif // USE_SCRIPT_GLOBVARS #endif // USE_SCRIPT_GLOBVARS
char web_mode; char web_mode;
char *glob_script = 0; char *glob_script = 0;
@ -994,6 +1002,17 @@ char *script;
script = glob_script_mem.script_ram; script = glob_script_mem.script_ram;
if (!*script) return -999; if (!*script) return -999;
glob_script_mem.max_ssize = SCRIPT_SVARSIZE;
{ char *lp = script + 2;
SCRIPT_SKIP_SPACES
if (isdigit(*lp)) {
uint8_t ssize = atoi(lp) + 1;
if (ssize < 10 || ssize > SCRIPT_MAXSSIZE) ssize = SCRIPT_MAXSSIZE;
glob_script_mem.max_ssize = ssize;
}
}
uint32_t xvars = Script_Find_Vars(script + 1); uint32_t xvars = Script_Find_Vars(script + 1);
uint16_t maxnvars = xvars & 0xffff; uint16_t maxnvars = xvars & 0xffff;
if (maxnvars < 1) { if (maxnvars < 1) {
@ -1034,7 +1053,9 @@ char *script;
//char strings[MAXSVARS*SCRIPT_MAXSSIZE]; //char strings[MAXSVARS*SCRIPT_MAXSSIZE];
//char *strings_p = strings; //char *strings_p = strings;
char *strings_op = (char*)calloc(maxsvars * SCRIPT_MAXSSIZE, 1); //char *strings_op = (char*)calloc(maxsvars * SCRIPT_MAXSSIZE, 1);
char *strings_op = (char*)calloc(maxsvars * glob_script_mem.max_ssize, 1);
char *strings_p = strings_op; char *strings_p = strings_op;
if (!strings_op) { if (!strings_op) {
free(imemptr); free(imemptr);
@ -1075,7 +1096,6 @@ char *script;
uint8_t numflt = 0; uint8_t numflt = 0;
uint16_t count; uint16_t count;
glob_script_mem.max_ssize = SCRIPT_SVARSIZE;
glob_script_mem.scriptptr = 0; glob_script_mem.scriptptr = 0;
char init = 0; char init = 0;
@ -1266,13 +1286,13 @@ char *script;
} }
} else { } else {
if (!strncmp(lp, ">D", 2)) { if (!strncmp(lp, ">D", 2)) {
lp += 2; /* lp += 2;
SCRIPT_SKIP_SPACES SCRIPT_SKIP_SPACES
if (isdigit(*lp)) { if (isdigit(*lp)) {
uint8_t ssize = atoi(lp) + 1; uint8_t ssize = atoi(lp) + 1;
if (ssize < 10 || ssize > SCRIPT_MAXSSIZE) ssize = SCRIPT_MAXSSIZE; if (ssize < 10 || ssize > SCRIPT_MAXSSIZE) ssize = SCRIPT_MAXSSIZE;
glob_script_mem.max_ssize = ssize; glob_script_mem.max_ssize = ssize;
} }*/
init = 1; init = 1;
} }
} }
@ -1522,10 +1542,6 @@ int32_t udp_call(char *url, uint32_t port, char *sbuf) {
return 0; return 0;
} }
#ifdef USE_SCRIPT_GLOBVARS
#define SCRIPT_UDP_BUFFER_SIZE 128
#define SCRIPT_UDP_PORT 1999
//#define SCRIPT_DEBUG_UDP //#define SCRIPT_DEBUG_UDP
void Restart_globvars(void) { void Restart_globvars(void) {
@ -1540,6 +1556,11 @@ void Script_Stop_UDP(void) {
glob_script_mem.Script_PortUdp.stop(); glob_script_mem.Script_PortUdp.stop();
glob_script_mem.udp_flags.udp_connected = 0; glob_script_mem.udp_flags.udp_connected = 0;
} }
if (glob_script_mem.packet_buffer) {
free(glob_script_mem.packet_buffer);
glob_script_mem.packet_buffer = 0;
}
} }
void Script_Init_UDP() { void Script_Init_UDP() {
@ -1547,6 +1568,9 @@ void Script_Init_UDP() {
if (!glob_script_mem.udp_flags.udp_used) return; if (!glob_script_mem.udp_flags.udp_used) return;
if (glob_script_mem.udp_flags.udp_connected) return; if (glob_script_mem.udp_flags.udp_connected) return;
glob_script_mem.packet_buffer = (char*)malloc(glob_script_mem.pb_size);
//if (glob_script_mem.Script_PortUdp.beginMulticast(WiFi.localIP(), IPAddress(239,255,255,250), SCRIPT_UDP_PORT)) { //if (glob_script_mem.Script_PortUdp.beginMulticast(WiFi.localIP(), IPAddress(239,255,255,250), SCRIPT_UDP_PORT)) {
#ifdef ESP8266 #ifdef ESP8266
if (glob_script_mem.Script_PortUdp.beginMulticast(WiFi.localIP(), IPAddress(239,255,255,250), SCRIPT_UDP_PORT)) { if (glob_script_mem.Script_PortUdp.beginMulticast(WiFi.localIP(), IPAddress(239,255,255,250), SCRIPT_UDP_PORT)) {
@ -1567,37 +1591,49 @@ void Script_Init_UDP() {
} }
} }
void Script_PollUdp(void) { void Script_PollUdp(void) {
if (TasmotaGlobal.global_state.network_down) return; if (TasmotaGlobal.global_state.network_down) return;
if (!glob_script_mem.udp_flags.udp_used) return; if (!glob_script_mem.udp_flags.udp_used) return;
if (glob_script_mem.udp_flags.udp_connected ) { if (glob_script_mem.udp_flags.udp_connected ) {
uint32_t timeout = millis(); uint32_t timeout = millis();
while (glob_script_mem.Script_PortUdp.parsePacket()) { while (1) {
uint16_t plen = glob_script_mem.Script_PortUdp.parsePacket();
if (!plen || plen > glob_script_mem.pb_size) {
glob_script_mem.Script_PortUdp.flush();
break;
}
// not more then 500 ms // not more then 500 ms
if (millis() - timeout > 500) { break;} if (millis() - timeout > 500) { break;}
char packet_buffer[SCRIPT_UDP_BUFFER_SIZE]; char *packet_buffer = glob_script_mem.packet_buffer;
int32_t len = glob_script_mem.Script_PortUdp.read(packet_buffer, SCRIPT_UDP_BUFFER_SIZE - 1); int32_t len = glob_script_mem.Script_PortUdp.read(packet_buffer, glob_script_mem.pb_size);
packet_buffer[len] = 0; packet_buffer[len] = 0;
glob_script_mem.script_udp_remote_ip = glob_script_mem.Script_PortUdp.remoteIP(); glob_script_mem.script_udp_remote_ip = glob_script_mem.Script_PortUdp.remoteIP();
#ifdef SCRIPT_DEBUG_UDP #ifdef SCRIPT_DEBUG_UDP
//AddLog(LOG_LEVEL_DEBUG, PSTR("UDP: Packet %s - %d - %s"), packet_buffer, len, script_udp_remote_ip.toString().c_str()); //AddLog(LOG_LEVEL_DEBUG, PSTR("UDP: Packet %s - %d - %s"), packet_buffer, len, script_udp_remote_ip.toString().c_str());
AddLog(LOG_LEVEL_DEBUG, PSTR("UDP: Packet %s - %d - %_I"), packet_buffer, len, (uint32_t)glob_script_mem.script_udp_remote_ip); AddLog(LOG_LEVEL_DEBUG, PSTR("UDP: received Packet %s - %d - %_I"), packet_buffer, len, (uint32_t)glob_script_mem.script_udp_remote_ip);
#endif #endif
char *lp = packet_buffer; char *lp = packet_buffer;
if (!strncmp(lp,"=>", 2)) { if (!strncmp(lp,"=>", 2)) {
lp += 2; lp += 2;
char *cp=strchr(lp, '='); char *cp = lp;
if (cp) { char umode = 0;
char vnam[32]; while (*cp) {
for (uint32_t count = 0; count<len; count++) { if (*cp == '=') {
if (lp[count] == '=') { umode = '=';
vnam[count] = 0; break;
break;
}
vnam[count] = lp[count];
} }
if (*cp == ':') {
umode = ':';
break;
}
cp++;
}
if (umode) {
char vnam[32];
*cp = 0;
strcpy(vnam, lp);
lp = cp + 1;
TS_FLOAT *fp; TS_FLOAT *fp;
char *sp; char *sp;
uint32_t index; uint32_t index;
@ -1606,12 +1642,44 @@ void Script_PollUdp(void) {
#ifdef SCRIPT_DEBUG_UDP #ifdef SCRIPT_DEBUG_UDP
AddLog(LOG_LEVEL_DEBUG, PSTR("SCR: num var found - %s - %d - %d"), vnam, res, index); AddLog(LOG_LEVEL_DEBUG, PSTR("SCR: num var found - %s - %d - %d"), vnam, res, index);
#endif #endif
*fp=CharToFloat(cp + 1); if (umode == '=') {
*fp = CharToFloat(lp);
} else {
TS_FLOAT udpf;
uint8_t *ucp = (uint8_t*) &udpf;
ucp[0] = lp[0];
ucp[1] = lp[1];
ucp[2] = lp[2];
ucp[3] = lp[3];
*fp = udpf;
}
} else if (res == STR_RES) { } else if (res == STR_RES) {
#ifdef SCRIPT_DEBUG_UDP #ifdef SCRIPT_DEBUG_UDP
AddLog(LOG_LEVEL_DEBUG, PSTR("SCR: string var found - %s - %d - %d"), vnam, res, index); AddLog(LOG_LEVEL_DEBUG, PSTR("SCR: string var found - %s - %d - %d"), vnam, res, index);
#endif #endif
strlcpy(sp, cp + 1, SCRIPT_MAX_SBSIZE); strlcpy(sp, lp, SCRIPT_MAX_SBSIZE);
} else if (res == NUM_ARRAY_RES) {
#ifdef SCRIPT_DEBUG_UDP
AddLog(LOG_LEVEL_DEBUG, PSTR("SCR: array var found - %s - %d - %d"), vnam, res, index);
#endif
uint16_t alen = lp[1] << 8 | lp[0];
lp += 2;
if (alen != index) {
AddLog(LOG_LEVEL_DEBUG, PSTR("SCR: warning: array lenght mismatch"));
if (alen < index) {
index = alen;
}
}
for (uint16_t count = 0; count < index; count++) {
TS_FLOAT udpf;
uint8_t *ucp = (uint8_t*) &udpf;
ucp[0] = lp[0];
ucp[1] = lp[1];
ucp[2] = lp[2];
ucp[3] = lp[3];
lp += sizeof(TS_FLOAT);
fp[count] = udpf;
}
} else { } else {
// error var not found // error var not found
} }
@ -1632,33 +1700,54 @@ void Script_PollUdp(void) {
} }
} }
void script_udp_sendvar(char *vname, TS_FLOAT *fp, char *sp); void script_udp_sendvar(char *vname, TS_FLOAT *fp, char *sp, uint16_t alen);
void script_udp_sendvar(char *vname, TS_FLOAT *fp, char *sp) { void script_udp_sendvar(char *vname, TS_FLOAT *fp, char *sp, uint16_t alen) {
if (!glob_script_mem.udp_flags.udp_used) return; if (!glob_script_mem.udp_flags.udp_used) return;
if (!glob_script_mem.udp_flags.udp_connected) return; if (!glob_script_mem.udp_flags.udp_connected) return;
char sbuf[SCRIPT_MAX_SBSIZE + 4]; char sbuf[SCRIPT_MAX_SBSIZE + 4];
strcpy(sbuf, "=>"); strcpy(sbuf, "=>");
strcat(sbuf, vname); strcat(sbuf, vname);
strcat(sbuf, "="); if (glob_script_mem.udp_flags.udp_binary_payload == 0 || !fp) {
if (fp) { strcat(sbuf, "=");
char flstr[16]; if (fp) {
dtostrfd(*fp, 8, flstr); char flstr[16];
strcat(sbuf, flstr); dtostrfd(*fp, 8, flstr);
#ifdef SCRIPT_DEBUG_UDP strcat(sbuf, flstr);
AddLog(LOG_LEVEL_DEBUG, PSTR("SCR: num var updated - %s"), sbuf); } else {
#endif strcat(sbuf, sp);
}
glob_script_mem.Script_PortUdp.beginPacket(IPAddress(239, 255, 255, 250), SCRIPT_UDP_PORT);
// Udp.print(String("RET UC: ") + String(recv_Packet));
glob_script_mem.Script_PortUdp.write((const uint8_t*)sbuf, strlen(sbuf));
glob_script_mem.Script_PortUdp.endPacket();
} else { } else {
strcat(sbuf, sp); // binary numeric
#ifdef SCRIPT_DEBUG_UDP strcat(sbuf, ":");
AddLog(LOG_LEVEL_DEBUG, PSTR("SCR: string var updated - %s"), sbuf); glob_script_mem.Script_PortUdp.beginPacket(IPAddress(239, 255, 255, 250), SCRIPT_UDP_PORT);
#endif glob_script_mem.Script_PortUdp.write((const uint8_t*)sbuf, strlen(sbuf));
if (alen) {
glob_script_mem.Script_PortUdp.write((const uint8_t*)&alen, 2);
for (uint16_t count = 0; count < alen; count++) {
uint8_t *ucp = (uint8_t*)&fp[count];
glob_script_mem.Script_PortUdp.write((const uint8_t*)ucp, sizeof(TS_FLOAT));
}
} else {
uint8_t *ucp = (uint8_t*)fp;
glob_script_mem.Script_PortUdp.write((const uint8_t*)ucp, sizeof(TS_FLOAT));
}
glob_script_mem.Script_PortUdp.endPacket();
} }
glob_script_mem.Script_PortUdp.beginPacket(IPAddress(239, 255, 255, 250), SCRIPT_UDP_PORT);
// Udp.print(String("RET UC: ") + String(recv_Packet)); #ifdef SCRIPT_DEBUG_UDP
glob_script_mem.Script_PortUdp.write((const uint8_t*)sbuf, strlen(sbuf)); if (fp) {
glob_script_mem.Script_PortUdp.endPacket(); AddLog(LOG_LEVEL_DEBUG, PSTR("SCR: num var updated - %s"), sbuf);
} else {
AddLog(LOG_LEVEL_DEBUG, PSTR("SCR: string var updated - %s"), sbuf);
}
#endif // SCRIPT_DEBUG_UDP
} }
#endif //USE_SCRIPT_GLOBVARS #endif //USE_SCRIPT_GLOBVARS
@ -2635,6 +2724,30 @@ uint32_t MeasurePulseTime(int32_t in) {
} }
#endif // USE_ANGLE_FUNC #endif // USE_ANGLE_FUNC
uint32_t check_varname(char *dvnam) {
uint16_t olen = strlen(dvnam);
struct T_INDEX *vtp = glob_script_mem.type;
for (uint32_t count = 0; count < glob_script_mem.numvars; count++) {
char *cp = glob_script_mem.glob_vnp + glob_script_mem.vnp_offset[count];
uint8_t slen = strlen(cp);
if (slen == olen && *cp == dvnam[0]) {
if (!strncmp(cp, dvnam, olen)) {
uint16_t index = vtp[count].index;
if (vtp[count].bits.is_string) {
return STR_RES;
} else {
if (vtp[count].bits.is_filter) {
return NUM_ARRAY_RES;
} else {
return NUM_RES;
}
}
}
}
}
return 0;
}
#ifdef USE_SCRIPT_GLOBVARS #ifdef USE_SCRIPT_GLOBVARS
uint32_t match_vars(char *dvnam, TS_FLOAT **fp, char **sp, uint32_t *ind) { uint32_t match_vars(char *dvnam, TS_FLOAT **fp, char **sp, uint32_t *ind) {
uint16_t olen = strlen(dvnam); uint16_t olen = strlen(dvnam);
@ -2648,8 +2761,11 @@ uint32_t match_vars(char *dvnam, TS_FLOAT **fp, char **sp, uint32_t *ind) {
if (vtp[count].bits.global > 0) { if (vtp[count].bits.global > 0) {
if (vtp[count].bits.is_string == 0) { if (vtp[count].bits.is_string == 0) {
if (vtp[count].bits.is_filter) { if (vtp[count].bits.is_filter) {
// error // array variable
return 0; uint16_t len = 0;
*fp = Get_MFAddr(index, &len, 0);
*ind = len;
return NUM_ARRAY_RES;
} else { } else {
*fp = &glob_script_mem.fvars[index]; *fp = &glob_script_mem.fvars[index];
*ind = count; *ind = count;
@ -3164,9 +3280,11 @@ chknext:
if (*lp!=')') { if (*lp!=')') {
lp = GetNumericArgument(lp, OPER_EQU, &pin, gv); lp = GetNumericArgument(lp, OPER_EQU, &pin, gv);
#ifdef CONFIG_IDF_TARGET_ESP32S3 #ifdef CONFIG_IDF_TARGET_ESP32S3
if (pin<1 || pin>20) pin = 1; if (pin < 1 || pin > 20) pin = 1;
#elif defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32C6)
if (pin < 0 || pin > 5) pin = 0;
#else #else
if (pin<32 || pin>39) pin = 32; if (pin < 32 || pin > 39) pin = 32;
#endif #endif
} }
lp++; lp++;
@ -3198,9 +3316,30 @@ chknext:
TS_FLOAT *fpd; TS_FLOAT *fpd;
lp = get_array_by_name(lp, &fpd, &alend, 0); lp = get_array_by_name(lp, &fpd, &alend, 0);
SCRIPT_SKIP_SPACES SCRIPT_SKIP_SPACES
uint16_t alens; uint16_t alens = 0;
TS_FLOAT *fps; TS_FLOAT *fps;
char *slp = lp; char *slp = lp;
#if (defined(USE_SML_M) || defined(USE_BINPLUGINS)) && defined(USE_SML_SCRIPT_CMD)
if (!strncmp_XP(lp, XPSTR("sml"), 3)) {
lp += 3;
// source is sml array
SML_TABLE *smlp = get_sml_table();
if (smlp) {
alens = smlp->SML_GetVal(0);
if (alens < alend) {
alend = alens;
}
for (uint32_t cnt = 0; cnt < alend; cnt++ ) {
fpd[cnt] = smlp->SML_GetVal(cnt + 1);
}
fvar = alens;
goto nfuncexit;
} else {
fvar = -1;
goto nfuncexit;
}
}
#endif
lp = get_array_by_name(lp, &fps, &alens, 0); lp = get_array_by_name(lp, &fps, &alens, 0);
if (lp == 0 || fps == 0) { if (lp == 0 || fps == 0) {
lp = slp; lp = slp;
@ -3341,9 +3480,11 @@ chknext:
uint8_t vtype; uint8_t vtype;
lp = isvar(lp + 4, &vtype, &ind, 0, 0, gv); lp = isvar(lp + 4, &vtype, &ind, 0, 0, gv);
if (!ind.bits.constant) { if (!ind.bits.constant) {
uint16_t index = glob_script_mem.type[ind.index].index; if (!glob_script_mem.FLAGS.ignore_line) {
fvar = glob_script_mem.fvars[index] != glob_script_mem.s_fvars[index]; uint16_t index = glob_script_mem.type[ind.index].index;
glob_script_mem.s_fvars[index] = glob_script_mem.fvars[index]; fvar = glob_script_mem.fvars[index] != glob_script_mem.s_fvars[index];
glob_script_mem.s_fvars[index] = glob_script_mem.fvars[index];
}
} else { } else {
fvar = 0; fvar = 0;
} }
@ -3425,6 +3566,7 @@ chknext:
tind->index = SCRIPT_CBSIZE; tind->index = SCRIPT_CBSIZE;
goto exit_settable; goto exit_settable;
} }
#ifdef USE_W8960 #ifdef USE_W8960
extern void W8960_SetGain(uint8_t sel, uint16_t value); extern void W8960_SetGain(uint8_t sel, uint16_t value);
@ -3511,6 +3653,70 @@ extern void W8960_SetGain(uint8_t sel, uint16_t value);
} }
goto nfuncexit; goto nfuncexit;
} }
#ifdef ESP32
if (!strncmp_XP(lp, XPSTR("ds("), 3)) {
lp = GetNumericArgument(lp + 3, OPER_EQU, &fvar, gv);
if (fvar == -1) {
fvar = esp_sleep_get_wakeup_cause();
} else {
if (fvar > 0) {
esp_sleep_enable_timer_wakeup(fvar * 1000000);
}
SCRIPT_SKIP_SPACES
if (*lp != ')') {
lp = GetNumericArgument(lp, OPER_EQU, &fvar, gv);
if (fvar != -1) {
gpio_num_t gpio_num = (gpio_num_t)fvar;
lp = GetNumericArgument(lp, OPER_EQU, &fvar, gv);
#if SOC_PM_SUPPORT_EXT1_WAKEUP
if (fvar == 0) {
esp_sleep_enable_ext1_wakeup_io(1 << gpio_num, ESP_EXT1_WAKEUP_ANY_HIGH);
#if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
rtc_gpio_pullup_dis(gpio_num);
rtc_gpio_pulldown_en(gpio_num);
#else
_Pragma("GCC warning \"'rtc io' not supported\"")
#endif
} else {
#if CONFIG_IDF_TARGET_ESP32
esp_sleep_enable_ext1_wakeup_io(1 << gpio_num, ESP_EXT1_WAKEUP_ALL_LOW);
#else
esp_sleep_enable_ext1_wakeup_io(1 << gpio_num, ESP_EXT1_WAKEUP_ANY_LOW);
#endif
#if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
rtc_gpio_pullup_en(gpio_num);
rtc_gpio_pulldown_dis(gpio_num);
#else
_Pragma("GCC warning \"'rtc io' not supported\"")
#endif
}
#else
_Pragma("GCC warning \"'EXT 1 wakeup' not supported using gpio mode\"")
const gpio_config_t config = {
.pin_bit_mask = BIT(gpio_num),
.mode = GPIO_MODE_INPUT,
.pull_up_en = (gpio_pullup_t)!fvar,
.pull_down_en = (gpio_pulldown_t)fvar
};
gpio_config(&config);
if (fvar == 0) {
esp_deep_sleep_enable_gpio_wakeup(1 << gpio_num, ESP_GPIO_WAKEUP_GPIO_LOW);
} else {
esp_deep_sleep_enable_gpio_wakeup(1 << gpio_num, ESP_GPIO_WAKEUP_GPIO_HIGH);
}
#endif // SOC_PM_SUPPORT_EXT1_WAKEUP
}
}
SettingsSaveAll();
esp_deep_sleep_start();
}
goto nfuncexit;
}
#endif // ESP32
break; break;
case 'e': case 'e':
if (!strncmp_XP(vname, XPSTR("epoch"), 5)) { if (!strncmp_XP(vname, XPSTR("epoch"), 5)) {
@ -3930,7 +4136,7 @@ extern void W8960_SetGain(uint8_t sel, uint16_t value);
if (!strncmp_XP(lp, XPSTR("fmt("), 4)) { if (!strncmp_XP(lp, XPSTR("fmt("), 4)) {
lp = GetNumericArgument(lp + 4, OPER_EQU, &fvar, gv); lp = GetNumericArgument(lp + 4, OPER_EQU, &fvar, gv);
if (!fvar) { if (!fvar) {
LittleFS.format(); fvar = LittleFS.format();
} else { } else {
//SD.format(); //SD.format();
} }
@ -4399,6 +4605,48 @@ extern void W8960_SetGain(uint8_t sel, uint16_t value);
} }
goto nfuncexit; goto nfuncexit;
} }
#ifdef USE_SCRIPT_GLOBVARS
if (!strncmp_XP(lp, XPSTR("gvrsa("), 6)) {
TS_FLOAT *fpd = 0;
uint16_t alend;
uint16_t ipos;
lp += 6;
SCRIPT_SKIP_SPACES
char vname[32];
char *cp = lp;
char *vnp = vname;
while (*cp) {
if (*cp == ')') {
break;
}
*vnp++ = *cp++;
}
*vnp = 0;
lp = get_array_by_name(lp, &fpd, &alend, &ipos);
if (fpd) {
uint8_t sv = glob_script_mem.udp_flags.udp_binary_payload;
glob_script_mem.udp_flags.udp_binary_payload = 1;
script_udp_sendvar(vname, fpd, 0, alend);
glob_script_mem.udp_flags.udp_binary_payload = sv;
fvar = ipos;
} else {
fvar = -1;
}
goto nfuncexit;
}
if (!strncmp_XP(lp, XPSTR("gvrbs"), 5)) {
fvar = glob_script_mem.pb_size;
tind->index = SCRIPT_UDP_PBS;
goto exit_settable;
}
if (!strncmp_XP(lp, XPSTR("gvrm"), 4)) {
fvar = glob_script_mem.udp_flags.udp_binary_payload;
tind->index = SCRIPT_UDP_MOD;
goto exit_settable;
}
#endif // USE_SCRIPT_GLOBVARS
break; break;
case 'h': case 'h':
if (!strncmp_XP(vname, XPSTR("hours"), 5)) { if (!strncmp_XP(vname, XPSTR("hours"), 5)) {
@ -6315,24 +6563,26 @@ void tmod_directModeOutput(uint32_t pin);
goto exit; goto exit;
} }
if (!strncmp_XP(lp, XPSTR("upd["), 4)) { if (!strncmp_XP(lp, XPSTR("upd["), 4)) {
// var was updated // var was updated ?
struct T_INDEX ind; struct T_INDEX ind;
uint8_t vtype; uint8_t vtype;
isvar(lp + 4, &vtype, &ind, 0, 0, gv); lp = isvar(lp + 4, &vtype, &ind, 0, 0, gv);
if (!ind.bits.constant) { if (!ind.bits.constant) {
if (!ind.bits.changed) { if (!ind.bits.changed) {
fvar = 0; fvar = 0;
len++; goto nfuncexit;
goto exit;
} else { } else {
glob_script_mem.type[ind.index].bits.changed = 0; if (!glob_script_mem.FLAGS.ignore_line) {
glob_script_mem.type[ind.index].bits.changed = 0;
}
fvar = 1; fvar = 1;
len++; goto nfuncexit;
goto exit;
} }
} }
goto notfound; goto notfound;
} }
#ifdef USE_SCRIPT_GLOBVARS
if (!strncmp_XP(lp, XPSTR("udp("), 4)) { if (!strncmp_XP(lp, XPSTR("udp("), 4)) {
char url[SCRIPT_MAX_SBSIZE]; char url[SCRIPT_MAX_SBSIZE];
lp = GetStringArgument(lp + 4, OPER_EQU, url, 0); lp = GetStringArgument(lp + 4, OPER_EQU, url, 0);
@ -6343,6 +6593,7 @@ void tmod_directModeOutput(uint32_t pin);
fvar = udp_call(url, port, payload); fvar = udp_call(url, port, payload);
goto nfuncexit; goto nfuncexit;
} }
#endif
break; break;
case 'w': case 'w':
@ -7357,7 +7608,7 @@ int32_t UpdVar(char *vname, float *fvar, uint32_t mode) {
glob_script_mem.type[ind.index].bits.changed = 1; glob_script_mem.type[ind.index].bits.changed = 1;
#ifdef USE_SCRIPT_GLOBVARS #ifdef USE_SCRIPT_GLOBVARS
if (glob_script_mem.type[ind.index].bits.global) { if (glob_script_mem.type[ind.index].bits.global) {
script_udp_sendvar(vname, &res, 0); script_udp_sendvar(vname, &res, 0, 0);
} }
#endif //USE_SCRIPT_GLOBVARS #endif //USE_SCRIPT_GLOBVARS
return 0; return 0;
@ -7895,6 +8146,7 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
if_state[ifstck] = 0; if_state[ifstck] = 0;
if_result[ifstck] = 0; if_result[ifstck] = 0;
if_exe[ifstck] = 1; if_exe[ifstck] = 1;
glob_script_mem.FLAGS.ignore_line = 0;
char cmpstr[SCRIPT_MAX_SBSIZE]; char cmpstr[SCRIPT_MAX_SBSIZE];
TS_FLOAT *dfvar; TS_FLOAT *dfvar;
@ -7954,6 +8206,8 @@ startline:
//if (if_state[s_ifstck]==3 && if_result[s_ifstck]) goto next_line; //if (if_state[s_ifstck]==3 && if_result[s_ifstck]) goto next_line;
//if (if_state[s_ifstck]==2 && !if_result[s_ifstck]) goto next_line; //if (if_state[s_ifstck]==2 && !if_result[s_ifstck]) goto next_line;
glob_script_mem.FLAGS.ignore_line = 0;
if (!strncmp(lp, "if", 2)) { if (!strncmp(lp, "if", 2)) {
lp += 2; lp += 2;
if (ifstck < IF_NEST - 1) ifstck++; if (ifstck < IF_NEST - 1) ifstck++;
@ -7962,6 +8216,24 @@ startline:
if (ifstck == 1) if_exe[ifstck] = 1; if (ifstck == 1) if_exe[ifstck] = 1;
else if_exe[ifstck] = if_exe[ifstck - 1]; else if_exe[ifstck] = if_exe[ifstck - 1];
and_or = 0; and_or = 0;
if (if_result[ifstck - 1] == 0) {
// not enabled
glob_script_mem.FLAGS.ignore_line = 1;
/*
while (*lp) {
if (*lp == SCRIPT_EOL) {
break;
}
if (*lp == '{') {
// then
if_state[ifstck] = 2;
break;
}
lp++;
}
goto next_line;
*/
}
} else if (!strncmp(lp, "then", 4) && if_state[ifstck] == 1) { } else if (!strncmp(lp, "then", 4) && if_state[ifstck] == 1) {
lp += 4; lp += 4;
if_state[ifstck] = 2; if_state[ifstck] = 2;
@ -7972,7 +8244,7 @@ startline:
if (if_exe[ifstck - 1]) if_exe[ifstck] = !if_result[ifstck]; if (if_exe[ifstck - 1]) if_exe[ifstck] = !if_result[ifstck];
} else if (!strncmp(lp, "endif", 5) && if_state[ifstck] >= 2) { } else if (!strncmp(lp, "endif", 5) && if_state[ifstck] >= 2) {
lp += 5; lp += 5;
if (ifstck>0) { if (ifstck > 0) {
if_state[ifstck] = 0; if_state[ifstck] = 0;
ifstck--; ifstck--;
} }
@ -8280,6 +8552,7 @@ getnext:
goto next_line; goto next_line;
} }
#endif // USE_SCRIPT_GLOBVARS #endif // USE_SCRIPT_GLOBVARS
#ifdef USE_LIGHT #ifdef USE_LIGHT
#ifdef USE_WS2812 #ifdef USE_WS2812
else if (!strncmp(lp, "ws2812(", 7)) { else if (!strncmp(lp, "ws2812(", 7)) {
@ -8469,7 +8742,7 @@ getnext:
// check for variable result // check for variable result
if (if_state[ifstck] == 1) { if (if_state[ifstck] == 1) {
// evaluate exxpression // evaluate expression
lp = Evaluate_expression(lp, and_or, &if_result[ifstck], gv); lp = Evaluate_expression(lp, and_or, &if_result[ifstck], gv);
SCRIPT_SKIP_SPACES SCRIPT_SKIP_SPACES
if (*lp == '{' && if_state[ifstck] == 1) { if (*lp == '{' && if_state[ifstck] == 1) {
@ -8629,7 +8902,7 @@ getnext:
#ifdef USE_SCRIPT_GLOBVARS #ifdef USE_SCRIPT_GLOBVARS
if (globvindex >= 0 ) { if (globvindex >= 0 ) {
if (glob_script_mem.type[globvindex].bits.global) { if (glob_script_mem.type[globvindex].bits.global) {
script_udp_sendvar(varname, dfvar, 0); script_udp_sendvar(varname, dfvar, 0, 0);
} }
} }
#endif //USE_SCRIPT_GLOBVARS #endif //USE_SCRIPT_GLOBVARS
@ -8677,6 +8950,16 @@ getnext:
} }
glob_script_mem.cmdbuffer_size = *dfvar; glob_script_mem.cmdbuffer_size = *dfvar;
break; break;
#ifdef USE_SCRIPT_GLOBVARS
case SCRIPT_UDP_PBS:
glob_script_mem.pb_size = *dfvar;
Restart_globvars();
break;
case SCRIPT_UDP_MOD:
glob_script_mem.udp_flags.udp_binary_payload = *dfvar;
break;
#endif
#if (defined(USE_SML_M) || defined(USE_BINPLUGINS)) && defined(USE_SML_SCRIPT_CMD) #if (defined(USE_SML_M) || defined(USE_BINPLUGINS)) && defined(USE_SML_SCRIPT_CMD)
case SML_JSON_ENABLE: case SML_JSON_ENABLE:
//sml_options = *dfvar; //sml_options = *dfvar;
@ -8722,7 +9005,7 @@ getnext:
#ifdef USE_SCRIPT_GLOBVARS #ifdef USE_SCRIPT_GLOBVARS
if (globvindex >= 0) { if (globvindex >= 0) {
if (glob_script_mem.type[globvindex].bits.global) { if (glob_script_mem.type[globvindex].bits.global) {
script_udp_sendvar(varname, 0, str); script_udp_sendvar(varname, 0, str, 0);
} }
} }
#endif //USE_SCRIPT_GLOBVARS #endif //USE_SCRIPT_GLOBVARS
@ -9200,7 +9483,6 @@ void script_sort_array(TS_FLOAT *array, uint16_t size) {
} while (swapped); } while (swapped);
} }
bool Is_gpio_used(uint8_t gpiopin) { bool Is_gpio_used(uint8_t gpiopin) {
if (gpiopin >= 0 && (gpiopin < nitems(TasmotaGlobal.gpio_pin)) && (TasmotaGlobal.gpio_pin[gpiopin] > 0)) { if (gpiopin >= 0 && (gpiopin < nitems(TasmotaGlobal.gpio_pin)) && (TasmotaGlobal.gpio_pin[gpiopin] > 0)) {
return true; return true;
@ -10419,6 +10701,44 @@ const char kScriptCommands[] PROGMEM = D_CMND_SCRIPT "|" D_CMND_SUBSCRIBE "|" D_
#endif #endif
; ;
void list_var(char *lp) {
TS_FLOAT fvar;
char str[SCRIPT_MAX_SBSIZE];
glob_script_mem.glob_error = 0;
if (check_varname(lp) == NUM_ARRAY_RES && !strchr(lp, '[')) {
TS_FLOAT *fpd = 0;
uint16_t alend;
char *cp = get_array_by_name(lp, &fpd, &alend, 0);
ResponseAppend_P(PSTR("\"%s\":["), lp);
for (uint16_t cnt = 0; cnt < alend; cnt++) {
TS_FLOAT tvar = *fpd++;
ext_snprintf_P(str, sizeof(str), PSTR("%*_f"), -glob_script_mem.script_dprec, &tvar);
if (cnt) {
ResponseAppend_P(PSTR(",%s"), str);
} else {
ResponseAppend_P(PSTR("%s"), str);
}
}
ResponseAppend_P(PSTR("]"));
} else {
glob_script_mem.glob_error = 0;
glob_script_mem.var_not_found = 0;
GetNumericArgument(lp, OPER_EQU, &fvar, 0);
if (glob_script_mem.var_not_found) {
ResponseAppend_P(PSTR("\"%s\":\"???\""), lp);
} else {
if (glob_script_mem.glob_error == 1) {
// was string, not number
GetStringArgument(lp, OPER_EQU, str, 0);
ResponseAppend_P(PSTR("\"%s\":\"%s\""), lp, str);
} else {
ext_snprintf_P(str, sizeof(str), PSTR("%*_f"), -glob_script_mem.script_dprec, &fvar);
ResponseAppend_P(PSTR("\"%s\":%s"), lp, str);
}
}
}
}
bool ScriptCommand(void) { bool ScriptCommand(void) {
char command[CMDSZ]; char command[CMDSZ];
bool serviced = true; bool serviced = true;
@ -10472,38 +10792,22 @@ bool ScriptCommand(void) {
if ('?' == XdrvMailbox.data[0]) { if ('?' == XdrvMailbox.data[0]) {
char *lp = XdrvMailbox.data; char *lp = XdrvMailbox.data;
lp++; lp++;
while (*lp==' ') lp++; Response_P(PSTR("{\"script\":{"));
TS_FLOAT fvar; while (1) {
char str[SCRIPT_MAX_SBSIZE]; while (*lp==' ') lp++;
glob_script_mem.glob_error = 0; char *cp = strchr(lp, ';');
TS_FLOAT *fpd = 0; if (cp) {
uint16_t alend; *cp = 0;
char *cp = get_array_by_name(lp, &fpd, &alend, 0); list_var(lp);
if (fpd && cp && (!strchr(lp, '[')) ) { ResponseAppend_P(PSTR(","));
// is array lp = cp + 1;
Response_P(PSTR("{\"script\":{\"%s\":["), lp);
for (uint16_t cnt = 0; cnt < alend; cnt++) {
TS_FLOAT tvar = *fpd++;
ext_snprintf_P(str, sizeof(str), PSTR("%*_f"), -glob_script_mem.script_dprec, &tvar);
if (cnt) {
ResponseAppend_P(PSTR(",%s"), str);
} else {
ResponseAppend_P(PSTR("%s"), str);
}
}
ResponseAppend_P(PSTR("]}}"));
} else {
glob_script_mem.glob_error = 0;
GetNumericArgument(lp, OPER_EQU, &fvar, 0);
if (glob_script_mem.glob_error == 1) {
// was string, not number
GetStringArgument(lp, OPER_EQU, str, 0);
Response_P(PSTR("{\"script\":{\"%s\":\"%s\"}}"), lp, str);
} else { } else {
ext_snprintf_P(str, sizeof(str), PSTR("%*_f"), -glob_script_mem.script_dprec, &fvar); list_var(lp);
Response_P(PSTR("{\"script\":{\"%s\":%s}}"), lp, str); ResponseAppend_P(PSTR("}"));
break;
} }
} }
ResponseAppend_P(PSTR("}"));
} }
return serviced; return serviced;
} }