1
0
mirror of https://github.com/s00500/ESPUI.git synced 2024-11-24 12:20:55 +00:00

Add conditional refactor from LITTLEFS to LittleFs and other small changes when using ESP-IDF 4.4 and above on ESP32. This should fix issue #179 etc. relating to LittleFS on ESP32.

Make EPSUI.list() function the same on ESP8266 as it does on ESP32
This commit is contained in:
Nick Reynolds 2022-10-08 18:28:19 +01:00
parent beb4135616
commit 9a1db8a4da
2 changed files with 164 additions and 39 deletions

View File

@ -27,7 +27,11 @@ void listDir(const char* dirname, uint8_t levels)
#endif #endif
#if defined(ESP32) #if defined(ESP32)
File root = LITTLEFS.open(dirname); #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
File root = LittleFS.open(dirname);
#else
File root = LITTLEFS.open(dirname);
#endif
#else #else
File root = LittleFS.open(dirname); File root = LittleFS.open(dirname);
#endif #endif
@ -72,7 +76,11 @@ void listDir(const char* dirname, uint8_t levels)
if (levels) if (levels)
{ {
listDir(file.name(), levels - 1); #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
listDir(file.path(), levels - 1);
#else
listDir(file.name(), levels - 1);
#endif
} }
} }
else else
@ -95,18 +103,45 @@ void listDir(const char* dirname, uint8_t levels)
void listDir(const char* dirname, uint8_t levels) void listDir(const char* dirname, uint8_t levels)
{ {
// ignoring levels for esp8266 #if defined(DEBUG_ESPUI)
Serial.printf_P(PSTR("Listing directory: %s\n"), dirname); if (ESPUI.verbosity)
{
Serial.printf_P(PSTR("Listing directory: %s\n"), dirname);
}
#endif
String str = ""; Dir dir = LittleFS.openDir(dirname);
Dir dir = LittleFS.openDir("/");
while (dir.next()) while (dir.next())
{ {
Serial.print(F(" FILE: ")); if (dir.isDirectory())
Serial.print(dir.fileName()); {
Serial.print(F(" SIZE: ")); #if defined(DEBUG_ESPUI)
Serial.println(dir.fileSize()); if (ESPUI.verbosity)
{
Serial.print(F(" DIR : "));
Serial.println(dir.fileName());
}
#endif
if (levels)
{
File file = dir.openFile("r");
listDir(file.fullName(), levels - 1);
file.close();
}
}
else
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.print(F(" FILE: "));
Serial.print(dir.fileName());
Serial.print(F(" SIZE: "));
Serial.println(dir.fileSize());
}
#endif
}
} }
} }
@ -115,7 +150,11 @@ void listDir(const char* dirname, uint8_t levels)
void ESPUIClass::list() void ESPUIClass::list()
{ {
#if defined(ESP32) #if defined(ESP32)
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
if (!LittleFS.begin())
#else
if (!LITTLEFS.begin()) if (!LITTLEFS.begin())
#endif
{ {
Serial.println(F("LITTLEFS Mount Failed")); Serial.println(F("LITTLEFS Mount Failed"));
return; return;
@ -131,15 +170,27 @@ void ESPUIClass::list()
listDir("/", 1); listDir("/", 1);
#if defined(ESP32) #if defined(ESP32)
Serial.println(LITTLEFS.totalBytes()); Serial.print(F("Total KB: "));
Serial.println(LITTLEFS.usedBytes()); #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
Serial.println(LittleFS.totalBytes()/1024);
#else
Serial.println(LITTLEFS.totalBytes()/1024);
#endif
Serial.print(F("Used KB: "));
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
Serial.println(LittleFS.usedBytes()/1024);
#else
Serial.println(LITTLEFS.usedBytes()/1024);
#endif
#else #else
FSInfo fs_info; FSInfo fs_info;
LittleFS.info(fs_info); LittleFS.info(fs_info);
Serial.println(fs_info.totalBytes); Serial.print(F("Total KB: "));
Serial.println(fs_info.usedBytes); Serial.println(fs_info.totalBytes/1024);
Serial.print(F("Used KB: "));
Serial.println(fs_info.usedBytes/1024);
#endif #endif
} }
@ -147,7 +198,11 @@ void ESPUIClass::list()
void deleteFile(const char* path) void deleteFile(const char* path)
{ {
#if defined(ESP32) #if defined(ESP32)
bool exists = LITTLEFS.exists(path); #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
bool exists = LittleFS.exists(path);
#else
bool exists = LITTLEFS.exists(path);
#endif
#else #else
bool exists = LittleFS.exists(path); bool exists = LittleFS.exists(path);
#endif #endif
@ -172,7 +227,11 @@ void deleteFile(const char* path)
#endif #endif
#if defined(ESP32) #if defined(ESP32)
bool didRemove = LITTLEFS.remove(path); #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
bool didRemove = LittleFS.remove(path);
#else
bool didRemove = LITTLEFS.remove(path);
#endif
#else #else
bool didRemove = LittleFS.remove(path); bool didRemove = LittleFS.remove(path);
#endif #endif
@ -206,7 +265,11 @@ void writeFile(const char* path, const char* data)
#endif #endif
#if defined(ESP32) #if defined(ESP32)
File file = LITTLEFS.open(path, FILE_WRITE); #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
File file = LittleFS.open(path, FILE_WRITE);
#else
File file = LITTLEFS.open(path, FILE_WRITE);
#endif
#else #else
File file = LittleFS.open(path, FILE_WRITE); File file = LittleFS.open(path, FILE_WRITE);
#endif #endif
@ -271,7 +334,7 @@ void writeFile(const char* path, const char* data)
// end LITTLEFS functions // end LITTLEFS functions
void ESPUIClass::prepareFileSystem() void ESPUIClass::prepareFileSystem(bool format)
{ {
// this function should only be used once // this function should only be used once
@ -281,32 +344,43 @@ void ESPUIClass::prepareFileSystem()
Serial.println(F("About to prepare filesystem...")); Serial.println(F("About to prepare filesystem..."));
} }
#endif #endif
#if defined(ESP32) #if defined(ESP32)
LITTLEFS.format(); if(format)
{
if (!LITTLEFS.begin(true)) #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
{ //LittleFS.format(); //It would seem this causes a crash so commented out for now. An unformatted partition will be automatically formatted fine.
#else
//LITTLEFS.format(); //It would seem this causes a crash so commented out for now. An unformatted partition will be automatically formatted fine.
#endif
}
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
if(!LittleFS.begin(true))
#else
if(!LITTLEFS.begin(true))
#endif
{
#if defined(DEBUG_ESPUI) #if defined(DEBUG_ESPUI)
if (this->verbosity) if (this->verbosity)
{ {
Serial.println(F("LITTLEFS Mount Failed")); Serial.println(F("LITTLEFS Mount Failed"));
} }
#endif #endif
return; return;
} }
#if defined(DEBUG_ESPUI) #if defined(DEBUG_ESPUI)
if (this->verbosity) if (this->verbosity)
{ {
listDir("/", 1); listDir("/", 1);
Serial.println(F("LITTLEFS Mount ESP32 Done")); Serial.println(F("LITTLEFS Mount ESP32 Done"));
} }
#endif #endif
#else #else
LittleFS.format(); if(format)
{
LittleFS.format();
}
LittleFS.begin(); LittleFS.begin();
#if defined(DEBUG_ESPUI) #if defined(DEBUG_ESPUI)
@ -337,6 +411,33 @@ void ESPUIClass::prepareFileSystem()
#endif #endif
// Now write // Now write
#ifdef ESP32
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
writeFile("/index.htm", HTML_INDEX);
LittleFS.mkdir("/css");
writeFile("/css/style.css", CSS_STYLE);
writeFile("/css/normalize.css", CSS_NORMALIZE);
LittleFS.mkdir("/js");
writeFile("/js/zepto.min.js", JS_ZEPTO);
writeFile("/js/controls.js", JS_CONTROLS);
writeFile("/js/slider.js", JS_SLIDER);
writeFile("/js/graph.js", JS_GRAPH);
writeFile("/js/tabbedcontent.js", JS_TABBEDCONTENT);
#else
writeFile("/index.htm", HTML_INDEX);
writeFile("/css/style.css", CSS_STYLE);
writeFile("/css/normalize.css", CSS_NORMALIZE);
writeFile("/js/zepto.min.js", JS_ZEPTO);
writeFile("/js/controls.js", JS_CONTROLS);
writeFile("/js/slider.js", JS_SLIDER);
writeFile("/js/graph.js", JS_GRAPH);
writeFile("/js/tabbedcontent.js", JS_TABBEDCONTENT);
#endif
#else
writeFile("/index.htm", HTML_INDEX); writeFile("/index.htm", HTML_INDEX);
writeFile("/css/style.css", CSS_STYLE); writeFile("/css/style.css", CSS_STYLE);
@ -348,6 +449,7 @@ void ESPUIClass::prepareFileSystem()
writeFile("/js/graph.js", JS_GRAPH); writeFile("/js/graph.js", JS_GRAPH);
writeFile("/js/tabbedcontent.js", JS_TABBEDCONTENT); writeFile("/js/tabbedcontent.js", JS_TABBEDCONTENT);
#endif
#if defined(DEBUG_ESPUI) #if defined(DEBUG_ESPUI)
if (this->verbosity) if (this->verbosity)
@ -368,7 +470,11 @@ void ESPUIClass::prepareFileSystem()
#endif #endif
#if defined(ESP32) #if defined(ESP32)
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
LittleFS.end();
#else
LITTLEFS.end(); LITTLEFS.end();
#endif
#else #else
LittleFS.end(); LittleFS.end();
#endif #endif
@ -1275,7 +1381,11 @@ void ESPUIClass::beginLITTLEFS(const char* _title, const char* username, const c
ws = new AsyncWebSocket("/ws"); ws = new AsyncWebSocket("/ws");
#if defined(ESP32) #if defined(ESP32)
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
bool fsBegin = LittleFS.begin();
#else
bool fsBegin = LITTLEFS.begin(); bool fsBegin = LITTLEFS.begin();
#endif
#else #else
bool fsBegin = LittleFS.begin(); bool fsBegin = LittleFS.begin();
#endif #endif
@ -1300,7 +1410,11 @@ void ESPUIClass::beginLITTLEFS(const char* _title, const char* username, const c
#endif #endif
#if defined(ESP32) #if defined(ESP32)
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
bool indexExists = LittleFS.exists("/index.htm");
#else
bool indexExists = LITTLEFS.exists("/index.htm"); bool indexExists = LITTLEFS.exists("/index.htm");
#endif
#else #else
bool indexExists = LittleFS.exists("/index.htm"); bool indexExists = LittleFS.exists("/index.htm");
#endif #endif
@ -1327,7 +1441,11 @@ void ESPUIClass::beginLITTLEFS(const char* _title, const char* username, const c
ws->setAuthentication(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword); ws->setAuthentication(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword);
} }
#if defined(ESP32) #if defined(ESP32)
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
server->serveStatic("/", LittleFS, "/").setDefaultFile("index.htm").setAuthentication(username, password);
#else
server->serveStatic("/", LITTLEFS, "/").setDefaultFile("index.htm").setAuthentication(username, password); server->serveStatic("/", LITTLEFS, "/").setDefaultFile("index.htm").setAuthentication(username, password);
#endif
#else #else
server->serveStatic("/", LittleFS, "/").setDefaultFile("index.htm").setAuthentication(username, password); server->serveStatic("/", LittleFS, "/").setDefaultFile("index.htm").setAuthentication(username, password);
#endif #endif
@ -1335,7 +1453,11 @@ void ESPUIClass::beginLITTLEFS(const char* _title, const char* username, const c
else else
{ {
#if defined(ESP32) #if defined(ESP32)
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
server->serveStatic("/", LittleFS, "/").setDefaultFile("index.htm");
#else
server->serveStatic("/", LITTLEFS, "/").setDefaultFile("index.htm"); server->serveStatic("/", LITTLEFS, "/").setDefaultFile("index.htm");
#endif
#else #else
server->serveStatic("/", LittleFS, "/").setDefaultFile("index.htm"); server->serveStatic("/", LittleFS, "/").setDefaultFile("index.htm");
#endif #endif

View File

@ -11,7 +11,11 @@
#if defined(ESP32) #if defined(ESP32)
#include <AsyncTCP.h> #include <AsyncTCP.h>
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include <LITTLEFS.h> #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
#include <LittleFS.h>
#else
#include <LITTLEFS.h>
#endif
#include "WiFi.h" #include "WiFi.h"
@ -246,8 +250,7 @@ public:
void beginLITTLEFS(const char* _title, const char* username = nullptr, const char* password = nullptr, void beginLITTLEFS(const char* _title, const char* username = nullptr, const char* password = nullptr,
uint16_t port = 80); // Setup server and page in LITTLEFS mode uint16_t port = 80); // Setup server and page in LITTLEFS mode
void prepareFileSystem(); // Initially preps the filesystem and loads a lot of void prepareFileSystem(bool format = true); // Initially preps the filesystem and loads a lot of stuff into LITTLEFS
// stuff into LITTLEFS
void list(); // Lists LITTLEFS directory void list(); // Lists LITTLEFS directory
uint16_t addControl(ControlType type, const char* label); uint16_t addControl(ControlType type, const char* label);