From 1f81fb9284e53a4b55169fca028a607d3219a644 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Thu, 21 Dec 2023 21:30:17 +0100 Subject: [PATCH 1/7] Implement JSON buffer in PSRAM to free up DRAM. --- wled00/FX_2Dfcn.cpp | 4 +-- wled00/FX_fcn.cpp | 5 +-- wled00/cfg.cpp | 69 +++++++++++++++++++++++------------------- wled00/ir.cpp | 4 +-- wled00/json.cpp | 2 +- wled00/mqtt.cpp | 4 +-- wled00/pin_manager.cpp | 5 ++- wled00/presets.cpp | 8 ++--- wled00/remote.cpp | 4 +-- wled00/set.cpp | 2 +- wled00/udp.cpp | 4 +-- wled00/util.cpp | 15 ++++++--- wled00/wled.cpp | 10 ++++++ wled00/wled.h | 9 ++++-- wled00/wled_eeprom.cpp | 4 +-- wled00/wled_serial.cpp | 12 ++++---- wled00/wled_server.cpp | 4 +-- wled00/ws.cpp | 14 ++++----- wled00/xml.cpp | 2 +- 19 files changed, 106 insertions(+), 75 deletions(-) diff --git a/wled00/FX_2Dfcn.cpp b/wled00/FX_2Dfcn.cpp index d2622f901..15ff0fdb5 100644 --- a/wled00/FX_2Dfcn.cpp +++ b/wled00/FX_2Dfcn.cpp @@ -94,12 +94,12 @@ void WS2812FX::setUpMatrix() { DEBUG_PRINT(F("Reading LED gap from ")); DEBUG_PRINTLN(fileName); // read the array into global JSON buffer - if (readObjectFromFile(fileName, nullptr, &doc)) { + if (readObjectFromFile(fileName, nullptr, pDoc)) { // the array is similar to ledmap, except it has only 3 values: // -1 ... missing pixel (do not increase pixel count) // 0 ... inactive pixel (it does count, but should be mapped out (-1)) // 1 ... active pixel (it will count and will be mapped) - JsonArray map = doc.as(); + JsonArray map = pDoc->as(); gapSize = map.size(); if (!map.isNull() && gapSize >= customMappingSize) { // not an empty map gapTable = new int8_t[gapSize]; diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 42984a051..d4e4cf9a8 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1670,7 +1670,7 @@ bool WS2812FX::deserializeMap(uint8_t n) { if (!requestJSONBufferLock(7)) return false; - if (!readObjectFromFile(fileName, nullptr, &doc)) { + if (!readObjectFromFile(fileName, nullptr, pDoc)) { releaseJSONBufferLock(); return false; //if file does not exist just exit } @@ -1685,7 +1685,8 @@ bool WS2812FX::deserializeMap(uint8_t n) { customMappingTable = nullptr; } - JsonArray map = doc[F("map")]; + JsonObject root = pDoc->as(); + JsonArray map = root[F("map")]; if (!map.isNull() && map.size()) { // not an empty map customMappingSize = map.size(); customMappingTable = new uint16_t[customMappingSize]; diff --git a/wled00/cfg.cpp b/wled00/cfg.cpp index b036dc1d4..948bcfada 100644 --- a/wled00/cfg.cpp +++ b/wled00/cfg.cpp @@ -609,7 +609,7 @@ void deserializeConfigFromFS() { DEBUG_PRINTLN(F("Reading settings from /cfg.json...")); - success = readObjectFromFile("/cfg.json", nullptr, &doc); + success = readObjectFromFile("/cfg.json", nullptr, pDoc); if (!success) { // if file does not exist, optionally try reading from EEPROM and then save defaults to FS releaseJSONBufferLock(); #ifdef WLED_ADD_EEPROM_SUPPORT @@ -630,7 +630,8 @@ void deserializeConfigFromFS() { // NOTE: This routine deserializes *and* applies the configuration // Therefore, must also initialize ethernet from this function - bool needsSave = deserializeConfig(doc.as(), true); + JsonObject root = pDoc->as(); + bool needsSave = deserializeConfig(root, true); releaseJSONBufferLock(); if (needsSave) serializeConfig(); // usermods required new parameters @@ -643,19 +644,21 @@ void serializeConfig() { if (!requestJSONBufferLock(2)) return; - JsonArray rev = doc.createNestedArray("rev"); + JsonObject root = pDoc->as(); + + JsonArray rev = root.createNestedArray("rev"); rev.add(1); //major settings revision rev.add(0); //minor settings revision - doc[F("vid")] = VERSION; + root[F("vid")] = VERSION; - JsonObject id = doc.createNestedObject("id"); + JsonObject id = root.createNestedObject("id"); id[F("mdns")] = cmDNS; id[F("name")] = serverDescription; id[F("inv")] = alexaInvocationName; id[F("sui")] = simplifiedUI; - JsonObject nw = doc.createNestedObject("nw"); + JsonObject nw = root.createNestedObject("nw"); #ifndef WLED_DISABLE_ESPNOW nw[F("espnow")] = enableESPNow; nw[F("linked_remote")] = linked_remote; @@ -677,7 +680,7 @@ void serializeConfig() { nw_ins_0_sn.add(staticSubnet[i]); } - JsonObject ap = doc.createNestedObject("ap"); + JsonObject ap = root.createNestedObject("ap"); ap[F("ssid")] = apSSID; ap[F("pskl")] = strlen(apPass); ap[F("chan")] = apChannel; @@ -690,12 +693,12 @@ void serializeConfig() { ap_ip.add(2); ap_ip.add(1); - JsonObject wifi = doc.createNestedObject("wifi"); + JsonObject wifi = root.createNestedObject("wifi"); wifi[F("sleep")] = !noWifiSleep; //wifi[F("phy")] = 1; #ifdef WLED_USE_ETHERNET - JsonObject ethernet = doc.createNestedObject("eth"); + JsonObject ethernet = root.createNestedObject("eth"); ethernet["type"] = ethernetType; if (ethernetType != WLED_ETH_NONE && ethernetType < WLED_NUM_ETH_TYPES) { JsonArray pins = ethernet.createNestedArray("pin"); @@ -718,7 +721,7 @@ void serializeConfig() { } #endif - JsonObject hw = doc.createNestedObject("hw"); + JsonObject hw = root.createNestedObject("hw"); JsonObject hw_led = hw.createNestedObject("led"); hw_led[F("total")] = strip.getLengthTotal(); //provided for compatibility on downgrade and per-output ABL @@ -830,7 +833,7 @@ void serializeConfig() { //JsonObject hw_status = hw.createNestedObject("status"); //hw_status["pin"] = -1; - JsonObject light = doc.createNestedObject(F("light")); + JsonObject light = root.createNestedObject(F("light")); light[F("scale-bri")] = briMultiplier; light[F("pal-mode")] = strip.paletteBlend; light[F("aseg")] = autoSegments; @@ -853,12 +856,12 @@ void serializeConfig() { light_nl[F("tbri")] = nightlightTargetBri; light_nl["macro"] = macroNl; - JsonObject def = doc.createNestedObject("def"); + JsonObject def = root.createNestedObject("def"); def["ps"] = bootPreset; def["on"] = turnOnAtBoot; def["bri"] = briS; - JsonObject interfaces = doc.createNestedObject("if"); + JsonObject interfaces = root.createNestedObject("if"); JsonObject if_sync = interfaces.createNestedObject("sync"); if_sync[F("port0")] = udpPort; @@ -961,7 +964,7 @@ void serializeConfig() { if_ntp[F("ln")] = longitude; if_ntp[F("lt")] = latitude; - JsonObject ol = doc.createNestedObject("ol"); + JsonObject ol = root.createNestedObject("ol"); ol[F("clock")] = overlayCurrent; ol[F("cntdwn")] = countdownMode; @@ -971,7 +974,7 @@ void serializeConfig() { ol[F("o5m")] = analogClock5MinuteMarks; ol[F("osec")] = analogClockSecondsTrail; - JsonObject timers = doc.createNestedObject(F("timers")); + JsonObject timers = root.createNestedObject(F("timers")); JsonObject cntdwn = timers.createNestedObject(F("cntdwn")); JsonArray goal = cntdwn.createNestedArray(F("goal")); @@ -999,14 +1002,14 @@ void serializeConfig() { } } - JsonObject ota = doc.createNestedObject("ota"); + JsonObject ota = root.createNestedObject("ota"); ota[F("lock")] = otaLock; ota[F("lock-wifi")] = wifiLock; ota[F("pskl")] = strlen(otaPass); ota[F("aota")] = aOtaEnabled; #ifdef WLED_ENABLE_DMX - JsonObject dmx = doc.createNestedObject("dmx"); + JsonObject dmx = root.createNestedObject("dmx"); dmx[F("chan")] = DMXChannels; dmx[F("gap")] = DMXGap; dmx["start"] = DMXStart; @@ -1020,11 +1023,11 @@ void serializeConfig() { dmx[F("e131proxy")] = e131ProxyUniverse; #endif - JsonObject usermods_settings = doc.createNestedObject("um"); + JsonObject usermods_settings = root.createNestedObject("um"); usermods.addToConfig(usermods_settings); File f = WLED_FS.open("/cfg.json", "w"); - if (f) serializeJson(doc, f); + if (f) serializeJson(*pDoc, f); f.close(); releaseJSONBufferLock(); @@ -1037,19 +1040,21 @@ bool deserializeConfigSec() { if (!requestJSONBufferLock(3)) return false; - bool success = readObjectFromFile("/wsec.json", nullptr, &doc); + bool success = readObjectFromFile("/wsec.json", nullptr, pDoc); if (!success) { releaseJSONBufferLock(); return false; } - JsonObject nw_ins_0 = doc["nw"]["ins"][0]; + JsonObject root = pDoc->as(); + + JsonObject nw_ins_0 = root["nw"]["ins"][0]; getStringFromJson(clientPass, nw_ins_0["psk"], 65); - JsonObject ap = doc["ap"]; + JsonObject ap = root["ap"]; getStringFromJson(apPass, ap["psk"] , 65); - [[maybe_unused]] JsonObject interfaces = doc["if"]; + [[maybe_unused]] JsonObject interfaces = root["if"]; #ifdef WLED_ENABLE_MQTT JsonObject if_mqtt = interfaces["mqtt"]; @@ -1060,10 +1065,10 @@ bool deserializeConfigSec() { getStringFromJson(hueApiKey, interfaces["hue"][F("key")], 47); #endif - getStringFromJson(settingsPIN, doc["pin"], 5); + getStringFromJson(settingsPIN, root["pin"], 5); correctPIN = !strlen(settingsPIN); - JsonObject ota = doc["ota"]; + JsonObject ota = root["ota"]; getStringFromJson(otaPass, ota[F("pwd")], 33); CJSON(otaLock, ota[F("lock")]); CJSON(wifiLock, ota[F("lock-wifi")]); @@ -1078,17 +1083,19 @@ void serializeConfigSec() { if (!requestJSONBufferLock(4)) return; - JsonObject nw = doc.createNestedObject("nw"); + JsonObject root = pDoc->as(); + + JsonObject nw = root.createNestedObject("nw"); JsonArray nw_ins = nw.createNestedArray("ins"); JsonObject nw_ins_0 = nw_ins.createNestedObject(); nw_ins_0["psk"] = clientPass; - JsonObject ap = doc.createNestedObject("ap"); + JsonObject ap = root.createNestedObject("ap"); ap["psk"] = apPass; - [[maybe_unused]] JsonObject interfaces = doc.createNestedObject("if"); + [[maybe_unused]] JsonObject interfaces = root.createNestedObject("if"); #ifdef WLED_ENABLE_MQTT JsonObject if_mqtt = interfaces.createNestedObject("mqtt"); if_mqtt["psk"] = mqttPass; @@ -1098,16 +1105,16 @@ void serializeConfigSec() { if_hue[F("key")] = hueApiKey; #endif - doc["pin"] = settingsPIN; + root["pin"] = settingsPIN; - JsonObject ota = doc.createNestedObject("ota"); + JsonObject ota = root.createNestedObject("ota"); ota[F("pwd")] = otaPass; ota[F("lock")] = otaLock; ota[F("lock-wifi")] = wifiLock; ota[F("aota")] = aOtaEnabled; File f = WLED_FS.open("/wsec.json", "w"); - if (f) serializeJson(doc, f); + if (f) serializeJson(*pDoc, f); f.close(); releaseJSONBufferLock(); } diff --git a/wled00/ir.cpp b/wled00/ir.cpp index fa253b015..3da4d68be 100644 --- a/wled00/ir.cpp +++ b/wled00/ir.cpp @@ -643,8 +643,8 @@ void decodeIRJson(uint32_t code) // this may fail for two reasons: ir.json does not exist or IR code not found // if the IR code is not found readObjectFromFile() will clean() doc JSON document // so we can differentiate between the two - readObjectFromFile("/ir.json", objKey, &doc); - fdo = doc.as(); + readObjectFromFile("/ir.json", objKey, pDoc); + fdo = pDoc->as(); lastValidCode = 0; if (fdo.isNull()) { //the received code does not exist diff --git a/wled00/json.cpp b/wled00/json.cpp index 5b20baac1..9a94796f2 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -1055,7 +1055,7 @@ void serveJson(AsyncWebServerRequest* request) servingClient = false; return; } - AsyncJsonResponse *response = new AsyncJsonResponse(&doc, subJson==JSON_PATH_FXDATA || subJson==JSON_PATH_EFFECTS); // will clear and convert JsonDocument into JsonArray if necessary + AsyncJsonResponse *response = new AsyncJsonResponse(pDoc, subJson==JSON_PATH_FXDATA || subJson==JSON_PATH_EFFECTS); // will clear and convert JsonDocument into JsonArray if necessary JsonVariant lDoc = response->getRoot(); diff --git a/wled00/mqtt.cpp b/wled00/mqtt.cpp index a5caaf472..3c753a9a9 100644 --- a/wled00/mqtt.cpp +++ b/wled00/mqtt.cpp @@ -109,8 +109,8 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties return; } if (payloadStr[0] == '{') { //JSON API - deserializeJson(doc, payloadStr); - deserializeState(doc.as()); + deserializeJson(*pDoc, payloadStr); + deserializeState(pDoc->as()); } else { //HTTP API String apireq = "win"; apireq += '&'; // reduce flash string usage apireq += payloadStr; diff --git a/wled00/pin_manager.cpp b/wled00/pin_manager.cpp index a1ac59ce7..102acd0ea 100644 --- a/wled00/pin_manager.cpp +++ b/wled00/pin_manager.cpp @@ -238,7 +238,7 @@ bool PinManagerClass::isPinAllocated(byte gpio, PinOwner tag) // Check if supplied GPIO is ok to use bool PinManagerClass::isPinOk(byte gpio, bool output) { -#ifdef ESP32 +#ifdef ARDUINO_ARCH_ESP32 if (digitalPinIsValid(gpio)) { #if defined(CONFIG_IDF_TARGET_ESP32C3) // strapping pins: 2, 8, & 9 @@ -257,6 +257,9 @@ bool PinManagerClass::isPinOk(byte gpio, bool output) // GPIO46 is input only and pulled down #else if (gpio > 5 && gpio < 12) return false; //SPI flash pins + #ifdef BOARD_HAS_PSRAM + if (gpio == 16 || gpio == 17) return false; //PSRAM pins + #endif #endif if (output) return digitalPinCanOutput(gpio); else return true; diff --git a/wled00/presets.cpp b/wled00/presets.cpp index 975b68974..be6005492 100644 --- a/wled00/presets.cpp +++ b/wled00/presets.cpp @@ -27,7 +27,7 @@ static void doSaveState() { if (!requestJSONBufferLock(10)) return; // will set fileDoc initPresetsFile(); // just in case if someone deleted presets.json using /edit - JsonObject sObj = doc.to(); + JsonObject sObj = pDoc->to(); DEBUG_PRINTLN(F("Serialize current state")); if (playlistSave) { @@ -42,7 +42,7 @@ static void doSaveState() { /* #ifdef WLED_DEBUG DEBUG_PRINTLN(F("Serialized preset")); - serializeJson(doc,Serial); + serializeJson(*pDoc,Serial); DEBUG_PRINTLN(); #endif */ @@ -83,9 +83,9 @@ bool getPresetName(byte index, String& name) { if (!requestJSONBufferLock(9)) return false; bool presetExists = false; - if (readObjectFromFileUsingId(getFileName(), index, &doc)) + if (readObjectFromFileUsingId(getFileName(), index, pDoc)) { - JsonObject fdo = doc.as(); + JsonObject fdo = pDoc->as(); if (fdo["n"]) { name = (const char*)(fdo["n"]); presetExists = true; diff --git a/wled00/remote.cpp b/wled00/remote.cpp index e547903d2..c41d88421 100644 --- a/wled00/remote.cpp +++ b/wled00/remote.cpp @@ -123,8 +123,8 @@ static bool remoteJson(int button) sprintf_P(objKey, PSTR("\"%d\":"), button); // attempt to read command from remote.json - readObjectFromFile("/remote.json", objKey, &doc); - JsonObject fdo = doc.as(); + readObjectFromFile("/remote.json", objKey, pDoc); + JsonObject fdo = pDoc->as(); if (fdo.isNull()) { // the received button does not exist if (!WLED_FS.exists("/remote.json")) errorFlag = ERR_FS_RMLOAD; //warn if file itself doesn't exist diff --git a/wled00/set.cpp b/wled00/set.cpp index a63cd09c9..34f1d1ab0 100644 --- a/wled00/set.cpp +++ b/wled00/set.cpp @@ -625,7 +625,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) } } - JsonObject um = doc.createNestedObject("um"); + JsonObject um = pDoc->createNestedObject("um"); size_t args = request->args(); uint16_t j=0; diff --git a/wled00/udp.cpp b/wled00/udp.cpp index e00fe0766..4c55a9727 100644 --- a/wled00/udp.cpp +++ b/wled00/udp.cpp @@ -664,8 +664,8 @@ void handleNotifications() apireq += (char*)udpIn; handleSet(nullptr, apireq); } else if (udpIn[0] == '{') { //JSON API - DeserializationError error = deserializeJson(doc, udpIn); - JsonObject root = doc.as(); + DeserializationError error = deserializeJson(*pDoc, udpIn); + JsonObject root = pDoc->as(); if (!error && !root.isNull()) deserializeState(root); } releaseJSONBufferLock(); diff --git a/wled00/util.cpp b/wled00/util.cpp index 8eedee8f4..d6fa9fb18 100644 --- a/wled00/util.cpp +++ b/wled00/util.cpp @@ -209,6 +209,10 @@ bool isAsterisksOnly(const char* str, byte maxLen) //threading/network callback details: https://github.com/Aircoookie/WLED/pull/2336#discussion_r762276994 bool requestJSONBufferLock(uint8_t module) { + if (pDoc == nullptr) { + DEBUG_PRINTLN(F("ERROR: JSON buffer not allocated!")); + return false; + } unsigned long now = millis(); while (jsonBufferLock && millis()-now < 1000) delay(1); // wait for a second for buffer lock @@ -224,8 +228,8 @@ bool requestJSONBufferLock(uint8_t module) DEBUG_PRINT(F("JSON buffer locked. (")); DEBUG_PRINT(jsonBufferLock); DEBUG_PRINTLN(")"); - fileDoc = &doc; // used for applying presets (presets.cpp) - doc.clear(); + fileDoc = pDoc; // used for applying presets (presets.cpp) + pDoc->clear(); return true; } @@ -556,11 +560,12 @@ void enumerateLedmaps() { #ifndef ESP8266 if (requestJSONBufferLock(21)) { - if (readObjectFromFile(fileName, nullptr, &doc)) { + if (readObjectFromFile(fileName, nullptr, pDoc)) { size_t len = 0; - if (!doc["n"].isNull()) { + JsonObject root = pDoc->as(); + if (!root["n"].isNull()) { // name field exists - const char *name = doc["n"].as(); + const char *name = root["n"].as(); if (name != nullptr) len = strlen(name); if (len > 0 && len < 33) { ledmapNames[i-1] = new char[len+1]; diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 57a79d4d4..84140f9b1 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -346,6 +346,11 @@ void WLED::setup() DEBUG_PRINT(F("heap ")); DEBUG_PRINTLN(ESP.getFreeHeap()); #if defined(ARDUINO_ARCH_ESP32) && defined(BOARD_HAS_PSRAM) +/* + * The following code is obsolete as PinManager::isPinOK() will return false for reserved GPIO. + * Additionally xml.cpp will inform UI about reserved GPIO. + * + #if defined(CONFIG_IDF_TARGET_ESP32S3) // S3: reserve GPIO 33-37 for "octal" PSRAM managed_pin_type pins[] = { {33, true}, {34, true}, {35, true}, {36, true}, {37, true} }; @@ -363,12 +368,17 @@ void WLED::setup() managed_pin_type pins[] = { {16, true}, {17, true} }; pinManager.allocateMultiplePins(pins, sizeof(pins)/sizeof(managed_pin_type), PinOwner::SPI_RAM); #endif +*/ #if defined(WLED_USE_PSRAM) + pDoc = new PSRAMDynamicJsonDocument(2*JSON_BUFFER_SIZE); + if (!pDoc) pDoc = new PSRAMDynamicJsonDocument(JSON_BUFFER_SIZE); // falback if double sized buffer could not be allocated + // if the above still fails requestJsonBufferLock() will always return false preventing crashes if (psramFound()) { DEBUG_PRINT(F("Total PSRAM: ")); DEBUG_PRINT(ESP.getPsramSize()/1024); DEBUG_PRINTLN("kB"); DEBUG_PRINT(F("Free PSRAM : ")); DEBUG_PRINT(ESP.getFreePsram()/1024); DEBUG_PRINTLN("kB"); } #else + if (!pDoc) pDoc = &gDoc; // just in case ... (it should be globally assigned) DEBUG_PRINTLN(F("PSRAM not used.")); #endif #endif diff --git a/wled00/wled.h b/wled00/wled.h index 868403d3d..f799a7be9 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2312190 +#define VERSION 2312210 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG @@ -758,7 +758,12 @@ WLED_GLOBAL int8_t spi_sclk _INIT(SPISCLKPIN); #endif // global ArduinoJson buffer -WLED_GLOBAL StaticJsonDocument doc; +#if defined(ARDUINO_ARCH_ESP32) && defined(BOARD_HAS_PSRAM) && defined(WLED_USE_PSRAM) +WLED_GLOBAL JsonDocument *pDoc _INIT(nullptr); +#else +WLED_GLOBAL StaticJsonDocument gDoc; +WLED_GLOBAL JsonDocument *pDoc _INIT(&gDoc); +#endif WLED_GLOBAL volatile uint8_t jsonBufferLock _INIT(0); // enable additional debug output diff --git a/wled00/wled_eeprom.cpp b/wled00/wled_eeprom.cpp index 7c9933a9a..90e7dd4b3 100644 --- a/wled00/wled_eeprom.cpp +++ b/wled00/wled_eeprom.cpp @@ -371,7 +371,7 @@ void deEEP() { DEBUGFS_PRINTLN(F("Allocating saving buffer for dEEP")); if (!requestJSONBufferLock(8)) return; - JsonObject sObj = doc.to(); + JsonObject sObj = pDoc->to(); sObj.createNestedObject("0"); EEPROM.begin(EEPSIZE); @@ -448,7 +448,7 @@ void deEEP() { releaseJSONBufferLock(); return; } - serializeJson(doc, f); + serializeJson(*pDoc, f); f.close(); releaseJSONBufferLock(); diff --git a/wled00/wled_serial.cpp b/wled00/wled_serial.cpp index 4d920e340..c6e984035 100644 --- a/wled00/wled_serial.cpp +++ b/wled00/wled_serial.cpp @@ -115,21 +115,21 @@ void handleSerial() bool verboseResponse = false; if (!requestJSONBufferLock(16)) return; Serial.setTimeout(100); - DeserializationError error = deserializeJson(doc, Serial); + DeserializationError error = deserializeJson(*pDoc, Serial); if (error) { releaseJSONBufferLock(); return; } - verboseResponse = deserializeState(doc.as()); + verboseResponse = deserializeState(pDoc->as()); //only send response if TX pin is unused for other purposes if (verboseResponse && (!pinManager.isPinAllocated(hardwareTX) || pinManager.getPinOwner(hardwareTX) == PinOwner::DebugOut)) { - doc.clear(); - JsonObject state = doc.createNestedObject("state"); + pDoc->clear(); + JsonObject state = pDoc->createNestedObject("state"); serializeState(state); - JsonObject info = doc.createNestedObject("info"); + JsonObject info = pDoc->createNestedObject("info"); serializeInfo(info); - serializeJson(doc, Serial); + serializeJson(*pDoc, Serial); Serial.println(); } releaseJSONBufferLock(); diff --git a/wled00/wled_server.cpp b/wled00/wled_server.cpp index 7d9d1a1ec..dcb21990e 100644 --- a/wled00/wled_server.cpp +++ b/wled00/wled_server.cpp @@ -180,8 +180,8 @@ void initServer() if (!requestJSONBufferLock(14)) return; - DeserializationError error = deserializeJson(doc, (uint8_t*)(request->_tempObject)); - JsonObject root = doc.as(); + DeserializationError error = deserializeJson(*pDoc, (uint8_t*)(request->_tempObject)); + JsonObject root = pDoc->as(); if (error || root.isNull()) { releaseJSONBufferLock(); serveJsonError(request, 400, ERR_JSON); diff --git a/wled00/ws.cpp b/wled00/ws.cpp index a4deca4c4..7082a848d 100644 --- a/wled00/ws.cpp +++ b/wled00/ws.cpp @@ -38,8 +38,8 @@ void wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTyp bool verboseResponse = false; if (!requestJSONBufferLock(11)) return; - DeserializationError error = deserializeJson(doc, data, len); - JsonObject root = doc.as(); + DeserializationError error = deserializeJson(*pDoc, data, len); + JsonObject root = pDoc->as(); if (error || root.isNull()) { releaseJSONBufferLock(); return; @@ -103,13 +103,13 @@ void sendDataWs(AsyncWebSocketClient * client) if (!requestJSONBufferLock(12)) return; - JsonObject state = doc.createNestedObject("state"); + JsonObject state = pDoc->createNestedObject("state"); serializeState(state); - JsonObject info = doc.createNestedObject("info"); + JsonObject info = pDoc->createNestedObject("info"); serializeInfo(info); - size_t len = measureJson(doc); - DEBUG_PRINTF("JSON buffer size: %u for WS request (%u).\n", doc.memoryUsage(), len); + size_t len = measureJson(*pDoc); + DEBUG_PRINTF("JSON buffer size: %u for WS request (%u).\n", pDoc->memoryUsage(), len); size_t heap1 = ESP.getFreeHeap(); DEBUG_PRINT(F("heap ")); DEBUG_PRINTLN(ESP.getFreeHeap()); @@ -136,7 +136,7 @@ void sendDataWs(AsyncWebSocketClient * client) } buffer->lock(); - serializeJson(doc, (char *)buffer->get(), len); + serializeJson(*pDoc, (char *)buffer->get(), len); DEBUG_PRINT(F("Sending WS data ")); if (client) { diff --git a/wled00/xml.cpp b/wled00/xml.cpp index 9a7fc4acd..05842911b 100644 --- a/wled00/xml.cpp +++ b/wled00/xml.cpp @@ -133,7 +133,7 @@ void appendGPIOinfo() { // usermod pin reservations will become unnecessary when settings pages will read cfg.json directly if (requestJSONBufferLock(6)) { // if we can't allocate JSON buffer ignore usermod pins - JsonObject mods = doc.createNestedObject(F("um")); + JsonObject mods = pDoc->createNestedObject(F("um")); usermods.addToConfig(mods); if (!mods.isNull()) fillUMPins(mods); releaseJSONBufferLock(); From 7971f3cbd879a2478cb5bcfad2d189ee9f900c67 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Sat, 23 Dec 2023 22:56:07 +0100 Subject: [PATCH 2/7] Fix for saving config. --- wled00/cfg.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/wled00/cfg.cpp b/wled00/cfg.cpp index 948bcfada..1e97439af 100644 --- a/wled00/cfg.cpp +++ b/wled00/cfg.cpp @@ -644,7 +644,7 @@ void serializeConfig() { if (!requestJSONBufferLock(2)) return; - JsonObject root = pDoc->as(); + JsonObject root = pDoc->to(); JsonArray rev = root.createNestedArray("rev"); rev.add(1); //major settings revision @@ -1027,7 +1027,7 @@ void serializeConfig() { usermods.addToConfig(usermods_settings); File f = WLED_FS.open("/cfg.json", "w"); - if (f) serializeJson(*pDoc, f); + if (f) serializeJson(root, f); f.close(); releaseJSONBufferLock(); @@ -1083,7 +1083,7 @@ void serializeConfigSec() { if (!requestJSONBufferLock(4)) return; - JsonObject root = pDoc->as(); + JsonObject root = pDoc->to(); JsonObject nw = root.createNestedObject("nw"); @@ -1114,7 +1114,7 @@ void serializeConfigSec() { ota[F("aota")] = aOtaEnabled; File f = WLED_FS.open("/wsec.json", "w"); - if (f) serializeJson(*pDoc, f); + if (f) serializeJson(root, f); f.close(); releaseJSONBufferLock(); } From 1c1b67e000170b43fe462a124d9d6e684cc9b3af Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Fri, 29 Dec 2023 23:07:29 +0100 Subject: [PATCH 3/7] Full per-port ABL implementation and bugfix. Update of BusManager class (static) --- usermods/quinled-an-penta/quinled-an-penta.h | 8 +- wled00/FX.h | 6 - wled00/FX_fcn.cpp | 46 +- wled00/bus_manager.cpp | 37 +- wled00/bus_manager.h | 48 +- wled00/button.cpp | 4 +- wled00/cfg.cpp | 26 +- wled00/data/settings_leds.htm | 64 +- wled00/html_settings.h | 1098 +++++++++--------- wled00/json.cpp | 4 +- wled00/set.cpp | 6 +- wled00/wled.cpp | 8 +- wled00/wled.h | 2 +- wled00/wled_eeprom.cpp | 2 +- wled00/xml.cpp | 21 +- 15 files changed, 702 insertions(+), 678 deletions(-) diff --git a/usermods/quinled-an-penta/quinled-an-penta.h b/usermods/quinled-an-penta/quinled-an-penta.h index 5153ee58a..10b784334 100644 --- a/usermods/quinled-an-penta/quinled-an-penta.h +++ b/usermods/quinled-an-penta/quinled-an-penta.h @@ -84,11 +84,11 @@ class QuinLEDAnPentaUsermod : public Usermod void getCurrentUsedLedPins() { for (int8_t lp = 0; lp <= 4; lp++) currentLedPins[lp] = 0; - byte numBusses = busses.getNumBusses(); + byte numBusses = BusManager::getNumBusses(); byte numUsedPins = 0; for (int8_t b = 0; b < numBusses; b++) { - Bus* curBus = busses.getBus(b); + Bus* curBus = BusManager::getBus(b); if (curBus != nullptr) { uint8_t pins[5] = {0, 0, 0, 0, 0}; currentBussesNumPins[b] = curBus->getPins(pins); @@ -104,11 +104,11 @@ class QuinLEDAnPentaUsermod : public Usermod void getCurrentLedcValues() { - byte numBusses = busses.getNumBusses(); + byte numBusses = BusManager::getNumBusses(); byte numLedc = 0; for (int8_t b = 0; b < numBusses; b++) { - Bus* curBus = busses.getBus(b); + Bus* curBus = BusManager::getBus(b); if (curBus != nullptr) { uint32_t curPixColor = curBus->getPixelColor(0); uint8_t _data[5] = {255, 255, 255, 255, 255}; diff --git a/wled00/FX.h b/wled00/FX.h index 3e1c7b238..b6eb58723 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -682,10 +682,7 @@ class WS2812FX { // 96 bytes WS2812FX() : paletteFade(0), paletteBlend(0), - milliampsPerLed(55), cctBlending(0), - ablMilliampsMax(ABL_MILLIAMPS_DEFAULT), - currentMilliamps(0), now(millis()), timebase(0), isMatrix(false), @@ -793,7 +790,6 @@ class WS2812FX { // 96 bytes uint8_t paletteBlend, - milliampsPerLed, cctBlending, getActiveSegmentsNum(void), getFirstSelectedSegId(void), @@ -811,8 +807,6 @@ class WS2812FX { // 96 bytes inline uint8_t getModeCount() { return _modeCount; } uint16_t - ablMilliampsMax, - currentMilliamps, getLengthPhysical(void), getLengthTotal(void), // will include virtual/nonexistent pixels in matrix getFps(); diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 796392fac..e9ab61054 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -900,8 +900,8 @@ void Segment::refreshLightCapabilities() { segStopIdx = stop; } - for (unsigned b = 0; b < busses.getNumBusses(); b++) { - Bus *bus = busses.getBus(b); + for (unsigned b = 0; b < BusManager::getNumBusses(); b++) { + Bus *bus = BusManager::getBus(b); if (bus == nullptr || bus->getLength()==0) break; if (!bus->isOk()) continue; if (bus->getStart() >= segStopIdx) continue; @@ -1086,7 +1086,7 @@ void WS2812FX::finalizeInit(void) { _hasWhiteChannel = _isOffRefreshRequired = false; //if busses failed to load, add default (fresh install, FS issue, ...) - if (busses.getNumBusses() == 0) { + if (BusManager::getNumBusses() == 0) { DEBUG_PRINTLN(F("No busses, init default")); const uint8_t defDataPins[] = {DATA_PINS}; const uint16_t defCounts[] = {PIXEL_COUNTS}; @@ -1099,13 +1099,13 @@ void WS2812FX::finalizeInit(void) { uint16_t count = defCounts[(i < defNumCounts) ? i : defNumCounts -1]; prevLen += count; BusConfig defCfg = BusConfig(DEFAULT_LED_TYPE, defPin, start, count, DEFAULT_LED_COLOR_ORDER, false, 0, RGBW_MODE_MANUAL_ONLY); - if (busses.add(defCfg) == -1) break; + if (BusManager::add(defCfg) == -1) break; } } _length = 0; - for (int i=0; igetStart() + bus->getLength() > MAX_LEDS) break; //RGBW mode is enabled if at least one of the strips is RGBW @@ -1162,7 +1162,7 @@ void WS2812FX::service() { _colors_t[1] = gamma32(seg.currentColor(1)); _colors_t[2] = gamma32(seg.currentColor(2)); seg.currentPalette(_currentPalette, seg.palette); // we need to pass reference - if (!cctFromRgb || correctWB) busses.setSegmentCCT(seg.currentBri(true), correctWB); + if (!cctFromRgb || correctWB) BusManager::setSegmentCCT(seg.currentBri(true), correctWB); // Effect blending // When two effects are being blended, each may have different segment data, this // data needs to be saved first and then restored before running previous mode. @@ -1193,7 +1193,7 @@ void WS2812FX::service() { _segment_index++; } _virtualSegmentLength = 0; - busses.setSegmentCCT(-1); + BusManager::setSegmentCCT(-1); _isServicing = false; _triggered = false; @@ -1212,13 +1212,13 @@ void WS2812FX::service() { void IRAM_ATTR WS2812FX::setPixelColor(unsigned i, uint32_t col) { if (i < customMappingSize) i = customMappingTable[i]; if (i >= _length) return; - busses.setPixelColor(i, col); + BusManager::setPixelColor(i, col); } uint32_t IRAM_ATTR WS2812FX::getPixelColor(uint16_t i) { if (i < customMappingSize) i = customMappingTable[i]; if (i >= _length) return 0; - return busses.getPixelColor(i); + return BusManager::getPixelColor(i); } void WS2812FX::show(void) { @@ -1229,7 +1229,7 @@ void WS2812FX::show(void) { // some buses send asynchronously and this method will return before // all of the data has been sent. // See https://github.com/Makuna/NeoPixelBus/wiki/ESP32-NeoMethods#neoesp32rmt-methods - busses.show(); + BusManager::show(); unsigned long showNow = millis(); size_t diff = showNow - _lastShow; @@ -1244,7 +1244,7 @@ void WS2812FX::show(void) { * On some hardware (ESP32), strip updates are done asynchronously. */ bool WS2812FX::isUpdating() { - return !busses.canAllShow(); + return !BusManager::canAllShow(); } /** @@ -1303,7 +1303,7 @@ void WS2812FX::setBrightness(uint8_t b, bool direct) { } // setting brightness with NeoPixelBusLg has no effect on already painted pixels, // so we need to force an update to existing buffer - busses.setBrightness(b); + BusManager::setBrightness(b); if (!direct) { unsigned long t = millis(); if (_segments[0].next_time > t + 22 && t - _lastShow > MIN_SHOW_DELAY) trigger(); //apply brightness change immediately if no refresh soon @@ -1359,8 +1359,8 @@ uint16_t WS2812FX::getLengthTotal(void) { uint16_t WS2812FX::getLengthPhysical(void) { uint16_t len = 0; - for (size_t b = 0; b < busses.getNumBusses(); b++) { - Bus *bus = busses.getBus(b); + for (size_t b = 0; b < BusManager::getNumBusses(); b++) { + Bus *bus = BusManager::getBus(b); if (bus->getType() >= TYPE_NET_DDP_RGB) continue; //exclude non-physical network busses len += bus->getLength(); } @@ -1371,8 +1371,8 @@ uint16_t WS2812FX::getLengthPhysical(void) { //returns if there is an RGBW bus (supports RGB and White, not only white) //not influenced by auto-white mode, also true if white slider does not affect output white channel bool WS2812FX::hasRGBWBus(void) { - for (size_t b = 0; b < busses.getNumBusses(); b++) { - Bus *bus = busses.getBus(b); + for (size_t b = 0; b < BusManager::getNumBusses(); b++) { + Bus *bus = BusManager::getBus(b); if (bus == nullptr || bus->getLength()==0) break; if (bus->hasRGB() && bus->hasWhite()) return true; } @@ -1381,8 +1381,8 @@ bool WS2812FX::hasRGBWBus(void) { bool WS2812FX::hasCCTBus(void) { if (cctFromRgb && !correctWB) return false; - for (size_t b = 0; b < busses.getNumBusses(); b++) { - Bus *bus = busses.getBus(b); + for (size_t b = 0; b < BusManager::getNumBusses(); b++) { + Bus *bus = BusManager::getBus(b); if (bus == nullptr || bus->getLength()==0) break; switch (bus->getType()) { case TYPE_ANALOG_5CH: @@ -1468,8 +1468,8 @@ void WS2812FX::makeAutoSegments(bool forceReset) { } #endif - for (size_t i = s; i < busses.getNumBusses(); i++) { - Bus* b = busses.getBus(i); + for (size_t i = s; i < BusManager::getNumBusses(); i++) { + Bus* b = BusManager::getBus(i); segStarts[s] = b->getStart(); segStops[s] = segStarts[s] + b->getLength(); @@ -1558,8 +1558,8 @@ void WS2812FX::fixInvalidSegments() { bool WS2812FX::checkSegmentAlignment() { bool aligned = false; for (segment &seg : _segments) { - for (unsigned b = 0; bgetStart() && seg.stop == bus->getStart() + bus->getLength()) aligned = true; } if (seg.start == 0 && seg.stop == _length) aligned = true; diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index 013e906f4..0dcacc042 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -125,7 +125,7 @@ BusDigital::BusDigital(BusConfig &bc, uint8_t nr, const ColorOrderMap &com) if (bc.type == TYPE_WS2812_1CH_X3) lenToCreate = NUM_ICS_WS2812_1CH_3X(bc.count); // only needs a third of "RGB" LEDs for NeoPixelBus _busPtr = PolyBus::create(_iType, _pins, lenToCreate + _skip, nr, _frequencykHz); _valid = (_busPtr != nullptr); - DEBUG_PRINTF("%successfully inited strip %u (len %u) with type %u and pins %u,%u (itype %u)\n", _valid?"S":"Uns", nr, bc.count, bc.type, _pins[0], _pins[1], _iType); + DEBUG_PRINTF("%successfully inited strip %u (len %u) with type %u and pins %u,%u (itype %u). mA=%d/%d\n", _valid?"S":"Uns", nr, bc.count, bc.type, _pins[0], _pins[1], _iType, _milliAmpsPerLed, _milliAmpsMax); } //fine tune power estimation constants for your setup @@ -150,7 +150,7 @@ uint8_t BusDigital::estimateCurrentAndLimitBri() { bool useWackyWS2815PowerModel = false; byte actualMilliampsPerLed = _milliAmpsPerLed; - if (_milliAmpsMax < MA_FOR_ESP || actualMilliampsPerLed == 0) { //0 mA per LED and too low numbers turn off calculation + if (_milliAmpsMax < MA_FOR_ESP/BusManager::getNumBusses() || actualMilliampsPerLed == 0) { //0 mA per LED and too low numbers turn off calculation return _bri; } @@ -159,7 +159,12 @@ uint8_t BusDigital::estimateCurrentAndLimitBri() { actualMilliampsPerLed = 12; // from testing an actual strip } - size_t powerBudget = (_milliAmpsMax - MA_FOR_ESP); //100mA for ESP power + size_t powerBudget = (_milliAmpsMax - MA_FOR_ESP/BusManager::getNumBusses()); //80/120mA for ESP power + if (powerBudget > getLength()) { //each LED uses about 1mA in standby, exclude that from power budget + powerBudget -= getLength(); + } else { + powerBudget = 0; + } uint32_t busPowerSum = 0; for (unsigned i = 0; i < getLength(); i++) { //sum up the usage of each LED @@ -178,29 +183,26 @@ uint8_t BusDigital::estimateCurrentAndLimitBri() { busPowerSum >>= 2; //same as /= 4 } - if (powerBudget > getLength()) { //each LED uses about 1mA in standby, exclude that from power budget - powerBudget -= getLength(); - } else { - powerBudget = 0; - } - // powerSum has all the values of channels summed (max would be getLength()*765 as white is excluded) so convert to milliAmps busPowerSum = (busPowerSum * actualMilliampsPerLed) / 765; + _milliAmpsTotal = busPowerSum * _bri / 255; uint8_t newBri = _bri; if (busPowerSum * _bri / 255 > powerBudget) { //scale brightness down to stay in current limit float scale = (float)(powerBudget * 255) / (float)(busPowerSum * _bri); - uint16_t scaleI = scale * 255; - uint8_t scaleB = (scaleI > 255) ? 255 : scaleI; + if (scale >= 1.0f) return _bri; + _milliAmpsTotal = ceilf((float)_milliAmpsTotal * scale); + uint8_t scaleB = min((int)(scale * 255), 255); newBri = unsigned(_bri * scaleB) / 256 + 1; } return newBri; } void BusDigital::show() { + _milliAmpsTotal = 0; if (!_valid) return; - uint8_t newBri = estimateCurrentAndLimitBri(); + uint8_t newBri = estimateCurrentAndLimitBri(); // will fill _milliAmpsTotal if (newBri < _bri) PolyBus::setBrightness(_busPtr, _iType, newBri); // limit brightness to stay within current limits if (_data) { // use _buffering this causes ~20% FPS drop @@ -646,9 +648,12 @@ void BusManager::removeAll() { } void BusManager::show() { + _milliAmpsUsed = 0; for (unsigned i = 0; i < numBusses; i++) { busses[i]->show(); + _milliAmpsUsed += busses[i]->getUsedCurrent(); } + if (_milliAmpsUsed) _milliAmpsUsed += MA_FOR_ESP; } void BusManager::setStatusPixel(uint32_t c) { @@ -714,3 +719,11 @@ uint16_t BusManager::getTotalLength() { int16_t Bus::_cct = -1; uint8_t Bus::_cctBlend = 0; uint8_t Bus::_gAWM = 255; + +uint16_t BusDigital::_milliAmpsTotal = 0; + +uint8_t BusManager::numBusses = 0; +Bus* BusManager::busses[WLED_MAX_BUSSES+WLED_MIN_VIRTUAL_BUSSES]; +ColorOrderMap BusManager::colorOrderMap = {}; +uint16_t BusManager::_milliAmpsUsed = 0; +uint16_t BusManager::_milliAmpsMax = ABL_MILLIAMPS_DEFAULT; \ No newline at end of file diff --git a/wled00/bus_manager.h b/wled00/bus_manager.h index 9d322749c..12fb81b39 100644 --- a/wled00/bus_manager.h +++ b/wled00/bus_manager.h @@ -137,6 +137,7 @@ class Bus { virtual uint8_t skippedLeds() { return 0; } virtual uint16_t getFrequency() { return 0U; } virtual uint16_t getLEDCurrent() { return 0; } + virtual uint16_t getUsedCurrent() { return 0; } virtual uint16_t getMaxCurrent() { return 0; } inline void setReversed(bool reversed) { _reversed = reversed; } inline uint16_t getStart() { return _start; } @@ -219,6 +220,7 @@ class BusDigital : public Bus { uint16_t getFrequency() { return _frequencykHz; } uint8_t estimateCurrentAndLimitBri(); uint16_t getLEDCurrent() { return _milliAmpsPerLed; } + uint16_t getUsedCurrent() { return _milliAmpsTotal; } uint16_t getMaxCurrent() { return _milliAmpsMax; } void reinit(); void cleanup(); @@ -233,7 +235,8 @@ class BusDigital : public Bus { uint16_t _milliAmpsMax; void * _busPtr; const ColorOrderMap &_colorOrderMap; - //bool _buffering; // temporary until we figure out why comparison "_data" causes severe FPS drop + + static uint16_t _milliAmpsTotal; // is overwitten/recalculated on each show() inline uint32_t restoreColorLossy(uint32_t c, uint8_t restoreBri) { if (restoreBri < 255) { @@ -314,39 +317,44 @@ class BusNetwork : public Bus { class BusManager { public: - BusManager() : numBusses(0) {}; + BusManager() {}; //utility to get the approx. memory usage of a given BusConfig static uint32_t memUsage(BusConfig &bc); + static uint16_t currentMilliamps(void) { return _milliAmpsUsed; } + static uint16_t ablMilliampsMax(void) { return _milliAmpsMax; } - int add(BusConfig &bc); + static int add(BusConfig &bc); //do not call this method from system context (network callback) - void removeAll(); + static void removeAll(); - void show(); - bool canAllShow(); - void setStatusPixel(uint32_t c); - void setPixelColor(uint16_t pix, uint32_t c); - void setBrightness(uint8_t b); - void setSegmentCCT(int16_t cct, bool allowWBCorrection = false); - uint32_t getPixelColor(uint16_t pix); + static void show(); + static bool canAllShow(); + static void setStatusPixel(uint32_t c); + static void setPixelColor(uint16_t pix, uint32_t c); + static void setBrightness(uint8_t b); + static void setSegmentCCT(int16_t cct, bool allowWBCorrection = false); + static void setMilliampsMax(uint16_t max) { _milliAmpsMax = max;} + static uint32_t getPixelColor(uint16_t pix); - Bus* getBus(uint8_t busNr); + static Bus* getBus(uint8_t busNr); //semi-duplicate of strip.getLengthTotal() (though that just returns strip._length, calculated in finalizeInit()) - uint16_t getTotalLength(); - inline uint8_t getNumBusses() const { return numBusses; } + static uint16_t getTotalLength(); + static uint8_t getNumBusses() { return numBusses; } - inline void updateColorOrderMap(const ColorOrderMap &com) { memcpy(&colorOrderMap, &com, sizeof(ColorOrderMap)); } - inline const ColorOrderMap& getColorOrderMap() const { return colorOrderMap; } + static void updateColorOrderMap(const ColorOrderMap &com) { memcpy(&colorOrderMap, &com, sizeof(ColorOrderMap)); } + static const ColorOrderMap& getColorOrderMap() { return colorOrderMap; } private: - uint8_t numBusses; - Bus* busses[WLED_MAX_BUSSES+WLED_MIN_VIRTUAL_BUSSES]; - ColorOrderMap colorOrderMap; + static uint8_t numBusses; + static Bus* busses[WLED_MAX_BUSSES+WLED_MIN_VIRTUAL_BUSSES]; + static ColorOrderMap colorOrderMap; + static uint16_t _milliAmpsUsed; + static uint16_t _milliAmpsMax; - inline uint8_t getNumVirtualBusses() { + static uint8_t getNumVirtualBusses() { int j = 0; for (int i=0; igetType() >= TYPE_NET_DDP_RGB && busses[i]->getType() < 96) j++; return j; diff --git a/wled00/button.cpp b/wled00/button.cpp index f1487396a..b1e7f2a79 100644 --- a/wled00/button.cpp +++ b/wled00/button.cpp @@ -347,10 +347,10 @@ void handleButton() void esp32RMTInvertIdle() { bool idle_out; - for (uint8_t u = 0; u < busses.getNumBusses(); u++) + for (uint8_t u = 0; u < BusManager::getNumBusses(); u++) { if (u > 7) return; // only 8 RMT channels, TODO: ESP32 variants have less RMT channels - Bus *bus = busses.getBus(u); + Bus *bus = BusManager::getBus(u); if (!bus || bus->getLength()==0 || !IS_DIGITAL(bus->getType()) || IS_2PIN(bus->getType())) continue; //assumes that bus number to rmt channel mapping stays 1:1 rmt_channel_t ch = static_cast(u); diff --git a/wled00/cfg.cpp b/wled00/cfg.cpp index 489793b24..4e2afb65f 100644 --- a/wled00/cfg.cpp +++ b/wled00/cfg.cpp @@ -87,8 +87,8 @@ bool deserializeConfig(JsonObject doc, bool fromFS) { JsonObject hw_led = hw["led"]; uint16_t total = hw_led[F("total")] | strip.getLengthTotal(); - CJSON(strip.ablMilliampsMax, hw_led[F("maxpwr")]); - CJSON(strip.milliampsPerLed, hw_led[F("ledma")]); // no longer used + uint16_t ablMilliampsMax = hw_led[F("maxpwr")] | BusManager::ablMilliampsMax(); + BusManager::setMilliampsMax(ablMilliampsMax); Bus::setGlobalAWMode(hw_led[F("rgbwm")] | AW_GLOBAL_DISABLED); CJSON(correctWB, hw_led["cct"]); CJSON(cctFromRgb, hw_led[F("cr")]); @@ -138,7 +138,7 @@ bool deserializeConfig(JsonObject doc, bool fromFS) { if (fromFS || !ins.isNull()) { uint8_t s = 0; // bus iterator - if (fromFS) busses.removeAll(); // can't safely manipulate busses directly in network callback + if (fromFS) BusManager::removeAll(); // can't safely manipulate busses directly in network callback uint32_t mem = 0, globalBufMem = 0; uint16_t maxlen = 0; bool busesChanged = false; @@ -164,8 +164,8 @@ bool deserializeConfig(JsonObject doc, bool fromFS) { bool refresh = elm["ref"] | false; uint16_t freqkHz = elm[F("freq")] | 0; // will be in kHz for DotStar and Hz for PWM (not yet implemented fully) uint8_t AWmode = elm[F("rgbwm")] | RGBW_MODE_MANUAL_ONLY; - uint8_t maPerLed = elm[F("ledma")] | strip.milliampsPerLed; // replace with 55 when removing strip.milliampsPerLed - uint16_t maMax = elm[F("maxpwr")] | (strip.ablMilliampsMax * length) / total; // rough (incorrect?) per strip ABL calculation when no config exists + uint8_t maPerLed = elm[F("ledma")] | 55; + uint16_t maMax = elm[F("maxpwr")] | (ablMilliampsMax * length) / total; // rough (incorrect?) per strip ABL calculation when no config exists // To disable brightness limiter we either set output max current to 0 or single LED current to 0 (we choose output max current) if ((ledType > TYPE_TM1814 && ledType < TYPE_WS2801) || ledType >= TYPE_NET_DDP_RGB) { // analog and virtual maPerLed = 0; @@ -179,7 +179,7 @@ bool deserializeConfig(JsonObject doc, bool fromFS) { maxlen = start + length; globalBufMem = maxlen * 4; } - if (mem + globalBufMem <= MAX_LED_MEMORY) if (busses.add(bc) == -1) break; // finalization will be done in WLED::beginStrip() + if (mem + globalBufMem <= MAX_LED_MEMORY) if (BusManager::add(bc) == -1) break; // finalization will be done in WLED::beginStrip() } else { if (busConfigs[s] != nullptr) delete busConfigs[s]; busConfigs[s] = new BusConfig(ledType, pins, start, length, colorOrder, reversed, skipFirst, AWmode, freqkHz, useGlobalLedBuffer, maPerLed, maMax); @@ -190,7 +190,7 @@ bool deserializeConfig(JsonObject doc, bool fromFS) { doInitBusses = busesChanged; // finalization done in beginStrip() } - if (hw_led["rev"]) busses.getBus(0)->setReversed(true); //set 0.11 global reversed setting for first bus + if (hw_led["rev"]) BusManager::getBus(0)->setReversed(true); //set 0.11 global reversed setting for first bus // read color order map configuration JsonArray hw_com = hw[F("com")]; @@ -205,7 +205,7 @@ bool deserializeConfig(JsonObject doc, bool fromFS) { com.add(start, len, colorOrder); s++; } - busses.updateColorOrderMap(com); + BusManager::updateColorOrderMap(com); } // read multiple button configuration @@ -722,8 +722,8 @@ void serializeConfig() { JsonObject hw_led = hw.createNestedObject("led"); hw_led[F("total")] = strip.getLengthTotal(); //provided for compatibility on downgrade and per-output ABL - hw_led[F("maxpwr")] = strip.ablMilliampsMax; - hw_led[F("ledma")] = strip.milliampsPerLed; // no longer used + hw_led[F("maxpwr")] = BusManager::ablMilliampsMax(); + hw_led[F("ledma")] = 0; // no longer used hw_led["cct"] = correctWB; hw_led[F("cr")] = cctFromRgb; hw_led[F("cb")] = strip.cctBlending; @@ -753,8 +753,8 @@ void serializeConfig() { JsonArray hw_led_ins = hw_led.createNestedArray("ins"); - for (uint8_t s = 0; s < busses.getNumBusses(); s++) { - Bus *bus = busses.getBus(s); + for (uint8_t s = 0; s < BusManager::getNumBusses(); s++) { + Bus *bus = BusManager::getBus(s); if (!bus || bus->getLength()==0) break; JsonObject ins = hw_led_ins.createNestedObject(); ins["start"] = bus->getStart(); @@ -775,7 +775,7 @@ void serializeConfig() { } JsonArray hw_com = hw.createNestedArray(F("com")); - const ColorOrderMap& com = busses.getColorOrderMap(); + const ColorOrderMap& com = BusManager::getColorOrderMap(); for (uint8_t s = 0; s < com.count(); s++) { const ColorOrderMapEntry *entry = com.get(s); if (!entry) break; diff --git a/wled00/data/settings_leds.htm b/wled00/data/settings_leds.htm index 835b396fa..229c38327 100644 --- a/wled00/data/settings_leds.htm +++ b/wled00/data/settings_leds.htm @@ -107,30 +107,36 @@ e.preventDefault(); if (!pinsOK()) {e.stopPropagation();return false;} // Prevent form submission and contact with server if (bquot > 100) {var msg = "Too many LEDs for me to handle!"; if (maxM < 10000) msg += "\n\rConsider using an ESP32."; alert(msg);} + if (d.Sf.PPL.checked) d.Sf.MA.value = 0; // submit 0 as ABL (PPL will handle it) if (d.Sf.checkValidity()) d.Sf.submit(); //https://stackoverflow.com/q/37323914 } function enABL() { - var en = d.Sf["ABL"].checked; - d.Sf["MA"].min = en ? 250 : 0; + var en = d.Sf.ABL.checked; gId('abl').style.display = (en) ? 'inline':'none'; gId('psu2').style.display = (en) ? 'inline':'none'; - if (!en) d.Sf["PPL"].checked = false; + if (!en) d.Sf.PPL.checked = false; enPPL(); UI(); } function enPPL() { - const abl = d.Sf["ABL"].checked; - const en = d.Sf["PPL"].checked; - d.Sf["MA"].readonly = en; - gId("ppldis").style.display = en ? 'inline' : 'none'; + const abl = d.Sf.ABL.checked; + const ppl = d.Sf.PPL.checked; + let sumMA = 0; + d.Sf.MA.readonly = ppl; + d.Sf.MA.min = abl && !ppl ? 250 : 0; + gId("psuMA").style.display = ppl ? 'none' : 'inline'; + gId("ppldis").style.display = ppl ? 'inline' : 'none'; + // set PPL minimum value and clear actual PPL limit if ABL disabled d.Sf.querySelectorAll("#mLC input[name^=MA]").forEach((i,n)=>{ - gId("PSU"+n).style.display = en ? "inline" : "none"; + gId("PSU"+n).style.display = ppl ? "inline" : "none"; const t = parseInt(d.Sf["LT"+n].value); // LED type SELECT - i.min = en && !((t >= 80 && t < 96) || (t >= 40 && t < 48)) ? 250 : 0; + i.min = ppl && !((t >= 80 && t < 96) || (t >= 40 && t < 48)) ? 250 : 0; if (!abl) i.value = 0; + else if (ppl) sumMA += parseInt(i.value,10); }); + if (ppl) d.Sf.MA.value = sumMA; // populate UI ABL value if PPL used } function enLA(s,n) { @@ -141,10 +147,11 @@ } function setABL() { - d.Sf["ABL"].checked = false; + console.log(d.Sf.MA.value); + d.Sf.ABL.checked = parseInt(d.Sf.MA.value) > 0; // check if ABL is enabled (max mA entered per output) d.Sf.querySelectorAll("#mLC input[name^=MA]").forEach((i,n)=>{ - if (parseInt(i.value) > 0) d.Sf["ABL"].checked = true; + if (parseInt(i.value) > 0) d.Sf.ABL.checked = true; }); // select appropriate LED current d.Sf.querySelectorAll("#mLC select[name^=LAsel]").forEach((sel,n)=>{ @@ -190,8 +197,8 @@ let isRGBW = false, gRGBW = false, memu = 0; let sumMA = 0, busMA = 0; let sLC = 0, sPC = 0, sDI = 0, maxLC = 0; - const ablEN = d.Sf["ABL"].checked; - const pplEN = d.Sf["PPL"].checked; + const ablEN = d.Sf.ABL.checked; + const pplEN = d.Sf.PPL.checked; // enable/disable LED fields d.Sf.querySelectorAll("#mLC select[name^=LT]").forEach((s)=>{ @@ -244,7 +251,7 @@ gId("rev"+n).innerHTML = (t >= 40 && t < 48) ? "Inverted output":"Reversed (rotated 180°)"; // change reverse text for analog gId("psd"+n).innerHTML = (t >= 40 && t < 48) ? "Index:":"Start:"; // change analog start description if (ablEN && pplEN && !((t >= 80 && t < 96) || (t >= 40 && t < 48))) { - sumMA += parseInt(d.Sf["MA"+n].value); + sumMA += parseInt(d.Sf["MA"+n].value); // summarize PPL ABL limit (fields) } }); // display global white channel overrides @@ -269,11 +276,12 @@ if (s+c > sLC) sLC = s+c; //update total count if (c > maxLC) maxLC = c; //max per output if (t < 80) sPC += c; //virtual out busses do not count towards physical LEDs - if (!((t >= 80 && t < 96) || (t >= 40 && t < 48))) sDI += c; + //if (!((t >= 80 && t < 96) || (t >= 40 && t < 48))) sDI += c; if (!((t >= 80 && t < 96) || (t >= 40 && t < 48))) { + sDI += c; // summarize digital LED count let maPL = parseInt(d.Sf["LA"+n].value); if (maPL == 255) maPL = 12; - busMA += maPL*c; + busMA += maPL*c; // summarize maximum bus current (calculated) } } // increase led count return; @@ -316,14 +324,18 @@ else LC.style.color = d.ro_gpio.some((e)=>e==parseInt(LC.value)) ? "orange" : "#fff"; } }); + // distribute ABL current if not using PPL, otherwise sumMA contains summarized ABL limit d.Sf.querySelectorAll("#mLC input[name^=LC]").forEach((s,n)=>{ let c = parseInt(s.value,10); //get LED count - let t = parseInt(d.Sf["LT"+n].value); - if (ablEN) { - let v = Math.round(parseInt(d.Sf["MA"].value,10)*c/sDI); - if (!pplEN && !((t >= 80 && t < 96) || (t >= 40 && t < 48))) d.Sf["MA"+n].value = v; - } else d.Sf["MA"+n].value = 0; + let t = parseInt(d.Sf["LT"+n].value); //get LED type + if (!ablEN || (t >= 80 && t < 96) || (t >= 40 && t < 48)) { + // virtual and analog LEDs have no limiter + d.Sf["MA"+n].value = 0; + return; + } + if (!pplEN) d.Sf["MA"+n].value = Math.round(parseInt(d.Sf.MA.value,10)*c/sDI); }); + if (pplEN) d.Sf.MA.value = sumMA; // update global ABL if using PPL // update total led count gId("lc").textContent = sLC; gId("pc").textContent = (sLC == sPC) ? "":"(" + sPC + " physical)"; @@ -336,7 +348,6 @@ gId('ledwarning').style.color = (maxLC > Math.max(maxPB,800) || bquot > 100) ? 'red':'orange'; gId('wreason').innerHTML = (bquot > 80) ? "80% of max. LED memory" +(bquot>100 ? ` (ERROR: Using over ${maxM}B!)` : "") : "800 LEDs per output"; // calculate power - if (pplEN) d.Sf.MA.value = sumMA; gId('ampwarning').style.display = (parseInt(d.Sf.MA.value,10) > 7200) ? 'inline':'none'; var val = Math.ceil((100 + busMA)/500)/2; val = (val > 5) ? Math.ceil(val) : val; @@ -409,7 +420,7 @@ mA/LED:
-
PSU: mA
+
PSU: mA
Color Order: gId("+").style.display = (i0) ? "inline":"none"; - if (!init) UI(); + if (!init) { + enPPL(); + UI(); + } } function addCOM(start=0,len=1,co=0) { @@ -750,7 +764,7 @@ Length: mA
+
Maximum PSU Current: mA
Use per-output limiter: