From f8da8f6929e915e130bbbdeee4c48051d82b4f73 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Sun, 7 Nov 2021 11:58:16 +0100 Subject: [PATCH] Added conditional compile for dynamic JSON buffer. - WLED_USE_DYNAMIC_JSON Minor fixes. --- usermods/ST7789_display/ST7789_display.h | 15 ++++++++--- .../usermod_v2_four_line_display.h | 3 ++- wled00/FX_fcn.cpp | 5 +++- wled00/cfg.cpp | 20 ++++++++++++--- wled00/data/index.js | 10 ++++---- wled00/data/simple.js | 10 ++++---- wled00/ir.cpp | 5 +++- wled00/json.cpp | 9 ++++--- wled00/mqtt.cpp | 7 +++++- wled00/presets.cpp | 12 +++++++-- wled00/set.cpp | 5 +++- wled00/wled.h | 2 ++ wled00/wled_eeprom.cpp | 5 +++- wled00/wled_serial.cpp | 25 ++++++++++--------- wled00/wled_server.cpp | 5 +++- wled00/ws.cpp | 11 ++++++-- wled00/xml.cpp | 6 ++++- 17 files changed, 111 insertions(+), 44 deletions(-) diff --git a/usermods/ST7789_display/ST7789_display.h b/usermods/ST7789_display/ST7789_display.h index 49e05fe0..b7431978 100644 --- a/usermods/ST7789_display/ST7789_display.h +++ b/usermods/ST7789_display/ST7789_display.h @@ -55,6 +55,7 @@ class St7789DisplayUsermod : public Usermod { private: //Private class members. You can declare variables and functions only accessible to your usermod here unsigned long lastTime = 0; + bool enabled = true; bool displayTurnedOff = false; long lastRedraw = 0; @@ -140,7 +141,7 @@ class St7789DisplayUsermod : public Usermod { void setup() { PinManagerPinType pins[] = { { TFT_MOSI, true }, { TFT_MISO, false}, { TFT_SCLK, true }, { TFT_CS, true}, { TFT_DC, true}, { TFT_RST, true }, { TFT_BL, true } }; - if (!pinManager.allocateMultiplePins(pins, 7, PinOwner::UM_FourLineDisplay)) { return; } + if (!pinManager.allocateMultiplePins(pins, 7, PinOwner::UM_FourLineDisplay)) { enabled = false; return; } tft.init(); tft.setRotation(0); //Rotation here is set up for the text to be readable with the port on the left. Use 1 to flip. @@ -321,7 +322,7 @@ class St7789DisplayUsermod : public Usermod { if (user.isNull()) user = root.createNestedObject("u"); JsonArray lightArr = user.createNestedArray("ST7789"); //name - lightArr.add(F("installed")); //unit + lightArr.add(enabled?F("installed"):F("disabled")); //unit } @@ -362,7 +363,15 @@ class St7789DisplayUsermod : public Usermod { */ void addToConfig(JsonObject& root) { - //JsonObject top = root.createNestedObject("exampleUsermod"); + JsonObject top = root.createNestedObject("ST7789"); + JsonArray pins = top.createNestedArray("pin"); + pins.add(TFT_MOSI); + pins.add(TFT_MISO); + pins.add(TFT_SCLK); + pins.add(TFT_CS); + pins.add(TFT_DC); + pins.add(TFT_RST); + pins.add(TFT_BL); //top["great"] = userVar0; //save this var persistently whenever settings are saved } diff --git a/usermods/usermod_v2_four_line_display/usermod_v2_four_line_display.h b/usermods/usermod_v2_four_line_display/usermod_v2_four_line_display.h index 329857f4..bb9468cd 100644 --- a/usermods/usermod_v2_four_line_display/usermod_v2_four_line_display.h +++ b/usermods/usermod_v2_four_line_display/usermod_v2_four_line_display.h @@ -657,8 +657,9 @@ class FourLineDisplayUsermod : public Usermod { top[FPSTR(_enabled)] = enabled; JsonArray io_pin = top.createNestedArray("pin"); for (byte i=0; i<5; i++) io_pin.add(ioPin[i]); - top["help4PinTypes"] = F("Clk,Data,CS,DC,RST"); // help for Settings page + top["help4Pins"] = F("Clk,Data,CS,DC,RST"); // help for Settings page top["type"] = type; + top["help4Type"] = F("1=SSD1306,2=SH1106,3=SSD1306_128x64,4=SSD1305,5=SSD1305_128x64,6=SSD1306_SPI,7=SSD1306_SPI_128x64"); // help for Settings page top[FPSTR(_flip)] = (bool) flip; top[FPSTR(_contrast)] = contrast; top[FPSTR(_refreshRate)] = refreshRate/1000; diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 390e7142..175b769b 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1089,10 +1089,13 @@ void WS2812FX::deserializeMap(uint8_t n) { return; } - //DynamicJsonDocument doc(JSON_BUFFER_SIZE); // full sized buffer for larger maps +#ifdef WLED_USE_DYNAMIC_JSON + DynamicJsonDocument doc(JSON_BUFFER_SIZE); +#else while (jsonBufferLock) delay(1); jsonBufferLock = true; doc.clear(); +#endif DEBUG_PRINT(F("Reading LED map from ")); DEBUG_PRINTLN(fileName); diff --git a/wled00/cfg.cpp b/wled00/cfg.cpp index 9f55fa7e..a8927715 100644 --- a/wled00/cfg.cpp +++ b/wled00/cfg.cpp @@ -427,10 +427,13 @@ void deserializeConfigFromFS() { return; } - //DynamicJsonDocument doc(JSON_BUFFER_SIZE); +#ifdef WLED_USE_DYNAMIC_JSON + DynamicJsonDocument doc(JSON_BUFFER_SIZE); +#else while (jsonBufferLock) delay(1); jsonBufferLock = true; doc.clear(); +#endif DEBUG_PRINTLN(F("Reading settings from /cfg.json...")); @@ -454,10 +457,13 @@ void serializeConfig() { DEBUG_PRINTLN(F("Writing settings to /cfg.json...")); - //DynamicJsonDocument doc(JSON_BUFFER_SIZE); +#ifdef WLED_USE_DYNAMIC_JSON + DynamicJsonDocument doc(JSON_BUFFER_SIZE); +#else while (jsonBufferLock) delay(1); jsonBufferLock = true; doc.clear(); +#endif JsonArray rev = doc.createNestedArray("rev"); rev.add(1); //major settings revision @@ -771,10 +777,13 @@ void serializeConfig() { bool deserializeConfigSec() { DEBUG_PRINTLN(F("Reading settings from /wsec.json...")); - //DynamicJsonDocument doc(JSON_BUFFER_SIZE); +#ifdef WLED_USE_DYNAMIC_JSON + DynamicJsonDocument doc(JSON_BUFFER_SIZE); +#else while (jsonBufferLock) delay(1); jsonBufferLock = true; doc.clear(); +#endif bool success = readObjectFromFile("/wsec.json", nullptr, &doc); if (!success) { @@ -819,10 +828,13 @@ bool deserializeConfigSec() { void serializeConfigSec() { DEBUG_PRINTLN(F("Writing settings to /wsec.json...")); - //DynamicJsonDocument doc(JSON_BUFFER_SIZE); +#ifdef WLED_USE_DYNAMIC_JSON + DynamicJsonDocument doc(JSON_BUFFER_SIZE); +#else while (jsonBufferLock) delay(1); jsonBufferLock = true; doc.clear(); +#endif JsonObject nw = doc.createNestedObject("nw"); diff --git a/wled00/data/index.js b/wled00/data/index.js index a7d4d7ab..8b926a09 100644 --- a/wled00/data/index.js +++ b/wled00/data/index.js @@ -67,7 +67,7 @@ function sCol(na, col) {d.documentElement.style.setProperty(na, col);} function gId(c) {return d.getElementById(c);} function gEBCN(c) {return d.getElementsByClassName(c);} function isEmpty(o) {return Object.keys(o).length === 0;} -function isO(i) {return (i && typeof i === 'object' && !Array.isArray(i));} +function isObj(i) {return (i && typeof i === 'object' && !Array.isArray(i));} function applyCfg() { @@ -253,7 +253,7 @@ function onLoad() loadPalettesData(redrawPalPrev); loadFX(()=>{ loadPresets(()=>{ - if (isO(lastinfo) && isEmpty(lastinfo)) loadInfo(requestJson); // if not filled by WS + if (isObj(lastinfo) && isEmpty(lastinfo)) loadInfo(requestJson); // if not filled by WS }); }); }); @@ -533,7 +533,7 @@ function populatePresets(fromls) pNum = 0; for (var key of (arr||[])) { - if (!isO(key[1])) continue; + if (!isObj(key[1])) continue; let i = parseInt(key[0]); var qll = key[1].ql; if (qll) pQL.push([i, qll, pName(i)]); @@ -2297,9 +2297,9 @@ function mergeDeep(target, ...sources) if (!sources.length) return target; const source = sources.shift(); - if (isO(target) && isO(source)) { + if (isObj(target) && isObj(source)) { for (const key in source) { - if (isO(source[key])) { + if (isObj(source[key])) { if (!target[key]) Object.assign(target, { [key]: {} }); mergeDeep(target[key], source[key]); } else { diff --git a/wled00/data/simple.js b/wled00/data/simple.js index caab2e64..73941a74 100644 --- a/wled00/data/simple.js +++ b/wled00/data/simple.js @@ -62,7 +62,7 @@ function sCol(na, col) {d.documentElement.style.setProperty(na, col);} function gId(c) {return d.getElementById(c);} function gEBCN(c) {return d.getElementsByClassName(c);} function isEmpty(o) {return Object.keys(o).length === 0;} -function isO(i) { return (i && typeof i === 'object' && !Array.isArray(i)); } +function isObj(i) { return (i && typeof i === 'object' && !Array.isArray(i)); } function applyCfg() { @@ -238,7 +238,7 @@ async function onLoad() loadPalettesData(redrawPalPrev); loadFX(()=>{ loadPresets(()=>{ - if (isO(lastinfo) && isEmpty(lastinfo)) loadInfo(requestJson); // if not filled by WS + if (isObj(lastinfo) && isEmpty(lastinfo)) loadInfo(requestJson); // if not filled by WS }); }); }); @@ -449,7 +449,7 @@ function populatePresets() pNum = 0; for (var key of (arr||[])) { - if (!isO(key[1])) continue; + if (!isObj(key[1])) continue; let i = parseInt(key[0]); var qll = key[1].ql; if (qll) pQL.push([i, qll, pName(i)]); @@ -1404,9 +1404,9 @@ function mergeDeep(target, ...sources) if (!sources.length) return target; const source = sources.shift(); - if (isO(target) && isO(source)) { + if (isObj(target) && isObj(source)) { for (const key in source) { - if (isO(source[key])) { + if (isObj(source[key])) { if (!target[key]) Object.assign(target, { [key]: {} }); mergeDeep(target[key], source[key]); } else { diff --git a/wled00/ir.cpp b/wled00/ir.cpp index d0ff0c39..f5de7710 100644 --- a/wled00/ir.cpp +++ b/wled00/ir.cpp @@ -575,10 +575,13 @@ void decodeIRJson(uint32_t code) JsonObject fdo; JsonObject jsonCmdObj; - //DynamicJsonDocument doc(JSON_BUFFER_SIZE); +#ifdef WLED_USE_DYNAMIC_JSON + DynamicJsonDocument doc(JSON_BUFFER_SIZE); +#else while (jsonBufferLock) delay(1); jsonBufferLock = true; doc.clear(); +#endif sprintf_P(objKey, PSTR("\"0x%lX\":"), (unsigned long)code); diff --git a/wled00/json.cpp b/wled00/json.cpp index d12888af..0cfac279 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -913,12 +913,15 @@ void serveJson(AsyncWebServerRequest* request) return; } - //AsyncJsonResponse* response = new AsyncJsonResponse(JSON_BUFFER_SIZE); +#ifdef WLED_USE_DYNAMIC_JSON + AsyncJsonResponse* response = new AsyncJsonResponse(JSON_BUFFER_SIZE); +#else while (jsonBufferLock) delay(1); jsonBufferLock = true; - doc.clear(); - + doc.clear(); AsyncJsonResponse *response = new AsyncJsonResponse(&doc); +#endif + JsonObject lDoc = response->getRoot(); switch (subJson) diff --git a/wled00/mqtt.cpp b/wled00/mqtt.cpp index 1b18fdc5..78614f1c 100644 --- a/wled00/mqtt.cpp +++ b/wled00/mqtt.cpp @@ -91,14 +91,19 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties colorUpdated(CALL_MODE_DIRECT_CHANGE); } else if (strcmp_P(topic, PSTR("/api")) == 0) { if (payload[0] == '{') { //JSON API - //DynamicJsonDocument doc(JSON_BUFFER_SIZE); + #ifdef WLED_USE_DYNAMIC_JSON + DynamicJsonDocument doc(JSON_BUFFER_SIZE); + #else while (jsonBufferLock) delay(1); jsonBufferLock = true; doc.clear(); + #endif + deserializeJson(doc, payloadStr); fileDoc = &doc; deserializeState(doc.as()); fileDoc = nullptr; + jsonBufferLock = false; } else { //HTTP API String apireq = "win&"; diff --git a/wled00/presets.cpp b/wled00/presets.cpp index a7f28652..6ddccbf3 100644 --- a/wled00/presets.cpp +++ b/wled00/presets.cpp @@ -20,10 +20,14 @@ bool applyPreset(byte index, byte callMode) deserializeState(fdo, callMode, index); } else { DEBUGFS_PRINTLN(F("Make read buf")); - //DynamicJsonDocument fDoc(JSON_BUFFER_SIZE); + #ifdef WLED_USE_DYNAMIC_JSON + DynamicJsonDocument doc(JSON_BUFFER_SIZE); + #else while (jsonBufferLock) delay(1); jsonBufferLock = true; doc.clear(); + #endif + errorFlag = readObjectFromFileUsingId(filename, index, &doc) ? ERR_NONE : ERR_FS_PLOAD; JsonObject fdo = doc.as(); if (fdo["ps"] == index) fdo.remove("ps"); @@ -50,10 +54,14 @@ void savePreset(byte index, bool persist, const char* pname, JsonObject saveobj) if (!fileDoc) { DEBUGFS_PRINTLN(F("Allocating saving buffer")); - //DynamicJsonDocument lDoc(JSON_BUFFER_SIZE); + #ifdef WLED_USE_DYNAMIC_JSON + DynamicJsonDocument doc(JSON_BUFFER_SIZE); + #else while (jsonBufferLock) delay(1); jsonBufferLock = true; doc.clear(); + #endif + sObj = doc.to(); if (pname) sObj["n"] = pname; diff --git a/wled00/set.cpp b/wled00/set.cpp index 406eee5b..99429e1d 100644 --- a/wled00/set.cpp +++ b/wled00/set.cpp @@ -415,10 +415,13 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) //USERMODS if (subPage == 8) { - //DynamicJsonDocument doc(JSON_BUFFER_SIZE); + #ifdef WLED_USE_DYNAMIC_JSON + DynamicJsonDocument doc(JSON_BUFFER_SIZE); + #else while (jsonBufferLock) delay(1); jsonBufferLock = true; doc.clear(); + #endif JsonObject um = doc.createNestedObject("um"); diff --git a/wled00/wled.h b/wled00/wled.h index 3d0ea67d..c0799529 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -603,7 +603,9 @@ WLED_GLOBAL int8_t loadLedmap _INIT(-1); WLED_GLOBAL UsermodManager usermods _INIT(UsermodManager()); // global ArduinoJson buffer +#ifndef WLED_USE_DYNAMIC_JSON WLED_GLOBAL StaticJsonDocument doc; +#endif WLED_GLOBAL volatile bool jsonBufferLock _INIT(false); // enable additional debug output diff --git a/wled00/wled_eeprom.cpp b/wled00/wled_eeprom.cpp index 4e78f978..f4b4e2ae 100644 --- a/wled00/wled_eeprom.cpp +++ b/wled00/wled_eeprom.cpp @@ -382,10 +382,13 @@ void deEEP() { DEBUG_PRINTLN(F("Preset file not found, attempting to load from EEPROM")); DEBUGFS_PRINTLN(F("Allocating saving buffer for dEEP")); - //DynamicJsonDocument doc(JSON_BUFFER_SIZE *2); +#ifdef WLED_USE_DYNAMIC_JSON + DynamicJsonDocument doc(JSON_BUFFER_SIZE); +#else while (jsonBufferLock) delay(1); jsonBufferLock = true; doc.clear(); +#endif JsonObject sObj = doc.to(); sObj.createNestedObject("0"); diff --git a/wled00/wled_serial.cpp b/wled00/wled_serial.cpp index 4c40c901..3a1adc3a 100644 --- a/wled00/wled_serial.cpp +++ b/wled00/wled_serial.cpp @@ -43,24 +43,25 @@ void handleSerial() } else if (next == '{') { //JSON API bool verboseResponse = false; + #ifdef WLED_USE_DYNAMIC_JSON + DynamicJsonDocument doc(JSON_BUFFER_SIZE); + #else while (jsonBufferLock) delay(1); jsonBufferLock = true; doc.clear(); - { - //DynamicJsonDocument doc(JSON_BUFFER_SIZE); - Serial.setTimeout(100); - DeserializationError error = deserializeJson(doc, Serial); - if (error) { - jsonBufferLock = false; - return; - } - fileDoc = &doc; - verboseResponse = deserializeState(doc.as()); - fileDoc = nullptr; + #endif + Serial.setTimeout(100); + DeserializationError error = deserializeJson(doc, Serial); + if (error) { + jsonBufferLock = false; + return; } + fileDoc = &doc; + verboseResponse = deserializeState(doc.as()); + fileDoc = nullptr; + //only send response if TX pin is unused for other purposes if (verboseResponse && !pinManager.isPinAllocated(1)) { - //DynamicJsonDocument doc(JSON_BUFFER_SIZE); doc.clear(); JsonObject state = doc.createNestedObject("state"); serializeState(state); diff --git a/wled00/wled_server.cpp b/wled00/wled_server.cpp index 6c55087d..b88f13ca 100644 --- a/wled00/wled_server.cpp +++ b/wled00/wled_server.cpp @@ -110,10 +110,13 @@ void initServer() bool verboseResponse = false; bool isConfig = false; { //scope JsonDocument so it releases its buffer - //DynamicJsonDocument jsonBuffer(JSON_BUFFER_SIZE); + #ifdef WLED_USE_DYNAMIC_JSON + DynamicJsonDocument doc(JSON_BUFFER_SIZE); + #else while (jsonBufferLock) delay(1); jsonBufferLock = true; doc.clear(); + #endif DeserializationError error = deserializeJson(doc, (uint8_t*)(request->_tempObject)); JsonObject root = doc.as(); diff --git a/wled00/ws.cpp b/wled00/ws.cpp index 31510d35..022d4e2e 100644 --- a/wled00/ws.cpp +++ b/wled00/ws.cpp @@ -36,10 +36,13 @@ void wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTyp } bool verboseResponse = false; { //scope JsonDocument so it releases its buffer - //DynamicJsonDocument doc(JSON_BUFFER_SIZE); + #ifdef WLED_USE_DYNAMIC_JSON + DynamicJsonDocument doc(JSON_BUFFER_SIZE); + #else while (jsonBufferLock) delay(1); jsonBufferLock = true; doc.clear(); + #endif DeserializationError error = deserializeJson(doc, data, len); JsonObject root = doc.as(); @@ -111,10 +114,14 @@ void sendDataWs(AsyncWebSocketClient * client) AsyncWebSocketMessageBuffer * buffer; { //scope JsonDocument so it releases its buffer - //DynamicJsonDocument doc(JSON_BUFFER_SIZE); + #ifdef WLED_USE_DYNAMIC_JSON + DynamicJsonDocument doc(JSON_BUFFER_SIZE); + #else while (jsonBufferLock) delay(1); jsonBufferLock = true; doc.clear(); + #endif + JsonObject state = doc.createNestedObject("state"); serializeState(state); JsonObject info = doc.createNestedObject("info"); diff --git a/wled00/xml.cpp b/wled00/xml.cpp index 28fbf232..2b3e42b0 100644 --- a/wled00/xml.cpp +++ b/wled00/xml.cpp @@ -253,10 +253,14 @@ void getSettingsJS(byte subPage, char* dest) // add reserved and usermod pins as d.um_p array oappend(SET_F("d.um_p=[6,7,8,9,10,11")); - //DynamicJsonDocument doc(JSON_BUFFER_SIZE/2); + #ifdef WLED_USE_DYNAMIC_JSON + DynamicJsonDocument doc(JSON_BUFFER_SIZE); + #else while (jsonBufferLock) delay(1); jsonBufferLock = true; doc.clear(); + #endif + JsonObject mods = doc.createNestedObject(F("um")); usermods.addToConfig(mods); if (!mods.isNull()) fillUMPins(mods);