Skip to content

Commit

Permalink
Merge pull request #3610 from Aircoookie/psram-4-json
Browse files Browse the repository at this point in the history
Allow PSRAM (SPI RAM) to be used for JSON buffer
  • Loading branch information
blazoncek authored Dec 30, 2023
2 parents 1c1b67e + d3be7a3 commit 301bdf2
Show file tree
Hide file tree
Showing 19 changed files with 105 additions and 74 deletions.
4 changes: 2 additions & 2 deletions wled00/FX_2Dfcn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,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>();
JsonArray map = pDoc->as<JsonArray>();
gapSize = map.size();
if (!map.isNull() && gapSize >= matrixSize) { // not an empty map
gapTable = new int8_t[gapSize];
Expand Down
5 changes: 3 additions & 2 deletions wled00/FX_fcn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1666,7 +1666,7 @@ bool WS2812FX::deserializeMap(uint8_t n) {

if (!isFile || !requestJSONBufferLock(7)) return false; // this will trigger setUpMatrix() when called from wled.cpp

if (!readObjectFromFile(fileName, nullptr, &doc)) {
if (!readObjectFromFile(fileName, nullptr, pDoc)) {
DEBUG_PRINT(F("ERROR Invalid ledmap in ")); DEBUG_PRINTLN(fileName);
releaseJSONBufferLock();
return false; // if file does not load properly then exit
Expand All @@ -1676,7 +1676,8 @@ bool WS2812FX::deserializeMap(uint8_t n) {

if (customMappingTable == nullptr) customMappingTable = new uint16_t[getLengthTotal()];

JsonArray map = doc[F("map")];
JsonObject root = pDoc->as<JsonObject>();
JsonArray map = root[F("map")];
if (!map.isNull() && map.size()) { // not an empty map
customMappingSize = min((unsigned)map.size(), (unsigned)getLengthTotal());
for (unsigned i=0; i<customMappingSize; i++) customMappingTable[i] = (uint16_t) (map[i]<0 ? 0xFFFFU : map[i]);
Expand Down
69 changes: 38 additions & 31 deletions wled00/cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<JsonObject>(), true);
JsonObject root = pDoc->as<JsonObject>();
bool needsSave = deserializeConfig(root, true);
releaseJSONBufferLock();

if (needsSave) serializeConfig(); // usermods required new parameters
Expand All @@ -643,19 +644,21 @@ void serializeConfig() {

if (!requestJSONBufferLock(2)) return;

JsonArray rev = doc.createNestedArray("rev");
JsonObject root = pDoc->to<JsonObject>();

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;
Expand All @@ -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;
Expand All @@ -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")] = (int)force802_3g;

#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");
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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"));
Expand Down Expand Up @@ -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;
Expand All @@ -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(root, f);
f.close();
releaseJSONBufferLock();

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

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"];
Expand All @@ -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")]);
Expand All @@ -1078,17 +1083,19 @@ void serializeConfigSec() {

if (!requestJSONBufferLock(4)) return;

JsonObject nw = doc.createNestedObject("nw");
JsonObject root = pDoc->to<JsonObject>();

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;
Expand All @@ -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(root, f);
f.close();
releaseJSONBufferLock();
}
4 changes: 2 additions & 2 deletions wled00/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonObject>();
readObjectFromFile("/ir.json", objKey, pDoc);
fdo = pDoc->as<JsonObject>();
lastValidCode = 0;
if (fdo.isNull()) {
//the received code does not exist
Expand Down
2 changes: 1 addition & 1 deletion wled00/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions wled00/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
return;
}
if (payloadStr[0] == '{') { //JSON API
deserializeJson(doc, payloadStr);
deserializeState(doc.as<JsonObject>());
deserializeJson(*pDoc, payloadStr);
deserializeState(pDoc->as<JsonObject>());
} else { //HTTP API
String apireq = "win"; apireq += '&'; // reduce flash string usage
apireq += payloadStr;
Expand Down
5 changes: 4 additions & 1 deletion wled00/pin_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions wled00/presets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
JsonObject sObj = pDoc->to<JsonObject>();

DEBUG_PRINTLN(F("Serialize current state"));
if (playlistSave) {
Expand All @@ -42,7 +42,7 @@ static void doSaveState() {
/*
#ifdef WLED_DEBUG
DEBUG_PRINTLN(F("Serialized preset"));
serializeJson(doc,Serial);
serializeJson(*pDoc,Serial);
DEBUG_PRINTLN();
#endif
*/
Expand Down Expand Up @@ -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>();
JsonObject fdo = pDoc->as<JsonObject>();
if (fdo["n"]) {
name = (const char*)(fdo["n"]);
presetExists = true;
Expand Down
4 changes: 2 additions & 2 deletions wled00/remote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonObject>();
readObjectFromFile("/remote.json", objKey, pDoc);
JsonObject fdo = pDoc->as<JsonObject>();
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
Expand Down
2 changes: 1 addition & 1 deletion wled00/set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,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;
Expand Down
4 changes: 2 additions & 2 deletions wled00/udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonObject>();
DeserializationError error = deserializeJson(*pDoc, udpIn);
JsonObject root = pDoc->as<JsonObject>();
if (!error && !root.isNull()) deserializeState(root);
}
releaseJSONBufferLock();
Expand Down
Loading

0 comments on commit 301bdf2

Please sign in to comment.