Async preset saving.

Minor bugfixes.
This commit is contained in:
Blaz Kristan 2022-10-08 18:25:51 +02:00
parent 4fb44d98db
commit 7642f8d702
8 changed files with 1054 additions and 1043 deletions

View File

@ -4524,7 +4524,7 @@ uint16_t mode_perlinmove(void) {
return FRAMETIME;
} // mode_perlinmove()
static const char _data_FX_MODE_PERLINMOVE[] PROGMEM = "Perlin Move@!,# of pixels,fade rate;,!;!;1d";
static const char _data_FX_MODE_PERLINMOVE[] PROGMEM = "Perlin Move@!,# of pixels,fade rate;!,!;!;1d";
/////////////////////////

View File

@ -399,7 +399,7 @@ function presetError(empty)
if (bckstr.length > 10) hasBackup = true;
} catch (e) {}
var cn = `<div class="pres c" ${empty?'style="padding:8px;margin-top: 16px;"':'onclick="loadPresets()" style="cursor:pointer;padding:8px;margin-top: 16px;"'}>`;
var cn = `<div class="pres c" ${empty?'style="padding:8px;margin-top: 16px;"':'onclick="pmtLast=0;loadPresets();" style="cursor:pointer;padding:8px;margin-top: 16px;"'}>`;
if (empty)
cn += `You have no presets yet!`;
else
@ -2240,7 +2240,7 @@ function saveP(i,pl)
}
populatePresets();
resetPUtil();
setTimeout(()=>{pmtLast=0; loadPresets();}, 500); // force reloading of presets
setTimeout(()=>{pmtLast=0; loadPresets();}, 750); // force reloading of presets
}
function testPl(i,bt) {
@ -2269,6 +2269,7 @@ function delP(i) {
requestJson(obj);
delete pJson[i];
populatePresets();
gId('putil').classList.add("staybot");
} else {
bt.style.color = "var(--c-r)";
bt.innerHTML = "<i class='icons btn-icon'>&#xe037;</i>Delete!";

View File

@ -135,7 +135,7 @@ void handleIR();
void deserializeSegment(JsonObject elem, byte it, byte presetId = 0);
bool deserializeState(JsonObject root, byte callMode = CALL_MODE_DIRECT_CHANGE, byte presetId = 0);
void serializeSegment(JsonObject& root, Segment& seg, byte id, bool forPreset = false, bool segmentBounds = true);
void serializeState(JsonObject root, bool forPreset = false, bool includeBri = true, bool segmentBounds = true);
void serializeState(JsonObject root, bool forPreset = false, bool includeBri = true, bool segmentBounds = true, bool selectedSegmentsOnly = false);
void serializeInfo(JsonObject root);
void serializeModeNames(JsonArray arr, const char *qstring);
void serializeModeData(JsonObject root);
@ -192,6 +192,7 @@ void shufflePlaylist();
void unloadPlaylist();
int16_t loadPlaylist(JsonObject playlistObject, byte presetId = 0);
void handlePlaylist();
void serializePlaylist(JsonObject obj);
//presets.cpp
void handlePresets();

File diff suppressed because it is too large Load Diff

View File

@ -481,7 +481,7 @@ void serializeSegment(JsonObject& root, Segment& seg, byte id, bool forPreset, b
root["mp12"] = seg.map1D2D;
}
void serializeState(JsonObject root, bool forPreset, bool includeBri, bool segmentBounds)
void serializeState(JsonObject root, bool forPreset, bool includeBri, bool segmentBounds, bool selectedSegmentsOnly)
{
if (includeBri) {
root["on"] = (bri > 0);
@ -517,11 +517,10 @@ void serializeState(JsonObject root, bool forPreset, bool includeBri, bool segme
root[F("mainseg")] = strip.getMainSegmentId();
bool selectedSegmentsOnly = root[F("sc")] | false;
JsonArray seg = root.createNestedArray("seg");
for (size_t s = 0; s < strip.getMaxSegments(); s++) {
if (s >= strip.getSegmentsNum()) {
if (forPreset && segmentBounds) { //disable segments not part of preset
if (forPreset && segmentBounds && !selectedSegmentsOnly) { //disable segments not part of preset
JsonObject seg0 = seg.createNestedObject();
seg0["stop"] = 0;
continue;
@ -529,7 +528,7 @@ void serializeState(JsonObject root, bool forPreset, bool includeBri, bool segme
break;
}
Segment &sg = strip.getSegment(s);
if (!forPreset && selectedSegmentsOnly && !sg.isSelected()) continue;
if (forPreset && selectedSegmentsOnly && !sg.isSelected()) continue;
if (sg.isActive()) {
JsonObject seg0 = seg.createNestedObject();
serializeSegment(seg0, sg, s, forPreset, segmentBounds);

View File

@ -149,3 +149,19 @@ void handlePlaylist() {
applyPreset(playlistEntries[playlistIndex].preset);
}
}
void serializePlaylist(JsonObject sObj) {
JsonObject playlist = sObj.createNestedObject("playlist");
JsonArray ps = playlist.createNestedArray("ps");
JsonArray dur = playlist.createNestedArray("dur");
JsonArray transition = playlist.createNestedArray(F("transition"));
playlist[F("repeat")] = playlistRepeat;
playlist["end"] = playlistEndPreset;
playlist["r"] = playlistOptions & PL_OPTION_SHUFFLE;
for (int i=0; i<playlistLen; i++) {
ps.add(playlistEntries[i].preset);
dur.add(playlistEntries[i].dur);
transition.add(playlistEntries[i].tr);
}
}

View File

@ -10,6 +10,70 @@ static char *tmpRAMbuffer = nullptr;
static volatile byte presetToApply = 0;
static volatile byte callModeToApply = 0;
static volatile byte presetToSave = 0;
static char quickLoad[3];
static char saveName[33];
static bool includeBri = true, segBounds = true, selectedOnly = false, playlistSave = false;;
static const char *getName(bool persist = true) {
return persist ? "/presets.json" : "/tmp.json";
}
static void doSaveState() {
bool persist = (presetToSave < 251);
const char *filename = getName(persist);
if (!requestJSONBufferLock(10)) return; // will set fileDoc
JsonObject sObj = doc.to<JsonObject>();
DEBUG_PRINTLN(F("Serialize current state"));
if (playlistSave) {
serializePlaylist(sObj);
if (includeBri) sObj["on"] = true;
} else {
serializeState(sObj, true, includeBri, segBounds, selectedOnly);
}
sObj["n"] = saveName;
if (quickLoad[0]) sObj[F("ql")] = quickLoad;
#ifdef WLED_DEBUG
DEBUG_PRINTLN(F("Serialized preset"));
serializeJson(doc,Serial);
DEBUG_PRINTLN();
#endif
#if defined(ARDUINO_ARCH_ESP32)
if (!persist) {
if (tmpRAMbuffer!=nullptr) free(tmpRAMbuffer);
size_t len = measureJson(*fileDoc) + 1;
DEBUG_PRINTLN(len);
// if possible use SPI RAM on ESP32
#ifdef WLED_USE_PSRAM
if (psramFound())
tmpRAMbuffer = (char*) ps_malloc(len);
else
#endif
tmpRAMbuffer = (char*) malloc(len);
if (tmpRAMbuffer!=nullptr) {
serializeJson(*fileDoc, tmpRAMbuffer, len);
} else {
writeObjectToFileUsingId(filename, presetToSave, fileDoc);
}
} else
#endif
writeObjectToFileUsingId(filename, presetToSave, fileDoc);
if (persist) presetsModifiedTime = toki.second(); //unix time
releaseJSONBufferLock();
updateFSInfo();
// clean up
presetToSave = 0;
saveName[0] = '\0';
quickLoad[0] = '\0';
playlistSave = false;
}
bool applyPreset(byte index, byte callMode)
{
@ -17,16 +81,16 @@ bool applyPreset(byte index, byte callMode)
DEBUG_PRINTLN(index);
presetToApply = index;
callModeToApply = callMode;
/*
// the following is needed in case of HTTP JSON API call to return correct state to the caller
// fromJson is true in case when deserializeState() was called with presetId==0
if (fromJson) handlePresets(true); // force immediate processing
*/
return true;
}
void handlePresets()
{
if (presetToSave) {
doSaveState();
return;
}
bool changePreset = false;
uint8_t tmpPreset = presetToApply; // store temporary since deserializeState() may call applyPreset()
uint8_t tmpMode = callModeToApply;
@ -34,60 +98,8 @@ void handlePresets()
if (tmpPreset == 0 || (fileDoc /*&& !force*/)) return; // JSON buffer already allocated and not force apply or no preset waiting
JsonObject fdo;
const char *filename = tmpPreset < 255 ? "/presets.json" : "/tmp.json";
const char *filename = getName(tmpPreset < 255);
/*
* The following code is no longer needed as handlePreset() is never run from
* network callback.
* **************************************************************************
*
//crude way to determine if this was called by a network request
uint8_t core = 1;
#ifdef ARDUINO_ARCH_ESP32
#if !defined(CONFIG_IDF_TARGET_ESP32C3) && !defined(CONFIG_IDF_TARGET_ESP32S2)
// this does not make sense on single core
core = xPortGetCoreID();
#endif
#endif
//only allow use of fileDoc from the core responsible for network requests (AKA HTTP JSON API)
//do not use active network request doc from preset called by main loop (playlist, schedule, ...)
if (fileDoc && core && force && tmpPreset < 255) {
DEBUG_PRINT(F("Force applying preset: "));
DEBUG_PRINTLN(presetToApply);
presetToApply = 0; //clear request for preset
callModeToApply = 0;
// this will overwrite doc with preset content but applyPreset() is the last in such case and content of doc is no longer needed
errorFlag = readObjectFromFileUsingId(filename, tmpPreset, fileDoc) ? ERR_NONE : ERR_FS_PLOAD;
JsonObject fdo = fileDoc->as<JsonObject>();
//HTTP API commands
const char* httpwin = fdo["win"];
if (httpwin) {
String apireq = "win"; // reduce flash string usage
apireq += F("&IN&"); // internal call
apireq += httpwin;
handleSet(nullptr, apireq, false); // may call applyPreset() via PL=
setValuesFromFirstSelectedSeg(); // fills legacy values
changePreset = true;
} else {
if (!fdo["seg"].isNull()) unloadPlaylist(); // if preset contains "seg" we must unload playlist
if (!fdo["seg"].isNull() || !fdo["on"].isNull() || !fdo["bri"].isNull() || !fdo["ps"].isNull() || !fdo[F("playlist")].isNull()) changePreset = true;
fdo.remove("ps"); //remove load request for presets to prevent recursive crash
deserializeState(fdo, tmpMode, tmpPreset); // may call applyPreset() which will overwrite presetToApply
}
if (!errorFlag && changePreset) presetCycCurr = currentPreset = tmpPreset;
colorUpdated(tmpMode);
return;
}
if (force) return; // something went wrong with force option (most likely WS request), quit and wait for async load
*/
// allocate buffer
if (!requestJSONBufferLock(9)) return; // will also assign fileDoc
@ -138,68 +150,49 @@ void handlePresets()
}
//called from handleSet(PS=) [network callback (fileDoc==nullptr), IR (irrational), deserializeState, UDP] and deserializeState() [network callback (filedoc!=nullptr)]
void savePreset(byte index, const char* pname, JsonObject saveobj)
void savePreset(byte index, const char* pname, JsonObject sObj)
{
if (index == 0 || (index > 250 && index < 255)) return;
char tmp[12];
JsonObject sObj = saveobj;
bool bufferAllocated = false;
bool persist = (index != 255);
const char *filename = persist ? "/presets.json" : "/tmp.json";
if (!fileDoc) {
// called from handleSet() HTTP API
if (!requestJSONBufferLock(10)) return;
sObj = fileDoc->to<JsonObject>();
bufferAllocated = true;
if (pname) strlcpy(saveName, pname, 33);
else {
if (sObj["n"].is<const char*>()) strlcpy(saveName, sObj["n"].as<const char*>(), 33);
else sprintf_P(saveName, PSTR("Preset %d"), index);
}
if (sObj["n"].isNull() && pname == nullptr) {
sprintf_P(tmp, PSTR("Preset %d"), index);
sObj["n"] = tmp;
} else if (pname) sObj["n"] = pname;
DEBUG_PRINT(F("Saving preset (")); DEBUG_PRINT(index); DEBUG_PRINT(F(") ")); DEBUG_PRINTLN(saveName);
presetToSave = index;
playlistSave = false;
if (sObj[F("ql")].is<const char*>()) strlcpy(quickLoad, sObj[F("ql")].as<const char*>(), 3); // only 2 chars for QL
sObj.remove("v");
sObj.remove(F("time"));
sObj.remove(F("error"));
sObj.remove(F("psave"));
sObj.remove(F("v"));
if (!sObj["o"]) {
DEBUGFS_PRINTLN(F("Serialize current state"));
if (sObj["ib"].isNull() && sObj["sb"].isNull()) serializeState(sObj, true);
else serializeState(sObj, true, sObj["ib"], sObj["sb"]);
if (persist) currentPreset = index;
}
sObj.remove("o");
if (sObj["o"].isNull()) { // "o" marks a playlist or manually entered API
includeBri = sObj["ib"].as<bool>();
segBounds = sObj["sb"].as<bool>();
selectedOnly = sObj[F("sc")].as<bool>();
sObj.remove("ib");
sObj.remove("sb");
sObj.remove(F("sc"));
sObj.remove(F("error"));
sObj.remove(F("time"));
#if defined(ARDUINO_ARCH_ESP32)
if (index==255) {
if (tmpRAMbuffer!=nullptr) free(tmpRAMbuffer);
size_t len = measureJson(*fileDoc) + 1;
DEBUG_PRINTLN(len);
// if possible use SPI RAM on ESP32
#ifdef WLED_USE_PSRAM
if (psramFound())
tmpRAMbuffer = (char*) ps_malloc(len);
else
#endif
tmpRAMbuffer = (char*) malloc(len);
if (tmpRAMbuffer!=nullptr) {
serializeJson(*fileDoc, tmpRAMbuffer, len);
} else {
writeObjectToFileUsingId(filename, index, fileDoc);
}
} else
#endif
writeObjectToFileUsingId(filename, index, fileDoc);
if (persist) presetsModifiedTime = toki.second(); //unix time
if (bufferAllocated) releaseJSONBufferLock();
// this is a playlist or API
sObj.remove("o");
if (sObj[F("playlist")].isNull()) {
presetToSave = 0; // we will save API immediately
if (index < 251 && fileDoc) {
if (sObj["n"].isNull()) sObj["n"] = saveName;
writeObjectToFileUsingId(getName(index), index, fileDoc);
presetsModifiedTime = toki.second(); //unix time
updateFSInfo();
}
} else {
// store playlist
includeBri = true; // !sObj["on"].isNull();
playlistSave = true;
}
}
}
void deletePreset(byte index) {
StaticJsonDocument<24> empty;

View File

@ -8,7 +8,7 @@
*/
// version code in format yymmddb (b = daily build)
#define VERSION 2210041
#define VERSION 2210081
//uncomment this if you have a "my_config.h" file you'd like to use
//#define WLED_USE_MY_CONFIG