@@ -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
2346void 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