2020-11-11 23:48:14 +01:00
|
|
|
#include "wled.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handles playlists, timed sequences of presets
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct PlaylistEntry {
|
|
|
|
uint8_t preset;
|
|
|
|
uint16_t dur;
|
|
|
|
uint16_t tr;
|
|
|
|
} ple;
|
|
|
|
|
2021-06-07 20:44:20 +02:00
|
|
|
bool playlistEndless = false;
|
|
|
|
int8_t playlistRepeat = 1;
|
|
|
|
byte playlistEndPreset = 0;
|
|
|
|
PlaylistEntry *playlistEntries = nullptr;
|
|
|
|
byte playlistLen;
|
|
|
|
int8_t playlistIndex = -1;
|
|
|
|
uint16_t playlistEntryDur = 0;
|
2020-11-11 23:48:14 +01:00
|
|
|
|
|
|
|
|
2021-02-05 17:36:53 +01:00
|
|
|
void shufflePlaylist() {
|
2021-05-11 14:54:03 +02:00
|
|
|
int currentIndex = playlistLen;
|
2021-06-07 20:44:20 +02:00
|
|
|
PlaylistEntry temporaryValue;
|
2021-02-05 17:36:53 +01:00
|
|
|
|
|
|
|
// While there remain elements to shuffle...
|
|
|
|
while (currentIndex--) {
|
|
|
|
// Pick a random element...
|
2021-05-11 14:54:03 +02:00
|
|
|
int randomIndex = random(0, currentIndex);
|
2021-02-05 17:36:53 +01:00
|
|
|
// And swap it with the current element.
|
2021-06-07 20:44:20 +02:00
|
|
|
temporaryValue = playlistEntries[currentIndex];
|
|
|
|
playlistEntries[currentIndex] = playlistEntries[randomIndex];
|
|
|
|
playlistEntries[randomIndex] = temporaryValue;
|
2021-02-05 17:36:53 +01:00
|
|
|
}
|
2021-06-07 20:44:20 +02:00
|
|
|
DEBUG_PRINTLN(F("Playlist shuffle."));
|
2021-02-05 17:36:53 +01:00
|
|
|
}
|
|
|
|
|
2021-02-28 15:34:18 +01:00
|
|
|
void unloadPlaylist() {
|
|
|
|
if (playlistEntries != nullptr) {
|
|
|
|
delete[] playlistEntries;
|
|
|
|
playlistEntries = nullptr;
|
|
|
|
}
|
|
|
|
currentPlaylist = playlistIndex = -1;
|
|
|
|
playlistLen = playlistEntryDur = 0;
|
2021-06-07 20:44:20 +02:00
|
|
|
DEBUG_PRINTLN(F("Playlist unloaded."));
|
2021-02-28 15:34:18 +01:00
|
|
|
}
|
|
|
|
|
2020-11-11 23:48:14 +01:00
|
|
|
void loadPlaylist(JsonObject playlistObj) {
|
2021-02-28 15:34:18 +01:00
|
|
|
unloadPlaylist();
|
|
|
|
|
2020-11-11 23:48:14 +01:00
|
|
|
JsonArray presets = playlistObj["ps"];
|
|
|
|
playlistLen = presets.size();
|
|
|
|
if (playlistLen == 0) return;
|
|
|
|
if (playlistLen > 100) playlistLen = 100;
|
2021-06-07 20:44:20 +02:00
|
|
|
playlistEntries = new PlaylistEntry[playlistLen];
|
|
|
|
if (playlistEntries == nullptr) return;
|
2020-11-11 23:48:14 +01:00
|
|
|
|
|
|
|
byte it = 0;
|
|
|
|
for (int ps : presets) {
|
|
|
|
if (it >= playlistLen) break;
|
2021-06-07 20:44:20 +02:00
|
|
|
playlistEntries[it].preset = ps;
|
2020-11-11 23:48:14 +01:00
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
it = 0;
|
|
|
|
JsonArray durations = playlistObj["dur"];
|
|
|
|
if (durations.isNull()) {
|
2021-06-07 20:44:20 +02:00
|
|
|
playlistEntries[0].dur = playlistObj["dur"] | 100;
|
2020-11-11 23:48:14 +01:00
|
|
|
it = 1;
|
|
|
|
} else {
|
|
|
|
for (int dur : durations) {
|
|
|
|
if (it >= playlistLen) break;
|
2021-06-07 20:44:20 +02:00
|
|
|
playlistEntries[it].dur = dur;
|
2020-11-11 23:48:14 +01:00
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
2021-06-07 20:44:20 +02:00
|
|
|
for (int i = it; i < playlistLen; i++) playlistEntries[i].dur = playlistEntries[it -1].dur;
|
2020-11-11 23:48:14 +01:00
|
|
|
|
|
|
|
it = 0;
|
2021-04-29 17:44:31 +02:00
|
|
|
JsonArray tr = playlistObj[F("transition")];
|
2020-11-11 23:48:14 +01:00
|
|
|
if (tr.isNull()) {
|
2021-06-07 20:44:20 +02:00
|
|
|
playlistEntries[0].tr = playlistObj[F("transition")] | (transitionDelay / 100);
|
2020-11-11 23:48:14 +01:00
|
|
|
it = 1;
|
|
|
|
} else {
|
|
|
|
for (int transition : tr) {
|
|
|
|
if (it >= playlistLen) break;
|
2021-06-07 20:44:20 +02:00
|
|
|
playlistEntries[it].tr = transition;
|
2020-11-11 23:48:14 +01:00
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
2021-06-07 20:44:20 +02:00
|
|
|
for (int i = it; i < playlistLen; i++) playlistEntries[i].tr = playlistEntries[it -1].tr;
|
2020-11-11 23:48:14 +01:00
|
|
|
|
|
|
|
playlistRepeat = playlistObj[F("repeat")] | 0;
|
|
|
|
playlistEndPreset = playlistObj[F("end")] | 0;
|
|
|
|
|
2021-06-07 20:44:20 +02:00
|
|
|
if (playlistRepeat <= 0) playlistRepeat--; // make it endless (-2 == endless & random)
|
2021-06-07 14:13:21 +02:00
|
|
|
|
2020-11-11 23:48:14 +01:00
|
|
|
currentPlaylist = 0; //TODO here we need the preset ID where the playlist is saved
|
2021-06-07 20:44:20 +02:00
|
|
|
DEBUG_PRINTLN(F("Playlist loaded."));
|
2020-11-11 23:48:14 +01:00
|
|
|
}
|
|
|
|
|
2021-02-05 17:36:53 +01:00
|
|
|
|
|
|
|
void handlePlaylist() {
|
2020-11-11 23:48:14 +01:00
|
|
|
if (currentPlaylist < 0 || playlistEntries == nullptr || presetCyclingEnabled) return;
|
2021-06-07 20:44:20 +02:00
|
|
|
|
2021-02-05 17:36:53 +01:00
|
|
|
if (millis() - presetCycledTime > (100*playlistEntryDur)) {
|
2020-11-11 23:48:14 +01:00
|
|
|
presetCycledTime = millis();
|
|
|
|
if (bri == 0 || nightlightActive) return;
|
|
|
|
|
2021-02-05 17:36:53 +01:00
|
|
|
++playlistIndex %= playlistLen; // -1 at 1st run (limit to playlistLen)
|
|
|
|
|
|
|
|
if (!playlistRepeat && !playlistIndex) { //stop if repeat == 0 and restart of playlist
|
2021-03-13 22:46:08 +01:00
|
|
|
unloadPlaylist();
|
2021-02-05 17:36:53 +01:00
|
|
|
if (playlistEndPreset) applyPreset(playlistEndPreset);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// playlist roll-over
|
|
|
|
if (!playlistIndex) {
|
2021-06-07 20:44:20 +02:00
|
|
|
// 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
|
2021-02-05 17:36:53 +01:00
|
|
|
shufflePlaylist(); // shuffle playlist and start over
|
2020-11-11 23:48:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonTransitionOnce = true;
|
2021-06-07 20:44:20 +02:00
|
|
|
transitionDelayTemp = playlistEntries[playlistIndex].tr * 100;
|
|
|
|
playlistEntryDur = playlistEntries[playlistIndex].dur;
|
2020-11-11 23:48:14 +01:00
|
|
|
if (playlistEntryDur == 0) playlistEntryDur = 10;
|
2021-06-07 20:44:20 +02:00
|
|
|
applyPreset(playlistEntries[playlistIndex].preset);
|
2020-11-11 23:48:14 +01:00
|
|
|
}
|
2021-02-05 17:36:53 +01:00
|
|
|
}
|