Bigger buffer testing

This commit is contained in:
cschwinne 2020-10-08 00:52:15 +02:00
parent d2ffb3ca9d
commit b0828a6280
9 changed files with 71 additions and 58 deletions

View File

@ -198,10 +198,10 @@ void clearEEPROM();
void writeStringToEEPROM(uint16_t pos, char* str, uint16_t len);
void readStringFromEEPROM(uint16_t pos, char* str, uint16_t len);
void saveSettingsToEEPROM();
void loadSettingsFromEEPROM(bool first);
void loadSettingsFromEEPROM();
void savedToPresets();
bool applyPreset(byte index, bool loadBri = true);
void savePreset(byte index, bool persist = true, const char* pname = nullptr, byte prio = 5, JsonObject saveobj = JsonObject());
bool applyPreset(byte index);
void savePreset(byte index, bool persist = true, const char* pname = nullptr, JsonObject saveobj = JsonObject());
void deletePreset(byte index);
void loadMacro(byte index, char* m);
void applyMacro(byte index);

View File

@ -10,7 +10,7 @@
#include "esp_spiffs.h" //FS info bare IDF function until FS wrapper is available for ESP32
#endif
#define FS_BUFSIZE 256
#define FS_BUFSIZE 512
//allow presets to be added until this percentage of FS space is used
#define FS_QUOTA 75
@ -86,16 +86,16 @@ bool bufferedFindSpace(uint16_t targetLen, File f) {
count = 0;
while (count < bufsize) {
if(buf[count] != ' ')
index = 0; // reset index if not space
if(buf[count] == ' ') {
if(++index >= targetLen) { // return true if space long enough
f.seek((f.position() - bufsize) + count +1 - targetLen);
DEBUGFS_PRINTF("Found at pos %d, took %d ms", f.position(), millis() - s);
return true;
}
} else {
index = 0; // reset index if not space
}
count++;
}
}
@ -115,7 +115,7 @@ bool bufferedFindObjectEnd(File f) {
uint16_t objDepth = 0; //num of '{' minus num of '}'. return once 0
uint16_t bufsize = 0, count = 0;
//size_t start = f.position();
byte buf[256];
byte buf[FS_BUFSIZE];
while (f.position() < f.size() -1) {
bufsize = f.read(buf, FS_BUFSIZE);
@ -297,7 +297,7 @@ bool readObjectFromFile(const char* file, const char* key, JsonDocument* dest)
if (!bufferedFind(key, f)) //key does not exist in file
{
f.close();
DEBUGFS_PRINTLN("Obj not found.");
DEBUGFS_PRINTLN(F("Obj not found."));
return false;
}

View File

@ -147,9 +147,6 @@ bool deserializeState(JsonObject root)
{
strip.applyToAllSelected = false;
bool stateResponse = root[F("v")] | false;
int ps = root[F("ps")] | -1;
if (ps >= 0) applyPreset(ps);
bri = root["bri"] | bri;
@ -240,17 +237,17 @@ bool deserializeState(JsonObject root)
colorUpdated(noNotification ? NOTIFIER_CALL_MODE_NO_NOTIFY : NOTIFIER_CALL_MODE_DIRECT_CHANGE);
//write presets to flash directly?
bool persistSaves = !(root[F("np")] | false);
ps = root[F("psave")] | -1;
int ps = root[F("psave")] | -1;
if (ps > 0) {
savePreset(ps, persistSaves, root["n"], root["p"] | 50, root["o"].as<JsonObject>());
savePreset(ps, true, nullptr, root);
} else {
ps = root[F("pdel")] | -1; //deletion
if (ps > 0) {
deletePreset(ps);
}
ps = root[F("ps")] | -1; //load preset (clears state request!)
if (ps >= 0) applyPreset(ps);
//HTTP API commands
const char* httpwin = root["win"];
if (httpwin) {

View File

@ -296,7 +296,7 @@ void handleNightlight()
if (bri == 0 || nightlightActive) return;
if (presetCycCurr < presetCycleMin || presetCycCurr > presetCycleMax) presetCycCurr = presetCycleMin;
applyPreset(presetCycCurr,presetApplyBri);
applyPreset(presetCycCurr);
presetCycCurr++;
if (presetCycCurr > 16) presetCycCurr = 1;
colorUpdated(NOTIFIER_CALL_MODE_PRESET_CYCLE);

View File

@ -486,15 +486,12 @@ bool handleSet(AsyncWebServerRequest *request, const String& req, bool apply)
if (v > 100) presetCycleTime = v/100;
}
pos = req.indexOf(F("PA=")); //apply brightness from preset
if (pos > 0) presetApplyBri = (req.charAt(pos+3) != '0');
pos = req.indexOf(F("PS=")); //saves current in preset
if (pos > 0) savePreset(getNumVal(&req, pos), persistSaves);
if (pos > 0) savePreset(getNumVal(&req, pos));
//apply preset
if (updateVal(&req, "PL=", &presetCycCurr, presetCycleMin, presetCycleMax)) {
applyPreset(presetCycCurr, presetApplyBri);
applyPreset(presetCycCurr);
}
//set brightness

View File

@ -85,13 +85,10 @@ void WLED::loop()
handleHue();
handleBlynk();
if (presetToApply) {
StaticJsonDocument<1024> temp;
errorFlag = !readObjectFromFileUsingId("/presets.json", presetToApply, &temp);
serializeJson(temp, Serial);
deserializeState(temp.as<JsonObject>());
/*if (presetToApply) {
applyPreset(presetToApply);
presetToApply = 0;
}
}*/
yield();
@ -192,7 +189,7 @@ void WLED::setup()
#endif
DEBUG_PRINTLN(F("Load EEPROM"));
loadSettingsFromEEPROM(true);
loadSettingsFromEEPROM();
beginStrip();
userSetup();
usermods.setup();
@ -252,8 +249,7 @@ void WLED::beginStrip()
pinMode(BTNPIN, INPUT_PULLUP);
#endif
if (bootPreset > 0)
applyPreset(bootPreset, turnOnAtBoot);
if (bootPreset > 0) applyPreset(bootPreset);
colorUpdated(NOTIFIER_CALL_MODE_INIT);
// init relay pin

View File

@ -167,10 +167,12 @@ WLED_GLOBAL char otaPass[33] _INIT(DEFAULT_OTA_PASS);
// Hardware CONFIG (only changeble HERE, not at runtime)
// LED strip pin, button pin and IR pin changeable in NpbWrapper.h!
WLED_GLOBAL byte presetToApply _INIT(0);
//WLED_GLOBAL byte presetToApply _INIT(0);
#if AUXPIN >= 0
WLED_GLOBAL byte auxDefaultState _INIT(0); // 0: input 1: high 2: low
WLED_GLOBAL byte auxTriggeredState _INIT(0); // 0: input 1: high 2: low
#endif
WLED_GLOBAL char ntpServerName[33] _INIT("0.wled.pool.ntp.org"); // NTP server to use
// WiFi CONFIG (all these can be changed via web UI, no need to set them here)
@ -425,7 +427,6 @@ WLED_GLOBAL byte presetCycleMin _INIT(1), presetCycleMax _INIT(5);
WLED_GLOBAL uint16_t presetCycleTime _INIT(12);
WLED_GLOBAL unsigned long presetCycledTime _INIT(0);
WLED_GLOBAL byte presetCycCurr _INIT(presetCycleMin);
WLED_GLOBAL bool presetApplyBri _INIT(true);
WLED_GLOBAL bool saveCurrPresetCycConf _INIT(false);
// realtime
@ -476,6 +477,7 @@ WLED_GLOBAL uint16_t olen _INIT(0);
WLED_GLOBAL size_t fsBytesUsed _INIT(0);
WLED_GLOBAL size_t fsBytesTotal _INIT(0);
WLED_GLOBAL unsigned long presetsModifiedTime _INIT(0L);
WLED_GLOBAL JsonDocument* fileDoc;
// presets
WLED_GLOBAL uint16_t savedPresets _INIT(0);

View File

@ -249,7 +249,7 @@ void saveSettingsToEEPROM()
EEPROM.write(2207, (presetCycleTime >> 8) & 0xFF);
EEPROM.write(2208, presetCycleMin);
EEPROM.write(2209, presetCycleMax);
EEPROM.write(2210, presetApplyBri);
// was EEPROM.write(2210, presetApplyBri);
// was EEPROM.write(2211, presetApplyCol);
// was EEPROM.write(2212, presetApplyFx);
saveCurrPresetCycConf = false;
@ -295,7 +295,7 @@ void saveSettingsToEEPROM()
/*
* Read all configuration from flash
*/
void loadSettingsFromEEPROM(bool first)
void loadSettingsFromEEPROM()
{
if (EEPROM.read(233) != 233) //first boot/reset to default
{
@ -343,7 +343,7 @@ void loadSettingsFromEEPROM(bool first)
staticSubnet[3] = EEPROM.read(245);
briS = EEPROM.read(249); bri = briS;
if (!EEPROM.read(369) && first)
if (!EEPROM.read(369))
{
bri = 0; briLast = briS;
}
@ -559,7 +559,7 @@ void loadSettingsFromEEPROM(bool first)
if (lastEEPROMversion < 21) presetCycleTime /= 100; //was stored in ms, now is in tenths of a second
presetCycleMin = EEPROM.read(2208);
presetCycleMax = EEPROM.read(2209);
presetApplyBri = EEPROM.read(2210);
//was presetApplyBri = EEPROM.read(2210);
//was presetApplyCol = EEPROM.read(2211);
//was presetApplyFx = EEPROM.read(2212);
}
@ -627,19 +627,31 @@ void savedToPresets()
}
}
bool applyPreset(byte index, bool loadBri)
bool applyPreset(byte index)
{
StaticJsonDocument<1024> temp;
errorFlag = readObjectFromFileUsingId("/presets.json", index, &temp) ? ERR_NONE : ERR_FS_PLOAD;
serializeJson(temp, Serial);
deserializeState(temp.as<JsonObject>());
if (fileDoc) {
errorFlag = readObjectFromFileUsingId("/presets.json", index, fileDoc) ? ERR_NONE : ERR_FS_PLOAD;
#ifdef WLED_DEBUG_FS
serializeJson(*fileDoc, Serial);
#endif
deserializeState(fileDoc->as<JsonObject>());
} else {
WLED_DEBUG_FS(F("Make read buf"));
DynamicJsonDocument fDoc(JSON_BUFFER_SIZE);
errorFlag = readObjectFromFileUsingId("/presets.json", index, &fDoc) ? ERR_NONE : ERR_FS_PLOAD;
#ifdef WLED_DEBUG_FS
serializeJson(fDoc, Serial);
#endif
deserializeState(fDoc.as<JsonObject>());
}
if (!errorFlag) {
currentPreset = index;
isPreset = true;
return true;
}
return false;
if (index == 255 || index == 0)
/*if (index == 255 || index == 0)
{
loadSettingsFromEEPROM(false);//load boot defaults
return true;
@ -681,29 +693,36 @@ bool applyPreset(byte index, bool loadBri)
}
currentPreset = index;
isPreset = true;
return true;
return true;*/
}
void savePreset(byte index, bool persist, const char* pname, byte priority, JsonObject saveobj)
void savePreset(byte index, bool persist, const char* pname, JsonObject saveobj)
{
StaticJsonDocument<1024> doc;
JsonObject sObj = doc.to<JsonObject>();
bool docAlloc = fileDoc;
JsonObject sObj = saveobj;
if (saveobj.isNull()) {
DEBUGFS_PRINTLN("Save current state");
serializeState(doc.to<JsonObject>(), true);
currentPreset = index;
if (!docAlloc) {
DEBUGFS_PRINTLN(F("Allocating saving buffer"));
fileDoc = new DynamicJsonDocument(JSON_BUFFER_SIZE);
sObj = fileDoc->to<JsonObject>();
if (pname) sObj["n"] = pname;
} else {
DEBUGFS_PRINTLN("Save custom");
sObj.set(saveobj);
DEBUGFS_PRINTLN(F("Reuse recv buffer"));
sObj.remove(F("psave"));
sObj.remove(F("v"));
}
sObj["p"] = priority;
if (pname) sObj["n"] = pname;
writeObjectToFileUsingId("/presets.json", index, &doc);
if (!sObj["o"]) {
DEBUGFS_PRINTLN(F("Save current state"));
serializeState(sObj, true);
currentPreset = index;
}
sObj.remove("o");
writeObjectToFileUsingId("/presets.json", index, fileDoc);
if (!docAlloc) delete fileDoc;
presetsModifiedTime = now(); //unix time
updateFSInfo();
return;
/*if (index > 16) return;
if (index < 1) {saveSettingsToEEPROM();return;}

View File

@ -85,7 +85,9 @@ void initServer()
if (error || root.isNull()) {
request->send(400, "application/json", F("{\"error\":9}")); return;
}
fileDoc = &jsonBuffer;
verboseResponse = deserializeState(root);
fileDoc = nullptr;
}
if (verboseResponse) { //if JSON contains "v"
serveJson(request); return;