-
Notifications
You must be signed in to change notification settings - Fork 0
/
cube.ino
1058 lines (919 loc) · 35.2 KB
/
cube.ino
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vim: set ts=8 sts=4 et sw=4 tw=99: */
#define USE_GET_MILLISECOND_TIMER
#include <FastLED.h>
#include <algorithm>
#include "cube.h"
#include "vector3.h"
#include "cube_util.h"
#include "accel.h"
#include "pattern.h"
extern "C" {
int _getpid(){ return -1;}
int _kill(int pid, int sig){ return -1; }
int _write_r() { return -1; }
void __cxa_pure_virtual() { abort(); }
}
namespace __gnu_cxx {
void __verbose_terminate_handler() {
abort();
}
}
uint32_t get_millisecond_timer() {
return millis() / 2.5;
}
CRGB leds[NUM_LEDS];
#define SINGLE_PATTERN_DEBUG
// Hacky gross code for effects that understand the physical object
// Format:
// Index 0 - NUM_SEGMENTS
// index 1..NUM_SEGMENTS (inclusive): start index for each segment
// Remaining indices: pixel in panel
#define P(x,y) (x)*LEDS_PER_ROW + y
static const PROGMEM int8_t segments[PANELS][LEDS_PER_PANEL + 11] = {
{ // side 11
8, 10, 18, 37, 50, 59, 68, 70, 72, 74,
P(2,2), P(2,1), P(2,0), P(3,0), P(4,0), P(5,0), P(6,0), P(7,0),
P(3,2), P(3,1), P(4,1), P(5,1), P(6,1), P(7,1), P(7,2), P(7,3), P(7,4), P(7,5), P(7,6), P(6,6), P(5,6), P(4,6), P(3,6), P(2,6), P(1,6), P(1,5), P(0,5),
P(4,2), P(5,2), P(6,2), P(6,3), P(6,4), P(6,5), P(5,5), P(4,5), P(3,5), P(2,5), P(2,4), P(1,4), P(0,4),
P(4,3), P(5,3), P(5,4), P(4,4), P(3,4), P(3,3), P(2,3), P(1,3), P(0,3),
P(7,7), P(6,7), P(5,7), P(4,7), P(3,7), P(2,7), P(1,7), P(0,7), P(0,6),
P(1,0), P(0,0),
P(1,1), P(0,1),
P(1,2), P(0,2),
-1
},
{ // side 14
7, 9, 16, 24, 31, 44, 54, 67, 73,
P(0,2), P(0,1), P(0,0), P(1,0), P(2,0), P(2,1), P(2,2),
P(1,1), P(1,2), P(1,3), P(0,3), P(0,4), P(1,4), P(1,5), P(1,6),
P(0,5), P(0,6), P(0,7), P(1,7), P(2,7), P(2,6), P(2,5),
P(3,0), P(3,1), P(4,1), P(4,0), P(5,0), P(5,1), P(5,2), P(6,2), P(6,1), P(6,0), P(7,0), P(7,1), P(7,2),
P(4,3), P(4,2), P(3,2), P(3,3), P(2,3), P(2,4), P(3,4), P(3,5), P(4,5), P(4,4),
P(3,7), P(3,6), P(4,6), P(4,7), P(5,7), P(5,6), P(5,5), P(6,5), P(6,6), P(6,7), P(7,7), P(7,6), P(7,5),
P(7,3), P(6,3), P(5,3), P(5,4), P(6,4), P(7,4),
-1, -1
},
{ // side 06
9, 11, 26, 32, 39, 45, 51, 57, 63, 71, 75,
P(0,0), P(1,0), P(2,0), P(3,0), P(4,0), P(4,1), P(3,1), P(3,2), P(3,3), P(2,3), P(2,4), P(2,5), P(1,5), P(1,6), P(1,7),
P(0,1), P(1,1), P(2,1), P(2,2), P(1,2), P(0,2),
P(0,3), P(1,3), P(1,4), P(0,4), P(0,5), P(0,6), P(0,7),
P(7,0), P(6,0), P(5,0), P(5,1), P(6,1), P(7,1),
P(6,2), P(5,2), P(4,2), P(4,3), P(5,3), P(6,3),
P(5,4), P(4,4), P(3,4), P(3,5), P(4,5), P(5,5),
P(4,6), P(3,6), P(2,6), P(2,7), P(3,7), P(4,7),
P(7,2), P(7,3), P(7,4), P(6,4), P(6,5), P(6,6), P(5,6), P(5,7),
P(7,5), P(7,6), P(7,7), P(6,7),
},
{ // side 13
8, 10, 17, 21, 25, 43, 47, 51, 58, 74,
P(0,0), P(0,1), P(0,2), P(1,2), P(1,1), P(1,0), P(2,0),
P(2,1), P(3,1), P(3,2), P(2,2),
P(4,1), P(5,1), P(5,2), P(4,2),
P(6,1), P(6,2), P(6,3), P(5,3), P(4,3), P(3,3), P(2,3), P(1,3), P(0,3), P(0,4), P(1,4), P(2,4), P(3,4), P(4,4), P(5,4), P(6,4), P(6,5), P(6,6),
P(4,5), P(5,5), P(5,6), P(4,6),
P(2,5), P(3,5), P(3,6), P(2,6),
P(0,7), P(0,6), P(0,5), P(1,5), P(1,6), P(1,7), P(2,7),
P(3,7), P(4,7), P(5,7), P(6,7), P(7,7), P(7,6), P(7,5), P(7,4), P(7,3), P(7,2), P(7,1), P(7,0), P(6,0), P(5,0), P(4,0), P(3,0),
-1
},
{ // side 08
6, 8, 15, 30, 37, 52, 60, 68,
P(2,1), P(2,0), P(1,0), P(1,1), P(0,1), P(0,2), P(1,2),
P(2,2), P(3,2), P(3,1), P(3,0), P(4,0), P(4,1), P(4,2), P(4,3), P(5,3), P(6,3), P(7,3), P(7,4), P(6,4), P(5,4), P(4,4),
P(6,5), P(7,5), P(7,6), P(6,6), P(6,7), P(5,7), P(5,6),
P(5,5), P(4,5), P(4,6), P(4,7), P(3,7), P(3,6), P(3,5), P(3,4), P(2,4), P(1,4), P(0,4), P(0,3), P(1,3), P(2,3), P(3,3),
P(5,2), P(6,2), P(7,2), P(7,1), P(6,1), P(6,0), P(5,0), P(5,1),
P(2,6), P(2,7), P(1,7), P(1,6), P(0,6), P(0,5), P(1,5), P(2,5),
-1,-1,-1,-1,-1,-1,-1,
},
{ // side 04
7, 9, 27, 35, 47, 53, 61, 65, 73,
P(5,0), P(4,0), P(3,0), P(2,0), P(1,0), P(0,0), P(0,1), P(0,2), P(0,3), P(0,4), P(0,5), P(0,6), P(0,7), P(1,7), P(2,7), P(3,7), P(4,7), P(5,7),
P(7,1), P(7,0), P(6,0), P(6,1), P(5,1), P(4,1), P(3,1), P(2,1),
P(1,1), P(1,2), P(2,2), P(3,2), P(4,2), P(4,3), P(4,4), P(4,5), P(3,5), P(2,5), P(1,5), P(1,6),
P(1,3), P(2,3), P(3,3), P(3,4), P(2,4), P(1,4),
P(7,2), P(6,2), P(5,2), P(5,3), P(5,4), P(5,5), P(6,5), P(7,5),
P(7,3), P(6,3), P(6,4), P(7,4),
P(7,6), P(7,7), P(6,7), P(6,6), P(5,6), P(4,6), P(3,6), P(2,6),
-1, -1
},
};
static const uint8_t primes[] = {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
};
uint8_t gHue = 0;
class Rain : public Pattern {
private:
static const int NUM_DROPLETS = 80;
/// This class maintains the state and calculates the animations to render a falling water droplet
/// Objects of this class can have three states:
/// - inactive: this object does nothing
/// - swelling: the droplet is at the top of the led strip and swells in intensity
/// - falling: the droplet falls downwards and accelerates
/// - bouncing: the droplet has bounced of the ground. A smaller, less intensive droplet bounces up
/// while a part of the drop remains on the ground.
/// After going through the swelling, falling and bouncing phases, the droplet automatically returns to the
/// inactive state.
class Droplet {
public:
Droplet()
: x(0), z(0), color(CRGB::Black), gravity(5),
position(0), speed(0), state(inactive)
{}
void init(int8_t x, int8_t z) {
this->x = x;
this->z = z;
reinit();
}
void reinit() {
this->position = 0;
this->speed = 0;
this->color = CHSV(144+random(32), 255, 255);
this->start = get_millisecond_timer()+1000*random8(5);
state = swelling;
}
/// perform one step and draw.
void step(CRGB *leds) {
if (get_millisecond_timer() >= start) {
step();
draw(leds);
}
}
private:
/// calculate the next step in the animation for this droplet
void step() {
if (state == falling || state == bouncing) {
position += speed;
speed += gravity;
// if we hit the bottom...
const uint16_t maxpos16 = (ROWS_PER_PANEL-1) << 8;
if (position > maxpos16) {
if (state == bouncing) {
// this is the second collision,
// deactivate.
state = inactive;
reinit();
} else {
// reverse direction and dampen the speed
position = maxpos16 - (position - maxpos16);
speed = -speed/4;
color.nscale8_video(collision_scaling);
state = bouncing;
}
}
} else if (state == swelling) {
++position;
if (color.blue <= 10 || color.blue - position <= 10) {
state = falling;
position = 0;
}
}
}
/// Draw the droplet on the led string
/// This will "smear" the light of this droplet between two leds. The closer
/// the droplets position is to that of a particular led, the brighter that
/// led will be
void draw(CRGB *leds) {
Vector3f gravity = accelerometerDirection();
if (state == falling || state == bouncing) {
uint8_t position8 = position >> 8;
uint8_t remainder = position; // get the lower bits
CRGB tc = color;
for (auto lidx : getPixel3dCompensated(gravity, x, position8,z)) {
leds[lidx] += tc.nscale8_video(256 - remainder);
}
if (state == bouncing) {
for (auto lidx : getPixel3dCompensated(gravity, x,ROWS_PER_PANEL-1,z)) {
leds[lidx] = color;
}
}
} else if (state == swelling) {
CRGB tc = color;
for (auto lidx : getPixel3dCompensated(gravity,x,0,z)) {
leds[lidx] = tc.nscale8_video(position);
}
}
}
// how much of a color is left when colliding with the floor, value
// between 0 and 256 where 256 means no loss.
static const uint16_t collision_scaling = 40;
int8_t x, z;
CRGB color;
uint16_t gravity;
uint16_t position;
int16_t speed;
enum stateval {
inactive,
swelling,
falling,
bouncing
};
stateval state;
unsigned long start;
};
Droplet *droplets;
public:
void setup() {
droplets = new Droplet[NUM_DROPLETS];
uint8_t idx = 0;
while (true) {
for (int8_t x = -1; x < LEDS_PER_ROW+1; x+=2) {
for (int8_t z = -1; z < LEDS_PER_ROW+1; z+=2) {
if (abs(x) <= 1 || abs(x-LEDS_PER_ROW) <= 1 ||
abs(z) <= 1 || abs(z-LEDS_PER_ROW) <= 1) {
droplets[idx++].init(x,z);
if (idx == NUM_DROPLETS)
return;
}
}
}
}
}
void show() override {
fill_solid(leds, NUM_LEDS, CRGB::Black);
for (uint8_t idx = 0; idx < NUM_DROPLETS; ++idx) {
droplets[idx].step(leds);
}
}
void teardown() {
delete[] droplets;
}
};
void setup() {
initAccelerometer();
random16_set_seed(seedOut(16));
nextCue();
FastLED.addLeds<WS2811_PORTD, PANELS>(leds, LEDS_PER_PANEL);
FastLED.setMaxRefreshRate(800);
FastLED.setCorrection(TypicalSMD5050);
FastLED.setBrightness(40);
}
class RelativelyPrimeFade : public Pattern {
private:
uint8_t shuffled[9];
public:
void setup() {
uint8_t n = ARRAY_SIZE(shuffled);
for (uint8_t i = 0; i < n; i++) {
shuffled[i] = primes[i+6];
}
for (uint8_t i = 0; i < n-2; i++) {
uint8_t j = random8(i, n);
uint8_t temp = shuffled[i];
shuffled[i] = shuffled[j];
shuffled[j] = temp;
}
}
void show() override {
uint8_t hue = 2*gHue;
for (uint8_t panel = 0; panel < PANELS; panel++) {
uint8_t num_segs = NUM_SEGS(panel);
for (int seg = 0; seg < num_segs; seg++) {
FOREACH_IN_SEGMENT(panel, seg, idx) {
uint8_t bright = beatsin16(shuffled[seg], 0, 255);
CHSV c(hue, 255, bright);
leds[SEG_LED(panel, idx)] = c;
hue += 5;
}
}
}
}
};
class TestSegments : public Pattern {
public:
void show() override {
uint8_t panel = 4;
uint8_t hue = 0;
uint8_t num_segs = NUM_SEGS(panel);
for (int seg = 0; seg < num_segs; seg++) {
FOREACH_IN_SEGMENT(panel, seg, idx) {
leds[SEG_LED(panel, idx)] = CHSV(hue, 255, 255);
}
hue += 30;
}
}
};
class PulseSegments : public Pattern {
private:
enum { GETTING_DARKER = 0, GETTING_BRIGHTER = 1 };
uint8_t pulseSegmentDirections[(NUM_LEDS+7)/8];
bool getPixelDirection(uint16_t i) {
uint16_t index = i / 8;
uint8_t bitNum = i & 0x07;
// using Arduino 'bitRead' function; expanded code below
return bitRead(pulseSegmentDirections[index], bitNum);
}
void setPixelDirection(uint16_t i, bool dir) {
uint16_t index = i / 8;
uint8_t bitNum = i & 0x07;
// using Arduino 'bitWrite' function; expanded code below
bitWrite( pulseSegmentDirections[index], bitNum, dir);
}
CRGB makeBrighter( const CRGB& color, fract8 howMuchBrighter) {
CRGB incrementalColor = color;
incrementalColor.nscale8( howMuchBrighter);
return color + incrementalColor;
}
CRGB makeDarker( const CRGB& color, fract8 howMuchDarker) {
CRGB newcolor = color;
newcolor.nscale8( 255 - howMuchDarker);
return newcolor;
}
void brightenOrDarkenEachPixel( fract8 fadeUpAmount, fract8 fadeDownAmount) {
for( uint16_t i = 0; i < NUM_LEDS; i++) {
if( getPixelDirection(i) == GETTING_DARKER) {
// This pixel is getting darker
leds[i] = makeDarker( leds[i], fadeDownAmount);
} else {
// This pixel is getting brighter
leds[i] = makeBrighter( leds[i], fadeUpAmount);
// now check to see if we've maxxed out the brightness
if( leds[i].r == 255 || leds[i].g == 255 || leds[i].b == 255) {
// if so, turn around and start getting darker
setPixelDirection(i, GETTING_DARKER);
}
}
}
}
static const uint8_t STARTING_BRIGHTNESS = 64;
static const uint8_t FADE_IN_SPEED = 28;
static const uint8_t FADE_OUT_SPEED = 16;
static const uint8_t DENSITY = 12;
public:
void show() override {
brightenOrDarkenEachPixel(FADE_IN_SPEED, FADE_OUT_SPEED);
for (uint8_t panel = 0; panel < PANELS; panel++) {
uint8_t num_segs = NUM_SEGS(panel);
if (random8() < DENSITY) {
uint8_t seg = random16(num_segs);
FOREACH_IN_SEGMENT(panel, seg, idx) {
uint16_t i = SEG_LED(panel, idx);
if ( leds[i].r < (STARTING_BRIGHTNESS/2)
|| leds[i].g < (STARTING_BRIGHTNESS/2)
|| leds[i].b < (STARTING_BRIGHTNESS/2)) {
leds[i] = CHSV(OFFSET_HUE(panel, gHue), 255, STARTING_BRIGHTNESS);
setPixelDirection(i, GETTING_BRIGHTER);
}
}
}
}
}
};
class CrawlWithHighlight : public Pattern {
public:
void show() override {
uint8_t offset = 5*gHue;
uint16_t pos = beatsin16(BPM, 0, LEDS_PER_PANEL);
fadeToBlackBy(leds, NUM_LEDS, 25);
for (uint8_t panel = 0; panel < PANELS; panel++) {
uint8_t hue = OFFSET_HUE(panel, offset);
uint8_t num_segs = NUM_SEGS(panel);
uint16_t offset_pos = pos + num_segs+2;
uint16_t seg;
for (seg = 0; seg < num_segs; seg++) {
if (offset_pos <= SEG_LAST(panel, seg))
break;
}
FOREACH_IN_SEGMENT(panel, seg, idx) {
uint8_t bright = 192;
if (offset_pos == idx)
bright = 255;
CHSV c(OFFSET_HUE(panel, hue), 255, bright);
leds[SEG_LED(panel, idx)] = c;
hue += 15;
}
}
}
};
class ChaseThroughPanels : public Pattern {
public:
void show() override {
uint8_t panel_map[][3] = { {1,2,3} , {0,4,5} };
uint8_t point = beatsin16(3, 0, LEDS_PER_PANEL*3);
uint8_t pos = point % LEDS_PER_PANEL;
uint8_t panel_point = point / LEDS_PER_PANEL;
fadeToBlackBy( leds, NUM_LEDS, 1);
for (uint8_t panel_idx = 0; panel_idx < 2; panel_idx++) {
uint8_t panel = panel_map[panel_idx][panel_point];
uint8_t num_segs = NUM_SEGS(panel);
uint16_t offset_pos = pos + num_segs+2;
uint16_t pixel = SEG_LED(panel, offset_pos);
leds[pixel] = CHSV(gHue * 5, 255, 255);
}
}
};
class HeightfieldRipple : public Pattern {
typedef __fp16 real;
private:
uint16_t up(uint8_t panel, uint8_t i, uint8_t j) {
if (i < LEDS_PER_ROW-1) {
return PIXEL_IN_PANEL(panel, P(i+1, j));
}
switch (panel) {
case 0:
return PIXEL_IN_PANEL(2, P(0, j));
case 1:
return PIXEL_IN_PANEL(5, P(0, j));
case 2:
return PIXEL_IN_PANEL(5, P(j,LEDS_PER_ROW-1));
case 3:
return PIXEL_IN_PANEL(5, P(LEDS_PER_ROW-1, FLIP(j)));
case 4:
return PIXEL_IN_PANEL(5, P(FLIP(j), 0));
case 5:
return PIXEL_IN_PANEL(3, P(LEDS_PER_ROW-1, FLIP(j)));
}
return 0;
}
uint16_t down(uint8_t panel, uint8_t i, uint8_t j) {
if (i > 0) {
return PIXEL_IN_PANEL(panel, P(i-1, j));
}
switch (panel) {
case 0:
return PIXEL_IN_PANEL(4, P(0, FLIP(j)));
case 1:
return PIXEL_IN_PANEL(0, P(j, 0));
case 2:
return PIXEL_IN_PANEL(0, P(LEDS_PER_ROW-1, j));
case 3:
return PIXEL_IN_PANEL(0, P(FLIP(j), LEDS_PER_ROW-1));
case 4:
return PIXEL_IN_PANEL(0, P(0, FLIP(j)));
case 5:
return PIXEL_IN_PANEL(1, P(LEDS_PER_ROW-1, j));
}
return 0;
}
uint16_t left(uint8_t panel, uint8_t i, uint8_t j) {
if (j > 0) {
return PIXEL_IN_PANEL(panel, P(i, j-1));
}
switch (panel) {
case 0:
return PIXEL_IN_PANEL(1, P(0, i));
case 5:
return PIXEL_IN_PANEL(4, P(LEDS_PER_ROW-1, FLIP(i)));
default:
uint8_t new_panel = panel-1;
if (new_panel == 0) {
new_panel = 4;
}
return PIXEL_IN_PANEL(new_panel, P(i, LEDS_PER_ROW-1));
}
return 0;
}
uint16_t right(uint8_t panel, uint8_t i, uint8_t j) {
if (j < LEDS_PER_ROW-1) {
return PIXEL_IN_PANEL(panel, P(i, j+1));
}
switch (panel) {
case 0:
return PIXEL_IN_PANEL(3, P(0, FLIP(i)));
case 5:
return PIXEL_IN_PANEL(2, P(LEDS_PER_ROW-1, i));
default:
uint8_t new_panel = panel+1;
if (new_panel == 5) {
new_panel = 1;
}
return PIXEL_IN_PANEL(new_panel, P(i, 0));
}
return 0;
}
real height[PANELS * LEDS_PER_PANEL];
real v[PANELS * LEDS_PER_PANEL];
uint64_t last = 0;
void droplet(uint8_t panel) {
uint8_t i = random8(LEDS_PER_ROW-1);
uint8_t j = random8(LEDS_PER_ROW-1);
height[PIXEL_IN_PANEL(panel, P(i ,j ))] += 512;
height[PIXEL_IN_PANEL(panel, P(i+1,j ))] += 512;
height[PIXEL_IN_PANEL(panel, P(i ,j+1))] += 512;
height[PIXEL_IN_PANEL(panel, P(i+1,j+1))] += 512;
v[PIXEL_IN_PANEL(panel, P(i , j ))] = 0;
v[PIXEL_IN_PANEL(panel, P(i+1, j ))] = 0;
v[PIXEL_IN_PANEL(panel, P(i , j+1))] = 0;
v[PIXEL_IN_PANEL(panel, P(i+1, j+1))] = 0;
}
void update_heights() {
for (uint8_t panel = 0; panel < PANELS; panel++) {
for (int8_t i = 0; i < LEDS_PER_ROW; i++) {
for (int8_t j = 0; j < LEDS_PER_ROW; j++) {
auto l = height[ left(panel, i, j)];
auto r = height[right(panel, i, j)];
auto u = height[ up(panel, i, j)];
auto d = height[ down(panel, i, j)];
uint16_t idx = PIXEL_IN_PANEL(panel, P(i,j));
v[idx] += ((l+r+u+d)/4 - height[idx]);
v[idx] *= 0.70;
height[idx] += v[idx];
height[idx] = MAX(32, height[idx] - 1.50);
}
}
}
}
public:
void setup() {
for (uint8_t panel = 0; panel < PANELS; panel++) {
for (int8_t i = 0; i < LEDS_PER_ROW; i++) {
for (int8_t j = 0; j < LEDS_PER_ROW; j++) {
height[PIXEL_IN_PANEL(panel, P(i, j))] = 64;
v[PIXEL_IN_PANEL(panel, P(i, j))] = 0;
}
}
}
}
void show() override {
uint64_t cur = get_millisecond_timer();
if (cur-last > 1000/PANELS) {
uint8_t panel = random8(PANELS);
droplet(panel);
last = cur;
}
for (uint8_t panel = 0; panel < PANELS; panel++) {
for (int8_t i = 0; i < LEDS_PER_ROW; i++) {
for (int8_t j = 0; j < LEDS_PER_ROW; j++) {
uint16_t idx = PIXEL_IN_PANEL(panel, P(i,j));
uint8_t bright = constrain(height[idx], 0, 255);
leds[idx] = CHSV(gHue, 255, bright);
}
}
}
update_heights();
}
};
class RainbowSegments : public Pattern {
public:
void show() override {
uint8_t offset = gHue;
for (uint8_t panel = 0; panel < PANELS; panel++) {
uint8_t hue = OFFSET_HUE(panel, offset);
uint8_t num_segs = NUM_SEGS(panel);
for (int seg = 0; seg < num_segs; seg++) {
FOREACH_IN_SEGMENT(panel, seg, idx) {
leds[SEG_LED(panel, idx)].setHue(hue);
}
hue += 15;
}
}
}
};
static void chase(uint8_t hueDelta) {
uint8_t hue = gHue;
uint16_t pos = beatsin16(BPM, 0, LEDS_PER_PANEL-1);
fadeToBlackBy( leds, NUM_LEDS, 40);
for (uint8_t panel = 0; panel < PANELS; panel++) {
uint16_t idx = SEG_LED(panel, pos+NUM_SEGS(panel)+2);
leds[idx] += CHSV(OFFSET_HUE(panel, hue), 255, 255);
hue += hueDelta;
}
}
class ChaseSolid : public Pattern {
public:
void show() override {
chase(0);
}
};
class ChaseRainbow : public Pattern {
public:
void show() override {
chase(15);
}
};
static void sweepTwo(bool solid) { // sweep simultaneously across two Us
//1,2,3 5,4,0
fadeToBlackBy( leds, NUM_LEDS, 40);
uint8_t hue = gHue;
uint8_t panel_map[][3] = { {1,2,3} , {0,4,5} };
uint8_t flip[][3] = { {1,1,1} , {0,0,1} };
uint8_t row_position = beatsin16(15, 0, ROWS_PER_PANEL * 3);
uint8_t row = row_position % ROWS_PER_PANEL;
uint8_t panel_point = row_position / ROWS_PER_PANEL;
for (uint8_t panel_idx = 0; panel_idx < 2; panel_idx++) {
uint8_t panel = panel_map[panel_idx][panel_point];
uint16_t pixel, delta;
if (!flip[panel_idx][panel_point]) {
pixel = PIXEL_IN_PANEL(panel, row*LEDS_PER_ROW);
delta = 1;
} else {
pixel = PIXEL_IN_PANEL(panel, row);
delta = 8;
}
uint8_t count = 0;
while (count < LEDS_PER_ROW) {
leds[pixel] = CHSV(hue + 128*panel_idx, 255, 255);
pixel += delta;
count++;
if (!solid) {
hue += 10;
}
}
}
}
class SweepTwoSolid : public Pattern {
public:
void show() override {
sweepTwo(true);
}
};
class SweepTwoRainbow : public Pattern {
public:
void show() override {
sweepTwo(false);
}
};
class Pulse : public Pattern {
public:
void show() override {
uint8_t beat = beatsin16(12, 64, 255);
uint8_t offset = 5*gHue;
for (uint8_t panel = 0; panel < PANELS; panel++) {
uint8_t hue = OFFSET_HUE(panel, offset);
uint8_t num_segs = NUM_SEGS(panel);
for (int seg = 0; seg < num_segs; seg++) {
FOREACH_IN_SEGMENT(panel, seg, idx) {
CHSV c(OFFSET_HUE(panel, hue), 255, beat);
leds[SEG_LED(panel, idx)] = c;
hue += 15;
}
}
}
}
};
class RainbowBrightnessChase : public Pattern {
public:
void show() override {
uint16_t pos = beatsin16(BPM, 0, LEDS_PER_PANEL);
int16_t direction = beatsin16(BPM, -LEDS_PER_PANEL, LEDS_PER_PANEL, 0, 16384);
uint8_t hue = gHue;
for (uint8_t panel = 0; panel < PANELS; panel++) {
uint8_t num_segs = NUM_SEGS(panel);
for (int seg = 0; seg < num_segs; seg++) {
uint16_t bright_idx = pos+num_segs+2;
FOREACH_IN_SEGMENT(panel, seg, idx) {
int16_t distance = std::max(abs(bright_idx - idx), 16);
uint8_t bright = 255 - 16*distance;
CHSV c(hue, 255, bright);
leds[SEG_LED(panel, idx)] = c;
hue += 5;
}
}
}
}
};
class FlickerSegments : public Pattern {
public:
void show() override {
uint8_t offset = gHue;
if (random16(2)) {
fadeToBlackBy(leds, NUM_LEDS, 25);
return;
}
for (uint8_t i = 0; i < PANELS/3; i++) {
uint8_t panel = random16(PANELS);
uint8_t hue = OFFSET_HUE(panel, offset);
uint8_t num_segs = NUM_SEGS(panel);
int seg = random16(num_segs);
FOREACH_IN_SEGMENT(panel, seg, idx) {
leds[SEG_LED(panel, idx)].setHue(hue);
hue += 15;
}
}
fadeToBlackBy( leds, NUM_LEDS, 25);
}
};
class FillSolid : public Pattern {
public:
void show() override {
uint8_t bright = beatsin16(BPM, 128, 255);
for (uint8_t panel = 0; panel < PANELS; panel++) {
fill_solid(
&leds[PIXEL_IN_PANEL(panel, 0)],
LEDS_PER_PANEL,
CHSV(OFFSET_HUE(panel, gHue), 255, bright)
);
}
}
};
class SweepPlane : public Pattern {
public:
void show() override {
fadeToBlackBy(leds, NUM_LEDS, 50);
uint8_t z = beatsin16(15, 0, LEDS_PER_ROW);
uint8_t hue = gHue;
for (uint8_t x = 0; x < LEDS_PER_ROW; x++) {
for (uint8_t y = 0; y < LEDS_PER_ROW; y++) {
if (x == 0 || x == LEDS_PER_ROW-1 || y == 0 || y == LEDS_PER_ROW-1) {
setPixel3d(x,y,z, CHSV(hue, 255, 255));
setPixel3d(x,y,z, CHSV(hue, 255, 255));
setPixel3d(x,y,LEDS_PER_ROW-z-1, CHSV(hue+128, 255, 255));
uint8_t x1 = z;
uint8_t y1 = x;
uint8_t z1 = y;
setPixel3d(x1,y1,z1, CHSV(hue, 255, 255));
setPixel3d(LEDS_PER_ROW-x1-1, y1, z1, CHSV(hue+128, 255, 255));
hue+=5;
}
}
}
}
};
class MakeWaves : public Pattern {
private:
void beginWave(uint8_t z, uint8_t hue, uint8_t bright) {
uint8_t width = z*2;
for (uint8_t x = 0; x < width; x++) {
for (uint8_t y = 0; y < width; y++) {
if (x == 0 || x == width-1 || y == 0 || y == width-1) {
uint8_t start = 4-z;
setPixel3d(start+x, start+y, 0, CHSV(hue, 255, bright));
hue += 5;
}
}
}
}
void endWave(uint8_t z, uint8_t hue, uint8_t bright) {
uint8_t width = z*2;
for (uint8_t x = 0; x < width; x++) {
for (uint8_t y = 0; y < width; y++) {
if (x == 0 || x == width-1 || y == 0 || y == width-1) {
uint8_t start = 4-z;
setPixel3d(start+x, start+y, LEDS_PER_ROW-1, CHSV(hue, 255, bright));
hue += 5;
}
}
}
}
void midWave(uint8_t z, uint8_t hue, uint8_t bright) {
for (uint8_t x = 0; x < LEDS_PER_ROW; x++) {
for (uint8_t y = 0; y < LEDS_PER_ROW; y++) {
if (x == 0 || x == LEDS_PER_ROW-1 || y == 0 || y == LEDS_PER_ROW-1) {
setPixel3d(x,y,z, CHSV(hue, 255, bright));
hue += 5;
}
}
}
}
void makeWave(uint8_t z, uint8_t hue, uint8_t bright) {
if (z < 4) {
beginWave(z, hue, bright);
} else if (z >= LEDS_PER_ROW + 4) {
endWave((LEDS_PER_ROW+8) - z - 1, hue, bright);
} else {
midWave(z-4, hue, bright);
}
}
public:
void show() override {
fadeToBlackBy(leds, NUM_LEDS, 25);
uint16_t base = scale16(beat16(25), (LEDS_PER_ROW+8));
for (uint8_t i = 0; i < LEDS_PER_ROW+8; i+= 6) {
makeWave((base + i) % (LEDS_PER_ROW+8), gHue+(30*i), 255);
makeWave((base + 1+ i) % (LEDS_PER_ROW+8), gHue+(30*i), 128);
makeWave((base + 2+ i) % (LEDS_PER_ROW+8), gHue+(30*i), 32);
}
}
};
//void rain() {
//}
//
//void testPattern() {
// rain();
//}
//
#define BUFFER 2
template <int8_t Y, int16_t S>
class PlanesWithGravity : public Pattern {
private:
uint16_t pos;
int16_t speed;
const uint16_t gravity = 3;
public:
void setup() {
this->pos = (Y + BUFFER) << 8;
this->speed = S;
}
void show() override {
fadeToBlackBy(leds, NUM_LEDS, 25);
Vector3f accelDir = accelerometerDirection();
//TODO Falling with gravity.
//
pos += speed;
speed += gravity;
int8_t y = pos >> 8;
y -= BUFFER;
for (int8_t x = -BUFFER; x < LEDS_PER_ROW+BUFFER; x++) {
for (int8_t z = -BUFFER; z < LEDS_PER_ROW+BUFFER; z++) {
setPixel3dCompensated(accelDir, x, y, z, CHSV(gHue,255,255));
}
}
if (y >= LEDS_PER_ROW + (BUFFER*2)) {
setup();
}
}
};
typedef PlanesWithGravity<-BUFFER, 0> FallingPlanes;
typedef PlanesWithGravity<LEDS_PER_ROW + BUFFER-1, -130> LaunchingPlanes;
// pattern ideas:
// rainbowify segments with bright crawlers
//
// rainbow from top to bottom gravity-compensated
//
//
class RainbowGravity : public Pattern {
public:
void show() override {
fill_solid(leds, NUM_LEDS, CRGB::Black);
uint8_t hue = gHue;
Vector3f accelDir = accelerometerDirection();
for (int8_t y = -3; y < LEDS_PER_ROW+3; y++) {
for (int8_t x = -3; x < LEDS_PER_ROW+3; x++) {
for (int8_t z = -3; z < LEDS_PER_ROW+3; z++) {
setPixel3dCompensated(accelDir, x, y, z, CHSV(hue,255,255));
}
}
hue+=15;
}
}
};
//TODO: generalize pattern list
static Pattern * const gTransitions[] = {
new RainbowSegments(),
new Pulse(),
new FillSolid(),
new RainbowGravity(),
new RainbowBrightnessChase(),
};
static Pattern * const gPatterns[] = {
new HeightfieldRipple(),
new RelativelyPrimeFade(),
new LaunchingPlanes(),
new FallingPlanes(),
new Rain(),
new MakeWaves(),
new ChaseThroughPanels(),
new PulseSegments(),
new CrawlWithHighlight(),
new ChaseRainbow(),
new FlickerSegments(),
new ChaseSolid(),
new SweepTwoSolid(),
new SweepTwoRainbow(),
};
#define NUM_CUES 7
#define SECONDS_PER_CUE 30
#define TRANSITION_CHANGE_CUE 0
#define PATTERN_CHANGE_CUE 1
#define RANDOM_FROM_ARRAY(arr) arr[random16(ARRAY_SIZE(arr))]
static uint8_t gCurrentCue = 0;