Skip to content

Commit ba4c3e3

Browse files
authored
Merge pull request wled#1724 from blazoncek/playlist-fix
Playlist handling.
2 parents 26096bc + b8de36b commit ba4c3e3

File tree

4 files changed

+52
-24
lines changed

4 files changed

+52
-24
lines changed

wled00/fcn_declare.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ void _overlayCronixie();
149149
void _drawOverlayCronixie();
150150

151151
//playlist.cpp
152+
void unloadPlaylist();
152153
void loadPlaylist(JsonObject playlistObject);
153154
void handlePlaylist();
154155

wled00/json.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ bool deserializeState(JsonObject root)
279279

280280
JsonObject playlist = root[F("playlist")];
281281
if (!playlist.isNull()) {
282-
loadPlaylist(playlist); return stateResponse;
282+
loadPlaylist(playlist);
283+
noNotification = true; //do not notify both for this request and the first playlist entry
283284
}
284285

285286
colorUpdated(noNotification ? NOTIFIER_CALL_MODE_NO_NOTIFY : NOTIFIER_CALL_MODE_DIRECT_CHANGE);

wled00/led.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ void toggleOnOff()
3030
{
3131
briLast = bri;
3232
bri = 0;
33+
unloadPlaylist();
3334
}
3435
}
3536

wled00/playlist.cpp

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,42 @@ typedef struct PlaylistEntry {
1010
uint16_t tr;
1111
} ple;
1212

13-
byte playlistRepeat = 1;
14-
byte playlistEndPreset = 0;
13+
int8_t playlistRepeat = 1;
14+
byte playlistEndPreset = 0;
15+
byte *playlistEntries = nullptr;
16+
byte playlistLen;
17+
int8_t playlistIndex = -1;
18+
uint16_t playlistEntryDur = 0;
1519

16-
uint8_t* playlistEntries;
1720

18-
byte playlistLen;
19-
int8_t playlistIndex = -1;
21+
void shufflePlaylist() {
22+
int currentIndex = playlistLen, randomIndex;
2023

21-
uint16_t playlistEntryDur = 0;
24+
PlaylistEntry temporaryValue, *entries = reinterpret_cast<PlaylistEntry*>(playlistEntries);
25+
26+
// While there remain elements to shuffle...
27+
while (currentIndex--) {
28+
// Pick a random element...
29+
randomIndex = random(0, currentIndex);
30+
// And swap it with the current element.
31+
temporaryValue = entries[currentIndex];
32+
entries[currentIndex] = entries[randomIndex];
33+
entries[randomIndex] = temporaryValue;
34+
}
35+
}
36+
37+
void unloadPlaylist() {
38+
if (playlistEntries != nullptr) {
39+
delete[] playlistEntries;
40+
playlistEntries = nullptr;
41+
}
42+
currentPlaylist = playlistIndex = -1;
43+
playlistLen = playlistEntryDur = 0;
44+
}
2245

2346
void loadPlaylist(JsonObject playlistObj) {
24-
delete playlistEntries;
25-
playlistIndex = -1; playlistEntryDur = 0;
47+
unloadPlaylist();
48+
2649
JsonArray presets = playlistObj["ps"];
2750
playlistLen = presets.size();
2851
if (playlistLen == 0) return;
@@ -72,26 +95,28 @@ void loadPlaylist(JsonObject playlistObj) {
7295
currentPlaylist = 0; //TODO here we need the preset ID where the playlist is saved
7396
}
7497

75-
void handlePlaylist()
76-
{
98+
99+
void handlePlaylist() {
77100
if (currentPlaylist < 0 || playlistEntries == nullptr || presetCyclingEnabled) return;
78101

79-
if (millis() - presetCycledTime > (100*playlistEntryDur))
80-
{
102+
if (millis() - presetCycledTime > (100*playlistEntryDur)) {
81103
presetCycledTime = millis();
82104
if (bri == 0 || nightlightActive) return;
83105

84-
playlistIndex++;
85-
if (playlistIndex >= playlistLen) {
86-
playlistIndex = 0;
87-
if (playlistRepeat == 1) { //stop
88-
currentPlaylist = -1;
89-
delete playlistEntries;
90-
playlistEntries = nullptr;
91-
if (playlistEndPreset) applyPreset(playlistEndPreset);
92-
return;
106+
++playlistIndex %= playlistLen; // -1 at 1st run (limit to playlistLen)
107+
108+
if (!playlistRepeat && !playlistIndex) { //stop if repeat == 0 and restart of playlist
109+
unloadPlaylist();
110+
if (playlistEndPreset) applyPreset(playlistEndPreset);
111+
return;
112+
}
113+
// playlist roll-over
114+
if (!playlistIndex) {
115+
if (playlistRepeat > 0) {// playlistRepeat < 0 => endless loop with shuffling presets
116+
playlistRepeat--; // decrease repeat count on each index reset
117+
} else {
118+
shufflePlaylist(); // shuffle playlist and start over
93119
}
94-
if (playlistRepeat > 1) playlistRepeat--;
95120
}
96121

97122
PlaylistEntry* entries = reinterpret_cast<PlaylistEntry*>(playlistEntries);
@@ -103,4 +128,4 @@ void handlePlaylist()
103128
playlistEntryDur = entries[playlistIndex].dur;
104129
if (playlistEntryDur == 0) playlistEntryDur = 10;
105130
}
106-
}
131+
}

0 commit comments

Comments
 (0)