From 799d4f94651791110fc7166954725d9958df0238 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Wed, 14 Sep 2022 22:28:06 +0200 Subject: [PATCH] Playlist load bugfix. serializeConfig() in loop() (prevent crash/hang in web server callback) --- wled00/FX.cpp | 2 +- wled00/cfg.cpp | 2 ++ wled00/json.cpp | 9 +++++++-- wled00/set.cpp | 4 ++-- wled00/wled.cpp | 26 ++++++++++---------------- wled00/wled.h | 7 ++++--- wled00/wled_server.cpp | 2 +- 7 files changed, 27 insertions(+), 25 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index 4ddb67f89..30d1c6fa7 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -7099,7 +7099,7 @@ uint16_t mode_2DGEQ(void) { // By Will Tatam. Code reduction by Ewoud Wijma. if (SEGENV.call == 0) for (int i=0; i= (256 - SEGMENT.intensity)) { + if (millis() - SEGENV.step >= (256U - SEGMENT.intensity)) { SEGENV.step = millis(); rippleTime = true; } diff --git a/wled00/cfg.cpp b/wled00/cfg.cpp index f615f9669..4b9119053 100644 --- a/wled00/cfg.cpp +++ b/wled00/cfg.cpp @@ -946,6 +946,8 @@ void serializeConfig() { if (f) serializeJson(doc, f); f.close(); releaseJSONBufferLock(); + + doSerializeConfig = false; } //settings in /wsec.json, not accessible via webserver, for passwords and tokens diff --git a/wled00/json.cpp b/wled00/json.cpp index 89e683dfa..4b49d104d 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -426,11 +426,14 @@ bool deserializeState(JsonObject root, byte callMode, byte presetId) } else if (root["win"].isNull() && getVal(root["ps"], &ps, 0, 0) && ps > 0 && ps < 251 && ps != currentPreset) { // b) preset ID only or preset that does not change state (use embedded cycling limits if they exist in getVal()) presetCycCurr = ps; + presetId = ps; root.remove(F("v")); // may be added in UI call root.remove(F("time")); // may be added in UI call root.remove("ps"); - if (root.size() == 0) applyPreset(ps, callMode); // async load (only preset ID was specified) - return stateResponse; + if (root.size() == 0) { + applyPreset(ps, callMode); // async load (only preset ID was specified) + return stateResponse; + } } } @@ -580,6 +583,8 @@ void serializeInfo(JsonObject root) leds["fps"] = strip.getFps(); leds[F("maxpwr")] = (strip.currentMilliamps)? strip.ablMilliampsMax : 0; leds[F("maxseg")] = strip.getMaxSegments(); + //leds[F("actseg")] = strip.getActiveSegmentsNum(); + //leds[F("seglock")] = false; //might be used in the future to prevent modifications to segment config leds[F("cpal")] = strip.customPalettes.size(); //number of custom palettes #ifndef WLED_DISABLE_2D diff --git a/wled00/set.cpp b/wled00/set.cpp index 76f142b63..190afa972 100644 --- a/wled00/set.cpp +++ b/wled00/set.cpp @@ -607,7 +607,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) } } usermods.readFromConfig(um); // force change of usermod parameters - + DEBUG_PRINTLN(F("Done re-init usermods.")); releaseJSONBufferLock(); } @@ -638,7 +638,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) #endif lastEditTime = millis(); - if (subPage != 2 && !doReboot) serializeConfig(); //do not save if factory reset or LED settings (which are saved after LED re-init) + if (subPage != 2 && !doReboot) doSerializeConfig = true; //serializeConfig(); //do not save if factory reset or LED settings (which are saved after LED re-init) if (subPage == 4) alexaInit(); } diff --git a/wled00/wled.cpp b/wled00/wled.cpp index f2e2f7711..1433b41d4 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -71,6 +71,8 @@ void WLED::loop() yield(); + if (doSerializeConfig) serializeConfig(); + if (doReboot && !doInitBusses) // if busses have to be inited & saved, wait until next iteration reset(); @@ -351,6 +353,7 @@ void WLED::setup() #endif updateFSInfo(); + strcpy_P(apSSID, PSTR("WLED-AP")); // otherwise it is empty on first boot until config is saved DEBUG_PRINTLN(F("Reading config")); deserializeConfigFromFS(); @@ -388,25 +391,16 @@ void WLED::setup() escapedMac = WiFi.macAddress(); escapedMac.replace(":", ""); escapedMac.toLowerCase(); - if (strcmp(cmDNS, "x") == 0) // fill in unique mdns default - { - strcpy_P(cmDNS, PSTR("wled-")); - sprintf(cmDNS + 5, "%*s", 6, escapedMac.c_str() + 6); - } - if (mqttDeviceTopic[0] == 0) { - strcpy_P(mqttDeviceTopic, PSTR("wled/")); - sprintf(mqttDeviceTopic + 5, "%*s", 6, escapedMac.c_str() + 6); - } - if (mqttClientID[0] == 0) { - strcpy_P(mqttClientID, PSTR("WLED-")); - sprintf(mqttClientID + 5, "%*s", 6, escapedMac.c_str() + 6); - } + // fill in unique mdns default + if (strcmp(cmDNS, "x") == 0) sprintf_P(cmDNS, PSTR("wled-%*s"), 6, escapedMac.c_str() + 6); + if (mqttDeviceTopic[0] == 0) sprintf_P(mqttDeviceTopic, PSTR("wled/%*s"), 6, escapedMac.c_str() + 6); + if (mqttClientID[0] == 0) sprintf_P(mqttClientID, PSTR("WLED-%*s"), 6, escapedMac.c_str() + 6); #ifdef WLED_ENABLE_ADALIGHT if (Serial.available() > 0 && Serial.peek() == 'I') handleImprovPacket(); #endif - strip.service(); + strip.service(); // why? #ifndef WLED_DISABLE_OTA if (aOtaEnabled) { @@ -474,10 +468,10 @@ void WLED::initAP(bool resetAP) if (apBehavior == AP_BEHAVIOR_BUTTON_ONLY && !resetAP) return; - if (!apSSID[0] || resetAP) + if (resetAP) { strcpy_P(apSSID, PSTR("WLED-AP")); - if (resetAP) strcpy_P(apPass, PSTR(DEFAULT_AP_PASS)); + } DEBUG_PRINT(F("Opening access point ")); DEBUG_PRINTLN(apSSID); WiFi.softAPConfig(IPAddress(4, 3, 2, 1), IPAddress(4, 3, 2, 1), IPAddress(255, 255, 255, 0)); diff --git a/wled00/wled.h b/wled00/wled.h index d9b978a42..893a97aaf 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2209131 +#define VERSION 2209141 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG @@ -619,8 +619,9 @@ WLED_GLOBAL byte errorFlag _INIT(0); WLED_GLOBAL String messageHead, messageSub; WLED_GLOBAL byte optionType; -WLED_GLOBAL bool doReboot _INIT(false); // flag to initiate reboot from async handlers -WLED_GLOBAL bool doPublishMqtt _INIT(false); +WLED_GLOBAL bool doSerializeConfig _INIT(false); // flag to initiate saving of config +WLED_GLOBAL bool doReboot _INIT(false); // flag to initiate reboot from async handlers +WLED_GLOBAL bool doPublishMqtt _INIT(false); // status led #if defined(STATUSLED) diff --git a/wled00/wled_server.cpp b/wled00/wled_server.cpp index da2aadb33..626c27082 100644 --- a/wled00/wled_server.cpp +++ b/wled00/wled_server.cpp @@ -207,7 +207,7 @@ void initServer() if (!isConfig) { serveJson(request); return; //if JSON contains "v" } else { - serializeConfig(); //Save new settings to FS + doSerializeConfig = true; //serializeConfig(); //Save new settings to FS } } request->send(200, "application/json", F("{\"success\":true}"));