Single JSON buffer.

This commit is contained in:
Blaž Kristan 2021-11-03 14:52:22 +01:00
parent f66fcfbe6d
commit bd521f858e
14 changed files with 158 additions and 54 deletions

View File

@ -86,8 +86,6 @@ void WS2812FX::finalizeInit(void)
busses.add(defCfg);
}
}
deserializeMap();
_length = 0;
for (uint8_t i=0; i<busses.getNumBusses(); i++) {
@ -1075,7 +1073,7 @@ uint32_t WS2812FX::color_from_palette(uint16_t i, bool mapping, bool wrap, uint8
}
//load custom mapping table from JSON file
//load custom mapping table from JSON file (called from finalizeInit() or deserializeState())
void WS2812FX::deserializeMap(uint8_t n) {
char fileName[32];
strcpy_P(fileName, PSTR("/ledmap"));
@ -1093,11 +1091,18 @@ void WS2812FX::deserializeMap(uint8_t n) {
return;
}
DynamicJsonDocument doc(JSON_BUFFER_SIZE); // full sized buffer for larger maps
//DynamicJsonDocument doc(JSON_BUFFER_SIZE); // full sized buffer for larger maps
while (jsonBufferLock) delay(1);
jsonBufferLock = true;
doc.clear();
DEBUG_PRINT(F("Reading LED map from "));
DEBUG_PRINTLN(fileName);
if (!readObjectFromFile(fileName, nullptr, &doc)) return; //if file does not exist just exit
if (!readObjectFromFile(fileName, nullptr, &doc)) {
jsonBufferLock = false;
return; //if file does not exist just exit
}
// erase old custom ledmap
if (customMappingTable != nullptr) {
@ -1114,6 +1119,8 @@ void WS2812FX::deserializeMap(uint8_t n) {
customMappingTable[i] = (uint16_t) map[i];
}
}
jsonBufferLock = false;
}
//gamma 2.8 lookup table used for color correction

View File

@ -14,6 +14,7 @@ void getStringFromJson(char* dest, const char* src, size_t len) {
}
bool deserializeConfig(JsonObject doc, bool fromFS) {
bool needsSave = false;
//int rev_major = doc["rev"][0]; // 1
//int rev_minor = doc["rev"][1]; // 0
@ -419,13 +420,12 @@ bool deserializeConfig(JsonObject doc, bool fromFS) {
DEBUG_PRINTLN(F("Starting usermod config."));
JsonObject usermods_settings = doc["um"];
if (!usermods_settings.isNull()) {
bool allComplete = usermods.readFromConfig(usermods_settings);
if (!allComplete && fromFS) serializeConfig();
needsSave = usermods.readFromConfig(usermods_settings);
}
if (fromFS) return false;
if (fromFS) return needsSave;
doReboot = doc[F("rb")] | doReboot;
return (doc["sv"] | true);
return (doc["sv"] | needsSave);
}
void deserializeConfigFromFS() {
@ -435,19 +435,26 @@ void deserializeConfigFromFS() {
return;
}
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
//DynamicJsonDocument doc(JSON_BUFFER_SIZE);
while (jsonBufferLock) delay(1);
jsonBufferLock = true;
doc.clear();
DEBUG_PRINTLN(F("Reading settings from /cfg.json..."));
success = readObjectFromFile("/cfg.json", nullptr, &doc);
if (!success) { //if file does not exist, try reading from EEPROM
deEEPSettings();
jsonBufferLock = false;
return;
}
// NOTE: This routine deserializes *and* applies the configuration
// Therefore, must also initialize ethernet from this function
deserializeConfig(doc.as<JsonObject>(), true);
bool needsSave = deserializeConfig(doc.as<JsonObject>(), true);
jsonBufferLock = false;
if (needsSave) serializeConfig(); // usermods required new prameters
}
void serializeConfig() {
@ -455,7 +462,10 @@ void serializeConfig() {
DEBUG_PRINTLN(F("Writing settings to /cfg.json..."));
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
//DynamicJsonDocument doc(JSON_BUFFER_SIZE);
while (jsonBufferLock) delay(1);
jsonBufferLock = true;
doc.clear();
JsonArray rev = doc.createNestedArray("rev");
rev.add(1); //major settings revision
@ -762,16 +772,23 @@ void serializeConfig() {
File f = WLED_FS.open("/cfg.json", "w");
if (f) serializeJson(doc, f);
f.close();
jsonBufferLock = false;
}
//settings in /wsec.json, not accessible via webserver, for passwords and tokens
bool deserializeConfigSec() {
DEBUG_PRINTLN(F("Reading settings from /wsec.json..."));
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
//DynamicJsonDocument doc(JSON_BUFFER_SIZE);
while (jsonBufferLock) delay(1);
jsonBufferLock = true;
doc.clear();
bool success = readObjectFromFile("/wsec.json", nullptr, &doc);
if (!success) return false;
if (!success) {
jsonBufferLock = false;
return false;
}
JsonObject nw_ins_0 = doc["nw"]["ins"][0];
getStringFromJson(clientPass, nw_ins_0["psk"], 65);
@ -803,13 +820,17 @@ bool deserializeConfigSec() {
CJSON(wifiLock, ota[F("lock-wifi")]);
CJSON(aOtaEnabled, ota[F("aota")]);
jsonBufferLock = false;
return true;
}
void serializeConfigSec() {
DEBUG_PRINTLN(F("Writing settings to /wsec.json..."));
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
//DynamicJsonDocument doc(JSON_BUFFER_SIZE);
while (jsonBufferLock) delay(1);
jsonBufferLock = true;
doc.clear();
JsonObject nw = doc.createNestedObject("nw");
@ -844,4 +865,5 @@ void serializeConfigSec() {
File f = WLED_FS.open("/wsec.json", "w");
if (f) serializeJson(doc, f);
f.close();
jsonBufferLock = false;
}

View File

@ -572,18 +572,22 @@ void decodeIRJson(uint32_t code)
{
char objKey[10];
String cmdStr;
DynamicJsonDocument irDoc(JSON_BUFFER_SIZE);
JsonObject fdo;
JsonObject jsonCmdObj;
//DynamicJsonDocument doc(JSON_BUFFER_SIZE);
while (jsonBufferLock) delay(1);
jsonBufferLock = true;
doc.clear();
sprintf_P(objKey, PSTR("\"0x%lX\":"), (unsigned long)code);
// attempt to read command from ir.json
// 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() irDoc JSON document
// if the IR code is not found readObjectFromFile() will clean() doc JSON document
// so we can differentiate between the two
readObjectFromFile("/ir.json", objKey, &irDoc);
fdo = irDoc.as<JsonObject>();
readObjectFromFile("/ir.json", objKey, &doc);
fdo = doc.as<JsonObject>();
lastValidCode = 0;
if (fdo.isNull()) {
//the received code does not exist
@ -630,10 +634,11 @@ void decodeIRJson(uint32_t code)
} else if (!jsonCmdObj.isNull()) {
// command is JSON object
//allow applyPreset() to reuse JSON buffer, or it would alloc. a second buffer and run out of mem.
fileDoc = &irDoc;
fileDoc = &doc;
deserializeState(jsonCmdObj, CALL_MODE_BUTTON);
fileDoc = nullptr;
}
jsonBufferLock = false;
}
void initIR()

View File

@ -204,13 +204,13 @@ void deserializeSegment(JsonObject elem, byte it, byte presetId)
}
strip.setPixelSegment(255);
strip.trigger();
// this is now handled using the "frz" toggle.
} else if (!elem["frz"] && iarr.isNull()) { //return to regular effect
seg.setOption(SEG_OPTION_FREEZE, false);
}
return; // seg.differs(prev);
}
// deserializes WLED state (fileDoc points to doc object if called from web server)
bool deserializeState(JsonObject root, byte callMode, byte presetId)
{
DEBUG_PRINTLN(F("Deserializing state"));
@ -324,7 +324,8 @@ bool deserializeState(JsonObject root, byte callMode, byte presetId)
int8_t ledmap = root[F("ledmap")] | -1;
if (ledmap >= 0) {
strip.deserializeMap(ledmap);
//strip.deserializeMap(ledmap); // requires separate JSON buffer
loadLedmap = ledmap;
}
int ps = root[F("psave")] | -1;

View File

@ -91,11 +91,15 @@ 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);
//DynamicJsonDocument doc(JSON_BUFFER_SIZE);
while (jsonBufferLock) delay(1);
jsonBufferLock = true;
doc.clear();
deserializeJson(doc, payloadStr);
fileDoc = &doc;
deserializeState(doc.as<JsonObject>());
fileDoc = nullptr;
jsonBufferLock = false;
} else { //HTTP API
String apireq = "win&";
apireq += (char*)payloadStr;

View File

@ -20,14 +20,18 @@ bool applyPreset(byte index, byte callMode)
deserializeState(fdo, callMode, index);
} else {
DEBUGFS_PRINTLN(F("Make read buf"));
DynamicJsonDocument fDoc(JSON_BUFFER_SIZE);
errorFlag = readObjectFromFileUsingId(filename, index, &fDoc) ? ERR_NONE : ERR_FS_PLOAD;
JsonObject fdo = fDoc.as<JsonObject>();
//DynamicJsonDocument fDoc(JSON_BUFFER_SIZE);
while (jsonBufferLock) delay(1);
jsonBufferLock = true;
doc.clear();
errorFlag = readObjectFromFileUsingId(filename, index, &doc) ? ERR_NONE : ERR_FS_PLOAD;
JsonObject fdo = doc.as<JsonObject>();
if (fdo["ps"] == index) fdo.remove("ps");
#ifdef WLED_DEBUG_FS
serializeJson(fDoc, Serial);
serializeJson(doc, Serial);
#endif
deserializeState(fdo, callMode, index);
jsonBufferLock = false;
}
if (!errorFlag) {
@ -40,22 +44,27 @@ bool applyPreset(byte index, byte callMode)
void savePreset(byte index, bool persist, const char* pname, JsonObject saveobj)
{
if (index == 0 || (index > 250 && persist) || (index<255 && !persist)) return;
bool docAlloc = (fileDoc != nullptr);
JsonObject sObj = saveobj;
const char *filename = persist ? "/presets.json" : "/tmp.json";
if (!docAlloc) {
if (!fileDoc) {
DEBUGFS_PRINTLN(F("Allocating saving buffer"));
DynamicJsonDocument lDoc(JSON_BUFFER_SIZE);
sObj = lDoc.to<JsonObject>();
//DynamicJsonDocument lDoc(JSON_BUFFER_SIZE);
while (jsonBufferLock) delay(1);
jsonBufferLock = true;
doc.clear();
sObj = doc.to<JsonObject>();
if (pname) sObj["n"] = pname;
DEBUGFS_PRINTLN(F("Save current state"));
serializeState(sObj, true);
if (persist) currentPreset = index;
writeObjectToFileUsingId(filename, index, &lDoc);
} else { //from JSON API
writeObjectToFileUsingId(filename, index, &doc);
jsonBufferLock = false;
} else { //from JSON API (fileDoc != nullptr)
DEBUGFS_PRINTLN(F("Reuse recv buffer"));
sObj.remove(F("psave"));
sObj.remove(F("v"));

View File

@ -415,7 +415,11 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
//USERMODS
if (subPage == 8)
{
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
//DynamicJsonDocument doc(JSON_BUFFER_SIZE);
while (jsonBufferLock) delay(1);
jsonBufferLock = true;
doc.clear();
JsonObject um = doc.createNestedObject("um");
size_t args = request->args();
@ -490,6 +494,8 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
usermods.readFromConfig(um); // force change of usermod parameters
}
jsonBufferLock = false;
if (subPage != 2 && (subPage != 6 || !doReboot)) serializeConfig(); //do not save if factory reset or LED settings (which are saved after LED re-init)
if (subPage == 4) alexaInit();
}

View File

@ -150,11 +150,16 @@ void WLED::loop()
delete busConfigs[i]; busConfigs[i] = nullptr;
}
strip.finalizeInit();
loadLedmap = 0;
if (aligned) strip.makeAutoSegments();
else strip.fixInvalidSegments();
yield();
serializeConfig();
}
if (loadLedmap >= 0) {
strip.deserializeMap(loadLedmap);
loadLedmap = -1;
}
yield();
handleWs();
@ -339,6 +344,7 @@ void WLED::beginStrip()
{
// Initialize NeoPixel Strip and button
strip.finalizeInit(); // busses created during deserializeConfig()
strip.deserializeMap();
strip.makeAutoSegments();
strip.setBrightness(0);
strip.setShowCallback(handleOverlayDraw);

View File

@ -598,10 +598,15 @@ WLED_GLOBAL BusManager busses _INIT(BusManager());
WLED_GLOBAL WS2812FX strip _INIT(WS2812FX());
WLED_GLOBAL BusConfig* busConfigs[WLED_MAX_BUSSES] _INIT({nullptr}); //temporary, to remember values from network callback until after
WLED_GLOBAL bool doInitBusses _INIT(false);
WLED_GLOBAL int8_t loadLedmap _INIT(-1);
// Usermod manager
WLED_GLOBAL UsermodManager usermods _INIT(UsermodManager());
// global ArduinoJson buffer
WLED_GLOBAL StaticJsonDocument<JSON_BUFFER_SIZE> doc;
WLED_GLOBAL volatile bool jsonBufferLock _INIT(false);
// enable additional debug output
#ifdef WLED_DEBUG
#ifndef ESP8266

View File

@ -373,8 +373,12 @@ void deEEP() {
DEBUG_PRINTLN(F("Preset file not found, attempting to load from EEPROM"));
DEBUGFS_PRINTLN(F("Allocating saving buffer for dEEP"));
DynamicJsonDocument dDoc(JSON_BUFFER_SIZE *2);
JsonObject sObj = dDoc.to<JsonObject>();
//DynamicJsonDocument doc(JSON_BUFFER_SIZE *2);
while (jsonBufferLock) delay(1);
jsonBufferLock = true;
doc.clear();
JsonObject sObj = doc.to<JsonObject>();
sObj.createNestedObject("0");
EEPROM.begin(EEPSIZE);
@ -433,8 +437,6 @@ void deEEP() {
}
}
for (uint16_t index = 1; index <= 16; index++) { //copy macros to presets.json
char m[65];
readStringFromEEPROM(1024+64*(index-1), m, 64);
@ -456,8 +458,11 @@ void deEEP() {
errorFlag = ERR_FS_GENERAL;
return;
}
serializeJson(dDoc, f);
serializeJson(doc, f);
f.close();
jsonBufferLock = false;
DEBUG_PRINTLN(F("deEEP complete!"));
}

View File

@ -43,18 +43,25 @@ void handleSerial()
}
else if (next == '{') { //JSON API
bool verboseResponse = false;
while (jsonBufferLock) delay(1);
jsonBufferLock = true;
doc.clear();
{
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
//DynamicJsonDocument doc(JSON_BUFFER_SIZE);
Serial.setTimeout(100);
DeserializationError error = deserializeJson(doc, Serial);
if (error) return;
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);
//DynamicJsonDocument doc(JSON_BUFFER_SIZE);
doc.clear();
JsonObject state = doc.createNestedObject("state");
serializeState(state);
JsonObject info = doc.createNestedObject("info");
@ -62,6 +69,7 @@ void handleSerial()
serializeJson(doc, Serial);
}
jsonBufferLock = false;
}
break;
case AdaState::Header_d:

View File

@ -110,9 +110,13 @@ void initServer()
bool verboseResponse = false;
bool isConfig = false;
{ //scope JsonDocument so it releases its buffer
DynamicJsonDocument jsonBuffer(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(jsonBuffer, (uint8_t*)(request->_tempObject));
JsonObject root = jsonBuffer.as<JsonObject>();
//DynamicJsonDocument jsonBuffer(JSON_BUFFER_SIZE);
while (jsonBufferLock) delay(1);
jsonBufferLock = true;
doc.clear();
DeserializationError error = deserializeJson(doc, (uint8_t*)(request->_tempObject));
JsonObject root = doc.as<JsonObject>();
if (error || root.isNull()) {
request->send(400, "application/json", F("{\"error\":9}")); return;
}
@ -124,12 +128,13 @@ void initServer()
serializeJson(root,Serial);
DEBUG_PRINTLN();
#endif
fileDoc = &jsonBuffer; // used for applying presets (presets.cpp)
fileDoc = &doc; // used for applying presets (presets.cpp)
verboseResponse = deserializeState(root);
fileDoc = nullptr;
} else {
verboseResponse = deserializeConfig(root); //use verboseResponse to determine whether cfg change should be saved immediately
}
jsonBufferLock = false;
}
if (verboseResponse) {
if (!isConfig) {

View File

@ -36,16 +36,24 @@ void wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTyp
}
bool verboseResponse = false;
{ //scope JsonDocument so it releases its buffer
DynamicJsonDocument jsonBuffer(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(jsonBuffer, data, len);
JsonObject root = jsonBuffer.as<JsonObject>();
if (error || root.isNull()) return;
//DynamicJsonDocument doc(JSON_BUFFER_SIZE);
while (jsonBufferLock) delay(1);
jsonBufferLock = true;
doc.clear();
DeserializationError error = deserializeJson(doc, data, len);
JsonObject root = doc.as<JsonObject>();
if (error || root.isNull()) {
jsonBufferLock = false;
return;
}
/*
#ifdef WLED_DEBUG
DEBUG_PRINT(F("Incoming WS: "));
serializeJson(root,Serial);
DEBUG_PRINTLN();
#endif
*/
if (root["v"] && root.size() == 1) {
//if the received value is just "{"v":true}", send only to this client
verboseResponse = true;
@ -53,7 +61,7 @@ void wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTyp
{
wsLiveClientId = root["lv"] ? client->id() : 0;
} else {
fileDoc = &jsonBuffer;
fileDoc = &doc;
verboseResponse = deserializeState(root);
fileDoc = nullptr;
if (!interfaceUpdateCallMode) {
@ -61,6 +69,7 @@ void wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTyp
if (millis() - lastInterfaceUpdate > 1700) verboseResponse = false;
}
}
jsonBufferLock = false;
}
//update if it takes longer than 300ms until next "broadcast"
if (verboseResponse && (millis() - lastInterfaceUpdate < 1700 || !interfaceUpdateCallMode)) sendDataWs(client);
@ -102,14 +111,20 @@ void sendDataWs(AsyncWebSocketClient * client)
AsyncWebSocketMessageBuffer * buffer;
{ //scope JsonDocument so it releases its buffer
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
//DynamicJsonDocument doc(JSON_BUFFER_SIZE);
while (jsonBufferLock) delay(1);
jsonBufferLock = true;
doc.clear();
JsonObject state = doc.createNestedObject("state");
serializeState(state);
JsonObject info = doc.createNestedObject("info");
serializeInfo(info);
size_t len = measureJson(doc);
buffer = ws.makeBuffer(len);
if (!buffer) return; //out of memory
if (!buffer) {
jsonBufferLock = false;
return; //out of memory
}
/*
#ifdef WLED_DEBUG
DEBUG_PRINT(F("Outgoing WS: "));
@ -118,6 +133,7 @@ void sendDataWs(AsyncWebSocketClient * client)
#endif
*/
serializeJson(doc, (char *)buffer->get(), len +1);
jsonBufferLock = false;
}
DEBUG_PRINT(F("Sending WS data "));
if (client) {

View File

@ -249,13 +249,18 @@ void getSettingsJS(byte subPage, char* dest)
{
char nS[8];
// Pin reservations will become unnecessary when settings pages will read cfg.json directly
// 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);
//DynamicJsonDocument doc(JSON_BUFFER_SIZE/2);
while (jsonBufferLock) delay(1);
jsonBufferLock = true;
doc.clear();
JsonObject mods = doc.createNestedObject(F("um"));
usermods.addToConfig(mods);
if (!mods.isNull()) fillUMPins(mods);
jsonBufferLock = false;
#ifdef WLED_ENABLE_DMX
oappend(SET_F(",2")); // DMX hardcoded pin