diff --git a/wled00/json.cpp b/wled00/json.cpp index 29464a9d9..76cb4667f 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -303,7 +303,7 @@ bool deserializeSegment(JsonObject elem, byte it, byte presetId) return true; } -// deserializes WLED state (fileDoc points to doc object if called from web server) +// deserializes WLED state // presetId is non-0 if called from handlePreset() bool deserializeState(JsonObject root, byte callMode, byte presetId) { diff --git a/wled00/playlist.cpp b/wled00/playlist.cpp index 882ccb0e0..67c4f6049 100644 --- a/wled00/playlist.cpp +++ b/wled00/playlist.cpp @@ -125,8 +125,7 @@ int16_t loadPlaylist(JsonObject playlistObj, byte presetId) { void handlePlaylist() { static unsigned long presetCycledTime = 0; - // if fileDoc is not null JSON buffer is in use so just quit - if (currentPlaylist < 0 || playlistEntries == nullptr || fileDoc != nullptr) return; + if (currentPlaylist < 0 || playlistEntries == nullptr) return; if (millis() - presetCycledTime > (100*playlistEntryDur)) { presetCycledTime = millis(); diff --git a/wled00/presets.cpp b/wled00/presets.cpp index 5a0af0e7f..857b2fbb3 100644 --- a/wled00/presets.cpp +++ b/wled00/presets.cpp @@ -27,7 +27,7 @@ static void doSaveState() { unsigned long start = millis(); while (strip.isUpdating() && millis()-start < (2*FRAMETIME_FIXED)+1) yield(); // wait 2 frames - if (!requestJSONBufferLock(10)) return; // will set fileDoc + if (!requestJSONBufferLock(10)) return; initPresetsFile(); // just in case if someone deleted presets.json using /edit JsonObject sObj = pDoc->to(); @@ -53,7 +53,7 @@ static void doSaveState() { #if defined(ARDUINO_ARCH_ESP32) if (!persist) { if (tmpRAMbuffer!=nullptr) free(tmpRAMbuffer); - size_t len = measureJson(*fileDoc) + 1; + size_t len = measureJson(*pDoc) + 1; DEBUG_PRINTLN(len); // if possible use SPI RAM on ESP32 if (psramFound()) @@ -61,13 +61,13 @@ static void doSaveState() { else tmpRAMbuffer = (char*) malloc(len); if (tmpRAMbuffer!=nullptr) { - serializeJson(*fileDoc, tmpRAMbuffer, len); + serializeJson(*pDoc, tmpRAMbuffer, len); } else { - writeObjectToFileUsingId(getPresetsFileName(persist), presetToSave, fileDoc); + writeObjectToFileUsingId(getPresetsFileName(persist), presetToSave, pDoc); } } else #endif - writeObjectToFileUsingId(getPresetsFileName(persist), presetToSave, fileDoc); + writeObjectToFileUsingId(getPresetsFileName(persist), presetToSave, pDoc); if (persist) presetsModifiedTime = toki.second(); //unix time releaseJSONBufferLock(); @@ -152,7 +152,7 @@ void handlePresets() return; } - if (presetToApply == 0 || fileDoc) return; // no preset waiting to apply, or JSON buffer is already allocated, return to loop until free + if (presetToApply == 0 || !requestJSONBufferLock(9)) return; // no preset waiting to apply, or JSON buffer is already allocated, return to loop until free bool changePreset = false; uint8_t tmpPreset = presetToApply; // store temporary since deserializeState() may call applyPreset() @@ -160,9 +160,6 @@ void handlePresets() JsonObject fdo; - // allocate buffer - if (!requestJSONBufferLock(9)) return; // will also assign fileDoc - presetToApply = 0; //clear request for preset callModeToApply = 0; @@ -171,14 +168,14 @@ void handlePresets() #ifdef ARDUINO_ARCH_ESP32 if (tmpPreset==255 && tmpRAMbuffer!=nullptr) { - deserializeJson(*fileDoc,tmpRAMbuffer); + deserializeJson(*pDoc,tmpRAMbuffer); errorFlag = ERR_NONE; } else #endif { - errorFlag = readObjectFromFileUsingId(getPresetsFileName(tmpPreset < 255), tmpPreset, fileDoc) ? ERR_NONE : ERR_FS_PLOAD; + errorFlag = readObjectFromFileUsingId(getPresetsFileName(tmpPreset < 255), tmpPreset, pDoc) ? ERR_NONE : ERR_FS_PLOAD; } - fdo = fileDoc->as(); + fdo = pDoc->as(); //HTTP API commands const char* httpwin = fdo["win"]; @@ -205,13 +202,13 @@ void handlePresets() } #endif - releaseJSONBufferLock(); // will also clear fileDoc + releaseJSONBufferLock(); if (changePreset) notify(tmpMode); // force UDP notification stateUpdated(tmpMode); // was colorUpdated() if anything breaks updateInterfaces(tmpMode); } -//called from handleSet(PS=) [network callback (fileDoc==nullptr), IR (irrational), deserializeState, UDP] and deserializeState() [network callback (filedoc!=nullptr)] +//called from handleSet(PS=) [network callback (sObj is empty), IR (irrational), deserializeState, UDP] and deserializeState() [network callback (filedoc!=nullptr)] void savePreset(byte index, const char* pname, JsonObject sObj) { if (!saveName) saveName = new char[33]; @@ -249,7 +246,7 @@ void savePreset(byte index, const char* pname, JsonObject sObj) if (sObj[F("playlist")].isNull()) { // we will save API call immediately (often causes presets.json corruption) presetToSave = 0; - if (index <= 250 && fileDoc) { // cannot save API calls to temporary preset (255) + if (index <= 250) { // cannot save API calls to temporary preset (255) sObj.remove("o"); sObj.remove("v"); sObj.remove("time"); @@ -257,7 +254,7 @@ void savePreset(byte index, const char* pname, JsonObject sObj) sObj.remove(F("psave")); if (sObj["n"].isNull()) sObj["n"] = saveName; initPresetsFile(); // just in case if someone deleted presets.json using /edit - writeObjectToFileUsingId(getPresetsFileName(), index, fileDoc); + writeObjectToFileUsingId(getPresetsFileName(), index, pDoc); presetsModifiedTime = toki.second(); //unix time updateFSInfo(); } diff --git a/wled00/util.cpp b/wled00/util.cpp index 1bd8ec319..ad7e4b670 100644 --- a/wled00/util.cpp +++ b/wled00/util.cpp @@ -228,7 +228,6 @@ bool requestJSONBufferLock(uint8_t module) DEBUG_PRINT(F("JSON buffer locked. (")); DEBUG_PRINT(jsonBufferLock); DEBUG_PRINTLN(")"); - fileDoc = pDoc; // used for applying presets (presets.cpp) pDoc->clear(); return true; } @@ -239,7 +238,6 @@ void releaseJSONBufferLock() DEBUG_PRINT(F("JSON buffer released. (")); DEBUG_PRINT(jsonBufferLock); DEBUG_PRINTLN(")"); - fileDoc = nullptr; jsonBufferLock = 0; } diff --git a/wled00/wled.h b/wled00/wled.h index 62fd4ec4d..1361f9f7e 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2403240 +#define VERSION 2403260 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG @@ -692,7 +692,6 @@ WLED_GLOBAL uint16_t olen _INIT(0); WLED_GLOBAL size_t fsBytesUsed _INIT(0); WLED_GLOBAL size_t fsBytesTotal _INIT(0); WLED_GLOBAL unsigned long presetsModifiedTime _INIT(0L); -WLED_GLOBAL JsonDocument* fileDoc; WLED_GLOBAL bool doCloseFile _INIT(false); // presets diff --git a/wled00/ws.cpp b/wled00/ws.cpp index 16636bb1e..307a0959e 100644 --- a/wled00/ws.cpp +++ b/wled00/ws.cpp @@ -55,7 +55,7 @@ void wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTyp } else { verboseResponse = deserializeState(root); } - releaseJSONBufferLock(); // will clean fileDoc + releaseJSONBufferLock(); if (!interfaceUpdateCallMode) { // individual client response only needed if no WS broadcast soon if (verboseResponse) {