From f130700cf03941443b1a7fceed85451c66af37a9 Mon Sep 17 00:00:00 2001 From: Marcus Date: Thu, 17 Oct 2024 22:43:56 +0200 Subject: [PATCH] add deleteFile() to CanGrow_LittleFS.h --- Arduino/CanGrow/CanGrow.ino | 4 +-- Arduino/CanGrow/include/CanGrow_LittleFS.h | 38 ++++++++++++++-------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index 4422621..4243987 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -126,9 +126,9 @@ void setup() { LFS_init(); if(existFile(CANGROW_CFG)) { - Serial.println(":: NICE"); + readFile(CANGROW_CFG); } else { - //Panic(); + writeFile(CANGROW_CFG, "{}"); } // read the configfile from LittleFS diff --git a/Arduino/CanGrow/include/CanGrow_LittleFS.h b/Arduino/CanGrow/include/CanGrow_LittleFS.h index 451f179..2ffb915 100644 --- a/Arduino/CanGrow/include/CanGrow_LittleFS.h +++ b/Arduino/CanGrow/include/CanGrow_LittleFS.h @@ -46,7 +46,7 @@ void LFS_init() { } 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 @@ -70,13 +70,11 @@ bool existFile(const char *path) { #endif if (!file) { - Serial.print(":: [LittleFS] File does not exist "); - Serial.println(path); + Serial.printf(":: [LittleFS] file does not exist: %s\n", path); file.close(); return false; } else { - Serial.print(":: [LittleFS] File does exist "); - Serial.println(path); + Serial.printf(":: [LittleFS] file does exist: %s\n", path); file.close(); return true; } @@ -93,13 +91,11 @@ void readFile(const char *path) { #endif if (!file) { - Serial.print(":: [LittleFS] Failed to open file for reading "); - Serial.println(path); + Serial.printf(":: [LittleFS] FAILED to open file for reading: %s\n"); return; } - Serial.print(":: [LittleFS] file content "); - Serial.println(path); + Serial.printf(":: [LittleFS] file content: %s\n", path); while (file.available()) { Serial.write(file.read()); } file.close(); } @@ -115,21 +111,35 @@ void writeFile(const char *path, const char *message) { #endif if (!file) { - Serial.print(":: [LittleFS] Failed to open file for writing "); - Serial.println(path); + Serial.printf(":: [LittleFS] FAILED to open file for writing: %s\n", path); return; } if (file.print(message)) { - Serial.println("File written"); + Serial.printf(":: [LittleFS] file written: %s\n", path); } else { - Serial.println("Write failed"); + Serial.printf(":: [LittleFS] writing file FAILED: %s\n", path); } delay(2000); // Make sure the CREATE and LASTWRITE times are different file.close(); } -void deleteFile() { +void deleteFile(const char *path) { + #ifdef ESP32 + fs::FS &fs = LittleFS; + File file = fs.open(path, FILE_WRITE); + #endif + Serial.printf(":: [LittleFS] deleting file: %s\n", path); + #ifdef ESP8266 + if (LittleFS.remove(path)) { + #endif + #ifdef ESP32 + if (fs.remove(path)) { + #endif + Serial.printf(":: [LittleFS] deleted file: %s\n", path); + } else { + Serial.printf(":: [LittleFS] deleting file FAILED: %s\n", path); + } }