Skip to content

Commit 2c58a87

Browse files
authored
Merge pull request #4356 from blazoncek/json-cycle
Proper fix for #3605 & #4346
2 parents a426e93 + 039858d commit 2c58a87

3 files changed

Lines changed: 11 additions & 20 deletions

File tree

wled00/fcn_declare.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ void userLoop();
372372
//util.cpp
373373
int getNumVal(const String* req, uint16_t pos);
374374
void parseNumber(const char* str, byte* val, byte minv=0, byte maxv=255);
375-
bool getVal(JsonVariant elem, byte* val, byte minv=0, byte maxv=255);
375+
bool getVal(JsonVariant elem, byte* val, byte minv=0, byte maxv=255); // getVal supports inc/decrementing and random ("X~Y(r|~[w][-][Z])" form)
376376
bool getBoolVal(JsonVariant elem, bool dflt);
377377
bool updateVal(const char* req, const char* key, byte* val, byte minv=0, byte maxv=255);
378378
size_t printSetFormCheckbox(Print& settingsScript, const char* key, int val);

wled00/json.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -223,30 +223,17 @@ bool deserializeSegment(JsonObject elem, byte it, byte presetId)
223223
#endif
224224

225225
byte fx = seg.mode;
226-
byte last = strip.getModeCount();
227-
// partial fix for #3605
228-
if (!elem["fx"].isNull() && elem["fx"].is<const char*>()) {
229-
const char *tmp = elem["fx"].as<const char *>();
230-
if (strlen(tmp) > 3 && (strchr(tmp,'r') || strchr(tmp,'~') != strrchr(tmp,'~'))) last = 0; // we have "X~Y(r|[w]~[-])" form
231-
}
232-
// end fix
233-
if (getVal(elem["fx"], &fx, 0, last)) { //load effect ('r' random, '~' inc/dec, 0-255 exact value, 5~10r pick random between 5 & 10)
226+
if (getVal(elem["fx"], &fx, 0, strip.getModeCount())) {
234227
if (!presetId && currentPlaylist>=0) unloadPlaylist();
235228
if (fx != seg.mode) seg.setMode(fx, elem[F("fxdef")]);
236229
}
237230

238-
//getVal also supports inc/decrementing and random
239231
getVal(elem["sx"], &seg.speed);
240232
getVal(elem["ix"], &seg.intensity);
241233

242234
uint8_t pal = seg.palette;
243-
last = strip.getPaletteCount();
244-
if (!elem["pal"].isNull() && elem["pal"].is<const char*>()) {
245-
const char *tmp = elem["pal"].as<const char *>();
246-
if (strlen(tmp) > 3 && (strchr(tmp,'r') || strchr(tmp,'~') != strrchr(tmp,'~'))) last = 0; // we have "X~Y(r|[w]~[-])" form
247-
}
248235
if (seg.getLightCapabilities() & 1) { // ignore palette for White and On/Off segments
249-
if (getVal(elem["pal"], &pal, 0, last)) seg.setPalette(pal);
236+
if (getVal(elem["pal"], &pal, 0, strip.getPaletteCount())) seg.setPalette(pal);
250237
}
251238

252239
getVal(elem["c1"], &seg.custom1);
@@ -467,7 +454,7 @@ bool deserializeState(JsonObject root, byte callMode, byte presetId)
467454
DEBUG_PRINTF_P(PSTR("Preset direct: %d\n"), currentPreset);
468455
} else if (!root["ps"].isNull()) {
469456
// we have "ps" call (i.e. from button or external API call) or "pd" that includes "ps" (i.e. from UI call)
470-
if (root["win"].isNull() && getVal(root["ps"], &presetCycCurr, 0, 0) && presetCycCurr > 0 && presetCycCurr < 251 && presetCycCurr != currentPreset) {
457+
if (root["win"].isNull() && getVal(root["ps"], &presetCycCurr, 1, 250) && presetCycCurr > 0 && presetCycCurr < 251 && presetCycCurr != currentPreset) {
471458
DEBUG_PRINTF_P(PSTR("Preset select: %d\n"), presetCycCurr);
472459
// b) preset ID only or preset that does not change state (use embedded cycling limits if they exist in getVal())
473460
applyPreset(presetCycCurr, callMode); // async load from file system (only preset ID was specified)

wled00/util.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,20 @@ void parseNumber(const char* str, byte* val, byte minv, byte maxv)
5252
*val = atoi(str);
5353
}
5454

55-
55+
//getVal supports inc/decrementing and random ("X~Y(r|~[w][-][Z])" form)
5656
bool getVal(JsonVariant elem, byte* val, byte vmin, byte vmax) {
5757
if (elem.is<int>()) {
5858
if (elem < 0) return false; //ignore e.g. {"ps":-1}
5959
*val = elem;
6060
return true;
6161
} else if (elem.is<const char*>()) {
6262
const char* str = elem;
63-
size_t len = strnlen(str, 12);
64-
if (len == 0 || len > 10) return false;
63+
size_t len = strnlen(str, 14);
64+
if (len == 0 || len > 12) return false;
65+
// fix for #3605 & #4346
66+
// ignore vmin and vmax and use as specified in API
67+
if (len > 3 && (strchr(str,'r') || strchr(str,'~') != strrchr(str,'~'))) vmax = vmin = 0; // we have "X~Y(r|~[w][-][Z])" form
68+
// end fix
6569
parseNumber(str, val, vmin, vmax);
6670
return true;
6771
}

0 commit comments

Comments
 (0)