- unloading playlist on playlist apply
- optimisations in handlePlaylist()
This commit is contained in:
Blaz Kristan 2021-06-07 20:44:20 +02:00
parent cbf3ae4db4
commit c58ad64a28
3 changed files with 32 additions and 36 deletions

View File

@ -131,8 +131,6 @@ void deserializeSegment(JsonObject elem, byte it)
effectSpeed = elem[F("sx")] | effectSpeed;
effectIntensity = elem[F("ix")] | effectIntensity;
effectPalette = elem["pal"] | effectPalette;
// it may be a good idea to also stop playlist if effect has changed
unloadPlaylist();
} else { //permanent
byte fx = elem["fx"] | seg.mode;
if (fx != seg.mode && fx < strip.getModeCount()) strip.setMode(id, fx);

View File

@ -10,29 +10,29 @@ typedef struct PlaylistEntry {
uint16_t tr;
} ple;
bool playlistEndless = false;
int8_t playlistRepeat = 1;
byte playlistEndPreset = 0;
byte *playlistEntries = nullptr;
byte playlistLen;
int8_t playlistIndex = -1;
uint16_t playlistEntryDur = 0;
bool playlistEndless = false;
int8_t playlistRepeat = 1;
byte playlistEndPreset = 0;
PlaylistEntry *playlistEntries = nullptr;
byte playlistLen;
int8_t playlistIndex = -1;
uint16_t playlistEntryDur = 0;
void shufflePlaylist() {
int currentIndex = playlistLen;
PlaylistEntry temporaryValue, *entries = reinterpret_cast<PlaylistEntry*>(playlistEntries);
PlaylistEntry temporaryValue;
// While there remain elements to shuffle...
while (currentIndex--) {
// Pick a random element...
int randomIndex = random(0, currentIndex);
// And swap it with the current element.
temporaryValue = entries[currentIndex];
entries[currentIndex] = entries[randomIndex];
entries[randomIndex] = temporaryValue;
temporaryValue = playlistEntries[currentIndex];
playlistEntries[currentIndex] = playlistEntries[randomIndex];
playlistEntries[randomIndex] = temporaryValue;
}
DEBUG_PRINTLN(F("Playlist shuffle."));
}
void unloadPlaylist() {
@ -42,6 +42,7 @@ void unloadPlaylist() {
}
currentPlaylist = playlistIndex = -1;
playlistLen = playlistEntryDur = 0;
DEBUG_PRINTLN(F("Playlist unloaded."));
}
void loadPlaylist(JsonObject playlistObj) {
@ -51,58 +52,57 @@ void loadPlaylist(JsonObject playlistObj) {
playlistLen = presets.size();
if (playlistLen == 0) return;
if (playlistLen > 100) playlistLen = 100;
uint16_t dataSize = sizeof(ple) * playlistLen;
playlistEntries = new byte[dataSize];
PlaylistEntry* entries = reinterpret_cast<PlaylistEntry*>(playlistEntries);
playlistEntries = new PlaylistEntry[playlistLen];
if (playlistEntries == nullptr) return;
byte it = 0;
for (int ps : presets) {
if (it >= playlistLen) break;
entries[it].preset = ps;
playlistEntries[it].preset = ps;
it++;
}
it = 0;
JsonArray durations = playlistObj["dur"];
if (durations.isNull()) {
entries[0].dur = playlistObj["dur"] | 100;
playlistEntries[0].dur = playlistObj["dur"] | 100;
it = 1;
} else {
for (int dur : durations) {
if (it >= playlistLen) break;
entries[it].dur = dur;
playlistEntries[it].dur = dur;
it++;
}
}
for (int i = it; i < playlistLen; i++) entries[i].dur = entries[it -1].dur;
for (int i = it; i < playlistLen; i++) playlistEntries[i].dur = playlistEntries[it -1].dur;
it = 0;
JsonArray tr = playlistObj[F("transition")];
if (tr.isNull()) {
entries[0].tr = playlistObj[F("transition")] | (transitionDelay / 100);
playlistEntries[0].tr = playlistObj[F("transition")] | (transitionDelay / 100);
it = 1;
} else {
for (int transition : tr) {
if (it >= playlistLen) break;
entries[it].tr = transition;
playlistEntries[it].tr = transition;
it++;
}
}
for (int i = it; i < playlistLen; i++) entries[i].tr = entries[it -1].tr;
for (int i = it; i < playlistLen; i++) playlistEntries[i].tr = playlistEntries[it -1].tr;
playlistRepeat = playlistObj[F("repeat")] | 0;
playlistEndPreset = playlistObj[F("end")] | 0;
playlistEndless = playlistRepeat < 1;
if (playlistEndless && playlistRepeat==0) playlistRepeat = 1; // it will never decrement
if (playlistRepeat <= 0) playlistRepeat--; // make it endless (-2 == endless & random)
currentPlaylist = 0; //TODO here we need the preset ID where the playlist is saved
DEBUG_PRINTLN(F("Playlist loaded."));
}
void handlePlaylist() {
if (currentPlaylist < 0 || playlistEntries == nullptr || presetCyclingEnabled) return;
if (millis() - presetCycledTime > (100*playlistEntryDur)) {
presetCycledTime = millis();
if (bri == 0 || nightlightActive) return;
@ -116,19 +116,17 @@ void handlePlaylist() {
}
// playlist roll-over
if (!playlistIndex) {
if (!playlistEndless) playlistRepeat--; // decrease repeat count on each index reset if not an endless playlist
if (playlistRepeat < 0) {// playlistRepeat < 0 => endless loop with shuffling presets
// playlistRepeat < 0 => endless loop
if (playlistRepeat > 0) playlistRepeat--; // decrease repeat count on each index reset if not an endless playlist
if (playlistRepeat < -1) { // playlistRepeat < -1 => with shuffling presets
shufflePlaylist(); // shuffle playlist and start over
}
}
PlaylistEntry* entries = reinterpret_cast<PlaylistEntry*>(playlistEntries);
jsonTransitionOnce = true;
transitionDelayTemp = entries[playlistIndex].tr * 100;
applyPreset(entries[playlistIndex].preset);
playlistEntryDur = entries[playlistIndex].dur;
transitionDelayTemp = playlistEntries[playlistIndex].tr * 100;
playlistEntryDur = playlistEntries[playlistIndex].dur;
if (playlistEntryDur == 0) playlistEntryDur = 10;
applyPreset(playlistEntries[playlistIndex].preset);
}
}

View File

@ -8,7 +8,7 @@
*/
// version code in format yymmddb (b = daily build)
#define VERSION 2106071
#define VERSION 2106072
//uncomment this if you have a "my_config.h" file you'd like to use
//#define WLED_USE_MY_CONFIG