Async preset saving.
Minor bugfixes.
This commit is contained in:
parent
4fb44d98db
commit
7642f8d702
@ -4524,7 +4524,7 @@ uint16_t mode_perlinmove(void) {
|
|||||||
|
|
||||||
return FRAMETIME;
|
return FRAMETIME;
|
||||||
} // mode_perlinmove()
|
} // 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";
|
||||||
|
|
||||||
|
|
||||||
/////////////////////////
|
/////////////////////////
|
||||||
|
@ -399,7 +399,7 @@ function presetError(empty)
|
|||||||
if (bckstr.length > 10) hasBackup = true;
|
if (bckstr.length > 10) hasBackup = true;
|
||||||
} catch (e) {}
|
} 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)
|
if (empty)
|
||||||
cn += `You have no presets yet!`;
|
cn += `You have no presets yet!`;
|
||||||
else
|
else
|
||||||
@ -2240,7 +2240,7 @@ function saveP(i,pl)
|
|||||||
}
|
}
|
||||||
populatePresets();
|
populatePresets();
|
||||||
resetPUtil();
|
resetPUtil();
|
||||||
setTimeout(()=>{pmtLast=0; loadPresets();}, 500); // force reloading of presets
|
setTimeout(()=>{pmtLast=0; loadPresets();}, 750); // force reloading of presets
|
||||||
}
|
}
|
||||||
|
|
||||||
function testPl(i,bt) {
|
function testPl(i,bt) {
|
||||||
@ -2269,6 +2269,7 @@ function delP(i) {
|
|||||||
requestJson(obj);
|
requestJson(obj);
|
||||||
delete pJson[i];
|
delete pJson[i];
|
||||||
populatePresets();
|
populatePresets();
|
||||||
|
gId('putil').classList.add("staybot");
|
||||||
} else {
|
} else {
|
||||||
bt.style.color = "var(--c-r)";
|
bt.style.color = "var(--c-r)";
|
||||||
bt.innerHTML = "<i class='icons btn-icon'></i>Delete!";
|
bt.innerHTML = "<i class='icons btn-icon'></i>Delete!";
|
||||||
|
@ -135,7 +135,7 @@ void handleIR();
|
|||||||
void deserializeSegment(JsonObject elem, byte it, byte presetId = 0);
|
void deserializeSegment(JsonObject elem, byte it, byte presetId = 0);
|
||||||
bool deserializeState(JsonObject root, byte callMode = CALL_MODE_DIRECT_CHANGE, 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 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 serializeInfo(JsonObject root);
|
||||||
void serializeModeNames(JsonArray arr, const char *qstring);
|
void serializeModeNames(JsonArray arr, const char *qstring);
|
||||||
void serializeModeData(JsonObject root);
|
void serializeModeData(JsonObject root);
|
||||||
@ -192,6 +192,7 @@ void shufflePlaylist();
|
|||||||
void unloadPlaylist();
|
void unloadPlaylist();
|
||||||
int16_t loadPlaylist(JsonObject playlistObject, byte presetId = 0);
|
int16_t loadPlaylist(JsonObject playlistObject, byte presetId = 0);
|
||||||
void handlePlaylist();
|
void handlePlaylist();
|
||||||
|
void serializePlaylist(JsonObject obj);
|
||||||
|
|
||||||
//presets.cpp
|
//presets.cpp
|
||||||
void handlePresets();
|
void handlePresets();
|
||||||
|
1847
wled00/html_ui.h
1847
wled00/html_ui.h
File diff suppressed because it is too large
Load Diff
@ -481,7 +481,7 @@ void serializeSegment(JsonObject& root, Segment& seg, byte id, bool forPreset, b
|
|||||||
root["mp12"] = seg.map1D2D;
|
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) {
|
if (includeBri) {
|
||||||
root["on"] = (bri > 0);
|
root["on"] = (bri > 0);
|
||||||
@ -517,11 +517,10 @@ void serializeState(JsonObject root, bool forPreset, bool includeBri, bool segme
|
|||||||
|
|
||||||
root[F("mainseg")] = strip.getMainSegmentId();
|
root[F("mainseg")] = strip.getMainSegmentId();
|
||||||
|
|
||||||
bool selectedSegmentsOnly = root[F("sc")] | false;
|
|
||||||
JsonArray seg = root.createNestedArray("seg");
|
JsonArray seg = root.createNestedArray("seg");
|
||||||
for (size_t s = 0; s < strip.getMaxSegments(); s++) {
|
for (size_t s = 0; s < strip.getMaxSegments(); s++) {
|
||||||
if (s >= strip.getSegmentsNum()) {
|
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();
|
JsonObject seg0 = seg.createNestedObject();
|
||||||
seg0["stop"] = 0;
|
seg0["stop"] = 0;
|
||||||
continue;
|
continue;
|
||||||
@ -529,7 +528,7 @@ void serializeState(JsonObject root, bool forPreset, bool includeBri, bool segme
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Segment &sg = strip.getSegment(s);
|
Segment &sg = strip.getSegment(s);
|
||||||
if (!forPreset && selectedSegmentsOnly && !sg.isSelected()) continue;
|
if (forPreset && selectedSegmentsOnly && !sg.isSelected()) continue;
|
||||||
if (sg.isActive()) {
|
if (sg.isActive()) {
|
||||||
JsonObject seg0 = seg.createNestedObject();
|
JsonObject seg0 = seg.createNestedObject();
|
||||||
serializeSegment(seg0, sg, s, forPreset, segmentBounds);
|
serializeSegment(seg0, sg, s, forPreset, segmentBounds);
|
||||||
|
@ -149,3 +149,19 @@ void handlePlaylist() {
|
|||||||
applyPreset(playlistEntries[playlistIndex].preset);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -10,6 +10,70 @@ static char *tmpRAMbuffer = nullptr;
|
|||||||
|
|
||||||
static volatile byte presetToApply = 0;
|
static volatile byte presetToApply = 0;
|
||||||
static volatile byte callModeToApply = 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)
|
bool applyPreset(byte index, byte callMode)
|
||||||
{
|
{
|
||||||
@ -17,16 +81,16 @@ bool applyPreset(byte index, byte callMode)
|
|||||||
DEBUG_PRINTLN(index);
|
DEBUG_PRINTLN(index);
|
||||||
presetToApply = index;
|
presetToApply = index;
|
||||||
callModeToApply = callMode;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void handlePresets()
|
void handlePresets()
|
||||||
{
|
{
|
||||||
|
if (presetToSave) {
|
||||||
|
doSaveState();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
bool changePreset = false;
|
bool changePreset = false;
|
||||||
uint8_t tmpPreset = presetToApply; // store temporary since deserializeState() may call applyPreset()
|
uint8_t tmpPreset = presetToApply; // store temporary since deserializeState() may call applyPreset()
|
||||||
uint8_t tmpMode = callModeToApply;
|
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
|
if (tmpPreset == 0 || (fileDoc /*&& !force*/)) return; // JSON buffer already allocated and not force apply or no preset waiting
|
||||||
|
|
||||||
JsonObject fdo;
|
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
|
// allocate buffer
|
||||||
if (!requestJSONBufferLock(9)) return; // will also assign fileDoc
|
if (!requestJSONBufferLock(9)) return; // will also assign fileDoc
|
||||||
|
|
||||||
@ -138,67 +150,48 @@ void handlePresets()
|
|||||||
}
|
}
|
||||||
|
|
||||||
//called from handleSet(PS=) [network callback (fileDoc==nullptr), IR (irrational), deserializeState, UDP] and deserializeState() [network callback (filedoc!=nullptr)]
|
//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;
|
if (index == 0 || (index > 250 && index < 255)) return;
|
||||||
char tmp[12];
|
if (pname) strlcpy(saveName, pname, 33);
|
||||||
JsonObject sObj = saveobj;
|
else {
|
||||||
bool bufferAllocated = false;
|
if (sObj["n"].is<const char*>()) strlcpy(saveName, sObj["n"].as<const char*>(), 33);
|
||||||
|
else sprintf_P(saveName, PSTR("Preset %d"), index);
|
||||||
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 (sObj["n"].isNull() && pname == nullptr) {
|
|
||||||
sprintf_P(tmp, PSTR("Preset %d"), index);
|
|
||||||
sObj["n"] = tmp;
|
|
||||||
} else if (pname) sObj["n"] = pname;
|
|
||||||
|
|
||||||
sObj.remove(F("psave"));
|
DEBUG_PRINT(F("Saving preset (")); DEBUG_PRINT(index); DEBUG_PRINT(F(") ")); DEBUG_PRINTLN(saveName);
|
||||||
sObj.remove(F("v"));
|
|
||||||
|
|
||||||
if (!sObj["o"]) {
|
presetToSave = index;
|
||||||
DEBUGFS_PRINTLN(F("Serialize current state"));
|
playlistSave = false;
|
||||||
if (sObj["ib"].isNull() && sObj["sb"].isNull()) serializeState(sObj, true);
|
if (sObj[F("ql")].is<const char*>()) strlcpy(quickLoad, sObj[F("ql")].as<const char*>(), 3); // only 2 chars for QL
|
||||||
else serializeState(sObj, true, sObj["ib"], sObj["sb"]);
|
sObj.remove("v");
|
||||||
if (persist) currentPreset = index;
|
|
||||||
}
|
|
||||||
sObj.remove("o");
|
|
||||||
sObj.remove("ib");
|
|
||||||
sObj.remove("sb");
|
|
||||||
sObj.remove(F("sc"));
|
|
||||||
sObj.remove(F("error"));
|
|
||||||
sObj.remove(F("time"));
|
sObj.remove(F("time"));
|
||||||
|
sObj.remove(F("error"));
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
sObj.remove(F("psave"));
|
||||||
if (index==255) {
|
if (sObj["o"].isNull()) { // "o" marks a playlist or manually entered API
|
||||||
if (tmpRAMbuffer!=nullptr) free(tmpRAMbuffer);
|
includeBri = sObj["ib"].as<bool>();
|
||||||
size_t len = measureJson(*fileDoc) + 1;
|
segBounds = sObj["sb"].as<bool>();
|
||||||
DEBUG_PRINTLN(len);
|
selectedOnly = sObj[F("sc")].as<bool>();
|
||||||
// if possible use SPI RAM on ESP32
|
sObj.remove("ib");
|
||||||
#ifdef WLED_USE_PSRAM
|
sObj.remove("sb");
|
||||||
if (psramFound())
|
sObj.remove(F("sc"));
|
||||||
tmpRAMbuffer = (char*) ps_malloc(len);
|
} else {
|
||||||
else
|
// this is a playlist or API
|
||||||
#endif
|
sObj.remove("o");
|
||||||
tmpRAMbuffer = (char*) malloc(len);
|
if (sObj[F("playlist")].isNull()) {
|
||||||
if (tmpRAMbuffer!=nullptr) {
|
presetToSave = 0; // we will save API immediately
|
||||||
serializeJson(*fileDoc, tmpRAMbuffer, len);
|
if (index < 251 && fileDoc) {
|
||||||
|
if (sObj["n"].isNull()) sObj["n"] = saveName;
|
||||||
|
writeObjectToFileUsingId(getName(index), index, fileDoc);
|
||||||
|
presetsModifiedTime = toki.second(); //unix time
|
||||||
|
updateFSInfo();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
writeObjectToFileUsingId(filename, index, fileDoc);
|
// store playlist
|
||||||
|
includeBri = true; // !sObj["on"].isNull();
|
||||||
|
playlistSave = true;
|
||||||
}
|
}
|
||||||
} else
|
}
|
||||||
#endif
|
|
||||||
writeObjectToFileUsingId(filename, index, fileDoc);
|
|
||||||
|
|
||||||
if (persist) presetsModifiedTime = toki.second(); //unix time
|
|
||||||
if (bufferAllocated) releaseJSONBufferLock();
|
|
||||||
updateFSInfo();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void deletePreset(byte index) {
|
void deletePreset(byte index) {
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// version code in format yymmddb (b = daily build)
|
// 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
|
//uncomment this if you have a "my_config.h" file you'd like to use
|
||||||
//#define WLED_USE_MY_CONFIG
|
//#define WLED_USE_MY_CONFIG
|
||||||
|
Loading…
Reference in New Issue
Block a user