-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathplaylist.cpp
More file actions
301 lines (259 loc) · 10.9 KB
/
Copy pathplaylist.cpp
File metadata and controls
301 lines (259 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "wled.h"
/*
* Handles playlists, timed sequences of presets
*/
typedef struct PlaylistEntry {
uint8_t preset; //ID of the preset to apply
uint16_t tr; //Duration of the transition TO this entry (in tenths of seconds)
uint32_t dur; //Duration of the entry (in milliseconds)
} ple;
static byte playlistRepeat = 1; //how many times to repeat the playlist (0 = infinitely)
static byte playlistEndPreset = 0; //what preset to apply after playlist end (0 = stay on last preset)
static byte playlistOptions = 0; //bit 0: shuffle playlist after each iteration. bits 1-7 TBD
static PlaylistEntry *playlistEntries = nullptr;
static byte playlistLen; //number of playlist entries
static int8_t playlistIndex = -1;
static uint32_t playlistEntryDur = 0; //duration of the current entry in milliseconds
static bool clockedPlaylistSynced = false;
//values we need to keep about the parent playlist while inside sub-playlist
static int16_t parentPlaylistIndex = -1;
static byte parentPlaylistRepeat = 0;
static byte parentPlaylistPresetId = 0; //for re-loading
void shufflePlaylist() {
int currentIndex = playlistLen;
PlaylistEntry temporaryValue;
// While there remain elements to shuffle...
while (currentIndex--) {
// Pick a random element...
int randomIndex = random(0, currentIndex);
// And swap it with the current element.
temporaryValue = playlistEntries[currentIndex];
playlistEntries[currentIndex] = playlistEntries[randomIndex];
playlistEntries[randomIndex] = temporaryValue;
}
DEBUG_PRINTLN(F("Playlist shuffle."));
}
// AI: below section was generated by an AI
static uint32_t playlistShuffleRand(uint32_t &state) {
state ^= state << 13;
state ^= state >> 17;
state ^= state << 5;
return state;
}
static uint32_t clockedPlaylistSeed(uint32_t cycle) {
uint32_t seed = 2166136261UL;
seed = (seed ^ (uint32_t)currentPlaylist) * 16777619UL;
seed = (seed ^ cycle) * 16777619UL;
seed = (seed ^ playlistLen) * 16777619UL;
if (seed == 0) seed = 2166136261UL;
return seed;
}
// AI: end
void unloadPlaylist() {
if (playlistEntries != nullptr) {
delete[] playlistEntries;
playlistEntries = nullptr;
}
currentPlaylist = playlistIndex = -1;
playlistLen = 0;
playlistOptions = 0;
playlistEntryDur = 0;
clockedPlaylistSynced = false;
DEBUG_PRINTLN(F("Playlist unloaded."));
}
// AI: below section was generated by an AI
static bool getClockedPlaylistState(uint8_t &entryIndex, uint32_t &entryOffset) {
if (!(playlistOptions & PL_OPTION_CLOCKED) || playlistLen == 0 || playlistEntries == nullptr) return false;
if (toki.getTimeSource() == TOKI_TS_NONE) return false;
// Clocked playlists live on an absolute Toki timeline; every entry must have finite duration.
uint64_t cycleDur = 0;
for (byte i = 0; i < playlistLen; i++) {
if (playlistEntries[i].dur == 0) return false; // infinite entries cannot be placed on a repeating wall-clock timeline
cycleDur += playlistEntries[i].dur;
}
if (cycleDur == 0) return false;
Toki::Time tm = toki.getTime();
uint64_t absoluteMs = (uint64_t)tm.sec * 1000ULL + tm.ms;
uint32_t cycle = (uint32_t)(absoluteMs / cycleDur);
uint64_t cycleOffset = absoluteMs % cycleDur;
uint8_t order[100];
for (byte i = 0; i < playlistLen; i++) order[i] = i;
if (playlistOptions & PL_OPTION_SHUFFLE) {
// Keep shuffle deterministic for all controllers within the same wall-clock cycle.
uint32_t seed = clockedPlaylistSeed(cycle);
for (int i = playlistLen - 1; i > 0; i--) {
int j = playlistShuffleRand(seed) % (i + 1);
uint8_t tmp = order[i];
order[i] = order[j];
order[j] = tmp;
}
}
for (byte i = 0; i < playlistLen; i++) {
uint8_t orderedIndex = order[i];
uint32_t dur = playlistEntries[orderedIndex].dur;
if (cycleOffset < dur) {
entryIndex = orderedIndex;
entryOffset = (uint32_t)cycleOffset;
return true;
}
cycleOffset -= dur;
}
entryIndex = order[playlistLen - 1];
entryOffset = playlistEntries[entryIndex].dur - 1;
return true;
}
// AI: end
int16_t loadPlaylist(JsonObject playlistObj, byte presetId) {
if (currentPlaylist > 0 && parentPlaylistPresetId > 0) return -1; // we are already in nested playlist, do nothing
if (currentPlaylist > 0) {
parentPlaylistIndex = playlistIndex;
parentPlaylistRepeat = playlistRepeat;
parentPlaylistPresetId = currentPlaylist;
}
unloadPlaylist();
JsonArray presets = playlistObj["ps"];
playlistLen = presets.size();
if (playlistLen == 0) return -1;
if (playlistLen > 100) playlistLen = 100;
playlistEntries = new(std::nothrow) PlaylistEntry[playlistLen];
if (playlistEntries == nullptr) return -1;
byte it = 0;
for (int ps : presets) {
if (it >= playlistLen) break;
playlistEntries[it].preset = ps;
it++;
}
it = 0;
JsonArray durations = playlistObj["dur"];
if (durations.isNull()) {
uint32_t durMs = playlistObj["dur"] | 100; // 10 seconds as fallback (tenths)
durMs = constrain(durMs, 0L, 42949670L) * 100UL; // limit to max value and convert to ms
playlistEntries[0].dur = (uint32_t)durMs;
it = 1;
} else {
for (int dur : durations) {
if (it >= playlistLen) break;
uint32_t durMs = constrain(dur, 0L, 42949670L) * 100UL; // limit to max value and convert to ms
playlistEntries[it].dur = (uint32_t)durMs;
it++;
}
}
if (it > 0) // should never happen but just in case
for (int i = it; i < playlistLen; i++) playlistEntries[i].dur = playlistEntries[it -1].dur;
it = 0;
JsonArray tr = playlistObj[F("transition")];
if (tr.isNull()) {
playlistEntries[0].tr = playlistObj[F("transition")] | (transitionDelay / 100);
it = 1;
} else {
for (int transition : tr) {
if (it >= playlistLen) break;
playlistEntries[it].tr = transition;
it++;
}
}
for (int i = it; i < playlistLen; i++) playlistEntries[i].tr = playlistEntries[it -1].tr;
int rep = playlistObj[F("repeat")];
bool shuffle = false;
if (rep < 0) { //support negative values as infinite + shuffle
rep = 0; shuffle = true;
}
playlistRepeat = rep;
if (playlistRepeat > 0) playlistRepeat++; //add one extra repetition immediately since it will be deducted on first start
playlistEndPreset = playlistObj["end"] | 0;
// if end preset is 255 restore original preset (if any running) upon playlist end
if (playlistEndPreset == 255 && currentPreset > 0) {
playlistEndPreset = currentPreset;
playlistOptions |= PL_OPTION_RESTORE; // for async save operation
}
if (playlistEndPreset > 250) playlistEndPreset = 0;
shuffle = shuffle || playlistObj["r"];
if (playlistObj[F("clocked")] | false) playlistOptions |= PL_OPTION_CLOCKED;
if (shuffle) playlistOptions |= PL_OPTION_SHUFFLE;
if (parentPlaylistPresetId == 0 && parentPlaylistIndex > -1) {
// we are re-loading playlist when returning from nested playlist
playlistIndex = parentPlaylistIndex;
playlistRepeat = parentPlaylistRepeat;
parentPlaylistIndex = -1;
parentPlaylistRepeat = 0;
} else if (rep == 0) {
// endless playlist will never return to parent so erase parent information if it was called from it
parentPlaylistPresetId = 0;
parentPlaylistIndex = -1;
parentPlaylistRepeat = 0;
}
currentPlaylist = presetId;
DEBUG_PRINTLN(F("Playlist loaded."));
return currentPlaylist;
}
void handlePlaylist() {
static unsigned long presetCycledTime = 0;
if (currentPlaylist < 0 || playlistEntries == nullptr) return;
if (playlistOptions & PL_OPTION_CLOCKED) {
if (toki.getTimeSource() == TOKI_TS_NONE) {
if (playlistIndex < 0 && !nightlightActive) {
playlistIndex = 0;
jsonTransitionOnce = true;
strip.setTransition(playlistEntries[playlistIndex].tr * 100);
playlistEntryDur = playlistEntries[playlistIndex].dur > 0 ? playlistEntries[playlistIndex].dur : UINT32_MAX;
strip.resetTimebase();
applyPresetFromPlaylist(playlistEntries[playlistIndex].preset);
}
return;
}
uint8_t derivedIndex = 0;
uint32_t entryOffset = 0;
if (!getClockedPlaylistState(derivedIndex, entryOffset)) return;
if (nightlightActive) return;
strip.timebase = (unsigned long)entryOffset - millis();
if (!clockedPlaylistSynced || playlistIndex != (int8_t)derivedIndex) {
playlistIndex = derivedIndex;
jsonTransitionOnce = true;
strip.setTransition(playlistEntries[playlistIndex].tr * 100);
playlistEntryDur = playlistEntries[playlistIndex].dur;
applyPresetFromPlaylist(playlistEntries[playlistIndex].preset);
}
clockedPlaylistSynced = true;
doAdvancePlaylist = false;
return;
}
if ((playlistEntryDur < UINT32_MAX && millis() - presetCycledTime > playlistEntryDur) || doAdvancePlaylist) {
presetCycledTime = millis();
if (bri == 0 || nightlightActive) return;
++playlistIndex %= playlistLen; // -1 at 1st run (limit to playlistLen)
// playlist roll-over
if (!playlistIndex) {
if (playlistRepeat == 1) { //stop if all repetitions are done
unloadPlaylist();
if (parentPlaylistPresetId > 0) {
applyPresetFromPlaylist(parentPlaylistPresetId); // reload previous playlist (unfortunately asynchronous)
parentPlaylistPresetId = 0; // reset previous playlist but do not reset Index or Repeat (they will be loaded & reset in loadPlaylist())
} else if (playlistEndPreset) applyPresetFromPlaylist(playlistEndPreset);
return;
}
if (playlistRepeat > 1) playlistRepeat--; // decrease repeat count on each index reset if not an endless playlist
// playlistRepeat == 0: endless loop
if (playlistOptions & PL_OPTION_SHUFFLE) shufflePlaylist(); // shuffle playlist and start over
}
jsonTransitionOnce = true;
strip.setTransition(playlistEntries[playlistIndex].tr * 100);
playlistEntryDur = playlistEntries[playlistIndex].dur > 0 ? playlistEntries[playlistIndex].dur : UINT32_MAX; // UINT32_MAX means infinite
applyPresetFromPlaylist(playlistEntries[playlistIndex].preset);
doAdvancePlaylist = false;
}
}
void serializePlaylist(JsonObject sObj) {
JsonObject playlist = sObj.createNestedObject(F("playlist"));
JsonArray ps = playlist.createNestedArray("ps");
JsonArray dur = playlist.createNestedArray("dur");
JsonArray transition = playlist.createNestedArray(F("transition"));
playlist[F("repeat")] = (playlistIndex < 0 && playlistRepeat > 0) ? playlistRepeat - 1 : playlistRepeat; // remove added repetition count (if not yet running)
playlist["end"] = playlistOptions & PL_OPTION_RESTORE ? 255 : playlistEndPreset;
playlist["r"] = playlistOptions & PL_OPTION_SHUFFLE;
playlist[F("clocked")] = (playlistOptions & PL_OPTION_CLOCKED) != 0;
for (int i=0; i<playlistLen; i++) {
ps.add(playlistEntries[i].preset);
dur.add((playlistEntries[i].dur) / 100); // convert ms back to tenths of seconds (backwards compatibility)
transition.add(playlistEntries[i].tr);
}
}