Merge pull request #1724 from blazoncek/playlist-fix
Playlist handling.
This commit is contained in:
commit
ba4c3e3852
@ -149,6 +149,7 @@ void _overlayCronixie();
|
|||||||
void _drawOverlayCronixie();
|
void _drawOverlayCronixie();
|
||||||
|
|
||||||
//playlist.cpp
|
//playlist.cpp
|
||||||
|
void unloadPlaylist();
|
||||||
void loadPlaylist(JsonObject playlistObject);
|
void loadPlaylist(JsonObject playlistObject);
|
||||||
void handlePlaylist();
|
void handlePlaylist();
|
||||||
|
|
||||||
|
@ -279,7 +279,8 @@ 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);
|
||||||
|
noNotification = true; //do not notify both for this request and the first playlist entry
|
||||||
}
|
}
|
||||||
|
|
||||||
colorUpdated(noNotification ? NOTIFIER_CALL_MODE_NO_NOTIFY : NOTIFIER_CALL_MODE_DIRECT_CHANGE);
|
colorUpdated(noNotification ? NOTIFIER_CALL_MODE_NO_NOTIFY : NOTIFIER_CALL_MODE_DIRECT_CHANGE);
|
||||||
|
@ -30,6 +30,7 @@ void toggleOnOff()
|
|||||||
{
|
{
|
||||||
briLast = bri;
|
briLast = bri;
|
||||||
bri = 0;
|
bri = 0;
|
||||||
|
unloadPlaylist();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,19 +10,42 @@ 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;
|
byte playlistLen;
|
||||||
int8_t playlistIndex = -1;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void unloadPlaylist() {
|
||||||
|
if (playlistEntries != nullptr) {
|
||||||
|
delete[] playlistEntries;
|
||||||
|
playlistEntries = nullptr;
|
||||||
|
}
|
||||||
|
currentPlaylist = playlistIndex = -1;
|
||||||
|
playlistLen = playlistEntryDur = 0;
|
||||||
|
}
|
||||||
|
|
||||||
void loadPlaylist(JsonObject playlistObj) {
|
void loadPlaylist(JsonObject playlistObj) {
|
||||||
delete playlistEntries;
|
unloadPlaylist();
|
||||||
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 +95,28 @@ 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
|
unloadPlaylist();
|
||||||
currentPlaylist = -1;
|
|
||||||
delete playlistEntries;
|
|
||||||
playlistEntries = nullptr;
|
|
||||||
if (playlistEndPreset) applyPreset(playlistEndPreset);
|
if (playlistEndPreset) applyPreset(playlistEndPreset);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (playlistRepeat > 1) playlistRepeat--;
|
// 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PlaylistEntry* entries = reinterpret_cast<PlaylistEntry*>(playlistEntries);
|
PlaylistEntry* entries = reinterpret_cast<PlaylistEntry*>(playlistEntries);
|
||||||
|
Loading…
Reference in New Issue
Block a user