forked from wled/WLED
-
-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathpresets.cpp
More file actions
403 lines (350 loc) · 15.7 KB
/
presets.cpp
File metadata and controls
403 lines (350 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include "wled.h"
/*
* Methods to handle saving and loading presets to/from the filesystem
*/
#ifdef ARDUINO_ARCH_ESP32
static char *tmpRAMbuffer = nullptr;
#endif
static volatile byte presetToApply = 0;
static volatile byte callModeToApply = 0;
static volatile byte presetToSave = 0;
static volatile int8_t saveLedmap = -1;
#define QLOAD_BUFFER 12 // string needed for quickload // WLEDMM 9->12 to prevent crashing with unicode
#define FNAME_BUFFER 32 // string needed for saveName
static char quickLoad[QLOAD_BUFFER+1] = {'\0'}; // 1 extra byte for '\0'
static char saveName[FNAME_BUFFER+1] = {'\0'}; // 1 extra byte for '\0'
static bool includeBri = true, segBounds = true, selectedOnly = false, playlistSave = false;
static const char *getFileName(bool persist = true) {
return persist ? "/presets.json" : "/tmp.json";
}
bool presetsSavePending(void) { // WLEDMM true if presetToSave, playlistSave or saveLedmap
if (presetToSave > 0) return(true);
if (playlistSave == true) return(true);
if (saveLedmap >= 0) return(true);
return(false);
}
bool presetsActionPending(void) { // WLEDMM true if presetToApply, presetToSave, playlistSave or saveLedmap
if (presetToApply > 0) return(true);
if (presetToSave > 0) return(true);
if (playlistSave == true) return(true);
if (saveLedmap >= 0) return(true);
return(false);
}
static void doSaveState() {
bool persist = (presetToSave < 251);
const char *filename = getFileName(persist);
if (!requestJSONBufferLock(10)) return; // will set fileDoc // async write
// WLEDMM Acquire file mutex before writing presets.json or tmp.json
if (esp32SemTake(presetFileMux, 2500) != pdTRUE) {
USER_PRINTLN(F("doSaveState(): preset file busy, cannot write"));
releaseJSONBufferLock();
return;
}
// wait for strip to finish updating, accessing FS during sendout causes glitches
#ifdef ARDUINO_ARCH_ESP32
unsigned wait_start = millis();
while (strip.isUpdating() && (millis() - wait_start < 40)) delay(1); // wait max 40ms
#endif
initPresetsFile(); // just in case if someone deleted presets.json using /edit
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;
if (saveLedmap >= 0) sObj[F("ledmap")] = saveLedmap;
/*
#ifdef WLED_DEBUG
DEBUG_PRINTLN(F("Serialized preset"));
serializeJson(doc,Serial);
DEBUG_PRINTLN();
#endif
*/
#if defined(ARDUINO_ARCH_ESP32)
if (!persist) {
if (tmpRAMbuffer!=nullptr) p_free(tmpRAMbuffer);
size_t len = measureJson(*fileDoc) + 1;
DEBUG_PRINTLN(len);
// if possible use SPI RAM on ESP32
tmpRAMbuffer = (char*) p_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
esp32SemGive(presetFileMux); // Release file mutex
releaseJSONBufferLock();
updateFSInfo();
// clean up
saveLedmap = -1;
presetToSave = 0;
memset(saveName, '\0', sizeof(saveName));
memset(quickLoad,'\0', sizeof(quickLoad));
playlistSave = false;
}
bool getPresetName(byte index, String& name)
{
if (!requestJSONBufferLock(19)) return false;
bool presetExists = false;
if (readObjectFromFileUsingId(getFileName(), index, &doc))
{
JsonObject fdo = doc.as<JsonObject>();
if (fdo["n"]) {
name = (const char*)(fdo["n"]);
presetExists = true;
}
}
releaseJSONBufferLock();
return presetExists;
}
void initPresetsFile()
{
if (WLED_FS.exists(getFileName())) {
// treat an empty JSON file (e.g. "{}", "{ }") the same as a missing file:
// f.size() < 4 is the same threshold used in appendObjectToFile() to detect an uninitialized file
File f = WLED_FS.open(getFileName(), "r");
bool empty = f && f.size() < 4; // file does exist due to previous "if"
if (f) f.close();
if (empty) WLED_FS.remove(getFileName()); // remove the empty file so it can be recreated below
else return; // file not empty -> keep (nothing to init)
}
StaticJsonDocument<64> doc;
JsonObject sObj = doc.to<JsonObject>();
sObj.createNestedObject("0");
File f = WLED_FS.open(getFileName(), "w");
if (!f) {
errorFlag = ERR_FS_GENERAL;
return;
}
serializeJson(doc, f);
f.close();
}
bool applyPreset(byte index, byte callMode)
{
DEBUG_PRINT(F("Request to apply preset: "));
DEBUG_PRINTLN(index);
presetToApply = index;
callModeToApply = callMode;
return true;
}
// apply preset or fallback to a effect and palette if it doesn't exist
void applyPresetWithFallback(uint8_t index, uint8_t callMode, uint8_t effectID, uint8_t paletteID)
{
applyPreset(index, callMode);
//these two will be overwritten if preset exists in handlePresets()
effectCurrent = effectID;
effectPalette = paletteID;
}
void handlePresets()
{
if (presetToSave) {
doSaveState();
return;
}
if (presetToApply == 0 || fileDoc) return; // no preset waiting to apply, or JSON buffer is already allocated, return to loop until free
bool changePreset = false;
uint8_t tmpPreset = presetToApply; // store temporary since deserializeState() may call applyPreset()
uint8_t tmpMode = callModeToApply;
JsonObject fdo;
const char *filename = getFileName(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();
// begin WLEDMM specific
// loopTask (arduino main loop) sometimes runs on core #1
if ((core == 1) && (strncmp(pcTaskGetTaskName(NULL), "loopTask", 8) == 0)) {
DEBUG_PRINTF("[applyPreset] called from loopTask on core %d; forcing core = 0\n", (int)core);
core = 0;
}
// async_tcp (network requests) sometimes runs on core #0
if ((core == 0) && (strncmp(pcTaskGetTaskName(NULL), "async_tcp", 9) == 0)) {
DEBUG_PRINTF("[applyPreset] called from async_tcp on core %d; forcing core = 1\n", (int)core);
core = 1;
}
// end WLEDMM specific
#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
presetToApply = 0; //clear request for preset
callModeToApply = 0;
byte presetErrorFlag = ERR_NONE;
DEBUG_PRINT(F("Applying preset: "));
DEBUG_PRINTLN(tmpPreset);
bool haveLocked = false;
#if defined(ARDUINO_ARCH_ESP32) // WLEDMM we apply this workaround to all esp32 boards (S3 and classic esp32 included)
//#if defined(ARDUINO_ARCH_ESP32S2) || defined(ARDUINO_ARCH_ESP32C3)
// in case we are called from web UI, wait until strip.service() is done
if (!suspendStripService) { suspendStripService = true; haveLocked = true; } // only lock service if not locked already
unsigned long waitstart = millis();
while (strip.isServicing() && millis() - waitstart < FRAMETIME_FIXED) delay(1); // wait for effects to finish updating
strip.fill(BLACK); strip.trigger(); // experimental: set LEDs to black while new preset loads (shortly freezing effects, but starts clean. Still better than a short black-out.)
unsigned long start = millis();
while (strip.isUpdating() && millis() - start < FRAMETIME_FIXED) delay(1); // wait for strip to finish updating, accessing FS during sendout causes glitches // WLEDMM delay instead of yield
#endif
#ifdef ARDUINO_ARCH_ESP32
if (tmpPreset==255 && tmpRAMbuffer!=nullptr) {
deserializeJson(*fileDoc,tmpRAMbuffer);
if ((errorFlag == ERR_FS_PLOAD) || (errorFlag == ERR_JSON)) errorFlag = ERR_NONE; // WLEDMM only reset our own error
} else
#endif
{
presetErrorFlag = readObjectFromFileUsingId(filename, tmpPreset, fileDoc) ? ERR_NONE : ERR_FS_PLOAD;
if ((errorFlag == ERR_FS_PLOAD) || (errorFlag == ERR_JSON)) errorFlag = ERR_NONE; // WLEDMM only reset our own error
if (presetErrorFlag == ERR_FS_PLOAD) errorFlag = presetErrorFlag;
}
if (haveLocked) suspendStripService = false; // WLEDMM unlock effects after presets file was loaded
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() || !fdo["on"].isNull() || !fdo["bri"].isNull() || !fdo["nl"].isNull() || !fdo["ps"].isNull() || !fdo[F("playlist")].isNull()) changePreset = true;
if (!(tmpMode == CALL_MODE_BUTTON_PRESET && fdo["ps"].is<const char *>() && strchr(fdo["ps"].as<const char *>(),'~') != strrchr(fdo["ps"].as<const char *>(),'~')))
fdo.remove("ps"); // remove load request for presets to prevent recursive crash (if not called by button and contains preset cycling string "1~5~")
deserializeState(fdo, CALL_MODE_NO_NOTIFY, tmpPreset); // may change presetToApply by calling applyPreset()
}
if (!presetErrorFlag && tmpPreset < 255 && changePreset) currentPreset = tmpPreset;
#if defined(ARDUINO_ARCH_ESP32)
//Aircoookie recommended not to delete buffer
if (tmpPreset==255 && tmpRAMbuffer!=nullptr) {
p_free(tmpRAMbuffer);
tmpRAMbuffer = nullptr;
}
#endif
releaseJSONBufferLock(); // will also clear fileDoc
if (changePreset) notify(tmpMode); // force UDP notification
stateUpdated(tmpMode); // was colorUpdated() if anything breaks
updateInterfaces(tmpMode);
}
//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 sObj)
{
if (index == 0 || (index > 250 && index < 255)) return;
if (pname) strlcpy(saveName, pname, FNAME_BUFFER+1);
else {
if (sObj["n"].is<const char*>()) strlcpy(saveName, sObj["n"].as<const char*>(), FNAME_BUFFER+1);
else sprintf_P(saveName, PSTR("Preset %d"), index);
}
DEBUG_PRINT(F("Saving preset (")); DEBUG_PRINT(index); DEBUG_PRINT(F(") ")); DEBUG_PRINTLN(saveName);
auto oldpresetToSave = presetToSave; // for recovery in case that esp32SemTake(presetFileMux) fails
auto oldplaylistSave = playlistSave;
char oldQuickLoad[QLOAD_BUFFER+1];
strlcpy(oldQuickLoad, quickLoad, sizeof(oldQuickLoad));
presetToSave = index;
playlistSave = false;
if (sObj[F("ql")].is<const char*>()) strlcpy(quickLoad, sObj[F("ql")].as<const char*>(), sizeof(quickLoad)); // client limits QL to 2 chars, buffer for 12 bytes to allow encoded unicode
if (sObj.size()==0 || sObj["o"].isNull()) { // no "o" means not a playlist or custom API call, saving of state is async (not immediately)
includeBri = sObj["ib"].as<bool>() || sObj.size()==0 || index==255; // temporary preset needs brightness
segBounds = sObj["sb"].as<bool>() || sObj.size()==0 || index==255; // temporary preset needs bounds
selectedOnly = sObj[F("sc")].as<bool>();
saveLedmap = sObj[F("ledmap")] | -1;
} else {
// this is a playlist or API call
if (sObj[F("playlist")].isNull()) {
// we will save API call immediately (often causes presets.json corruption in the past)
// WLEDMM Acquire file mutex before writing presets.json, to prevent presets.json corruption
if (esp32SemTake(presetFileMux, 2500) != pdTRUE) {
USER_PRINTLN(F("savePreset(): preset file busy, cannot write"));
presetToSave = oldpresetToSave;
playlistSave = oldplaylistSave;
strlcpy(quickLoad, oldQuickLoad, sizeof(quickLoad));
return; // early exit, no change
}
presetToSave = 0;
if (index > 250 || !fileDoc) {
esp32SemGive(presetFileMux); // Release file mutex
presetToSave = oldpresetToSave; // bugfix: restore previous state on error exit
playlistSave = oldplaylistSave;
strlcpy(quickLoad, oldQuickLoad, sizeof(quickLoad));
return; // cannot save API calls to temporary preset (255)
}
sObj.remove("o");
sObj.remove("v");
sObj.remove("time");
sObj.remove(F("error"));
sObj.remove(F("psave"));
if (sObj["n"].isNull()) sObj["n"] = saveName;
// wait for strip to finish updating, accessing FS during sendout causes glitches
#ifdef ARDUINO_ARCH_ESP32
unsigned wait_start = millis();
while (strip.isUpdating() && (millis() - wait_start < 40)) delay(1); // wait max 40ms
#endif
initPresetsFile(); // just in case if someone deleted presets.json using /edit
writeObjectToFileUsingId(getFileName(index<255), index, fileDoc);
esp32SemGive(presetFileMux); // Release file mutex
presetsModifiedTime = toki.second(); //unix time
updateFSInfo();
} else {
// store playlist
// WARNING: playlist will be loaded in json.cpp after this call and will have repeat counter increased by 1
includeBri = true; // !sObj["on"].isNull();
playlistSave = true;
}
}
}
void deletePreset(byte index) {
// WLEDMM Acquire file mutex before writing presets.json, to prevent presets.json corruption
if (esp32SemTake(presetFileMux, 2500) != pdTRUE) {
USER_PRINTLN(F("deletePreset(): preset file busy, cannot write"));
return; // early exit, no change
}
StaticJsonDocument<24> empty;
writeObjectToFileUsingId(getFileName(), index, &empty);
esp32SemGive(presetFileMux); // Release file mutex
presetsModifiedTime = toki.second(); //unix time
updateFSInfo();
}