Added conditional compile for dynamic JSON buffer.

- WLED_USE_DYNAMIC_JSON
Minor fixes.
This commit is contained in:
Blaz Kristan 2021-11-07 11:58:16 +01:00
parent f7de055f67
commit f8da8f6929
17 changed files with 111 additions and 44 deletions

View File

@ -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
}

View File

@ -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;

View File

@ -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);

View File

@ -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");

View File

@ -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 {

View File

@ -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 {

View File

@ -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);

View File

@ -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)

View File

@ -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<JsonObject>());
fileDoc = nullptr;
jsonBufferLock = false;
} else { //HTTP API
String apireq = "win&";

View File

@ -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<JsonObject>();
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<JsonObject>();
if (pname) sObj["n"] = pname;

View File

@ -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");

View File

@ -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<JSON_BUFFER_SIZE> doc;
#endif
WLED_GLOBAL volatile bool jsonBufferLock _INIT(false);
// enable additional debug output

View File

@ -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<JsonObject>();
sObj.createNestedObject("0");

View File

@ -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<JsonObject>());
fileDoc = nullptr;
#endif
Serial.setTimeout(100);
DeserializationError error = deserializeJson(doc, Serial);
if (error) {
jsonBufferLock = false;
return;
}
fileDoc = &doc;
verboseResponse = deserializeState(doc.as<JsonObject>());
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);

View File

@ -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<JsonObject>();

View File

@ -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<JsonObject>();
@ -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");

View File

@ -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);