diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index ba4f006..a0d917a 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -43,8 +43,6 @@ #include "Arduino.h" #include #include "AsyncTCP.h" -// LittleFS auto format -#define FORMAT_LITTLEFS_IF_FAILED true #endif // https://github.com/arduino/ArduinoCore-avr/tree/master/libraries/SPI @@ -121,26 +119,12 @@ void setup() { #ifdef ESP32 if(digitalRead(PinWIPE) == HIGH) { #endif - Serial.println("!! formatting LittleFS !!"); - - #ifdef ESP32 - LittleFS.begin(); - #endif - LittleFS.format(); + LFS_format(); Panic(); } - Serial.println(":: initialise LittleFS ::"); - #ifdef ESP8266 - if(!LittleFS.begin()) { - #endif - #ifdef ESP32 - if(!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)) { - #endif - Serial.println("!! LittleFS: failed initialising !!"); - Panic(); - } + LFS_init(); // read the configfile from LittleFS File lfs_configfile = LittleFS.open(configfile, "r"); diff --git a/Arduino/CanGrow/include/CanGrow_LittleFS.h b/Arduino/CanGrow/include/CanGrow_LittleFS.h index 81a908c..8d49b73 100644 --- a/Arduino/CanGrow/include/CanGrow_LittleFS.h +++ b/Arduino/CanGrow/include/CanGrow_LittleFS.h @@ -27,9 +27,265 @@ * */ -#ifdef ESP8266 +// LittleFS auto format +#define FORMAT_LITTLEFS_IF_FAILED true -#endif +void LFS_init() { + Serial.println(":: LittleFS: initializing"); + #ifdef ESP8266 + if(!LittleFS.begin()) { + #endif + #ifdef ESP32 + if(!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)) { + #endif + Serial.println("!! LittleFS: initializing FAILED !!"); + Panic(); + } +} -#ifdef ESP32 -#endif +void LFS_format() { + Serial.println(":: LittleFS: formatting"); + // ESP32 LittleFS needs begin() first, otherwise it would crash + // ESP8266 does not need it, so we leave it + #ifdef ESP32 + LittleFS.begin(); + #endif + if(LittleFS.format()) { + Serial.println(":: LittleFS: formatting done!"); + } else { + Serial.println("!! LittleFS: formatting FAILED !!"); + } +} + +void readFile() { + +} + +void writeFile() { + +} + +void deleteFile() { + +} + + +///* + //* ESP8266 functions + //*/ + +///*functions from https://github.com/esp8266/Arduino/blob/master/libraries/LittleFS/examples/LittleFS_Timestamp/LittleFS_Timestamp.ino*/ +//#ifdef ESP8266 +//void listDir(const char *dirname) { + //Serial.printf("Listing directory: %s\n", dirname); + + //Dir root = LittleFS.openDir(dirname); + + //while (root.next()) { + //File file = root.openFile("r"); + //Serial.print(" FILE: "); + //Serial.print(root.fileName()); + //Serial.print(" SIZE: "); + //Serial.print(file.size()); + //time_t cr = file.getCreationTime(); + //time_t lw = file.getLastWrite(); + //file.close(); + //struct tm *tmstruct = localtime(&cr); + //Serial.printf(" CREATION: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec); + //tmstruct = localtime(&lw); + //Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec); + //} +//} + + +//void readFile(const char *path) { + //Serial.printf("Reading file: %s\n", path); + + //File file = LittleFS.open(path, "r"); + //if (!file) { + //Serial.println("Failed to open file for reading"); + //return; + //} + + //Serial.print("Read from file: "); + //while (file.available()) { Serial.write(file.read()); } + //file.close(); +//} + +//void writeFile(const char *path, const char *message) { + //Serial.printf("Writing file: %s\n", path); + + //File file = LittleFS.open(path, "w"); + //if (!file) { + //Serial.println("Failed to open file for writing"); + //return; + //} + //if (file.print(message)) { + //Serial.println("File written"); + //} else { + //Serial.println("Write failed"); + //} + //delay(2000); // Make sure the CREATE and LASTWRITE times are different + //file.close(); +//} + +//void appendFile(const char *path, const char *message) { + //Serial.printf("Appending to file: %s\n", path); + + //File file = LittleFS.open(path, "a"); + //if (!file) { + //Serial.println("Failed to open file for appending"); + //return; + //} + //if (file.print(message)) { + //Serial.println("Message appended"); + //} else { + //Serial.println("Append failed"); + //} + //file.close(); +//} + +//void renameFile(const char *path1, const char *path2) { + //Serial.printf("Renaming file %s to %s\n", path1, path2); + //if (LittleFS.rename(path1, path2)) { + //Serial.println("File renamed"); + //} else { + //Serial.println("Rename failed"); + //} +//} + +//void deleteFile(const char *path) { + //Serial.printf("Deleting file: %s\n", path); + //if (LittleFS.remove(path)) { + //Serial.println("File deleted"); + //} else { + //Serial.println("Delete failed"); + //} +//} +//#endif + + +///* + //* ESP32 functions + //*/ + +///*functions from https://github.com/espressif/arduino-esp32/blob/master/libraries/LittleFS/examples/LITTLEFS_time/LITTLEFS_time.ino*/ +//#ifdef ESP32 +//void listDir(fs::FS &fs, const char *dirname, uint8_t levels) { + //Serial.printf("Listing directory: %s\n", dirname); + + //File root = fs.open(dirname); + //if (!root) { + //Serial.println("Failed to open directory"); + //return; + //} + //if (!root.isDirectory()) { + //Serial.println("Not a directory"); + //return; + //} + + //File file = root.openNextFile(); + //while (file) { + //if (file.isDirectory()) { + //Serial.print(" DIR : "); + //Serial.print(file.name()); + //time_t t = file.getLastWrite(); + //struct tm *tmstruct = localtime(&t); + //Serial.printf( + //" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, + //tmstruct->tm_min, tmstruct->tm_sec + //); + //if (levels) { + //listDir(fs, file.path(), levels - 1); + //} + //} else { + //Serial.print(" FILE: "); + //Serial.print(file.name()); + //Serial.print(" SIZE: "); + //Serial.print(file.size()); + //time_t t = file.getLastWrite(); + //struct tm *tmstruct = localtime(&t); + //Serial.printf( + //" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, + //tmstruct->tm_min, tmstruct->tm_sec + //); + //} + //file = root.openNextFile(); + //} +//} + +//void removeDir(fs::FS &fs, const char *path) { + //Serial.printf("Removing Dir: %s\n", path); + //if (fs.rmdir(path)) { + //Serial.println("Dir removed"); + //} else { + //Serial.println("rmdir failed"); + //} +//} + +//void readFile(fs::FS &fs, const char *path) { + //Serial.printf("Reading file: %s\n", path); + + //File file = fs.open(path); + //if (!file) { + //Serial.println("Failed to open file for reading"); + //return; + //} + + //Serial.print("Read from file: "); + //while (file.available()) { + //Serial.write(file.read()); + //} + //file.close(); +//} + +//void writeFile(fs::FS &fs, const char *path, const char *message) { + //Serial.printf("Writing file: %s\n", path); + + //File file = fs.open(path, FILE_WRITE); + //if (!file) { + //Serial.println("Failed to open file for writing"); + //return; + //} + //if (file.print(message)) { + //Serial.println("File written"); + //} else { + //Serial.println("Write failed"); + //} + //file.close(); +//} + +//void appendFile(fs::FS &fs, const char *path, const char *message) { + //Serial.printf("Appending to file: %s\n", path); + + //File file = fs.open(path, FILE_APPEND); + //if (!file) { + //Serial.println("Failed to open file for appending"); + //return; + //} + //if (file.print(message)) { + //Serial.println("Message appended"); + //} else { + //Serial.println("Append failed"); + //} + //file.close(); +//} + +//void renameFile(fs::FS &fs, const char *path1, const char *path2) { + //Serial.printf("Renaming file %s to %s\n", path1, path2); + //if (fs.rename(path1, path2)) { + //Serial.println("File renamed"); + //} else { + //Serial.println("Rename failed"); + //} +//} + +//void deleteFile(fs::FS &fs, const char *path) { + //Serial.printf("Deleting file: %s\n", path); + //if (fs.remove(path)) { + //Serial.println("File deleted"); + //} else { + //Serial.println("Delete failed"); + //} +//} +//#endif