Skip to content

Commit

Permalink
Better fix for #4154
Browse files Browse the repository at this point in the history
  • Loading branch information
blazoncek committed Oct 13, 2024
1 parent a60231b commit 49f044e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
19 changes: 12 additions & 7 deletions wled00/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,20 +454,25 @@ bool deserializeState(JsonObject root, byte callMode, byte presetId)
handleSet(nullptr, apireq, false); // may set stateChanged
}

// applying preset (2 cases: a) API call includes all preset values ("pd"), b) API only specifies preset ID ("ps"))
// Applying preset from JSON API has 2 cases: a) "pd" AKA "preset direct" and b) "ps" AKA "preset select"
// a) "preset direct" can only be an integer value representing preset ID. "preset direct" assumes JSON API contains the rest of preset content (i.e. from UI call)
// "preset direct" JSON can contain "ps" API (i.e. call from UI to cycle presets) in such case stateChanged has to be false (i.e. no "win" or "seg" API)
// b) "preset select" can be cycling ("1~5~""), random ("r" or "1~5r"), ID, etc. value allowed from JSON API. This type of call assumes no state changing content in API call
byte presetToRestore = 0;
// a) already applied preset content (requires "seg" or "win" but will ignore the rest)
if (!root[F("pd")].isNull() && stateChanged) {
// a) already applied preset content (requires "seg" or "win" but will ignore the rest)
currentPreset = root[F("pd")] | currentPreset;
if (root["win"].isNull()) presetCycCurr = currentPreset; // otherwise it was set in handleSet() [set.cpp]
if (root["win"].isNull()) presetCycCurr = currentPreset; // otherwise presetCycCurr was set in handleSet() [set.cpp]
presetToRestore = currentPreset; // stateUpdated() will clear the preset, so we need to restore it after
DEBUG_PRINTF_P(PSTR("Preset direct: %d\n"), currentPreset);
} else if (!root["ps"].isNull()) {
ps = presetCycCurr;
if (root["win"].isNull() && getVal(root["ps"], &ps, 0, 0) && ps > 0 && ps < 251 && ps != currentPreset) {
// we have "ps" call (i.e. from button or external API call) or "pd" that includes "ps" (i.e. from UI call)
if (root["win"].isNull() && getVal(root["ps"], &presetCycCurr, 0, 0) && presetCycCurr > 0 && presetCycCurr < 251 && presetCycCurr != currentPreset) {
DEBUG_PRINTF_P(PSTR("Preset select: %d\n"), presetCycCurr);
// b) preset ID only or preset that does not change state (use embedded cycling limits if they exist in getVal())
applyPreset(ps, callMode); // async load from file system (only preset ID was specified)
applyPreset(presetCycCurr, callMode); // async load from file system (only preset ID was specified)
return stateResponse;
}
} else presetCycCurr = currentPreset; // restore presetCycCurr
}

JsonObject playlist = root[F("playlist")];
Expand Down
4 changes: 2 additions & 2 deletions wled00/presets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void initPresetsFile()
bool applyPresetFromPlaylist(byte index)
{
DEBUG_PRINTF_P(PSTR("Request to apply preset: %d\n"), index);
presetToApply = presetCycCurr = index;
presetToApply = index;
callModeToApply = CALL_MODE_DIRECT_CHANGE;
return true;
}
Expand All @@ -127,7 +127,7 @@ bool applyPreset(byte index, byte callMode)
{
unloadPlaylist(); // applying a preset unloads the playlist (#3827)
DEBUG_PRINTF_P(PSTR("Request to apply preset: %u\n"), index);
presetToApply = presetCycCurr = index;
presetToApply = index;
callModeToApply = callMode;
return true;
}
Expand Down

0 comments on commit 49f044e

Please sign in to comment.