From 7f1514e6da269972fabe458ef3b5cdabd40cba2d Mon Sep 17 00:00:00 2001 From: gemu2015 Date: Sat, 30 May 2020 10:48:12 +0200 Subject: [PATCH 1/2] some fixes and optimizations --- tasmota/xdrv_10_scripter.ino | 139 ++++++++++++++++++++++------------- 1 file changed, 88 insertions(+), 51 deletions(-) diff --git a/tasmota/xdrv_10_scripter.ino b/tasmota/xdrv_10_scripter.ino index 434028092..a2562a36c 100755 --- a/tasmota/xdrv_10_scripter.ino +++ b/tasmota/xdrv_10_scripter.ino @@ -1276,6 +1276,7 @@ chknext: #endif //USE_ENERGY_SENSOR break; case 'f': +//#define DEBUG_FS #ifdef USE_SCRIPT_FATFS if (!strncmp(vname,"fo(",3)) { lp+=3; @@ -1304,6 +1305,9 @@ chknext: for (uint8_t cnt=0;cnt=SFS_MAX) ind=SFS_MAX-1; - glob_script_mem.files[ind].close(); - glob_script_mem.file_flags[ind].is_open=0; + if (fvar>=0) { + uint8_t ind=fvar; + if (ind>=SFS_MAX) ind=SFS_MAX-1; +#ifdef DEBUG_FS + AddLog_P2(LOG_LEVEL_INFO,PSTR("closing file %d"),ind); +#endif + glob_script_mem.files[ind].close(); + glob_script_mem.file_flags[ind].is_open=0; + } fvar=0; lp++; len=0; @@ -2951,7 +2966,6 @@ int16_t Run_Scripter(const char *type, int8_t tlen, char *js) { toLogEOL("for error",lp); } } else if (!strncmp(lp,"next",4)) { - lp+=4; lp_next=lp; if (floop>0) { // for next loop @@ -4300,8 +4314,7 @@ void Script_Check_Hue(String *response) { uint8_t hue_script_found=Run_Scripter(">H",-2,0); if (hue_script_found!=99) return; - char line[128]; - char tmp[128]; + char tmp[256]; uint8_t hue_devs=0; uint8_t vindex=0; char *cp; @@ -4316,17 +4329,7 @@ void Script_Check_Hue(String *response) { } if (*lp!=';') { // check this line - memcpy(line,lp,sizeof(line)); - line[sizeof(line)-1]=0; - cp=line; - for (uint32_t i=0; im",-2,0); if (msect==99) { - char line[128]; - char tmp[128]; + char tmp[256]; char *lp=glob_script_mem.section_ptr+2; while (lp) { while (*lp==SCRIPT_EOL) { @@ -5530,17 +5532,7 @@ uint8_t msect=Run_Scripter(">m",-2,0); } if (*lp!=';') { // send this line to smtp - memcpy(line,lp,sizeof(line)); - line[sizeof(line)-1]=0; - char *cp=line; - for (uint32_t i=0; iprintln(tmp); func(tmp); } @@ -5563,8 +5555,7 @@ uint8_t msect=Run_Scripter(">m",-2,0); void ScriptJsonAppend(void) { uint8_t web_script=Run_Scripter(">J",-2,0); if (web_script==99) { - char line[128]; - char tmp[128]; + char tmp[256]; char *lp=glob_script_mem.section_ptr+2; while (lp) { while (*lp==SCRIPT_EOL) { @@ -5575,17 +5566,7 @@ void ScriptJsonAppend(void) { } if (*lp!=';') { // send this line to mqtt - memcpy(line,lp,sizeof(line)); - line[sizeof(line)-1]=0; - char *cp=line; - for (uint32_t i=0; it1",3,0); + } +} + +void script_task2(void *arg) { + //uint32_t lastms=millis(); + //uint32_t time; + while (1) { + //time=millis()-lastms; + //lastms=millis(); + //time=esp32_tasks[1].task_timer-time; + //if (timet2",3,0); + } +} +uint32_t scripter_create_task(uint32_t num, uint32_t time, uint32_t core) { + //return 0; + BaseType_t res = 0; + if (core > 1) { core = 1; } + if (num == 1) { + if (esp32_tasks[0].task_t) { vTaskDelete(esp32_tasks[0].task_t); } + res = xTaskCreatePinnedToCore(script_task1, "T1", STASK_STACK, NULL, STASK_PRIO, &esp32_tasks[0].task_t, core); + esp32_tasks[0].task_timer = time; + } else { + if (esp32_tasks[1].task_t) { vTaskDelete(esp32_tasks[1].task_t); } + res = xTaskCreatePinnedToCore(script_task2, "T2", STASK_STACK, NULL, STASK_PRIO, &esp32_tasks[1].task_t, core); + esp32_tasks[1].task_timer = time; + } + return res; +} +#else + uint16_t task_timer1; uint16_t task_timer2; TaskHandle_t task_t1; @@ -5625,13 +5667,6 @@ void script_task2(void *arg) { Run_Scripter(">t2",3,0); } } -#ifndef STASK_STACK -#define STASK_STACK 4096 -#endif - -#ifndef STASK_PRIO -#define STASK_PRIO 5 -#endif uint32_t scripter_create_task(uint32_t num, uint32_t time, uint32_t core) { //return 0; @@ -5648,6 +5683,8 @@ uint32_t scripter_create_task(uint32_t num, uint32_t time, uint32_t core) { } return res; } +#endif + #endif // USE_SCRIPT_TASK #endif // ESP32 /*********************************************************************************************\ From 54806fd527357603e6f60477d73ebf04a6f3b48b Mon Sep 17 00:00:00 2001 From: gemu2015 Date: Sat, 30 May 2020 15:29:47 +0200 Subject: [PATCH 2/2] files system update --- tasmota/xdrv_10_scripter.ino | 145 +++++++++++++++++++++++------------ tasmota/xdrv_13_display.ino | 21 +++-- 2 files changed, 110 insertions(+), 56 deletions(-) diff --git a/tasmota/xdrv_10_scripter.ino b/tasmota/xdrv_10_scripter.ino index a2562a36c..0edfad516 100755 --- a/tasmota/xdrv_10_scripter.ino +++ b/tasmota/xdrv_10_scripter.ino @@ -64,7 +64,6 @@ keywords if then else endif, or, and are better readable for beginners (others m #define MAX_SCRIPT_SIZE MAX_RULE_SIZE*MAX_RULE_SETS - uint32_t EncodeLightId(uint8_t relay_id); uint32_t DecodeLightId(uint32_t hue_id); @@ -79,30 +78,40 @@ uint32_t DecodeLightId(uint32_t hue_id); #endif #endif // USE_SCRIPT_COMPRESSION -#if defined(ESP32) && defined(ESP32_SCRIPT_SIZE) && !defined(USE_24C256) && !defined(USE_SCRIPT_FATFS) +#if (defined(LITTLEFS_SCRIPT_SIZE) && !defined(USE_24C256) && !defined(USE_SCRIPT_FATFS)) || (USE_SCRIPT_FATFS==-1) + +#ifdef ESP32 #include "FS.h" #include "SPIFFS.h" +#else +#include +#endif +FS *fsp; void SaveFile(const char *name,const uint8_t *buf,uint32_t len) { - File file = SPIFFS.open(name, FILE_WRITE); + File file = fsp->open(name, "w"); if (!file) return; file.write(buf, len); file.close(); } #define FORMAT_SPIFFS_IF_FAILED true -uint8_t spiffs_mounted=0; +uint8_t fs_mounted=0; void LoadFile(const char *name,uint8_t *buf,uint32_t len) { - if (!spiffs_mounted) { + if (!fs_mounted) { +#ifdef ESP32 if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){ +#else + if(!fsp->begin()){ +#endif //Serial.println("SPIFFS Mount Failed"); return; } - spiffs_mounted=1; + fs_mounted=1; } - File file = SPIFFS.open(name); + File file = fsp->open(name, "r"); if (!file) return; file.read(buf, len); file.close(); @@ -116,29 +125,43 @@ enum {OPER_EQU=1,OPER_PLS,OPER_MIN,OPER_MUL,OPER_DIV,OPER_PLSEQU,OPER_MINEQU,OPE enum {SCRIPT_LOGLEVEL=1,SCRIPT_TELEPERIOD}; #ifdef USE_SCRIPT_FATFS + +#if USE_SCRIPT_FATFS>=0 #include - -//#define USE_MMC - -#ifdef USE_MMC -#include -#undef FS_USED -#define FS_USED SD_MMC -#else #include -#undef FS_USED -#define FS_USED SD +#ifdef ESP32 +FS *fsp; +#else +SDClass *fsp; +#endif #endif #ifndef ESP32 +// esp8266 + +#if USE_SCRIPT_FATFS>=0 +// old fs #undef FILE_WRITE #define FILE_WRITE (sdfat::O_READ | sdfat::O_WRITE | sdfat::O_CREAT) #define FILE_APPEND (sdfat::O_READ | sdfat::O_WRITE | sdfat::O_CREAT | sdfat::O_APPEND) + +#else +// new fs +#undef FILE_WRITE +#define FILE_WRITE "w" +#undef FILE_READ +#define FILE_READ "r" +#undef FILE_APPEND +#define FILE_APPEND "a" #endif +#endif // USE_SCRIPT_FATFS>=0 + + #ifndef FAT_SCRIPT_SIZE #define FAT_SCRIPT_SIZE 4096 #endif + #ifdef ESP32 #undef FAT_SCRIPT_NAME #define FAT_SCRIPT_NAME "/script.txt" @@ -150,7 +173,8 @@ enum {SCRIPT_LOGLEVEL=1,SCRIPT_TELEPERIOD}; #if USE_STANDARD_SPI_LIBRARY==0 #warning ("FATFS standard spi should be used"); #endif -#endif + +#endif // USE_SCRIPT_FATFS #ifdef SUPPORT_MQTT_EVENT #include // Import LinkedList library @@ -624,10 +648,18 @@ char *script; #ifdef USE_SCRIPT_FATFS if (!glob_script_mem.script_sd_found) { + +#if USE_SCRIPT_FATFS>=0 + fsp=&SD; + #ifdef USE_MMC - if (FS_USED.begin()) { + if (fsp->begin()) { #else - if (FS_USED.begin(USE_SCRIPT_FATFS)) { + if (SD.begin(USE_SCRIPT_FATFS)) { +#endif + +#else + if (fsp->begin()) { #endif glob_script_mem.script_sd_found=1; } else { @@ -1308,7 +1340,7 @@ chknext: #ifdef DEBUG_FS AddLog_P2(LOG_LEVEL_INFO,PSTR("open file for read %d"),cnt); #endif - glob_script_mem.files[cnt]=FS_USED.open(str,FILE_READ); + glob_script_mem.files[cnt]=fsp->open(str,FILE_READ); if (glob_script_mem.files[cnt].isDirectory()) { glob_script_mem.files[cnt].rewindDirectory(); glob_script_mem.file_flags[cnt].is_dir=1; @@ -1318,12 +1350,12 @@ chknext: } else { if (mode==1) { - glob_script_mem.files[cnt]=FS_USED.open(str,FILE_WRITE); + glob_script_mem.files[cnt]=fsp->open(str,FILE_WRITE); #ifdef DEBUG_FS AddLog_P2(LOG_LEVEL_INFO,PSTR("open file for write %d"),cnt); #endif } else { - glob_script_mem.files[cnt]=FS_USED.open(str,FILE_APPEND); + glob_script_mem.files[cnt]=fsp->open(str,FILE_APPEND); #ifdef DEBUG_FS AddLog_P2(LOG_LEVEL_INFO,PSTR("open file for append %d"),cnt); #endif @@ -1465,7 +1497,7 @@ chknext: lp+=3; char str[glob_script_mem.max_ssize+1]; lp=GetStringResult(lp,OPER_EQU,str,0); - FS_USED.remove(str); + fsp->remove(str); lp++; len=0; goto exit; @@ -1505,7 +1537,7 @@ chknext: char str[glob_script_mem.max_ssize+1]; lp=GetStringResult(lp,OPER_EQU,str,0); // execute script - File ef=FS_USED.open(str); + File ef=fsp->open(str,FILE_READ); if (ef) { uint16_t fsiz=ef.size(); if (fsiz<2048) { @@ -1527,7 +1559,7 @@ chknext: lp+=4; char str[glob_script_mem.max_ssize+1]; lp=GetStringResult(lp,OPER_EQU,str,0); - fvar=FS_USED.mkdir(str); + fvar=fsp->mkdir(str); lp++; len=0; goto exit; @@ -1536,7 +1568,7 @@ chknext: lp+=4; char str[glob_script_mem.max_ssize+1]; lp=GetStringResult(lp,OPER_EQU,str,0); - fvar=FS_USED.rmdir(str); + fvar=fsp->rmdir(str); lp++; len=0; goto exit; @@ -1545,7 +1577,7 @@ chknext: lp+=3; char str[glob_script_mem.max_ssize+1]; lp=GetStringResult(lp,OPER_EQU,str,0); - if (FS_USED.exists(str)) fvar=1; + if (fsp->exists(str)) fvar=1; else fvar=0; lp++; len=0; @@ -3714,7 +3746,7 @@ void ListDir(char *path, uint8_t depth) { char format[12]; sprintf(format,"%%-%ds",24-depth); - File dir=FS_USED.open(path); + File dir=fsp->open(path, FILE_READ); if (dir) { dir.rewindDirectory(); if (strlen(path)>1) { @@ -3836,8 +3868,8 @@ void script_upload(void) { if (upload.status == UPLOAD_FILE_START) { char npath[48]; sprintf(npath,"%s/%s",path,upload.filename.c_str()); - FS_USED.remove(npath); - upload_file=FS_USED.open(npath,FILE_WRITE); + fsp->remove(npath); + upload_file=fsp->open(npath,FILE_WRITE); if (!upload_file) Web.upload_error=1; } else if(upload.status == UPLOAD_FILE_WRITE) { if (upload_file) upload_file.write(upload.buf,upload.currentSize); @@ -3856,12 +3888,12 @@ uint8_t DownloadFile(char *file) { File download_file; WiFiClient download_Client; - if (!FS_USED.exists(file)) { + if (!fsp->exists(file)) { AddLog_P(LOG_LEVEL_INFO,PSTR("file not found")); return 0; } - download_file=FS_USED.open(file,FILE_READ); + download_file=fsp->open(file,FILE_READ); if (!download_file) { AddLog_P(LOG_LEVEL_INFO,PSTR("could not open file")); return 0; @@ -4041,16 +4073,16 @@ void ScriptSaveSettings(void) { #if !defined(USE_24C256) && defined(USE_SCRIPT_FATFS) if (glob_script_mem.flags&1) { - FS_USED.remove(FAT_SCRIPT_NAME); - File file=FS_USED.open(FAT_SCRIPT_NAME,FILE_WRITE); + fsp->remove(FAT_SCRIPT_NAME); + File file=fsp->open(FAT_SCRIPT_NAME,FILE_WRITE); file.write((const uint8_t*)glob_script_mem.script_ram,FAT_SCRIPT_SIZE); file.close(); } #endif -#if defined(ESP32) && defined(ESP32_SCRIPT_SIZE) && !defined(USE_24C256) && !defined(USE_SCRIPT_FATFS) +#if defined(LITTLEFS_SCRIPT_SIZE) && !defined(USE_24C256) && !defined(USE_SCRIPT_FATFS) if (glob_script_mem.flags&1) { - SaveFile("/script.txt",(uint8_t*)glob_script_mem.script_ram,ESP32_SCRIPT_SIZE); + SaveFile("/script.txt",(uint8_t*)glob_script_mem.script_ram,LITTLEFS_SCRIPT_SIZE); } #endif } @@ -4065,7 +4097,7 @@ void ScriptSaveSettings(void) { #ifdef USE_SCRIPT_COMPRESSION #ifndef USE_24C256 #ifndef USE_SCRIPT_FATFS -#ifndef ESP32_SCRIPT_SIZE +#ifndef LITTLEFS_SCRIPT_SIZE //AddLog_P2(LOG_LEVEL_INFO,PSTR("in string: %s len = %d"),glob_script_mem.script_ram,strlen(glob_script_mem.script_ram)); uint32_t len_compressed = SCRIPT_COMPRESS(glob_script_mem.script_ram, strlen(glob_script_mem.script_ram), Settings.rules[0], MAX_SCRIPT_SIZE-1); @@ -5710,7 +5742,7 @@ bool Xdrv10(uint8_t function) #ifdef USE_SCRIPT_COMPRESSION #ifndef USE_24C256 #ifndef USE_SCRIPT_FATFS -#ifndef ESP32_SCRIPT_SIZE +#ifndef LITTLEFS_SCRIPT_SIZE int32_t len_decompressed; sprt=(char*)calloc(UNISHOXRSIZE+8,1); if (!sprt) { break; } @@ -5756,10 +5788,14 @@ bool Xdrv10(uint8_t function) #endif #endif + #ifdef USE_SCRIPT_FATFS +#if USE_SCRIPT_FATFS>=0 + fsp = &SD; + #ifdef USE_MMC - if (FS_USED.begin()) { + if (fsp->begin()) { #else #ifdef ESP32 @@ -5767,10 +5803,15 @@ bool Xdrv10(uint8_t function) SPI.begin(Pin(GPIO_SPI_CLK),Pin(GPIO_SPI_MISO),Pin(GPIO_SPI_MOSI), -1); } #endif - if (FS_USED.begin(USE_SCRIPT_FATFS)) { + if (SD.begin(USE_SCRIPT_FATFS)) { #endif - //FS_USED.dateTimeCallback(dateTime); +#else + fsp = &LittleFS; + if (fsp->begin()) { +#endif + + //fsp->dateTimeCallback(dateTime); glob_script_mem.script_sd_found=1; char *script; @@ -5778,8 +5819,8 @@ bool Xdrv10(uint8_t function) if (!script) break; glob_script_mem.script_ram=script; glob_script_mem.script_size=FAT_SCRIPT_SIZE; - if (FS_USED.exists(FAT_SCRIPT_NAME)) { - File file=FS_USED.open(FAT_SCRIPT_NAME,FILE_READ); + if (fsp->exists(FAT_SCRIPT_NAME)) { + File file=fsp->open(FAT_SCRIPT_NAME,FILE_READ); file.read((uint8_t*)script,FAT_SCRIPT_SIZE); file.close(); } @@ -5796,15 +5837,21 @@ bool Xdrv10(uint8_t function) #endif -#if defined(ESP32) && defined(ESP32_SCRIPT_SIZE) && !defined(USE_24C256) && !defined(USE_SCRIPT_FATFS) +#if defined(LITTLEFS_SCRIPT_SIZE) && !defined(USE_24C256) && !defined(USE_SCRIPT_FATFS) + +#ifdef ESP32 + fsp = &SPIFFS; +#else + fsp = &LittleFS; +#endif char *script; - script=(char*)calloc(ESP32_SCRIPT_SIZE+4,1); + script=(char*)calloc(LITTLEFS_SCRIPT_SIZE+4,1); if (!script) break; - LoadFile("/script.txt",(uint8_t*)script,ESP32_SCRIPT_SIZE); + LoadFile("/script.txt",(uint8_t*)script,LITTLEFS_SCRIPT_SIZE); glob_script_mem.script_ram=script; - glob_script_mem.script_size=ESP32_SCRIPT_SIZE; - script[ESP32_SCRIPT_SIZE-1]=0; + glob_script_mem.script_size=LITTLEFS_SCRIPT_SIZE; + script[LITTLEFS_SCRIPT_SIZE-1]=0; // use rules storage for permanent vars glob_script_mem.script_pram=(uint8_t*)Settings.rules[0]; glob_script_mem.script_pram_size=MAX_SCRIPT_SIZE; diff --git a/tasmota/xdrv_13_display.ino b/tasmota/xdrv_13_display.ino index d2466d051..43245c794 100644 --- a/tasmota/xdrv_13_display.ino +++ b/tasmota/xdrv_13_display.ino @@ -505,7 +505,7 @@ void DisplayText(void) cp += var; linebuf[fill] = 0; break; -#if defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT) +#if defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT) && USE_SCRIPT_FATFS>=0 case 'P': { char *ep=strchr(cp,':'); if (ep) { @@ -1510,8 +1510,13 @@ void rgb888_to_565(uint8_t *in, uint16_t *out, uint32_t len); #endif #endif +#if defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT) && USE_SCRIPT_FATFS>=0 -#if defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT) +#ifdef ESP32 +extern FS *fsp; +#else +extern SDClass *fsp; +#endif #define XBUFF_LEN 128 void Draw_RGB_Bitmap(char *file,uint16_t xp, uint16_t yp) { if (!renderer) return; @@ -1527,7 +1532,7 @@ void Draw_RGB_Bitmap(char *file,uint16_t xp, uint16_t yp) { if (!strcmp(estr,"rgb")) { // special rgb format - fp=SD.open(file,FILE_READ); + fp=fsp->open(file,FILE_READ); if (!fp) return; uint16_t xsize; fp.read((uint8_t*)&xsize,2); @@ -1564,7 +1569,7 @@ void Draw_RGB_Bitmap(char *file,uint16_t xp, uint16_t yp) { #ifdef ESP32 #ifdef JPEG_PICTS if (psramFound()) { - fp=SD.open(file,FILE_READ); + fp=fsp->open(file,FILE_READ); if (!fp) return; uint32_t size = fp.size(); uint8_t *mem = (uint8_t *)heap_caps_malloc(size+4, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); @@ -1850,7 +1855,9 @@ void DisplayCheckGraph() { #if defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT) +#ifdef ESP32 #include +#endif void Save_graph(uint8_t num, char *path) { if (!renderer) return; @@ -1858,8 +1865,8 @@ void Save_graph(uint8_t num, char *path) { struct GRAPH *gp=graph[index]; if (!gp) return; File fp; - SD.remove(path); - fp=SD.open(path,FILE_WRITE); + fsp->remove(path); + fp=fsp->open(path,FILE_WRITE); if (!fp) return; char str[32]; sprintf_P(str,PSTR("%d\t%d\t%d\t"),gp->xcnt,gp->xs,gp->ys); @@ -1884,7 +1891,7 @@ void Restore_graph(uint8_t num, char *path) { struct GRAPH *gp=graph[index]; if (!gp) return; File fp; - fp=SD.open(path,FILE_READ); + fp=fsp->open(path,FILE_READ); if (!fp) return; char vbuff[32]; char *cp=vbuff;