From 9c295d188408aa065c82af931511743d1f2f922e Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Mon, 4 Oct 2021 20:22:04 +0200 Subject: [PATCH] Implemented temporary presets. --- wled00/fcn_declare.h | 2 ++ wled00/presets.cpp | 24 ++++++++++++++---------- wled00/wled.h | 2 +- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index f77c975b..7d668b8c 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -184,7 +184,9 @@ void handlePlaylist(); //presets.cpp bool applyPreset(byte index, byte callMode = CALL_MODE_DIRECT_CHANGE); +inline bool applyTemporaryPreset() {return applyPreset(255);}; void savePreset(byte index, bool persist = true, const char* pname = nullptr, JsonObject saveobj = JsonObject()); +inline void saveTemporaryPreset() {savePreset(255, false);}; void deletePreset(byte index); //set.cpp diff --git a/wled00/presets.cpp b/wled00/presets.cpp index 8ffc96b1..7ba2ea84 100644 --- a/wled00/presets.cpp +++ b/wled00/presets.cpp @@ -7,8 +7,11 @@ bool applyPreset(byte index, byte callMode) { if (index == 0) return false; + + const char *filename = index < 255 ? "/presets.json" : "/tmp.json"; + if (fileDoc) { - errorFlag = readObjectFromFileUsingId("/presets.json", index, fileDoc) ? ERR_NONE : ERR_FS_PLOAD; + errorFlag = readObjectFromFileUsingId(filename, index, fileDoc) ? ERR_NONE : ERR_FS_PLOAD; JsonObject fdo = fileDoc->as(); if (fdo["ps"] == index) fdo.remove("ps"); //remove load request for same presets to prevent recursive crash #ifdef WLED_DEBUG_FS @@ -18,7 +21,7 @@ bool applyPreset(byte index, byte callMode) } else { DEBUGFS_PRINTLN(F("Make read buf")); DynamicJsonDocument fDoc(JSON_BUFFER_SIZE); - errorFlag = readObjectFromFileUsingId("/presets.json", index, &fDoc) ? ERR_NONE : ERR_FS_PLOAD; + errorFlag = readObjectFromFileUsingId(filename, index, &fDoc) ? ERR_NONE : ERR_FS_PLOAD; JsonObject fdo = fDoc.as(); if (fdo["ps"] == index) fdo.remove("ps"); #ifdef WLED_DEBUG_FS @@ -28,19 +31,20 @@ bool applyPreset(byte index, byte callMode) } if (!errorFlag) { - currentPreset = index; + if (index < 255) currentPreset = index; return true; } return false; } -//persist=false is not currently honored void savePreset(byte index, bool persist, const char* pname, JsonObject saveobj) { - if (index == 0 || index > 250) return; + 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) { DEBUGFS_PRINTLN(F("Allocating saving buffer")); DynamicJsonDocument lDoc(JSON_BUFFER_SIZE); @@ -48,9 +52,9 @@ void savePreset(byte index, bool persist, const char* pname, JsonObject saveobj) if (pname) sObj["n"] = pname; DEBUGFS_PRINTLN(F("Save current state")); serializeState(sObj, true); - currentPreset = index; + if (persist) currentPreset = index; - writeObjectToFileUsingId("/presets.json", index, &lDoc); + writeObjectToFileUsingId(filename, index, &lDoc); } else { //from JSON API DEBUGFS_PRINTLN(F("Reuse recv buffer")); sObj.remove(F("psave")); @@ -59,7 +63,7 @@ void savePreset(byte index, bool persist, const char* pname, JsonObject saveobj) if (!sObj["o"]) { DEBUGFS_PRINTLN(F("Save current state")); serializeState(sObj, true, sObj["ib"], sObj["sb"]); - currentPreset = index; + if (persist) currentPreset = index; } sObj.remove("o"); sObj.remove("ib"); @@ -67,9 +71,9 @@ void savePreset(byte index, bool persist, const char* pname, JsonObject saveobj) sObj.remove(F("error")); sObj.remove(F("time")); - writeObjectToFileUsingId("/presets.json", index, fileDoc); + writeObjectToFileUsingId(filename, index, fileDoc); } - presetsModifiedTime = toki.second(); //unix time + if (persist) presetsModifiedTime = toki.second(); //unix time updateFSInfo(); } diff --git a/wled00/wled.h b/wled00/wled.h index 2fb422b0..3135e769 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2110022 +#define VERSION 2110041 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG