Skip to content

Commit

Permalink
LittleFS by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Aircoookie committed Sep 13, 2020
1 parent 6ef4582 commit 96713ef
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 98 deletions.
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ platform = ${common.platform_wled_default}
upload_speed = 921600
board_build.ldscript = ${common.ldscript_4m1m}
build_flags = ${common.build_flags_esp8266}
monitor_filters = esp8266_exception_decoder

[env:heltec_wifi_kit_8]
board = d1_mini
Expand Down
3 changes: 1 addition & 2 deletions wled00/fcn_declare.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ void handleE131Packet(e131_packet_t* p, IPAddress clientIP, bool isArtnet);
bool handleFileRead(AsyncWebServerRequest*, String path);
bool writeObjectToFileUsingId(const char* file, uint16_t id, JsonDocument* content);
bool writeObjectToFile(const char* file, const char* key, JsonDocument* content);
bool appendObjectToFile(const char* file, const char* key, JsonDocument* content, File input);
bool readObjectFromFileUsingId(const char* file, uint16_t id, JsonDocument* dest);
bool readObjectFromFile(const char* file, const char* key, JsonDocument* dest);

Expand Down Expand Up @@ -195,7 +194,7 @@ void saveSettingsToEEPROM();
void loadSettingsFromEEPROM(bool first);
void savedToPresets();
bool applyPreset(byte index, bool loadBri = true);
void savePreset(byte index, bool persist = true, const char* pname = nullptr, byte prio = 50);
void savePreset(byte index, bool persist = true, const char* pname = nullptr, byte prio = 50, JsonObject saveobj = JsonObject());
void loadMacro(byte index, char* m);
void applyMacro(byte index);
void saveMacro(byte index, String mc, bool persist = true); //only commit on single save, not in settings
Expand Down
188 changes: 100 additions & 88 deletions wled00/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,22 @@
//find() that reads and buffers data from file stream in 256-byte blocks.
//Significantly faster, f.find(key) can take SECONDS for multi-kB files
bool bufferedFind(const char *target, File f) {
#ifdef WLED_DEBUG_FS
DEBUGFS_PRINT("Find ");
DEBUGFS_PRINTLN(target);
uint32_t s = millis();
#endif

if (!f || !f.size()) return false;
size_t targetLen = strlen(target);
Serial.print("bfind ");
Serial.println(target);
//Serial.println(f.position());

size_t index = 0;
byte c;
uint16_t bufsize = 0, count = 0;
byte buf[256];
f.seek(0);

while (f.position() < f.size() -1) {
//c = f.read();
Serial.println(f.position());
bufsize = f.read(buf, 256);
count = 0;
while (count < bufsize) {
Expand All @@ -41,21 +43,27 @@ bool bufferedFind(const char *target, File f) {
if(buf[count] == target[index]) {
if(++index >= targetLen) { // return true if all chars in the target match
f.seek((f.position() - bufsize) + count +1);
DEBUGFS_PRINTF("Found at pos %d, took %d ms", f.position(), millis() - s);
return true;
}
}
count++;
}
}
Serial.println("No match");
DEBUGFS_PRINTF("No match, took %d ms\n", millis() - s);
return false;
}

//find empty spots in file stream in 256-byte blocks.
bool bufferedFindSpace(uint16_t targetLen, File f) {
Serial.print("bfs ");
#ifdef WLED_DEBUG_FS
DEBUGFS_PRINTF("Find %d spaces\n", targetLen);
uint32_t s = millis();
#endif

if (!f || !f.size()) return false;
Serial.print(targetLen);
DEBUGFS_PRINTF("Filesize %d\n", f.size());

uint16_t index = 0;
uint16_t bufsize = 0, count = 0;
byte buf[256];
Expand All @@ -64,26 +72,77 @@ bool bufferedFindSpace(uint16_t targetLen, File f) {
while (f.position() < f.size() -1) {
bufsize = f.read(buf, 256);
count = 0;

while (count < bufsize) {
Serial.print(count);
Serial.write(' ');
Serial.println(index);
if(buf[count] != ' ')
index = 0; // reset index if not space

if(buf[count] == ' ') {
if(++index >= targetLen) { // return true if space long enough
f.seek((f.position() - bufsize) + count +1 - targetLen);
Serial.print("SPAAAACE!");
DEBUGFS_PRINTF("Found at pos %d, took %d ms", f.position(), millis() - s);
return true;
}
}
count++;
}
}
DEBUGFS_PRINTF("No match, took %d ms\n", millis() - s);
return false;
}

bool appendObjectToFile(File f, const char* key, JsonDocument* content, uint32_t s)
{
#ifdef WLED_DEBUG_FS
DEBUG_PRINTLN("Append");
uint32_t s1 = millis();
#endif
uint32_t pos = 0;
if (!f) return false;
if (f.size() < 3) f.print("{}");

//if there is enough empty space in file, insert there instead of appending
uint32_t contentLen = measureJson(*content);
DEBUGFS_PRINTF("CLen %d\n", contentLen);
if (bufferedFindSpace(contentLen + strlen(key) + 1, f)) {
if (f.position() > 2) f.write(','); //add comma if not first object
f.print(key);
serializeJson(*content, f);
return true;
}

//check if last character in file is '}' (typical)
f.seek(1, SeekEnd);
if (f.read() == '}') pos = f.size() -1;

if (pos == 0) //not found
{
DEBUGFS_PRINTLN("not }");
while (bufferedFind("}",f)) //find last closing bracket in JSON if not last char
{
pos = f.position();
}
}
DEBUGFS_PRINT("pos "); DEBUGFS_PRINTLN(pos);
if (pos > 2)
{
f.seek(pos, SeekSet);
f.write(',');
} else { //file content is not valid JSON object
f.seek(0, SeekSet);
f.write('{'); //start JSON
}

f.print(key);

//Append object
serializeJson(*content, f);
f.write('}');

f.close();
DEBUGFS_PRINTF("Appended, took %d ms (total %d)", millis() - s1, millis() - s);
}

bool writeObjectToFileUsingId(const char* file, uint16_t id, JsonDocument* content)
{
char objKey[10];
Expand All @@ -93,114 +152,62 @@ bool writeObjectToFileUsingId(const char* file, uint16_t id, JsonDocument* conte

bool writeObjectToFile(const char* file, const char* key, JsonDocument* content)
{
uint32_t s = 0; //timing
#ifdef WLED_DEBUG_FS
DEBUGFS_PRINTF("Write to %s with key %s >>>\n", file, key);
serializeJson(*content, Serial); DEBUGFS_PRINTLN();
s = millis();
#endif

uint32_t pos = 0;
File f = WLED_FS.open(file, "r+");
if (!f) f = WLED_FS.open(file, "w");
if (!f) return false;

f.seek(0, SeekSet);

Serial.print("Writing to ");
Serial.print(file);
Serial.print(" with key ");
Serial.print(key);
Serial.print(" > ");
serializeJson(*content, Serial);
Serial.println();
if (!f && !WLED_FS.exists(file)) f = WLED_FS.open(file, "w+");
if (!f) {
DEBUGFS_PRINTLN("Failed to open!");
return false;
}

if (!bufferedFind(key, f)) //key does not exist in file
{
Serial.println("Key not found");
return appendObjectToFile(file, key, content, f);
return appendObjectToFile(f, key, content, s);
}

Serial.println("Key found!");
//exists
pos = f.position();
Serial.println(pos);
//measure out end of old object
StaticJsonDocument<1024> doc;
deserializeJson(doc, f);
uint32_t pos2 = f.position();

uint32_t oldLen = pos2 - pos;
Serial.print("Old obj len: ");
Serial.print(oldLen);
Serial.print(" > ");
serializeJson(doc, Serial);
Serial.println();
#ifdef WLED_DEBUG_FS
DEBUGFS_PRINTF("Old obj len %d >>> ", oldLen);
serializeJson(doc, Serial);
DEBUGFS_PRINTLN();
#endif

if (!content->isNull() && measureJson(*content) <= oldLen) //replace
{
Serial.println("replace");
DEBUG_PRINTLN("replace");
f.seek(pos);
serializeJson(*content, f);
//pad rest
for (uint32_t i = f.position(); i < pos2; i++) {
f.write(' ');
}
} else { //delete
Serial.println("delete");
DEBUG_PRINTLN("delete");
pos -= strlen(key);
if (pos > 3) pos--; //also delete leading comma if not first object
f.seek(pos);
for (uint32_t i = pos; i < pos2; i++) {
f.write(' ');
}
if (!content->isNull()) return appendObjectToFile(file, key, content, f);
}
f.close();
}

bool appendObjectToFile(const char* file, const char* key, JsonDocument* content, File input)
{
Serial.println("Append");
uint32_t pos = 0;
File f = (input) ? input : WLED_FS.open(file, "r+");
if (!f) f = WLED_FS.open(file,"w");
if (!f) return false;
if (f.size() < 3) f.print("{}");

//if there is enough empty space in file, insert there instead of appending
uint32_t contentLen = measureJson(*content);
Serial.print("clen"); Serial.println(contentLen);
if (bufferedFindSpace(contentLen + strlen(key) + 1, f)) {
Serial.println("space");
if (f.position() > 2) f.write(','); //add comma if not first object
f.print(key);
serializeJson(*content, f);
return true;
}

//check if last character in file is '}' (typical)
f.seek(1, SeekEnd);
if (f.read() == '}') pos = f.size() -1;

if (pos == 0) //not found
{
Serial.println("not}");
while (bufferedFind("}",f)) //find last closing bracket in JSON if not last char
{
pos = f.position();
}
}
Serial.print("pos"); Serial.println(pos);
if (pos > 2)
{
f.seek(pos, SeekSet);
f.write(',');
} else { //file content is not valid JSON object
f.seek(0, SeekSet);
f.write('{'); //start JSON
if (!content->isNull()) return appendObjectToFile(f, key, content, s);
}

f.print(key);

//Append object
serializeJson(*content, f);
f.write('}');

f.close();
DEBUGFS_PRINTF("Deleted, took %d ms\n", millis() - s);
return true;
}

bool readObjectFromFileUsingId(const char* file, uint16_t id, JsonDocument* dest)
Expand All @@ -212,19 +219,24 @@ bool readObjectFromFileUsingId(const char* file, uint16_t id, JsonDocument* dest

bool readObjectFromFile(const char* file, const char* key, JsonDocument* dest)
{
#ifdef WLED_DEBUG_FS
DEBUGFS_PRINTF("Read from %s with key %s >>>\n", file, key);
uint32_t s = millis();
#endif
File f = WLED_FS.open(file, "r");
if (!f) return false;
//f.setTimeout(0);

if (!bufferedFind(key, f)) //key does not exist in file
{
f.close();
DEBUGFS_PRINTLN("Obj not found.");
return false;
}

deserializeJson(*dest, f);

f.close();
DEBUGFS_PRINTF("Read, took %d ms\n", millis() - s);
return true;
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion wled00/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ bool deserializeState(JsonObject root)
bool persistSaves = !(root["np"] | false);

ps = root["psave"] | -1;
if (ps >= 0) savePreset(ps, persistSaves, root["n"], root["p"]);
if (ps >= 0) savePreset(ps, persistSaves, root["n"], root["p"] | 50, root["o"].as<JsonObject>());

return stateResponse;
}
Expand Down
10 changes: 7 additions & 3 deletions wled00/wled.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,16 @@
// to toggle usb serial debug (un)comment the following line
//#define WLED_DEBUG

// filesystem specific debugging
#define WLED_DEBUG_FS

// Library inclusions.
#include <Arduino.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESPAsyncTCP.h>
#include <LittleFS.h>
extern "C"
{
#include <user_interface.h>
Expand Down Expand Up @@ -119,7 +123,7 @@
#endif

//Filesystem to use for preset and config files. SPIFFS or LittleFS on ESP8266, SPIFFS only on ESP32
#define WLED_FS SPIFFS
#define WLED_FS LittleFS

// remove flicker because PWM signal of RGB channels can become out of phase (part of core as of Arduino core v2.7.0)
//#if defined(WLED_USE_ANALOG_LEDS) && defined(ESP8266)
Expand Down Expand Up @@ -500,7 +504,7 @@ WLED_GLOBAL UsermodManager usermods _INIT(UsermodManager());
#endif
#define DEBUG_PRINT(x) Serial.print(x)
#define DEBUG_PRINTLN(x) Serial.println(x)
#define DEBUG_PRINTF(x) Serial.printf(x)
#define DEBUG_PRINTF(x...) Serial.printf(x)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)
Expand All @@ -510,7 +514,7 @@ WLED_GLOBAL UsermodManager usermods _INIT(UsermodManager());
#ifdef WLED_DEBUG_FS
#define DEBUGFS_PRINT(x) Serial.print(x)
#define DEBUGFS_PRINTLN(x) Serial.println(x)
#define DEBUGFS_PRINTF(x) Serial.printf(x)
#define DEBUGFS_PRINTF(x...) Serial.printf(x)
#else
#define DEBUGFS_PRINT(x)
#define DEBUGFS_PRINTLN(x)
Expand Down
Loading

0 comments on commit 96713ef

Please sign in to comment.