Playlist handling.

This commit is contained in:
Blaz Kristan 2021-02-05 17:36:53 +01:00
parent 94941a7732
commit a1c2c04510
2 changed files with 84 additions and 26 deletions

View File

@ -279,7 +279,7 @@ bool deserializeState(JsonObject root)
JsonObject playlist = root[F("playlist")]; JsonObject playlist = root[F("playlist")];
if (!playlist.isNull()) { if (!playlist.isNull()) {
loadPlaylist(playlist); return stateResponse; loadPlaylist(playlist); //return stateResponse;
} }
colorUpdated(noNotification ? NOTIFIER_CALL_MODE_NO_NOTIFY : NOTIFIER_CALL_MODE_DIRECT_CHANGE); colorUpdated(noNotification ? NOTIFIER_CALL_MODE_NO_NOTIFY : NOTIFIER_CALL_MODE_DIRECT_CHANGE);

View File

@ -10,19 +10,73 @@ typedef struct PlaylistEntry {
uint16_t tr; uint16_t tr;
} ple; } ple;
byte playlistRepeat = 1; int8_t playlistRepeat = 1;
byte playlistEndPreset = 0; byte playlistEndPreset = 0;
byte *playlistEntries = nullptr;
uint8_t* playlistEntries; byte playlistLen;
int8_t playlistIndex = -1;
byte playlistLen;
int8_t playlistIndex = -1;
uint16_t playlistEntryDur = 0; uint16_t playlistEntryDur = 0;
void shufflePlaylist() {
int currentIndex = playlistLen, randomIndex;
PlaylistEntry temporaryValue, *entries = reinterpret_cast<PlaylistEntry*>(playlistEntries);
// While there remain elements to shuffle...
while (currentIndex--) {
// Pick a random element...
randomIndex = random(0, currentIndex);
// And swap it with the current element.
temporaryValue = entries[currentIndex];
entries[currentIndex] = entries[randomIndex];
entries[randomIndex] = temporaryValue;
}
}
/*
* The same thing as saving and loading playlist can be achieved using JSON API saved in a preset.
*
void deserializePlaylist() {
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DEBUG_PRINTLN(F("Reading playlist from /playlist.json..."));
if (!readObjectFromFile("/playlist.json", nullptr, &doc)) return; //if file does not exist just exit
JsonObject playlist = doc[F("playlist")];
if (!playlist.isNull()) loadPlaylist(playlist);
}
void serializePlaylist() {
DynamicJsonDocument doc(JSON_BUFFER_SIZE/8); // we don't need big buffer (>1k is ok)
DEBUG_PRINTLN(F("Writing playlist to /playlist.json..."));
PlaylistEntry* entries = reinterpret_cast<PlaylistEntry*>(playlistEntries);
JsonObject playlist = doc.createNestedObject(F("playlist"));
JsonArray ps = playlist.createNestedArray(F("ps"));
JsonArray dur = playlist.createNestedArray(F("dur"));
JsonArray tr = playlist.createNestedArray(F("transition"));
for (uint8_t i=0; i<playlistLen; i++) {
ps.add(entries[i].preset);
dur.add(entries[i].dur);
tr.add(entries[i].tr);
}
playlist[F("repeat")] = playlistRepeat; // TODO: this one is decreasing with each loop
playlist[F("end")] = playlistEndPreset;
File f = WLED_FS.open("/playlist.json", "w");
if (f) serializeJson(doc, f);
f.close();
}
*/
void loadPlaylist(JsonObject playlistObj) { void loadPlaylist(JsonObject playlistObj) {
delete playlistEntries; if (playlistEntries != nullptr) {delete[] playlistEntries; playlistEntries = nullptr;}
playlistIndex = -1; playlistEntryDur = 0; currentPlaylist = playlistIndex = -1; playlistEntryDur = 0;
JsonArray presets = playlistObj["ps"]; JsonArray presets = playlistObj["ps"];
playlistLen = presets.size(); playlistLen = presets.size();
if (playlistLen == 0) return; if (playlistLen == 0) return;
@ -72,26 +126,30 @@ void loadPlaylist(JsonObject playlistObj) {
currentPlaylist = 0; //TODO here we need the preset ID where the playlist is saved currentPlaylist = 0; //TODO here we need the preset ID where the playlist is saved
} }
void handlePlaylist()
{ void handlePlaylist() {
if (currentPlaylist < 0 || playlistEntries == nullptr || presetCyclingEnabled) return; if (currentPlaylist < 0 || playlistEntries == nullptr || presetCyclingEnabled) return;
if (millis() - presetCycledTime > (100*playlistEntryDur)) if (millis() - presetCycledTime > (100*playlistEntryDur)) {
{
presetCycledTime = millis(); presetCycledTime = millis();
if (bri == 0 || nightlightActive) return; if (bri == 0 || nightlightActive) return;
playlistIndex++; ++playlistIndex %= playlistLen; // -1 at 1st run (limit to playlistLen)
if (playlistIndex >= playlistLen) {
playlistIndex = 0; if (!playlistRepeat && !playlistIndex) { //stop if repeat == 0 and restart of playlist
if (playlistRepeat == 1) { //stop currentPlaylist = -1;
currentPlaylist = -1; delete[] playlistEntries;
delete playlistEntries; playlistEntries = nullptr;
playlistEntries = nullptr; if (playlistEndPreset) applyPreset(playlistEndPreset);
if (playlistEndPreset) applyPreset(playlistEndPreset); return;
return; }
// playlist roll-over
if (!playlistIndex) {
if (playlistRepeat > 0) {// playlistRepeat < 0 => endless loop with shuffling presets
playlistRepeat--; // decrease repeat count on each index reset
} else {
shufflePlaylist(); // shuffle playlist and start over
} }
if (playlistRepeat > 1) playlistRepeat--;
} }
PlaylistEntry* entries = reinterpret_cast<PlaylistEntry*>(playlistEntries); PlaylistEntry* entries = reinterpret_cast<PlaylistEntry*>(playlistEntries);