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

View file

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

View file

@ -31,38 +31,101 @@
#define FORMAT_LITTLEFS_IF_FAILED true
void LFS_init() {
Serial.println(":: LittleFS: initializing");
Serial.println(":: [LittleFS] initializing");
// ESP8266 crashes with first argument set
#ifdef ESP8266
if(!LittleFS.begin()) {
#endif
// ESP32 works, do autoformat if mount fails
#ifdef ESP32
if(!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)) {
#endif
Serial.println("!! LittleFS: initializing FAILED !!");
Serial.println("!! [LittleFS] initializing FAILED !!");
Panic();
}
}
void LFS_format() {
Serial.println(":: LittleFS: formatting");
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!");
Serial.println(":: [LittleFS] formatting done!");
} 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() {