Merge pull request #8582 from gemu2015/scripter-fix

Scripter fix + little fs support
This commit is contained in:
Theo Arends 2020-05-30 15:51:31 +02:00 committed by GitHub
commit 1ddf65e88c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 198 additions and 107 deletions

View File

@ -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 <LittleFS.h>
#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 <SPI.h>
//#define USE_MMC
#ifdef USE_MMC
#include <SD_MMC.h>
#undef FS_USED
#define FS_USED SD_MMC
#else
#include <SD.h>
#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 <LinkedList.h> // 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 {
@ -1276,6 +1308,7 @@ chknext:
#endif //USE_ENERGY_SENSOR
break;
case 'f':
//#define DEBUG_FS
#ifdef USE_SCRIPT_FATFS
if (!strncmp(vname,"fo(",3)) {
lp+=3;
@ -1304,7 +1337,10 @@ chknext:
for (uint8_t cnt=0;cnt<SFS_MAX;cnt++) {
if (!glob_script_mem.file_flags[cnt].is_open) {
if (mode==0) {
glob_script_mem.files[cnt]=FS_USED.open(str,FILE_READ);
#ifdef DEBUG_FS
AddLog_P2(LOG_LEVEL_INFO,PSTR("open file for read %d"),cnt);
#endif
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;
@ -1314,9 +1350,15 @@ 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
}
}
if (glob_script_mem.files[cnt]) {
@ -1335,10 +1377,15 @@ chknext:
if (!strncmp(vname,"fc(",3)) {
lp+=3;
lp=GetNumericResult(lp,OPER_EQU,&fvar,0);
uint8_t ind=fvar;
if (ind>=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;
@ -1450,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;
@ -1490,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) {
@ -1512,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;
@ -1521,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;
@ -1530,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;
@ -2951,7 +2998,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
@ -3700,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) {
@ -3822,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);
@ -3842,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;
@ -4027,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
}
@ -4051,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);
@ -4300,8 +4346,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 +4361,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; i<sizeof(line); i++) {
if (!*cp || *cp=='\n' || *cp=='\r') {
*cp=0;
break;
}
cp++;
}
Replace_Cmd_Vars(line,0,tmp,sizeof(tmp));
Replace_Cmd_Vars(lp,1,tmp,sizeof(tmp));
// check for hue defintions
// NAME, TYPE , vars
cp=tmp;
@ -5518,8 +5553,7 @@ nextwebline:
void script_send_email_body(void(*func)(char *)) {
uint8_t msect=Run_Scripter(">m",-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 +5564,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; i<sizeof(line); i++) {
if (!*cp || *cp=='\n' || *cp=='\r') {
*cp=0;
break;
}
cp++;
}
Replace_Cmd_Vars(line,0,tmp,sizeof(tmp));
Replace_Cmd_Vars(lp,1,tmp,sizeof(tmp));
//client->println(tmp);
func(tmp);
}
@ -5563,8 +5587,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 +5598,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; i<sizeof(line); i++) {
if (!*cp || *cp=='\n' || *cp=='\r') {
*cp=0;
break;
}
cp++;
}
Replace_Cmd_Vars(line,0,tmp,sizeof(tmp));
Replace_Cmd_Vars(lp,1,tmp,sizeof(tmp));
ResponseAppend_P(PSTR("%s"),tmp);
}
if (*lp==SCRIPT_EOL) {
@ -5607,6 +5620,67 @@ bool RulesProcessEvent(char *json_event) {
#ifdef ESP32
#ifdef USE_SCRIPT_TASK
#ifndef STASK_STACK
#define STASK_STACK 8192
#endif
#ifndef STASK_PRIO
#define STASK_PRIO 1
#endif
#if 1
struct ESP32_Task {
uint16_t task_timer;
TaskHandle_t task_t;
} esp32_tasks[2];
void script_task1(void *arg) {
//uint32_t lastms=millis();
//uint32_t time;
while (1) {
//time=millis()-lastms;
//lastms=millis();
//time=esp32_tasks[0].task_timer-time;
//if (time<esp32_tasks[1].task_timer) {delay(time); }
//if (time<=esp32_tasks[0].task_timer) {vTaskDelay( pdMS_TO_TICKS( time ) ); }
delay(esp32_tasks[0].task_timer);
Run_Scripter(">t1",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 (time<esp32_tasks[1].task_timer) {delay(time); }
//if (time<=esp32_tasks[1].task_timer) {vTaskDelay( pdMS_TO_TICKS( time ) ); }
delay(esp32_tasks[1].task_timer);
Run_Scripter(">t2",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 +5699,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 +5715,8 @@ uint32_t scripter_create_task(uint32_t num, uint32_t time, uint32_t core) {
}
return res;
}
#endif
#endif // USE_SCRIPT_TASK
#endif // ESP32
/*********************************************************************************************\
@ -5673,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; }
@ -5719,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
@ -5730,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;
@ -5741,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();
}
@ -5759,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;

View File

@ -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 <SD.h>
#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;