From 5903a2b9c552cf075c7d81b21ef9df2f848620a3 Mon Sep 17 00:00:00 2001 From: gemu2015 Date: Tue, 15 Oct 2019 20:07:12 +0200 Subject: [PATCH 1/2] scripter button events, remove comments on paste --- sonoff/xdrv_10_scripter.ino | 173 ++++++++++++++++++++++++++++++++++-- 1 file changed, 164 insertions(+), 9 deletions(-) diff --git a/sonoff/xdrv_10_scripter.ino b/sonoff/xdrv_10_scripter.ino index a43975abb..ef4175566 100644 --- a/sonoff/xdrv_10_scripter.ino +++ b/sonoff/xdrv_10_scripter.ino @@ -177,6 +177,10 @@ uint8_t fast_script=0; uint32_t script_lastmillis; +#ifdef USE_BUTTON_EVENT +int8_t script_button[MAX_KEYS]; +#endif + char *GetNumericResult(char *lp,uint8_t lastop,float *fp,JsonObject *jo); char *GetStringResult(char *lp,uint8_t lastop,char *cp,JsonObject *jo); char *ForceStringVar(char *lp,char *dstr); @@ -1062,6 +1066,17 @@ chknext: } goto exit; } +#ifdef USE_BUTTON_EVENT + if (!strncmp(vname,"bt[",3)) { + // tasmota button state + GetNumericResult(vname+3,OPER_EQU,&fvar,0); + uint32_t index=fvar; + if (index<1 || index>MAX_KEYS) index=1; + fvar=script_button[index-1]; + len++; + goto exit; + } +#endif break; case 'c': if (!strncmp(vname,"chg[",4)) { @@ -2122,7 +2137,20 @@ void Replace_Cmd_Vars(char *srcbuf,char *dstbuf,uint16_t dstsize) { } } } else { - dstbuf[count]=*cp; + if (*cp=='\\') { + cp++; + if (*cp=='n') { + dstbuf[count]='\n'; + } else if (*cp=='r') { + dstbuf[count]='\r'; + } else if (*cp=='\\') { + dstbuf[count]='\\'; + } else { + dstbuf[count]=*cp; + } + } else { + dstbuf[count]=*cp; + } if (*cp==0) { break; } @@ -2970,22 +2998,80 @@ const char HTTP_FORM_SCRIPT1[] PROGMEM = "script enable
" "
" ""; const char HTTP_SCRIPT_FORM_END[] PROGMEM = @@ -3291,7 +3377,15 @@ void HandleScriptConfiguration(void) { WSContentStart_P(S_CONFIGURE_SCRIPT); WSContentSendStyle(); WSContentSend_P(HTTP_FORM_SCRIPT); + + +#ifdef xSCRIPT_STRIP_COMMENTS + uint16_t ssize=glob_script_mem.script_size; + if (bitRead(Settings.rule_enabled, 1)) ssize*=2; + WSContentSend_P(HTTP_FORM_SCRIPT1,1,1,bitRead(Settings.rule_enabled,0) ? " checked" : "",ssize); +#else WSContentSend_P(HTTP_FORM_SCRIPT1,1,1,bitRead(Settings.rule_enabled,0) ? " checked" : "",glob_script_mem.script_size); +#endif // script is to larg for WSContentSend_P if (glob_script_mem.script_ram[0]) { @@ -3329,6 +3423,38 @@ void ScriptSaveSettings(void) { str.replace("\r\n","\n"); str.replace("\r","\n"); +#ifdef xSCRIPT_STRIP_COMMENTS + if (bitRead(Settings.rule_enabled, 1)) { + char *sp=(char*)str.c_str(); + char *sp1=sp; + char *dp=sp; + uint8_t flg=0; + while (*sp) { + while (*sp==' ') sp++; + sp1=sp; + sp=strchr(sp,'\n'); + if (!sp) { + flg=1; + } else { + *sp=0; + } + if (*sp1!=';') { + uint8_t slen=strlen(sp1); + if (slen) { + strcpy(dp,sp1); + dp+=slen; + *dp++='\n'; + } + } + if (flg) { + *dp=0; + break; + } + sp++; + } + } +#endif + strlcpy(glob_script_mem.script_ram,str.c_str(), glob_script_mem.script_size); #ifdef USE_24C256 @@ -3923,11 +4049,20 @@ bool ScriptCommand(void) { } else if ((CMND_SCRIPT == command_code) && (index > 0)) { - if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 2)) { + if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 4)) { switch (XdrvMailbox.payload) { case 0: // Off case 1: // On - bitWrite(Settings.rule_enabled, index -1, XdrvMailbox.payload); + bitWrite(Settings.rule_enabled, index -1, XdrvMailbox.payload); + break; +#ifdef xSCRIPT_STRIP_COMMENTS + case 2: + bitWrite(Settings.rule_enabled, 1,0); + break; + case 3: + bitWrite(Settings.rule_enabled, 1,1); + break; +#endif } } else { if ('>' == XdrvMailbox.data[0]) { @@ -4519,6 +4654,12 @@ bool Xdrv10(uint8_t function) glob_script_mem.script_pram=(uint8_t*)Settings.mems[0]; glob_script_mem.script_pram_size=MAX_RULE_MEMS*10; +#ifdef USE_BUTTON_EVENT + for (uint32_t cnt=0;cntb",2,0); + } + } + break; +#endif } return result; } + + #endif // Do not USE_RULES #endif // USE_SCRIPT From f6ed6edd75abb9689352dccb7399a05c2e862863 Mon Sep 17 00:00:00 2001 From: gemu2015 Date: Wed, 16 Oct 2019 05:33:22 +0200 Subject: [PATCH 2/2] scripter better button event --- sonoff/xdrv_10_scripter.ino | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sonoff/xdrv_10_scripter.ino b/sonoff/xdrv_10_scripter.ino index ef4175566..ee78cc740 100644 --- a/sonoff/xdrv_10_scripter.ino +++ b/sonoff/xdrv_10_scripter.ino @@ -1073,6 +1073,7 @@ chknext: uint32_t index=fvar; if (index<1 || index>MAX_KEYS) index=1; fvar=script_button[index-1]; + script_button[index-1]|=0x80; len++; goto exit; } @@ -4794,7 +4795,7 @@ bool Xdrv10(uint8_t function) #ifdef USE_BUTTON_EVENT case FUNC_BUTTON_PRESSED: if (bitRead(Settings.rule_enabled, 0)) { - if (script_button[XdrvMailbox.index]!=XdrvMailbox.payload) { + if ((script_button[XdrvMailbox.index]&1)!=(XdrvMailbox.payload&1)) { script_button[XdrvMailbox.index]=XdrvMailbox.payload; Run_Scripter(">b",2,0); }