From acfbe890b316cd0242c76e4a3ce46089eed7cf1b Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Tue, 16 Apr 2024 13:25:21 +0200 Subject: [PATCH] suspend playlist engine while auto-change is active * adding suspendPlaylist() to playlist engine code * autoplaylist usermod calls suspendPlaylist() before switching to another preset * fix a potential overflow on `lfc` (uint8_t -> uint16_t) --- .../usermod_v2_auto_playlist.h | 5 ++--- wled00/fcn_declare.h | 1 + wled00/playlist.cpp | 12 ++++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/usermods/usermod_v2_auto_playlist/usermod_v2_auto_playlist.h b/usermods/usermod_v2_auto_playlist/usermod_v2_auto_playlist.h index afe5852f..4ac05c24 100644 --- a/usermods/usermod_v2_auto_playlist/usermod_v2_auto_playlist.h +++ b/usermods/usermod_v2_auto_playlist/usermod_v2_auto_playlist.h @@ -113,7 +113,7 @@ class AutoPlaylistUsermod : public Usermod { energy /= ENERGY_SCALE; // scale down so we get 0 sometimes - uint8_t lfc = float(fftResult[0]) * fftDeScaler[0] / 0.85f; // might as well undo pink noise here too. + uint16_t lfc = float(fftResult[0]) * fftDeScaler[0] / 0.85f; // might as well undo pink noise here too. uint16_t zcr = *(uint16_t*)um_data->u_data[11]; // WLED-MM/TroyHacks: Calculate the long- and short-running averages @@ -244,8 +244,7 @@ class AutoPlaylistUsermod : public Usermod { // go into freefall - this logic stops that from triggering right // after change_lockout. Better for smaller change_lockout values. - // SH7: this method is sub-optimal, as its interfering with the "playlist" engine - // we shoud find a better method for triggering playlist changes + suspendPlaylist(); // suspend the playlist engine before changing to another preset applyPreset(newpreset); #ifdef USERMOD_AUTO_PLAYLIST_DEBUG diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index dcf4019e..a818d3fa 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -208,6 +208,7 @@ void _overlayAnalogCountdown(); void _overlayAnalogClock(); //playlist.cpp +void suspendPlaylist(); // WLEDMM support function for auto playlist usermod void shufflePlaylist(); void unloadPlaylist(); int16_t loadPlaylist(JsonObject playlistObject, byte presetId = 0); diff --git a/wled00/playlist.cpp b/wled00/playlist.cpp index 8a6227e1..236cc324 100644 --- a/wled00/playlist.cpp +++ b/wled00/playlist.cpp @@ -41,6 +41,12 @@ void shufflePlaylist() { DEBUG_PRINTLN(F("Playlist shuffle.")); } +// WLEDMM supporting function for auto_playlist usermod +// prevents the active playlist from progressing (until it gets unloaded) +static bool playlistSuspended = false; +void suspendPlaylist() { + playlistSuspended = true; +} void unloadPlaylist() { if (playlistEntries != nullptr) { @@ -49,6 +55,7 @@ void unloadPlaylist() { } currentPlaylist = playlistIndex = -1; playlistLen = playlistEntryDur = playlistOptions = 0; + playlistSuspended = false; // WLEDMM DEBUG_PRINTLN(F("Playlist unloaded.")); } @@ -125,6 +132,11 @@ void handlePlaylist() { // if fileDoc is not null JSON buffer is in use so just quit if (currentPlaylist < 0 || playlistEntries == nullptr || fileDoc != nullptr) return; + if (playlistSuspended) { // WLEDMM + if (millis() - presetCycledTime > (100*playlistEntryDur)) presetCycledTime = millis(); // keep updating timer + return; // but don't progress to next extry, and don't shuffle + } + if (millis() - presetCycledTime > (100*playlistEntryDur)) { presetCycledTime = millis(); if (bri == 0 || nightlightActive) return;