-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoodFairground.h
executable file
·691 lines (544 loc) · 16.7 KB
/
MoodFairground.h
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/*
FairgroundStar. Firmware for an Arduino-based star-shaped fairground/circus-style LED light.
Copyright (C) 2015 Tim Davis
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __INC_MOOD_FAIRGROUND_H
#define __INC_MOOD_FAIRGROUND_H
#include "Mood.h"
#include "Palettes.h"
#include "Groups.h"
#include "config.h"
#define SLOW 9
#define MEDIUM 6
#define FAST 3
#define NUM_ANIMS 7
#define ANIM_PaintAndWipe 0
#define ANIM_Fifo 1
#define ANIM_ContinuousPaint 2
#define ANIM_Marching 3
#define ANIM_Flash 4
#define ANIM_ContinuousPaintMono 5
#define ANIM_CentrePaintAndWipeMono 6
struct AnimGroupConfig {
uint8_t repeats;
uint8_t lowSpeed;
uint8_t hiSpeed;
};
// sensible repetion and speed settings for each combination of group and anim
const AnimGroupConfig animGroupConfig[NUM_FAIRGROUND_GROUPS][NUM_ANIMS] = {
// arms
{
{3, SLOW, MEDIUM}, // paint and wipe
{2, SLOW, MEDIUM}, // fifo
{4, MEDIUM, MEDIUM}, // cont. paint
{2, SLOW, MEDIUM}, // marching
{7, FAST, FAST}, // flash
{5, MEDIUM, FAST}, // cont. paint mono
{5, SLOW, MEDIUM} // centre paint and wipe mono
},
// rings
{
{4, SLOW, MEDIUM}, // paint and wipe
{3, SLOW, MEDIUM}, // fifo
{5, SLOW, MEDIUM}, // cont. paint
{2, SLOW, MEDIUM}, // marching
{7, FAST, FAST}, // flash
{5, SLOW, MEDIUM}, // cont. paint mono
{5, SLOW, MEDIUM} // centre paint and wipe mono
},
// rows
{
{3, SLOW, MEDIUM}, // paint and wipe
{2, SLOW, MEDIUM}, // fifo
{5, MEDIUM, MEDIUM}, // cont. paint
{3, SLOW, SLOW}, // marching
{7, FAST, FAST}, // flash
{7, MEDIUM, FAST}, // cont. paint mono
{5, MEDIUM, FAST} // centre paint and wipe mono
},
// spiral
{
{2, SLOW, MEDIUM}, // paint and wipe
{1, SLOW, MEDIUM}, // fifo
{4, MEDIUM, MEDIUM}, // cont. paint
{3, SLOW, SLOW}, // marching
{7, FAST, FAST}, // flash
{8, MEDIUM, FAST}, // cont. paint mono
{0, 0, 0} // centre paint and wipe mono
}
};
class MoodFairground : public Mood {
uint8_t frameDelay;
uint8_t frameDelayCount;
uint8_t curGroup;
uint8_t curGroupSize;
uint8_t curPalette;
uint8_t curPaletteIndex;
uint8_t seqStep;
uint8_t anim;
uint8_t animRepeats;
struct {
bool painting;
uint8_t curSetNum;
bool used;
} configPaintAndWipe;
struct {
bool started;
bool filling;
uint8_t curSetNum;
int8_t endSetNum;
uint8_t initialPaletteIndex;
bool used;
} configFifo;
struct {
uint8_t curSetNum;
bool used;
} configContinuousPaint;
struct {
uint8_t curSetNum;
bool used;
} configContinuousPaintMono;
struct {
uint8_t offset;
uint8_t iterations;
bool used;
} configMarching;
struct {
bool flashOn;
bool used;
} configFlash;
struct {
bool painting;
uint8_t offset;
bool used;
} configCentrePaintAndWipeMono;
public:
/////////////////////////////////////////
MoodFairground() {
frameDelay = SLOW;
frameDelayCount = frameDelay;
curPaletteIndex = 0;
changeStep(true);
}
/////////////////////////////////////////
bool run(void) {
// wait for frameDelay frames before executing next step
if (++frameDelayCount < frameDelay) return false;
frameDelayCount = 0;
// run anim step
bool finished = false;
switch (anim) {
case ANIM_PaintAndWipe:
finished = paintAndWipe();
break;
case ANIM_Fifo:
finished = fifo();
break;
case ANIM_ContinuousPaint:
finished = continuousPaint();
break;
case ANIM_ContinuousPaintMono:
finished = continuousPaintMono();
break;
case ANIM_Marching:
finished = marching();
break;
case ANIM_Flash:
finished = flash();
break;
case ANIM_CentrePaintAndWipeMono:
finished = centrePaintAndWipeMono();
break;
}
if (finished) {
animRepeats--;
if (animRepeats == 0) changeStep(false);
}
return true;
}
private:
/////////////////////////////////////////
void changePaletteAndGroup(void) {
curGroup = random8(NUM_FAIRGROUND_GROUPS);
curGroupSize = fairgroundGroups[curGroup].size;
curPalette = fairgroundPalettes[random8(NUM_FAIRGROUND_PALETTES)];
curPaletteIndex = 0;
}
/////////////////////////////////////////
void initAnimConfigs(void) {
configPaintAndWipe.painting = true;
configPaintAndWipe.curSetNum = 0;
configPaintAndWipe.used = false;
configFifo.filling = true;
configFifo.curSetNum = 0;
configFifo.endSetNum = curGroupSize;
configFifo.initialPaletteIndex;
configFifo.used = false;
configContinuousPaint.curSetNum = 0;
configContinuousPaint.used = false;
configContinuousPaintMono.curSetNum = 0;
configContinuousPaintMono.used = false;
configMarching.offset = 0;
configMarching.iterations = 5;
configMarching.used = false;
configFlash.flashOn = true;
configFlash.used = false;
configCentrePaintAndWipeMono.offset = 0;
configCentrePaintAndWipeMono.used = false;
};
/////////////////////////////////////////
void changeStep(bool reset) {
// choose next step
if (reset) {
seqStep = 0;
}
else {
uint8_t ran8 = random8();
switch (seqStep) {
case 0: // initial 1st half anim
if (ran8 < 170) seqStep = 1;
else if (ran8 < 200) seqStep = 3;
else seqStep = 4;
break;
case 1: // optional extra 1st half anim
if (ran8 < 80) seqStep = 2;
else if (ran8 < 120) seqStep = 3;
else seqStep = 4;
break;
case 2: // optional extra 1st half anim
if (ran8 < 100) seqStep = 3;
else seqStep = 4;
break;
case 3: // optional flash
seqStep = 4;
break;
case 4: // 2nd half anim
if (configFlash.used) {
if (ran8 < 200) seqStep = 5;
else seqStep = 0;
}
else {
if (ran8 < 150) seqStep = 5;
else if (ran8 < 190) seqStep = 6;
else seqStep = 0;
}
break;
case 5: // optional extra 2nd half anim
if (configFlash.used) seqStep = 0;
else {
if (ran8 < 100) seqStep = 6;
else seqStep = 0;
}
break;
case 6: // optional flash (if 1st flash didn't happen)
seqStep = 0;
break;
}
}
do {
initialiseStep();
} while (animRepeats == 0);
// common bulbs have constant colour
fairgroundGroups[curGroup].setCommon(
palettes[curPalette].colours[ random8(palettes[curPalette].size) ]
);
}
void initialiseStep(void) {
if (curGroup == GROUP_FAIRGROUND_ARMS2) {
// reset override
curGroup = GROUP_FAIRGROUND_ARMS;
}
else if (curGroup == GROUP_FAIRGROUND_ROWS_REV) {
// reset override
curGroup = GROUP_FAIRGROUND_ROWS;
}
switch (seqStep) {
case 0: // initial 1st half anim
// reset everything
changePaletteAndGroup();
initAnimConfigs();
// fall thru
case 1: // optional extra 1st half anim
switch (random8(0, 3)) {
case 0:
anim = ANIM_PaintAndWipe;
configPaintAndWipe.used = true;
break;
case 1:
anim = ANIM_Fifo;
configFifo.used = true;
break;
case 2:
anim = ANIM_CentrePaintAndWipeMono;
configCentrePaintAndWipeMono.used = true;
break;
}
if (seqStep == 0) {
frameDelay = animGroupConfig[curGroup][anim].lowSpeed;
}
else {
// speed up in step 1
frameDelay = animGroupConfig[curGroup][anim].hiSpeed;
}
break;
case 2: // if one anim unused, use it
if (animGroupConfig[curGroup][anim].repeats == 0) {
// appear to have previously chosen a disabled anim and as this
// step is deterministic, no point re-running it
switch (random8(0, 3)) {
case 0:
anim = ANIM_PaintAndWipe;
break;
case 1:
anim = ANIM_Fifo;
break;
case 2:
anim = ANIM_CentrePaintAndWipeMono;
break;
}
}
else {
if (!configPaintAndWipe.used) anim = ANIM_PaintAndWipe;
else if (!configFifo.used) anim = ANIM_Fifo;
else if (!configCentrePaintAndWipeMono.used) anim = ANIM_CentrePaintAndWipeMono;
else anim = ANIM_Fifo;
}
frameDelay = animGroupConfig[curGroup][anim].hiSpeed;
break;
case 3: // optional flash
case 6:
anim = ANIM_Flash;
frameDelay = animGroupConfig[curGroup][anim].hiSpeed;
break;
case 4: // 2nd half anim
case 5:
switch (random8(0, 3)) {
case 0:
anim = ANIM_ContinuousPaint;
configContinuousPaint.used = true;
break;
case 1:
anim = ANIM_ContinuousPaintMono;
configContinuousPaintMono.used = true;
break;
case 2:
anim = ANIM_Marching;
configMarching.used = true;
break;
}
if (seqStep == 4) {
frameDelay = animGroupConfig[curGroup][anim].lowSpeed;
}
else {
// speed up in step 5
frameDelay = animGroupConfig[curGroup][anim].hiSpeed;
}
break;
}
animRepeats = animGroupConfig[curGroup][anim].repeats;
// group overrides for particular anims
if (anim == ANIM_CentrePaintAndWipeMono && curGroup == GROUP_FAIRGROUND_ARMS) {
curGroup = GROUP_FAIRGROUND_ARMS2;
}
else if (anim == ANIM_Fifo && curGroup == GROUP_FAIRGROUND_ROWS) {
curGroup = GROUP_FAIRGROUND_ROWS_REV;
}
}
/////////////////////////////////////////
// Animations
/////////////////////////////////////////
inline void setPaletteColour(uint8_t setNum) {
fairgroundGroups[curGroup].setColour(setNum, palettes[curPalette].colours[curPaletteIndex]);
}
/////////////////////////////////////////
inline void setBlack(uint8_t setNum) {
fairgroundGroups[curGroup].setColour(setNum, CRGB::Black);
}
/////////////////////////////////////////
inline void incPaletteIndex(void) {
curPaletteIndex = (curPaletteIndex + 1) % palettes[curPalette].size;
}
/////////////////////////////////////////
bool paintAndWipe(void) {
if (configPaintAndWipe.painting) {
// paint
setPaletteColour(configPaintAndWipe.curSetNum);
incPaletteIndex();
configPaintAndWipe.curSetNum++;
if (configPaintAndWipe.curSetNum == curGroupSize) {
configPaintAndWipe.painting = false;
configPaintAndWipe.curSetNum = 0;
}
}
else {
// wipe
setBlack(configPaintAndWipe.curSetNum);
configPaintAndWipe.curSetNum++;
if (configPaintAndWipe.curSetNum == curGroupSize) {
configPaintAndWipe.painting = true;
configPaintAndWipe.curSetNum = 0;
//finished
return true;
}
}
return false;
}
/////////////////////////////////////////
bool fifo(void) {
if (configFifo.filling) {
// fill
if (configFifo.curSetNum == 0 && configFifo.endSetNum == curGroupSize) {
// record first colour used when filling so the same first colour
// can be used for emptying
configFifo.initialPaletteIndex = curPaletteIndex;
}
if (configFifo.curSetNum > 0) {
setBlack(configFifo.curSetNum - 1);
}
setPaletteColour(configFifo.curSetNum);
configFifo.curSetNum++;
if (configFifo.curSetNum == configFifo.endSetNum) {
configFifo.curSetNum = 0;
configFifo.endSetNum--;
incPaletteIndex();
if (configFifo.endSetNum == 0) {
configFifo.filling = false;
configFifo.endSetNum = curGroupSize - 1;
configFifo.curSetNum = configFifo.endSetNum + 1;
curPaletteIndex = configFifo.initialPaletteIndex;
}
}
}
else {
// empty
if (configFifo.curSetNum > 0) {
setBlack(configFifo.curSetNum - 1);
}
if (configFifo.curSetNum < curGroupSize) {
setPaletteColour(configFifo.curSetNum);
}
configFifo.curSetNum++;
if (configFifo.curSetNum > curGroupSize) {
configFifo.endSetNum--;
configFifo.curSetNum = configFifo.endSetNum + 1;
incPaletteIndex();
if (configFifo.endSetNum < 0) {
// finished
configFifo.filling = true;
configFifo.curSetNum == 0;
configFifo.endSetNum = curGroupSize;
return true;
}
}
}
return false;
}
/////////////////////////////////////////
bool continuousPaint(void) {
setPaletteColour(configContinuousPaint.curSetNum);
configContinuousPaint.curSetNum++;
incPaletteIndex();
if (configContinuousPaint.curSetNum == curGroupSize) {
configContinuousPaint.curSetNum = 0;
// finished
return true;
}
return false;
}
/////////////////////////////////////////
bool continuousPaintMono(void) {
setPaletteColour(configContinuousPaintMono.curSetNum);
configContinuousPaintMono.curSetNum++;
if (configContinuousPaintMono.curSetNum == curGroupSize) {
configContinuousPaintMono.curSetNum = 0;
incPaletteIndex();
// finished
return true;
}
return false;
}
/////////////////////////////////////////
bool marching(void) {
// check offset isn't out of range due to palette changing since last execution
if (configMarching.offset >= palettes[curPalette].size) {
configMarching.offset = 0;
}
curPaletteIndex = configMarching.offset;
for (uint8_t curSetNum=0; curSetNum < curGroupSize; curSetNum++) {
setPaletteColour(curSetNum);
incPaletteIndex();
}
if (configMarching.offset == 0) configMarching.offset = palettes[curPalette].size - 1;
else configMarching.offset --;
configMarching.iterations --;
if (configMarching.iterations == 0) {
configMarching.iterations = 5;
// finished
return true;
}
return false;
}
/////////////////////////////////////////
bool flash(void) {
if (configFlash.flashOn) {
for (uint8_t curSetNum=0; curSetNum < curGroupSize; curSetNum++) {
setPaletteColour(curSetNum);
fairgroundGroups[curGroup].setCommon(palettes[curPalette].colours[curPaletteIndex]);
incPaletteIndex();
}
configFlash.flashOn = false;
}
else {
for (uint8_t curSetNum=0; curSetNum < curGroupSize; curSetNum++) {
setBlack(curSetNum);
fairgroundGroups[curGroup].setCommon(CRGB::Black);
}
configFlash.flashOn = true;
// finished
return true;
}
return false;
}
/////////////////////////////////////////
bool centrePaintAndWipeMono(void) {
uint8_t midPoint = curGroupSize / 2;
fairgroundGroups[curGroup].setCommon(
palettes[curPalette].colours[curPaletteIndex]
);
for (uint8_t set = midPoint - configCentrePaintAndWipeMono.offset;
set <= midPoint + configCentrePaintAndWipeMono.offset;
set++) {
// paint or wipe
if (configCentrePaintAndWipeMono.painting) {
setPaletteColour(set);
}
else {
setBlack(set);
}
}
configCentrePaintAndWipeMono.offset++;
if (midPoint + configCentrePaintAndWipeMono.offset == curGroupSize) {
// full up, switch mode
configCentrePaintAndWipeMono.offset = 0;
configCentrePaintAndWipeMono.painting = !configCentrePaintAndWipeMono.painting;
if (configCentrePaintAndWipeMono.painting) {
// just changed to painting mode
incPaletteIndex();
//finished
return true;
}
}
return false;
}
};
#endif