forked from kitesurfer1404/WS2812FX
-
Notifications
You must be signed in to change notification settings - Fork 5
/
WS2812FX.c
1593 lines (1297 loc) · 43 KB
/
WS2812FX.c
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
/*
WS2812FX.cpp - Library for WS2812 LED effects.
Harm Aldick - 2016
www.aldick.org
Ported to esp-open-rtos by PCSaito - 2018
www.github.com/pcsaito
FEATURES
* A lot of blinken modes and counting
LICENSE
The MIT License (MIT)
Copyright (c) 2016 Harm Aldick
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
CHANGELOG
2016-05-28 Initial beta release
2016-06-03 Code cleanup, minor improvements, new modes
2016-06-04 2 new fx, fixed setColor (now also resets _mode_color)
2017-02-02 removed "blackout" on mode, speed or color-change
2018-04-24 ported to esp-open-rtos to use in esp-homekit-demo
*/
#include "WS2812FX.h"
#include <math.h>
#include <stdlib.h>
#include "FreeRTOS.h"
#include "task.h"
#define CALL_MODE(n) _mode[n]();
uint8_t _mode_index = DEFAULT_MODE;
uint8_t _speed = DEFAULT_SPEED;
uint8_t _brightness = 0;
uint8_t _target_brightness = 0;
bool _running = false;
bool _inverted = false;
bool _slow_start = false;
uint16_t _led_count = 0;
uint32_t _color = DEFAULT_COLOR;
uint32_t _mode_color = DEFAULT_COLOR;
uint32_t _mode_delay = 100;
uint32_t _counter_mode_call = 0;
uint32_t _counter_mode_step = 0;
uint32_t _mode_last_call_time = 0;
uint8_t get_random_wheel_index(uint8_t);
mode _mode[MODE_COUNT];
ws2812_pixel_t *pixels;
//Helpers
uint32_t color32(uint8_t r, uint8_t g, uint8_t b) {
return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
}
uint32_t constrain(uint32_t amt, uint32_t low, uint32_t high) {
return (amt < low) ? low : ((amt > high) ? high : amt);
}
float fconstrain(float amt, float low, float high) {
return (amt < low) ? low : ((amt > high) ? high : amt);
}
uint32_t randomInRange(uint32_t min, uint32_t max) {
if (min < max) {
uint32_t randomValue = rand() % (max - min);
return randomValue + min;
} else if (min == max) {
return min;
}
return 0;
}
long map(long x, long in_min, long in_max, long out_min, long out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
static uint16_t min(uint16_t a, uint16_t b) {
return (a > b) ? b : a;
}
static uint32_t max(uint32_t a, uint32_t b) {
return (a > b) ? a : b;
}
//LED Adapter
void WS2812_show(void) {
ws2812_i2s_update(pixels, PIXEL_RGB);
}
void WS2812_setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b) {
if (_inverted) {
n = (_led_count - 1) - n;
}
ws2812_pixel_t px;
px.red = map(r, 0, BRIGHTNESS_MAX, BRIGHTNESS_MIN, _brightness);
px.green = map(g, 0, BRIGHTNESS_MAX, BRIGHTNESS_MIN, _brightness);
px.blue = map(b, 0, BRIGHTNESS_MAX, BRIGHTNESS_MIN, _brightness);
pixels[n] = px;
}
void WS2812_setPixelColor32(uint16_t n, uint32_t c) {
uint8_t r = (uint8_t)(c >> 16);
uint8_t g = (uint8_t)(c >> 8);
uint8_t b = (uint8_t)c;
WS2812_setPixelColor(n, r, g, b);
}
uint32_t WS2812_getPixelColor(uint16_t n) {
return color32(pixels[n].red, pixels[n].green, pixels[n].blue);
}
void WS2812_clear() {
for (int i = 0; i < _led_count; i++) {
WS2812_setPixelColor(i, 0, 0, 0);
}
ws2812_i2s_update(pixels, PIXEL_RGB);
}
void WS2812_init(uint16_t pixel_count) {
_led_count = pixel_count;
pixels = (ws2812_pixel_t*) malloc(_led_count * sizeof(ws2812_pixel_t));
// initialise the onboard led as a secondary indicator (handy for testing)
// gpio_enable(LED_INBUILT_GPIO, GPIO_OUTPUT);
// initialise the LED strip
ws2812_i2s_init(_led_count, PIXEL_RGB);
WS2812_clear();
}
//WS2812FX
void WS2812FX_init(uint16_t pixel_count) {
WS2812_init(pixel_count);
xTaskCreate(WS2812FX_service, "fxService", 255, NULL, 2, NULL);
WS2812FX_initModes();
WS2812FX_start();
}
void WS2812FX_service(void *_args) {
uint32_t now = 0;
while (true) {
if(_running) {
//printf("_brightness : _target_brightness %ld : %ld \n", _brightness, _target_brightness);
if (_slow_start) {
if ((_brightness < _target_brightness)) {
uint8_t new_brightness = (BRIGHTNESS_FILTER * _brightness) + ((1.0-BRIGHTNESS_FILTER) * _target_brightness);
float soft_start = fconstrain((float)(_brightness * 4) / (float)BRIGHTNESS_MAX, 0.1, 1.0);
uint8_t delta = (new_brightness - _brightness) * soft_start;
_brightness = _brightness + constrain(delta, 1, delta);
} else {
_brightness = (BRIGHTNESS_FILTER * _brightness) + ((1.0-BRIGHTNESS_FILTER) * _target_brightness);
}
} else {
_brightness = _target_brightness;
}
now = xTaskGetTickCount() * portTICK_PERIOD_MS;
if(now - _mode_last_call_time > _mode_delay) {
_counter_mode_call++;
_mode_last_call_time = now;
CALL_MODE(_mode_index);
//gpio_toggle(LED_INBUILT_GPIO); //led indicator
}
}
vTaskDelay(33 / portTICK_PERIOD_MS);
}
}
void WS2812FX_start() {
_counter_mode_call = 0;
_counter_mode_step = 0;
_running = true;
}
void WS2812FX_stop() {
_running = false;
}
void WS2812FX_setMode360(float m) {
//printf("WS2812FX_setMode360: %f", m);
uint8_t mode = map((uint16_t)m, 0, 360, 0, MODE_COUNT-1);
//printf("WS2812FX_setMode: %d", mode);
WS2812FX_setMode(mode);
}
void WS2812FX_setMode(uint8_t m) {
_counter_mode_call = 0;
_counter_mode_step = 0;
_mode_index = constrain(m, 0, MODE_COUNT-1);
_mode_color = _color;
}
void WS2812FX_setSpeed(uint8_t s) {
_counter_mode_call = 0;
_counter_mode_step = 0;
_speed = constrain(s, SPEED_MIN, SPEED_MAX);
}
void WS2812FX_setColor(uint8_t r, uint8_t g, uint8_t b) {
WS2812FX_setColor32(((uint32_t)r << 16) | ((uint32_t)g << 8) | b);
}
void WS2812FX_setColor32(uint32_t c) {
_color = c;
_counter_mode_call = 0;
_counter_mode_step = 0;
_mode_color = _color;
}
void WS2812FX_setBrightness(uint8_t b) {
_target_brightness = constrain(b, BRIGHTNESS_MIN, BRIGHTNESS_MAX);
//printf("WS2812FX_setBrightness: %ld \n", _target_brightness);
}
void WS2812FX_forceBrightness(uint8_t b) {
_target_brightness = constrain(b, BRIGHTNESS_MIN, BRIGHTNESS_MAX);
_brightness = _target_brightness;
}
bool WS2812FX_isRunning() {
return _running;
}
uint8_t WS2812FX_getMode(void) {
return _mode_index;
}
uint8_t WS2812FX_getSpeed(void) {
return _speed;
}
uint8_t WS2812FX_getBrightness(void) {
return _target_brightness;
}
uint16_t WS2812FX_getLength(void) {
return _led_count;
}
uint8_t WS2812FX_getModeCount(void) {
return MODE_COUNT;
}
uint32_t WS2812FX_getColor(void) {
return _color;
}
void WS2812FX_setInverted(bool inverted) {
_inverted = inverted;
}
void WS2812FX_setSlowStart(bool slow_start) {
_slow_start = slow_start;
}
/* #####################################################
#
# Color and Blinken Functions
#
##################################################### */
/*
* Turns everything off. Doh.
*/
void WS2812FX_strip_off() {
WS2812_clear();
}
/*
* Put a value 0 to 255 in to get a color value.
* The colours are a transition r -> g -> b -> back to r
* Inspired by the Adafruit examples.
*/
uint32_t WS2812FX_color_wheel(uint8_t pos) {
pos = 255 - pos;
if(pos < 85) {
return ((uint32_t)(255 - pos * 3) << 16) | ((uint32_t)(0) << 8) | (pos * 3);
} else if(pos < 170) {
pos -= 85;
return ((uint32_t)(0) << 16) | ((uint32_t)(pos * 3) << 8) | (255 - pos * 3);
} else {
pos -= 170;
return ((uint32_t)(pos * 3) << 16) | ((uint32_t)(255 - pos * 3) << 8) | (0);
}
}
/*
* Returns a new, random wheel index with a minimum distance of 42 from pos.
*/
uint8_t WS2812FX_get_random_wheel_index(uint8_t pos) {
uint8_t r = 0;
uint8_t x = 0;
uint8_t y = 0;
uint8_t d = 0;
while(d < 42) {
r = randomInRange(0, 256);
x = abs(pos - r);
y = 255 - x;
d = min(x, y);
}
return r;
}
/*
* No blinking. Just plain old static light.
*/
void WS2812FX_mode_static(void) {
for(uint16_t i=0; i < _led_count; i++) {
WS2812_setPixelColor32(i, _color);
}
WS2812_show();
_mode_delay = 50;
}
/*
* Normal blinking. 50% on/off time.
*/
void WS2812FX_mode_blink(void) {
if(_counter_mode_call % 2 == 1) {
for(uint16_t i=0; i < _led_count; i++) {
WS2812_setPixelColor32(i, _color);
}
WS2812_show();
} else {
WS2812FX_strip_off();
}
_mode_delay = 100 + ((1986 * (uint32_t)(SPEED_MAX - _speed)) / SPEED_MAX);
}
/*
* Lights all LEDs after each other up. Then turns them in
* that order off. Repeat.
*/
void WS2812FX_mode_color_wipe(void) {
if(_counter_mode_step < _led_count) {
WS2812_setPixelColor32(_counter_mode_step, _color);
} else {
WS2812_setPixelColor32(_counter_mode_step - _led_count, 0);
}
WS2812_show();
_counter_mode_step = (_counter_mode_step + 1) % (_led_count * 2);
_mode_delay = 5 + ((50 * (uint32_t)(SPEED_MAX - _speed)) / _led_count);
}
/*
* Turns all LEDs after each other to a random color.
* Then starts over with another color.
*/
void WS2812FX_mode_color_wipe_random(void) {
if(_counter_mode_step == 0) {
_mode_color = WS2812FX_get_random_wheel_index(_mode_color);
}
WS2812_setPixelColor32(_counter_mode_step, WS2812FX_color_wheel(_mode_color));
WS2812_show();
_counter_mode_step = (_counter_mode_step + 1) % _led_count;
_mode_delay = 5 + ((50 * (uint32_t)(SPEED_MAX - _speed)) / _led_count);
}
/*
* Lights all LEDs in one random color up. Then switches them
* to the next random color.
*/
void WS2812FX_mode_random_color(void) {
_mode_color = WS2812FX_get_random_wheel_index(_mode_color);
for(uint16_t i=0; i < _led_count; i++) {
WS2812_setPixelColor32(i, WS2812FX_color_wheel(_mode_color));
}
WS2812_show();
_mode_delay = 100 + ((5000 * (uint32_t)(SPEED_MAX - _speed)) / SPEED_MAX);
}
/*
* Lights every LED in a random color. Changes one random LED after the other
* to another random color.
*/
void WS2812FX_mode_single_dynamic(void) {
if(_counter_mode_call == 0) {
for(uint16_t i=0; i < _led_count; i++) {
WS2812_setPixelColor32(i, WS2812FX_color_wheel(randomInRange(0, 256)));
}
}
WS2812_setPixelColor32(randomInRange(0, _led_count), WS2812FX_color_wheel(randomInRange(0, 256)));
WS2812_show();
_mode_delay = 10 + ((5000 * (uint32_t)(SPEED_MAX - _speed)) / SPEED_MAX);
}
/*
* Lights every LED in a random color. Changes all LED at the same time
* to new random colors.
*/
void WS2812FX_mode_multi_dynamic(void) {
for(uint16_t i=0; i < _led_count; i++) {
WS2812_setPixelColor32(i, WS2812FX_color_wheel(randomInRange(0, 256)));
}
WS2812_show();
_mode_delay = 100 + ((5000 * (uint32_t)(SPEED_MAX - _speed)) / SPEED_MAX);
}
/*
* Does the "standby-breathing" of well known i-Devices. Fixed Speed.
* Use mode "fade" if you like to have something similar with a different speed.
*/
void WS2812FX_mode_breath(void) {
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // step
uint16_t breath_delay_steps[] = { 7, 9, 13, 15, 16, 17, 18, 930, 19, 18, 15, 13, 9, 7, 4, 5, 10 }; // magic numbers for breathing LED
uint8_t breath_brightness_steps[] = { 150, 125, 100, 75, 50, 25, 16, 15, 16, 25, 50, 75, 100, 125, 150, 220, 255 }; // even more magic numbers!
if(_counter_mode_call == 0) {
_mode_color = breath_brightness_steps[0] + 1;
}
uint8_t breath_brightness = _mode_color; // we use _mode_color to store the brightness
if(_counter_mode_step < 8) {
breath_brightness--;
} else {
breath_brightness++;
}
// update index of current delay when target brightness is reached, start over after the last step
if(breath_brightness == breath_brightness_steps[_counter_mode_step]) {
_counter_mode_step = (_counter_mode_step + 1) % (sizeof(breath_brightness_steps)/sizeof(uint8_t));
}
for(uint16_t i=0; i < _led_count; i++) {
WS2812_setPixelColor32(i, _color); // set all LEDs to selected color
}
int b = map(breath_brightness, 0, 255, 0, _brightness); // keep brightness below brightness set by user
WS2812FX_forceBrightness(b); // set new brightness to leds
WS2812_show();
_mode_color = breath_brightness; // we use _mode_color to store the brightness
_mode_delay = breath_delay_steps[_counter_mode_step];
}
/*
* Fades the LEDs on and (almost) off again.
*/
void WS2812FX_mode_fade(void) {
for(uint16_t i=0; i < _led_count; i++) {
WS2812_setPixelColor32(i, _color);
}
int b = _counter_mode_step - 127;
b = 255 - (abs(b) * 2);
b = map(b, 0, 255, min(25, _brightness), _brightness);
WS2812FX_forceBrightness(b);
WS2812_show();
_counter_mode_step = (_counter_mode_step + 1) % 256;
_mode_delay = 5 + ((15 * (uint32_t)(SPEED_MAX - _speed)) / SPEED_MAX);
}
/*
* Runs a single pixel back and forth.
*/
void WS2812FX_mode_scan(void) {
if(_counter_mode_step > (_led_count*2) - 2) {
_counter_mode_step = 0;
}
_counter_mode_step++;
int i = _counter_mode_step - (_led_count - 1);
i = abs(i);
WS2812_clear();
WS2812_setPixelColor32(abs(i), _color);
WS2812_show();
_mode_delay = 10 + ((30 * (uint32_t)(SPEED_MAX - _speed)) / _led_count);
}
/*
* Runs two pixel back and forth in opposite directions.
*/
void WS2812FX_mode_dual_scan(void) {
if(_counter_mode_step > (_led_count*2) - 2) {
_counter_mode_step = 0;
}
_counter_mode_step++;
int i = _counter_mode_step - (_led_count - 1);
i = abs(i);
WS2812_clear();
WS2812_setPixelColor32(i, _color);
WS2812_setPixelColor32(_led_count - (i+1), _color);
WS2812_show();
_mode_delay = 10 + ((30 * (uint32_t)(SPEED_MAX - _speed)) / _led_count);
}
/*
* Cycles all LEDs at once through a rainbow.
*/
void WS2812FX_mode_rainbow(void) {
uint32_t color = WS2812FX_color_wheel(_counter_mode_step);
for(uint16_t i=0; i < _led_count; i++) {
WS2812_setPixelColor32(i, color);
}
WS2812_show();
_counter_mode_step = (_counter_mode_step + 1) % 256;
_mode_delay = 1 + ((100 * (uint32_t)(SPEED_MAX - _speed)) / SPEED_MAX);
}
/*
* Cycles a rainbow over the entire string of LEDs.
*/
void WS2812FX_mode_rainbow_cycle(void) {
for(uint16_t i=0; i < _led_count; i++) {
WS2812_setPixelColor32(i, WS2812FX_color_wheel(((i * 256 / _led_count) + _counter_mode_step) % 256));
}
WS2812_show();
_counter_mode_step = (_counter_mode_step + 1) % 256;
_mode_delay = 1 + ((50 * (uint32_t)(SPEED_MAX - _speed)) / SPEED_MAX);
}
/*
* Theatre-style crawling lights.
* Inspired by the Adafruit examples.
*/
void WS2812FX_mode_theater_chase(void) {
uint8_t j = _counter_mode_call % 6;
if(j % 2 == 0) {
for(uint16_t i=0; i < _led_count; i=i+3) {
WS2812_setPixelColor32(i+(j/2), _color);
}
WS2812_show();
_mode_delay = 50 + ((500 * (uint32_t)(SPEED_MAX - _speed)) / SPEED_MAX);
} else {
for(uint16_t i=0; i < _led_count; i=i+3) {
WS2812_setPixelColor32(i+(j/2), 0);
}
_mode_delay = 1;
}
}
/*
* Theatre-style crawling lights with rainbow effect.
* Inspired by the Adafruit examples.
*/
void WS2812FX_mode_theater_chase_rainbow(void) {
uint8_t j = _counter_mode_call % 6;
if(j % 2 == 0) {
for(uint16_t i=0; i < _led_count; i=i+3) {
WS2812_setPixelColor32(i+(j/2), WS2812FX_color_wheel((i+_counter_mode_step) % 256));
}
WS2812_show();
_mode_delay = 50 + ((500 * (uint32_t)(SPEED_MAX - _speed)) / SPEED_MAX);
} else {
for(uint16_t i=0; i < _led_count; i=i+3) {
WS2812_setPixelColor32(i+(j/2), 0);
}
_mode_delay = 1;
}
_counter_mode_step = (_counter_mode_step + 1) % 256;
}
/*
* Running lights effect with smooth sine transition.
*/
void WS2812FX_mode_running_lights(void) {
uint8_t r = ((_color >> 16) & 0xFF);
uint8_t g = ((_color >> 8) & 0xFF);
uint8_t b = (_color & 0xFF);
for(uint16_t i=0; i < _led_count; i++) {
int s = (sin(i+_counter_mode_call) * 127) + 128;
WS2812_setPixelColor(i, (((uint32_t)(r * s)) / 255), (((uint32_t)(g * s)) / 255), (((uint32_t)(b * s)) / 255));
}
WS2812_show();
_mode_delay = 35 + ((350 * (uint32_t)(SPEED_MAX - _speed)) / SPEED_MAX);
}
/*
* Blink several LEDs on, reset, repeat.
* Inspired by www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/
*/
void WS2812FX_mode_twinkle(void) {
if(_counter_mode_step == 0) {
WS2812FX_strip_off();
uint32_t min_leds = max(1, _led_count/5); // make sure, at least one LED is on
uint32_t max_leds = max(1, _led_count/2); // make sure, at least one LED is on
_counter_mode_step = randomInRange(min_leds, max_leds);
}
WS2812_setPixelColor32(randomInRange(0, _led_count), _mode_color);
WS2812_show();
_counter_mode_step--;
_mode_delay = 50 + ((1986 * (uint32_t)(SPEED_MAX - _speed)) / SPEED_MAX);
}
/*
* Blink several LEDs in random colors on, reset, repeat.
* Inspired by www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/
*/
void WS2812FX_mode_twinkle_random(void) {
_mode_color = WS2812FX_color_wheel(randomInRange(0, 256));
WS2812FX_mode_twinkle();
}
/*
* Blink several LEDs on, fading out.
*/
void WS2812FX_mode_twinkle_fade(void) {
for(uint16_t i=0; i < _led_count; i++) {
uint32_t px_rgb = WS2812_getPixelColor(i);
uint8_t px_r = (px_rgb & 0x00FF0000) >> 16;
uint8_t px_g = (px_rgb & 0x0000FF00) >> 8;
uint8_t px_b = (px_rgb & 0x000000FF) >> 0;
// fade out (divide by 2)
px_r = px_r >> 1;
px_g = px_g >> 1;
px_b = px_b >> 1;
WS2812_setPixelColor(i, px_r, px_g, px_b);
}
if(randomInRange(0, 3) == 0) {
WS2812_setPixelColor32(randomInRange(0, _led_count), _mode_color);
}
WS2812_show();
_mode_delay = 100 + ((100 * (uint32_t)(SPEED_MAX - _speed)) / SPEED_MAX);
}
/*
* Blink several LEDs in random colors on, fading out.
*/
void WS2812FX_mode_twinkle_fade_random(void) {
_mode_color = WS2812FX_color_wheel(randomInRange(0, 256));
WS2812FX_mode_twinkle_fade();
}
/*
* Blinks one LED at a time.
* Inspired by www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/
*/
void WS2812FX_mode_sparkle(void) {
WS2812_clear();
WS2812_setPixelColor32(randomInRange(0, _led_count),_color);
WS2812_show();
_mode_delay = 10 + ((200 * (uint32_t)(SPEED_MAX - _speed)) / SPEED_MAX);
}
/*
* Lights all LEDs in the _color. Flashes single white pixels randomly.
* Inspired by www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/
*/
void WS2812FX_mode_flash_sparkle(void) {
for(uint16_t i=0; i < _led_count; i++) {
WS2812_setPixelColor32(i, _color);
}
if(randomInRange(0, 10) == 7) {
WS2812_setPixelColor(randomInRange(0, _led_count), 255, 255, 255);
_mode_delay = 20;
} else {
_mode_delay = 20 + ((200 * (uint32_t)(SPEED_MAX - _speed)) / SPEED_MAX);
}
WS2812_show();
}
/*
* Like flash sparkle. With more flash.
* Inspired by www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/
*/
void WS2812FX_mode_hyper_sparkle(void) {
for(uint16_t i=0; i < _led_count; i++) {
WS2812_setPixelColor32(i, _color);
}
if(randomInRange(0, 10) < 4) {
for(uint16_t i=0; i < max(1, _led_count/3); i++) {
WS2812_setPixelColor(randomInRange(0, _led_count), 255, 255, 255);
}
_mode_delay = 20;
} else {
_mode_delay = 15 + ((120 * (uint32_t)(SPEED_MAX - _speed)) / SPEED_MAX);
}
WS2812_show();
}
/*
* Classic Strobe effect.
*/
void WS2812FX_mode_strobe(void) {
if(_counter_mode_call % 2 == 0) {
for(uint16_t i=0; i < _led_count; i++) {
WS2812_setPixelColor32(i, _color);
}
_mode_delay = 20;
} else {
for(uint16_t i=0; i < _led_count; i++) {
WS2812_setPixelColor32(i, 0);
}
_mode_delay = 50 + ((1986 * (uint32_t)(SPEED_MAX - _speed)) / SPEED_MAX);
}
WS2812_show();
}
/*
* Strobe effect with different strobe count and pause, controled by _speed.
*/
void WS2812FX_mode_multi_strobe(void) {
for(uint16_t i=0; i < _led_count; i++) {
WS2812_setPixelColor32(i, 0);
}
if(_counter_mode_step < (2 * ((_speed / 10) + 1))) {
if(_counter_mode_step % 2 == 0) {
for(uint16_t i=0; i < _led_count; i++) {
WS2812_setPixelColor32(i, _color);
}
_mode_delay = 20;
} else {
_mode_delay = 50;
}
} else {
_mode_delay = 100 + ((9 - (_speed % 10)) * 125);
}
WS2812_show();
_counter_mode_step = (_counter_mode_step + 1) % ((2 * ((_speed / 10) + 1)) + 1);
}
/*
* Classic Strobe effect. Cycling through the rainbow.
*/
void WS2812FX_mode_strobe_rainbow(void) {
if(_counter_mode_call % 2 == 0) {
for(uint16_t i=0; i < _led_count; i++) {
WS2812_setPixelColor32(i, WS2812FX_color_wheel(_counter_mode_call % 256));
}
_mode_delay = 20;
} else {
for(uint16_t i=0; i < _led_count; i++) {
WS2812_setPixelColor32(i, 0);
}
_mode_delay = 50 + ((1986 * (uint32_t)(SPEED_MAX - _speed)) / SPEED_MAX);
}
WS2812_show();
}
/*
* Classic Blink effect. Cycling through the rainbow.
*/
void WS2812FX_mode_blink_rainbow(void) {
if(_counter_mode_call % 2 == 1) {
for(uint16_t i=0; i < _led_count; i++) {
WS2812_setPixelColor32(i, WS2812FX_color_wheel(_counter_mode_call % 256));
}
WS2812_show();
} else {
WS2812FX_strip_off();
}
_mode_delay = 100 + ((1986 * (uint32_t)(SPEED_MAX - _speed)) / SPEED_MAX);
}
/*
* _color running on white.
*/
void WS2812FX_mode_chase_white(void) {
for(uint16_t i=0; i < _led_count; i++) {
WS2812_setPixelColor(i, 255, 255, 255);
}
uint16_t n = _counter_mode_step;
uint16_t m = (_counter_mode_step + 1) % _led_count;
WS2812_setPixelColor32(n, _color);
WS2812_setPixelColor32(m, _color);
WS2812_show();
_counter_mode_step = (_counter_mode_step + 1) % _led_count;
_mode_delay = 10 + ((30 * (uint32_t)(SPEED_MAX - _speed)) / _led_count);
}
/*
* White running on _color.
*/
void WS2812FX_mode_chase_color(void) {
for(uint16_t i=0; i < _led_count; i++) {
WS2812_setPixelColor32(i, _color);
}
uint16_t n = _counter_mode_step;
uint16_t m = (_counter_mode_step + 1) % _led_count;
WS2812_setPixelColor(n, 255, 255, 255);
WS2812_setPixelColor(m, 255, 255, 255);
WS2812_show();
_counter_mode_step = (_counter_mode_step + 1) % _led_count;
_mode_delay = 10 + ((30 * (uint32_t)(SPEED_MAX - _speed)) / _led_count);
}
/*
* White running followed by random color.
*/
void WS2812FX_mode_chase_random(void) {
if(_counter_mode_step == 0) {
WS2812_setPixelColor32(_led_count-1, WS2812FX_color_wheel(_mode_color));
_mode_color = WS2812FX_get_random_wheel_index(_mode_color);
}
for(uint16_t i=0; i < _counter_mode_step; i++) {
WS2812_setPixelColor32(i, WS2812FX_color_wheel(_mode_color));
}
uint16_t n = _counter_mode_step;
uint16_t m = (_counter_mode_step + 1) % _led_count;
WS2812_setPixelColor(n, 255, 255, 255);
WS2812_setPixelColor(m, 255, 255, 255);
WS2812_show();
_counter_mode_step = (_counter_mode_step + 1) % _led_count;
_mode_delay = 10 + ((30 * (uint32_t)(SPEED_MAX - _speed)) / _led_count);
}
/*
* White running on rainbow.
*/
void WS2812FX_mode_chase_rainbow(void) {
for(uint16_t i=0; i < _led_count; i++) {
WS2812_setPixelColor32(i, WS2812FX_color_wheel(((i * 256 / _led_count) + (_counter_mode_call % 256)) % 256));
}
uint16_t n = _counter_mode_step;
uint16_t m = (_counter_mode_step + 1) % _led_count;
WS2812_setPixelColor(n, 255, 255, 255);
WS2812_setPixelColor(m, 255, 255, 255);
WS2812_show();
_counter_mode_step = (_counter_mode_step + 1) % _led_count;
_mode_delay = 10 + ((30 * (uint32_t)(SPEED_MAX - _speed)) / _led_count);
}
/*
* White flashes running on _color.
*/
void WS2812FX_mode_chase_flash(void) {
const static uint8_t flash_count = 4;
uint8_t flash_step = _counter_mode_call % ((flash_count * 2) + 1);
for(uint16_t i=0; i < _led_count; i++) {
WS2812_setPixelColor32(i, _color);
}
if(flash_step < (flash_count * 2)) {
if(flash_step % 2 == 0) {
uint16_t n = _counter_mode_step;
uint16_t m = (_counter_mode_step + 1) % _led_count;
WS2812_setPixelColor(n, 255, 255, 255);
WS2812_setPixelColor(m, 255, 255, 255);
_mode_delay = 20;
} else {
_mode_delay = 30;
}
} else {
_counter_mode_step = (_counter_mode_step + 1) % _led_count;
_mode_delay = 10 + ((30 * (uint32_t)(SPEED_MAX - _speed)) / _led_count);
}
WS2812_show();
}
/*
* White flashes running, followed by random color.
*/
void WS2812FX_mode_chase_flash_random(void) {
const static uint8_t flash_count = 4;
uint8_t flash_step = _counter_mode_call % ((flash_count * 2) + 1);
for(uint16_t i=0; i < _counter_mode_step; i++) {
WS2812_setPixelColor32(i, WS2812FX_color_wheel(_mode_color));
}
if(flash_step < (flash_count * 2)) {
uint16_t n = _counter_mode_step;
uint16_t m = (_counter_mode_step + 1) % _led_count;
if(flash_step % 2 == 0) {
WS2812_setPixelColor(n, 255, 255, 255);
WS2812_setPixelColor(m, 255, 255, 255);
_mode_delay = 20;
} else {
WS2812_setPixelColor32(n, WS2812FX_color_wheel(_mode_color));
WS2812_setPixelColor(m, 0, 0, 0);
_mode_delay = 30;
}
} else {
_counter_mode_step = (_counter_mode_step + 1) % _led_count;
_mode_delay = 1 + ((10 * (uint32_t)(SPEED_MAX - _speed)) / _led_count);
if(_counter_mode_step == 0) {
_mode_color = WS2812FX_get_random_wheel_index(_mode_color);
}
}
WS2812_show();
}