LittleFS wip

This commit is contained in:
Marcus 2024-10-17 22:20:25 +02:00
parent 4595bea4e5
commit 985b2cac5d
3 changed files with 78 additions and 11 deletions

View file

@ -125,9 +125,14 @@ void setup() {
LFS_init(); LFS_init();
if(existFile(CANGROW_CFG)) {
Serial.println(":: NICE");
} else {
//Panic();
}
// read the configfile from LittleFS // read the configfile from LittleFS
File lfs_configfile = LittleFS.open(configfile, "r"); /* File lfs_configfile = LittleFS.open(configfile, "r");
if(!lfs_configfile) { if(!lfs_configfile) {
Serial.println("!! LittleFS: config.json not found, creating it !!"); Serial.println("!! LittleFS: config.json not found, creating it !!");
lfs_configfile.close(); lfs_configfile.close();
@ -152,7 +157,7 @@ void setup() {
Serial.println(configfile_content); Serial.println(configfile_content);
Serial.println("-- LittleFS: config.json end --"); Serial.println("-- LittleFS: config.json end --");
} }
*/
} }
void loop() { void loop() {

View file

@ -29,10 +29,9 @@
#include "CanGrow_Version.h" #include "CanGrow_Version.h"
#define CANGROW_CFG "/config.json"
const char* configfile = "config.json";
#ifdef ESP8266 #ifdef ESP8266
const uint8_t PinWIPE = D4; const uint8_t PinWIPE = D4;
#endif #endif

View file

@ -31,38 +31,101 @@
#define FORMAT_LITTLEFS_IF_FAILED true #define FORMAT_LITTLEFS_IF_FAILED true
void LFS_init() { void LFS_init() {
Serial.println(":: LittleFS: initializing"); Serial.println(":: [LittleFS] initializing");
// ESP8266 crashes with first argument set
#ifdef ESP8266 #ifdef ESP8266
if(!LittleFS.begin()) { if(!LittleFS.begin()) {
#endif #endif
// ESP32 works, do autoformat if mount fails
#ifdef ESP32 #ifdef ESP32
if(!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)) { if(!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)) {
#endif #endif
Serial.println("!! LittleFS: initializing FAILED !!"); Serial.println("!! [LittleFS] initializing FAILED !!");
Panic(); Panic();
} }
} }
void LFS_format() { void LFS_format() {
Serial.println(":: LittleFS: formatting"); Serial.println(":: [LittleFS] formatting");
// ESP32 LittleFS needs begin() first, otherwise it would crash // ESP32 LittleFS needs begin() first, otherwise it would crash
// ESP8266 does not need it, so we leave it // ESP8266 does not need it, so we leave it
#ifdef ESP32 #ifdef ESP32
LittleFS.begin(); LittleFS.begin();
#endif #endif
if(LittleFS.format()) { if(LittleFS.format()) {
Serial.println(":: LittleFS: formatting done!"); Serial.println(":: [LittleFS] formatting done!");
} else { } else {
Serial.println("!! LittleFS: formatting FAILED !!"); Serial.println("!! [LittleFS] formatting FAILED !!");
} }
} }
void readFile() { bool existFile(const char *path) {
#ifdef ESP8266
File file = LittleFS.open(path, "r");
#endif
#ifdef ESP32
fs::FS &fs = LittleFS;
File file = fs.open(path);
#endif
if (!file) {
Serial.print(":: [LittleFS] File does not exist ");
Serial.println(path);
file.close();
return false;
} else {
Serial.print(":: [LittleFS] File does exist ");
Serial.println(path);
file.close();
return true;
}
} }
void writeFile() { void readFile(const char *path) {
#ifdef ESP8266
File file = LittleFS.open(path, "r");
#endif
#ifdef ESP32
fs::FS &fs = LittleFS;
File file = fs.open(path);
#endif
if (!file) {
Serial.print(":: [LittleFS] Failed to open file for reading ");
Serial.println(path);
return;
}
Serial.print(":: [LittleFS] file content ");
Serial.println(path);
while (file.available()) { Serial.write(file.read()); }
file.close();
}
void writeFile(const char *path, const char *message) {
#ifdef ESP8266
File file = LittleFS.open(path, "w");
#endif
#ifdef ESP32
fs::FS &fs = LittleFS;
File file = fs.open(path, FILE_WRITE);
#endif
if (!file) {
Serial.print(":: [LittleFS] Failed to open file for writing ");
Serial.println(path);
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 deleteFile() { void deleteFile() {