files system update

This commit is contained in:
gemu2015 2020-05-30 15:29:47 +02:00
parent 7f1514e6da
commit 54806fd527
2 changed files with 110 additions and 56 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 #define MAX_SCRIPT_SIZE MAX_RULE_SIZE*MAX_RULE_SETS
uint32_t EncodeLightId(uint8_t relay_id); uint32_t EncodeLightId(uint8_t relay_id);
uint32_t DecodeLightId(uint32_t hue_id); uint32_t DecodeLightId(uint32_t hue_id);
@ -79,30 +78,40 @@ uint32_t DecodeLightId(uint32_t hue_id);
#endif #endif
#endif // USE_SCRIPT_COMPRESSION #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 "FS.h"
#include "SPIFFS.h" #include "SPIFFS.h"
#else
#include <LittleFS.h>
#endif
FS *fsp;
void SaveFile(const char *name,const uint8_t *buf,uint32_t len) { 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; if (!file) return;
file.write(buf, len); file.write(buf, len);
file.close(); file.close();
} }
#define FORMAT_SPIFFS_IF_FAILED true #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) { 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)){ if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){
#else
if(!fsp->begin()){
#endif
//Serial.println("SPIFFS Mount Failed"); //Serial.println("SPIFFS Mount Failed");
return; return;
} }
spiffs_mounted=1; fs_mounted=1;
} }
File file = SPIFFS.open(name); File file = fsp->open(name, "r");
if (!file) return; if (!file) return;
file.read(buf, len); file.read(buf, len);
file.close(); 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}; enum {SCRIPT_LOGLEVEL=1,SCRIPT_TELEPERIOD};
#ifdef USE_SCRIPT_FATFS #ifdef USE_SCRIPT_FATFS
#if USE_SCRIPT_FATFS>=0
#include <SPI.h> #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> #include <SD.h>
#undef FS_USED #ifdef ESP32
#define FS_USED SD FS *fsp;
#else
SDClass *fsp;
#endif
#endif #endif
#ifndef ESP32 #ifndef ESP32
// esp8266
#if USE_SCRIPT_FATFS>=0
// old fs
#undef FILE_WRITE #undef FILE_WRITE
#define FILE_WRITE (sdfat::O_READ | sdfat::O_WRITE | sdfat::O_CREAT) #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) #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
#endif // USE_SCRIPT_FATFS>=0
#ifndef FAT_SCRIPT_SIZE #ifndef FAT_SCRIPT_SIZE
#define FAT_SCRIPT_SIZE 4096 #define FAT_SCRIPT_SIZE 4096
#endif #endif
#ifdef ESP32 #ifdef ESP32
#undef FAT_SCRIPT_NAME #undef FAT_SCRIPT_NAME
#define FAT_SCRIPT_NAME "/script.txt" #define FAT_SCRIPT_NAME "/script.txt"
@ -150,7 +173,8 @@ enum {SCRIPT_LOGLEVEL=1,SCRIPT_TELEPERIOD};
#if USE_STANDARD_SPI_LIBRARY==0 #if USE_STANDARD_SPI_LIBRARY==0
#warning ("FATFS standard spi should be used"); #warning ("FATFS standard spi should be used");
#endif #endif
#endif
#endif // USE_SCRIPT_FATFS
#ifdef SUPPORT_MQTT_EVENT #ifdef SUPPORT_MQTT_EVENT
#include <LinkedList.h> // Import LinkedList library #include <LinkedList.h> // Import LinkedList library
@ -624,10 +648,18 @@ char *script;
#ifdef USE_SCRIPT_FATFS #ifdef USE_SCRIPT_FATFS
if (!glob_script_mem.script_sd_found) { if (!glob_script_mem.script_sd_found) {
#if USE_SCRIPT_FATFS>=0
fsp=&SD;
#ifdef USE_MMC #ifdef USE_MMC
if (FS_USED.begin()) { if (fsp->begin()) {
#else #else
if (FS_USED.begin(USE_SCRIPT_FATFS)) { if (SD.begin(USE_SCRIPT_FATFS)) {
#endif
#else
if (fsp->begin()) {
#endif #endif
glob_script_mem.script_sd_found=1; glob_script_mem.script_sd_found=1;
} else { } else {
@ -1308,7 +1340,7 @@ chknext:
#ifdef DEBUG_FS #ifdef DEBUG_FS
AddLog_P2(LOG_LEVEL_INFO,PSTR("open file for read %d"),cnt); AddLog_P2(LOG_LEVEL_INFO,PSTR("open file for read %d"),cnt);
#endif #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()) { if (glob_script_mem.files[cnt].isDirectory()) {
glob_script_mem.files[cnt].rewindDirectory(); glob_script_mem.files[cnt].rewindDirectory();
glob_script_mem.file_flags[cnt].is_dir=1; glob_script_mem.file_flags[cnt].is_dir=1;
@ -1318,12 +1350,12 @@ chknext:
} }
else { else {
if (mode==1) { 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 #ifdef DEBUG_FS
AddLog_P2(LOG_LEVEL_INFO,PSTR("open file for write %d"),cnt); AddLog_P2(LOG_LEVEL_INFO,PSTR("open file for write %d"),cnt);
#endif #endif
} else { } 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 #ifdef DEBUG_FS
AddLog_P2(LOG_LEVEL_INFO,PSTR("open file for append %d"),cnt); AddLog_P2(LOG_LEVEL_INFO,PSTR("open file for append %d"),cnt);
#endif #endif
@ -1465,7 +1497,7 @@ chknext:
lp+=3; lp+=3;
char str[glob_script_mem.max_ssize+1]; char str[glob_script_mem.max_ssize+1];
lp=GetStringResult(lp,OPER_EQU,str,0); lp=GetStringResult(lp,OPER_EQU,str,0);
FS_USED.remove(str); fsp->remove(str);
lp++; lp++;
len=0; len=0;
goto exit; goto exit;
@ -1505,7 +1537,7 @@ chknext:
char str[glob_script_mem.max_ssize+1]; char str[glob_script_mem.max_ssize+1];
lp=GetStringResult(lp,OPER_EQU,str,0); lp=GetStringResult(lp,OPER_EQU,str,0);
// execute script // execute script
File ef=FS_USED.open(str); File ef=fsp->open(str,FILE_READ);
if (ef) { if (ef) {
uint16_t fsiz=ef.size(); uint16_t fsiz=ef.size();
if (fsiz<2048) { if (fsiz<2048) {
@ -1527,7 +1559,7 @@ chknext:
lp+=4; lp+=4;
char str[glob_script_mem.max_ssize+1]; char str[glob_script_mem.max_ssize+1];
lp=GetStringResult(lp,OPER_EQU,str,0); lp=GetStringResult(lp,OPER_EQU,str,0);
fvar=FS_USED.mkdir(str); fvar=fsp->mkdir(str);
lp++; lp++;
len=0; len=0;
goto exit; goto exit;
@ -1536,7 +1568,7 @@ chknext:
lp+=4; lp+=4;
char str[glob_script_mem.max_ssize+1]; char str[glob_script_mem.max_ssize+1];
lp=GetStringResult(lp,OPER_EQU,str,0); lp=GetStringResult(lp,OPER_EQU,str,0);
fvar=FS_USED.rmdir(str); fvar=fsp->rmdir(str);
lp++; lp++;
len=0; len=0;
goto exit; goto exit;
@ -1545,7 +1577,7 @@ chknext:
lp+=3; lp+=3;
char str[glob_script_mem.max_ssize+1]; char str[glob_script_mem.max_ssize+1];
lp=GetStringResult(lp,OPER_EQU,str,0); lp=GetStringResult(lp,OPER_EQU,str,0);
if (FS_USED.exists(str)) fvar=1; if (fsp->exists(str)) fvar=1;
else fvar=0; else fvar=0;
lp++; lp++;
len=0; len=0;
@ -3714,7 +3746,7 @@ void ListDir(char *path, uint8_t depth) {
char format[12]; char format[12];
sprintf(format,"%%-%ds",24-depth); sprintf(format,"%%-%ds",24-depth);
File dir=FS_USED.open(path); File dir=fsp->open(path, FILE_READ);
if (dir) { if (dir) {
dir.rewindDirectory(); dir.rewindDirectory();
if (strlen(path)>1) { if (strlen(path)>1) {
@ -3836,8 +3868,8 @@ void script_upload(void) {
if (upload.status == UPLOAD_FILE_START) { if (upload.status == UPLOAD_FILE_START) {
char npath[48]; char npath[48];
sprintf(npath,"%s/%s",path,upload.filename.c_str()); sprintf(npath,"%s/%s",path,upload.filename.c_str());
FS_USED.remove(npath); fsp->remove(npath);
upload_file=FS_USED.open(npath,FILE_WRITE); upload_file=fsp->open(npath,FILE_WRITE);
if (!upload_file) Web.upload_error=1; if (!upload_file) Web.upload_error=1;
} else if(upload.status == UPLOAD_FILE_WRITE) { } else if(upload.status == UPLOAD_FILE_WRITE) {
if (upload_file) upload_file.write(upload.buf,upload.currentSize); if (upload_file) upload_file.write(upload.buf,upload.currentSize);
@ -3856,12 +3888,12 @@ uint8_t DownloadFile(char *file) {
File download_file; File download_file;
WiFiClient download_Client; WiFiClient download_Client;
if (!FS_USED.exists(file)) { if (!fsp->exists(file)) {
AddLog_P(LOG_LEVEL_INFO,PSTR("file not found")); AddLog_P(LOG_LEVEL_INFO,PSTR("file not found"));
return 0; return 0;
} }
download_file=FS_USED.open(file,FILE_READ); download_file=fsp->open(file,FILE_READ);
if (!download_file) { if (!download_file) {
AddLog_P(LOG_LEVEL_INFO,PSTR("could not open file")); AddLog_P(LOG_LEVEL_INFO,PSTR("could not open file"));
return 0; return 0;
@ -4041,16 +4073,16 @@ void ScriptSaveSettings(void) {
#if !defined(USE_24C256) && defined(USE_SCRIPT_FATFS) #if !defined(USE_24C256) && defined(USE_SCRIPT_FATFS)
if (glob_script_mem.flags&1) { if (glob_script_mem.flags&1) {
FS_USED.remove(FAT_SCRIPT_NAME); fsp->remove(FAT_SCRIPT_NAME);
File file=FS_USED.open(FAT_SCRIPT_NAME,FILE_WRITE); File file=fsp->open(FAT_SCRIPT_NAME,FILE_WRITE);
file.write((const uint8_t*)glob_script_mem.script_ram,FAT_SCRIPT_SIZE); file.write((const uint8_t*)glob_script_mem.script_ram,FAT_SCRIPT_SIZE);
file.close(); file.close();
} }
#endif #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) { 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 #endif
} }
@ -4065,7 +4097,7 @@ void ScriptSaveSettings(void) {
#ifdef USE_SCRIPT_COMPRESSION #ifdef USE_SCRIPT_COMPRESSION
#ifndef USE_24C256 #ifndef USE_24C256
#ifndef USE_SCRIPT_FATFS #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)); //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); 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 #ifdef USE_SCRIPT_COMPRESSION
#ifndef USE_24C256 #ifndef USE_24C256
#ifndef USE_SCRIPT_FATFS #ifndef USE_SCRIPT_FATFS
#ifndef ESP32_SCRIPT_SIZE #ifndef LITTLEFS_SCRIPT_SIZE
int32_t len_decompressed; int32_t len_decompressed;
sprt=(char*)calloc(UNISHOXRSIZE+8,1); sprt=(char*)calloc(UNISHOXRSIZE+8,1);
if (!sprt) { break; } if (!sprt) { break; }
@ -5756,10 +5788,14 @@ bool Xdrv10(uint8_t function)
#endif #endif
#endif #endif
#ifdef USE_SCRIPT_FATFS #ifdef USE_SCRIPT_FATFS
#if USE_SCRIPT_FATFS>=0
fsp = &SD;
#ifdef USE_MMC #ifdef USE_MMC
if (FS_USED.begin()) { if (fsp->begin()) {
#else #else
#ifdef ESP32 #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); SPI.begin(Pin(GPIO_SPI_CLK),Pin(GPIO_SPI_MISO),Pin(GPIO_SPI_MOSI), -1);
} }
#endif #endif
if (FS_USED.begin(USE_SCRIPT_FATFS)) { if (SD.begin(USE_SCRIPT_FATFS)) {
#endif #endif
//FS_USED.dateTimeCallback(dateTime); #else
fsp = &LittleFS;
if (fsp->begin()) {
#endif
//fsp->dateTimeCallback(dateTime);
glob_script_mem.script_sd_found=1; glob_script_mem.script_sd_found=1;
char *script; char *script;
@ -5778,8 +5819,8 @@ bool Xdrv10(uint8_t function)
if (!script) break; if (!script) break;
glob_script_mem.script_ram=script; glob_script_mem.script_ram=script;
glob_script_mem.script_size=FAT_SCRIPT_SIZE; glob_script_mem.script_size=FAT_SCRIPT_SIZE;
if (FS_USED.exists(FAT_SCRIPT_NAME)) { if (fsp->exists(FAT_SCRIPT_NAME)) {
File file=FS_USED.open(FAT_SCRIPT_NAME,FILE_READ); File file=fsp->open(FAT_SCRIPT_NAME,FILE_READ);
file.read((uint8_t*)script,FAT_SCRIPT_SIZE); file.read((uint8_t*)script,FAT_SCRIPT_SIZE);
file.close(); file.close();
} }
@ -5796,15 +5837,21 @@ bool Xdrv10(uint8_t function)
#endif #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; char *script;
script=(char*)calloc(ESP32_SCRIPT_SIZE+4,1); script=(char*)calloc(LITTLEFS_SCRIPT_SIZE+4,1);
if (!script) break; 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_ram=script;
glob_script_mem.script_size=ESP32_SCRIPT_SIZE; glob_script_mem.script_size=LITTLEFS_SCRIPT_SIZE;
script[ESP32_SCRIPT_SIZE-1]=0; script[LITTLEFS_SCRIPT_SIZE-1]=0;
// use rules storage for permanent vars // use rules storage for permanent vars
glob_script_mem.script_pram=(uint8_t*)Settings.rules[0]; glob_script_mem.script_pram=(uint8_t*)Settings.rules[0];
glob_script_mem.script_pram_size=MAX_SCRIPT_SIZE; glob_script_mem.script_pram_size=MAX_SCRIPT_SIZE;

View File

@ -505,7 +505,7 @@ void DisplayText(void)
cp += var; cp += var;
linebuf[fill] = 0; linebuf[fill] = 0;
break; break;
#if defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT) #if defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT) && USE_SCRIPT_FATFS>=0
case 'P': case 'P':
{ char *ep=strchr(cp,':'); { char *ep=strchr(cp,':');
if (ep) { if (ep) {
@ -1510,8 +1510,13 @@ void rgb888_to_565(uint8_t *in, uint16_t *out, uint32_t len);
#endif #endif
#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 #define XBUFF_LEN 128
void Draw_RGB_Bitmap(char *file,uint16_t xp, uint16_t yp) { void Draw_RGB_Bitmap(char *file,uint16_t xp, uint16_t yp) {
if (!renderer) return; if (!renderer) return;
@ -1527,7 +1532,7 @@ void Draw_RGB_Bitmap(char *file,uint16_t xp, uint16_t yp) {
if (!strcmp(estr,"rgb")) { if (!strcmp(estr,"rgb")) {
// special rgb format // special rgb format
fp=SD.open(file,FILE_READ); fp=fsp->open(file,FILE_READ);
if (!fp) return; if (!fp) return;
uint16_t xsize; uint16_t xsize;
fp.read((uint8_t*)&xsize,2); 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 ESP32
#ifdef JPEG_PICTS #ifdef JPEG_PICTS
if (psramFound()) { if (psramFound()) {
fp=SD.open(file,FILE_READ); fp=fsp->open(file,FILE_READ);
if (!fp) return; if (!fp) return;
uint32_t size = fp.size(); uint32_t size = fp.size();
uint8_t *mem = (uint8_t *)heap_caps_malloc(size+4, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); 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) #if defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT)
#ifdef ESP32
#include <SD.h> #include <SD.h>
#endif
void Save_graph(uint8_t num, char *path) { void Save_graph(uint8_t num, char *path) {
if (!renderer) return; if (!renderer) return;
@ -1858,8 +1865,8 @@ void Save_graph(uint8_t num, char *path) {
struct GRAPH *gp=graph[index]; struct GRAPH *gp=graph[index];
if (!gp) return; if (!gp) return;
File fp; File fp;
SD.remove(path); fsp->remove(path);
fp=SD.open(path,FILE_WRITE); fp=fsp->open(path,FILE_WRITE);
if (!fp) return; if (!fp) return;
char str[32]; char str[32];
sprintf_P(str,PSTR("%d\t%d\t%d\t"),gp->xcnt,gp->xs,gp->ys); 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]; struct GRAPH *gp=graph[index];
if (!gp) return; if (!gp) return;
File fp; File fp;
fp=SD.open(path,FILE_READ); fp=fsp->open(path,FILE_READ);
if (!fp) return; if (!fp) return;
char vbuff[32]; char vbuff[32];
char *cp=vbuff; char *cp=vbuff;