-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheffects.py
1322 lines (1077 loc) · 41.9 KB
/
effects.py
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
# SPDX-FileCopyrightText: Copyright (c) 2025 Jose D. Montoya
#
# SPDX-License-Identifier: MIT
"""
`effects`
================================================================================
Create and control LED effects.
* Author: Jose D. Montoya
"""
import time
from random import choice
from colors import (
BLACK,
BLUE,
RED,
PURPLE,
CYAN,
ORANGE,
ORANGEYELLOW,
BLUE,
GREEN,
YELLOW,
)
from neopixel import NEOPIXEL
from functions import rgb255, hsv_to_rgb
import math
import random
def blink(
led_object,
color: tuple = RED,
background_color: tuple = BLACK,
dwell: float = 0.5,
duration: int = 5,
) -> None:
"""
Blink the NeoPixels.
:param tuple color: the color to blink. Default is RED
:param tuple background_color: the background color. Default is BLACK
:param float dwell: time delay between each color change: default 0.5 seconds
:param int duration: the duration in seconds. Default is 5 seconds
:return: None
"""
start_time = time.time()
while time.time() - start_time < duration:
led_object.neopixel_list = [color] * led_object.num_leds
led_object.ShowNeoPixels(led_object.neopixel_list)
time.sleep(dwell)
led_object.neopixel_list = [background_color] * led_object.num_leds
led_object.ShowNeoPixels(led_object.neopixel_list)
time.sleep(dwell)
def chasing_color(
led_object,
palette: list = [RED, GREEN, BLUE],
time_delta: float = 0.1,
duration: int = 10,
) -> None:
"""
Cycle through the colors in the list.
:param tuple color: the color to chase. Default is [RED, GREEN, BLUE]
:param float time_delta: time delay between each color change: default 0.1 seconds
:param int duration: duration in seconds: default 10 seconds
:return: None
"""
rgb = 0
i = 0
led_object.fill_all(color=BLACK)
start_time = time.time()
if led_object.palette_colors is None:
palette = [RED, GREEN, BLUE]
else:
buf = led_object.palette_colors
colors_palette = []
for _ in range(3):
selection = choice(buf)
colors_palette.append(selection)
buf.remove(selection)
palette = colors_palette
while time.time() - start_time < duration:
if rgb == 0:
color = palette[0]
elif rgb == 1:
color = palette[1]
else:
color = palette[2]
led_object.neopixel_list[i] = color
led_object.ShowNeoPixels(led_object.neopixel_list)
time.sleep(time_delta / 3)
led_object.neopixel_list[i] = BLACK
led_object.ShowNeoPixels(led_object.neopixel_list)
rgb = (rgb + 1) % 3
i = (i + 1) % led_object.num_leds
time.sleep(time_delta)
def blink_rainbow(
led_object,
background_color=BLACK,
dwell: float = 0.2,
duration: int = 10,
) -> None:
"""
Blink the NeoPixels.
:param tuple background_color: the background color. Default is BLACK
:param float dwell: time delay between each color change: default 0.2 seconds
:param int duration: the duration in seconds. Default is 5 seconds
:return: None
"""
from rainbow import rainbow_colors
rainbow_set = rainbow_colors
seed = choice(range(0, 31))
start_time = time.time()
while time.time() - start_time < duration:
led_object.neopixel_list = [rainbow_set[seed]] * led_object.num_leds
led_object.ShowNeoPixels(led_object.neopixel_list)
time.sleep(dwell)
led_object.neopixel_list = [background_color] * led_object.num_leds
led_object.ShowNeoPixels(led_object.neopixel_list)
time.sleep(dwell)
if seed < 31:
seed = seed + 1
else:
seed = choice(range(0, 31))
def follow_rgb(
led_object,
loops: int = 3,
color_list: any = None,
dwell: float = 0.2,
duration: int = 5,
) -> None:
"""
Follow the colors Red, White, Blue.
:param int loops: number of loops. Default is 3
:param list color_list: list of colors. Default is None, You can pass a list of colors using
the RGB format. For example [(255, 0, 0), (0, 255, 0), (0, 0, 255)]. Also you can define
a palette of colors in the led object and the function will use the colors in the palette.
:param float dwell: time delay between each color change. Default is 0.2 seconds
:param int duration: duration in seconds. Default is 5 seconds
:return: None
"""
if color_list is None:
if led_object.palette_colors is None:
color_list = [BLACK, RED, GREEN, BLUE, YELLOW, PURPLE, CYAN, ORANGE]
else:
if len(led_object.palette_colors) > led_object.num_leds // 2:
color_list = led_object.palette_colors[::2][
: led_object.num_leds // 2
]
reference = len(color_list)
start_time = time.time()
while time.time() - start_time < duration:
for i in range(led_object.num_leds * loops):
for value in range(reference):
led_object.neopixel_list[(i + value) % led_object.num_leds] = (
color_list[value]
)
led_object.ShowNeoPixels(led_object.neopixel_list)
time.sleep(dwell)
def wipe(
led_object,
color1: tuple = GREEN,
color2: tuple = YELLOW,
delta_time: float = 0.1,
ccw: bool = False,
clear: bool = False,
duration: int = 5,
) -> None:
"""
Wipe the NeoPixels.
:param tuple color1: the color to wipe. Default is GREEN
:param tuple color2: the color to wipe. Default is YELLOW
:param float delta_time: time delay between each color change: default 0.1 seconds
:param bool ccw: counter-clockwise or clockwise. Default is False
:param bool clear: clear the NeoPixels after each color. Default is False
:duration int duration: duration in seconds. Default is 5 seconds
:return: None
"""
led_object.fill_all(color=BLACK)
start_time = time.time()
while time.time() - start_time < duration:
for i in range(led_object.num_leds):
if ccw:
led_object.neopixel_list[led_object.num_leds - 1 - i] = color1
else:
led_object.neopixel_list[i] = color1
led_object.ShowNeoPixels(led_object.neopixel_list)
time.sleep(delta_time)
if clear:
led_object.fill_all(color=BLACK)
for i in range(led_object.num_leds):
if ccw:
led_object.neopixel_list[led_object.num_leds - 1 - i] = color2
else:
led_object.neopixel_list[i] = color2
led_object.ShowNeoPixels(led_object.neopixel_list)
time.sleep(delta_time)
if clear:
led_object.fill_all(color=BLACK)
def pacman(led_object, duration: int = 15):
"""
PACMAN ANIMATION Adapted from https://github.com/wled-dev/WLED/pull/4536 # by BobLoeffler68
MIT LICENSE
:param led_object: led object
:param neopixel_list: list of neopixel colors
:param int num_leds: number of leds.
:param int duration: duration in seconds. Default is 15 seconds
"""
direction = 1
black_dir = -1
if led_object.num_leds > 150:
start_blinking_ghosts = led_object.num_leds // 4
else:
start_blinking_ghosts = led_object.num_leds // 3
pacman = [BLUE, 10]
ghosts_original = [[RED, 6], [PURPLE, 4], [CYAN, 2], [ORANGE, 0]]
ghosts = [[RED, 6], [PURPLE, 4], [CYAN, 2], [ORANGE, 0]]
power_pellet = [ORANGEYELLOW, led_object.num_leds - 1]
led_object.neopixel_list[power_pellet[1]] = power_pellet[0]
NEOPIXEL.ShowNeoPixels(led_object, led_object.neopixel_list)
ghost_timer = time.ticks_ms()
flag = "beep"
start_time = time.time()
while time.time() - start_time < duration:
delta = time.ticks_ms() - ghost_timer
if delta > 250:
if power_pellet[0] == ORANGEYELLOW:
power_pellet[0] = BLACK
else:
power_pellet[0] = ORANGEYELLOW
led_object.neopixel_list[power_pellet[1]] = power_pellet[0]
ghost_timer = time.ticks_ms()
if pacman[1] >= led_object.num_leds - 2:
direction = direction * -1
black_dir = black_dir * -1
for ghost in ghosts:
ghost[0] = BLUE
led_object.neopixel_list[pacman[1]] = pacman[0]
led_object.neopixel_list[pacman[1] + black_dir] = BLACK
pacman[1] += direction
if ghosts[3][1] <= start_blinking_ghosts and direction == -1:
if flag == "beep":
for i, ghost in enumerate(ghosts):
ghost[0] = BLACK
flag = "bop"
else:
for i, ghost in enumerate(ghosts):
ghost[0] = ghosts_original[i][0]
flag = "beep"
for i, ghost in enumerate(ghosts):
led_object.neopixel_list[ghost[1]] = ghost[0]
led_object.neopixel_list[ghost[1] + black_dir] = BLACK
ghost[1] += direction
if ghosts[3][1] <= 0:
direction = direction * -1
black_dir = black_dir * -1
for i, ghost in enumerate(ghosts):
ghost[0] = ghosts_original[i][0]
NEOPIXEL.ShowNeoPixels(led_object, led_object.neopixel_list)
time.sleep(0.1)
def rainbow_cycle(
led_object,
time_delta: float = 0.1,
duration: int = 5,
) -> None:
"""
Cycle through the rainbow colors.
:param float time_delta: time delay between each color change: default 0.1 seconds
:param int duration: duration in seconds: default 5 seconds
:return: None
"""
from rainbow import rainbow_colors
rainbow_set = rainbow_colors
start_time = time.time()
while time.time() - start_time < duration:
rainbow_set = rainbow_set[-1:] + rainbow_set[:-1]
for i in range(led_object.num_leds):
led_object.neopixel_list[i] = rainbow_set[i]
NEOPIXEL.ShowNeoPixels(led_object, led_object.neopixel_list)
time.sleep(time_delta)
def random_color(
led_object,
start: int = 0,
delta_time: float = 0.1,
duration: int = 5,
):
"""
Random color effect. This function will set random colors to the leds.
:param int num_leds: number of leds.
:param int start: start index. Default is 0
:param float delta_time: time delay between each color change: default 0.1 seconds
:param int duration: duration in seconds. Default is 5 seconds
"""
limits = range(0, 256)
start_time = time.time()
while time.time() - start_time < duration:
for i in range(start, led_object.num_leds):
led_object.neopixel_list[i] = (
choice(limits),
choice(limits),
choice(limits),
)
NEOPIXEL.ShowNeoPixels(led_object, led_object.neopixel_list)
time.sleep(delta_time)
def twinkle(led_object, delta_time: float = 0.1, duration: int = 5):
"""
Twinke effect. This function will set random colors to the leds.
:param led_object: led object
:param float delta_time: time delay between each color change: default 0.1 seconds
:param int duration: duration in seconds. Default is 5 seconds
"""
if led_object.palette_colors is None:
led_object.palette_colors = [
RED,
GREEN,
BLUE,
YELLOW,
PURPLE,
CYAN,
ORANGE,
]
start_time = time.time()
while time.time() - start_time < duration:
neopixel_list = [
choice(led_object.palette_colors)
for _ in range(led_object.num_leds)
]
NEOPIXEL.ShowNeoPixels(led_object, neopixel_list)
time.sleep(delta_time)
def get_led_segments(led_list, segment_length) -> list:
segments = []
for i in range(0, len(led_list), segment_length):
segments.append(led_list[i : i + segment_length])
return segments
def assign_values_to_segments(segments, values) -> list:
assigned_segments = []
for i, segment in enumerate(segments):
if i < len(values):
assigned_segment = [values[i]] * len(segment)
else:
assigned_segment = [BLACK] * len(segment) # or some default value
assigned_segments.append(assigned_segment)
return assigned_segments
def flatten_segments(assigned_segments) -> list:
return [led for segment in assigned_segments for led in segment]
def rainbow_sine(
led_object,
shrinkage: float = 0.1,
animation_speed: float = 0.05,
speed: float = 0.05,
saturation: float = 1.0,
value: float = 1.0,
duration: int = 5,
):
"""
Rainbow sine wave effect.
:param led_object: led object
:param float shrinkage: shrinkage value. Default is 0.1. This value will control the
size of the color segments. The bigger the number the smaller the segments will be
:param float animation_speed: animation speed. Default is 0.05. This value will control
the speed of the animation
:param float speed: speed of the animation. Default is 0.05 seconds. This value will control
how often the animation is updated. You can fin tune animation increase and speed to get the
desired effect
:param float saturation: saturation value. Default is 1.0
:param float value: value. Default is 1.0
:param int duration: duration in seconds. Default is 5 seconds
"""
animation = 0
start_time = time.time()
while time.time() - start_time < duration:
for i in range(led_object.num_leds):
hue = math.sin(animation + (i + 1) * shrinkage)
hue = (hue + 1) / 2
color = rgb255(hsv_to_rgb(hue, saturation, value))
led_object.neopixel_list[i] = color
NEOPIXEL.ShowNeoPixels(led_object, led_object.neopixel_list)
animation += animation_speed
time.sleep(speed)
def white_wave(
led_object,
animation_speed: float = 0.08,
fade_animation_speed: float = 0.08,
speed: float = 0.01,
shrinkage: float = 0.3,
duration: int = 5,
):
"""
White wave effect.
:param led_object: led object
:param float animation_speed: animation speed. Default is 0.08. This value will control the
speed of the animation
:param float fade_animation_speed: fade animation speed. Default is 0.08. This value will control
the speed of the fade animation
:param float speed: speed of the animation. Default is 0.01 seconds. This value will control
how often the animation is updated. You can fin tune animation increase and speed to get the
desired effect
:param float shrinkage: shrinkage value. Default is 0.3. This value will control the
size of the segments. The bigger the number the smaller the segments will be
:param int duration: duration in seconds. Default is 5 seconds
"""
animation = 0
fade_animation = 0
start_time = time.time()
while time.time() - start_time < duration:
# Calculate fade effect using sine wave
fade_effect = (math.sin(fade_animation) + 1) / 2
# Set global brightness based on fade effect
led_object.brightness = fade_effect
for i in range(led_object.num_leds):
brightness = math.sin(animation + i * shrinkage)
brightness = (brightness + 1) / 2
color = rgb255(hsv_to_rgb(0.0, 0.0, brightness))
led_object.neopixel_list[i] = color
NEOPIXEL.ShowNeoPixels(led_object, led_object.neopixel_list)
# Increment animation variables
animation += animation_speed
fade_animation += fade_animation_speed
# Small delay to control the speed of the animation
time.sleep(speed)
def white_wave_color(
led_object,
animation_speed: float = 0.08,
fade_animation_speed: float = 0.08,
frequency: float = 0.003,
speed: float = 0.1,
duration: int = 5,
):
"""
White wave effect.
:param led_object: led object
:param float animation_speed: animation speed. Default is 0.08. This value will control the
speed of the animation
:param float fade_animation_speed: fade animation speed. Default is 0.08. This value will control
the speed of the fade animation
:param float frequency: frequency value. Default is 0.003. This value will control the
frequency of the wave
:param float speed: speed of the animation. Default is 0.1 seconds. This value will control
how often the animation is updated. You can fin tune animation increase and speed to get the
desired effect
:param int duration: duration in seconds. Default is 5 seconds
"""
animation = 0
fade_animation = 0
shrinkage = 0
freq = 0
expand = 0
start_time = time.time()
while time.time() - start_time < duration:
shrinkage = math.sin(freq)
shrinkage = (shrinkage + 1) / 2
expand = math.cos(freq)
expand = (expand + 1) / 2
for i in range(led_object.num_leds):
# Calculate brightness for each LED using sine wave
saturation = math.sin(animation + i * shrinkage)
saturation = (saturation + 1) / 2
saturation = int(saturation * 255)
brightness = math.sin(animation + i * expand)
brightness = (brightness + 1) / 2
brightness = int(brightness * 255)
led_object.neopixel_list[i] = (
saturation,
int(saturation / (brightness + 1)),
brightness,
)
NEOPIXEL.ShowNeoPixels(led_object, led_object.neopixel_list)
# Increment animation variables
animation += animation_speed
fade_animation += fade_animation_speed
freq += frequency
# Small delay to control the speed of the animation
time.sleep(speed)
def linear_interpolation(
led_object,
shrinkage: float = 0.5,
animation_increase: int = 0.1,
speed: float = 0.01,
duration: int = 5,
):
"""
Linear interpolation effect. For this animation if you want to use a palette. The palette must
be set in the led object. The palette must be a list of tuples with RGB values. The function will
select two random colors from the palette and will interpolate between them.
:param led_object: led object
:param float shrinkage: shrinkage value. Default is 0.5. This value will control the
size of the color segments. The bigger the number the smaller the segments will be
:param float animation_increase: animation increase value. Default is 0.1. This value wil
control the speed of the animation
:param float speed: speed of the animation. Default is 0.01 seconds. This value will control
how often the animation is updated. You can fin tune animation increase and speed to get the
desired effect
:param int duration: duration in seconds. Default is 5 seconds
"""
from functions import lerp8by8
animation = 0
if led_object.palette_colors is None:
color1 = (255, 0, 0)
color2 = (0, 0, 255)
else:
color1 = choice(led_object.palette_colors)
color2 = choice(led_object.palette_colors)
start_time = time.time()
while time.time() - start_time < duration:
for i in range(led_object.num_leds):
interpolation = math.sin(animation + i * shrinkage)
interpolation = (interpolation + 1) / 2
interpolation *= 255
r = lerp8by8(color1[0], color2[0], int(interpolation))
g = lerp8by8(color1[1], color2[1], int(interpolation))
b = lerp8by8(color1[2], color2[2], int(interpolation))
led_object.neopixel_list[i] = (r, g, b)
NEOPIXEL.ShowNeoPixels(led_object, led_object.neopixel_list)
animation += animation_increase
time.sleep(speed)
def lerp_phase(
led_object,
animation_increase: int = 0.1,
speed: float = 0.01,
shrinkage: float = 0.2,
phase_increase: float = 1.5,
duration: int = 5,
):
"""
Linear interpolation effect. For this animation if you want to use a palette. The palette must
be set in the led object. The palette must be a list of tuples with RGB values. The function will
select two random colors from the palette and will interpolate between them.
:param led_object: led object
:param float shrinkage: shrinkage value. Default is 0.5. This value will control the
size of the color segments. The bigger the number the smaller the segments will be
:param float animation_increase: animation increase value. Default is 0.1. This value wil
control the speed of the animation
:param float speed: speed of the animation. Default is 0.01 seconds. This value will control
how often the animation is updated. You can fin tune animation increase and speed to get the
desired effect
:param float phase_increase: phase increase value. Default is 1.5. This value will control the
speed of the phase. The bigger the number the faster the phase will change, this will be reflect
in how the interpolation from one color to the other will change
:param int duration: duration in seconds. Default is 5 seconds
"""
from functions import lerp8by8
animation = 0
if led_object.palette_colors is None:
color1 = (255, 0, 0)
color2 = (0, 0, 255)
else:
color1 = choice(led_object.palette_colors)
color2 = choice(led_object.palette_colors)
start_time = time.time()
while time.time() - start_time < duration:
for i in range(led_object.num_leds):
interpolation = math.sin(i * shrinkage)
interpolation *= 127
phase = math.sin(animation * phase_increase)
interpolation *= phase
interpolation += 127
r = lerp8by8(color1[0], color2[0], int(interpolation))
g = lerp8by8(color1[1], color2[1], int(interpolation))
b = lerp8by8(color1[2], color2[2], int(interpolation))
led_object.neopixel_list[i] = (r, g, b)
NEOPIXEL.ShowNeoPixels(led_object, led_object.neopixel_list)
animation += animation_increase
time.sleep(speed)
def fadein_fadeout_random_color(
led_object, fade_increment: float = 0.03, speed=0.1, duration: int = 5
):
"""
White wave effect.
:param led_object: led object
:param float fade: fade value. Default is 0.03. You can play with the value
For lower values the transition between colors will be smoother. For higher
values the transition will be more abrupt. However this will depend on the
animation speed paramer
:param float speed: speed of the animation. Default is 0.1 seconds.
:param int duration: duration in seconds. Default is 5 seconds
"""
colorlist = [
random.randint(0, 256),
random.randint(0, 256),
random.randint(0, 256),
]
color_index = random.randint(0, len(colorlist) - 1)
fade = 0
start_time = time.time()
while time.time() - start_time < duration:
for i in range(led_object.num_leds):
brightness = math.sin(fade + math.pi)
brightness = (brightness + 1) / 2
brightness = int(brightness * 255)
colorlist[color_index] = brightness
led_object.neopixel_list[i] = (
colorlist[0],
colorlist[1],
colorlist[2],
)
NEOPIXEL.ShowNeoPixels(led_object, led_object.neopixel_list)
if fade >= 2 * math.pi:
fade = 0
colorlist = [
random.randint(0, 256),
random.randint(0, 256),
random.randint(0, 256),
]
color_index = random.randint(0, len(colorlist) - 1)
# Increment animation variables
fade += fade_increment
# Small delay to control the speed of the animation
time.sleep(speed)
def fadein_fadeout_fragmented(
led_object,
fragments: int = 3,
fade_increment: float = 0.08,
speed=0.01,
duration: int = 5,
):
"""
White wave effect.
:param led_object: led object
:param int fragments: number of fragments. Default is 3
:param float fade: fade value. Default is 0.03. You can play with the value
For lower values the transition between colors will be smoother. For higher
values the transition will be more abrupt. However this will depend on the
animation speed paramer
:param float speed: speed of the animation. Default is 0.1 seconds.
:param int duration: duration in seconds. Default is 5 seconds
"""
fade = 0
fragment_size = led_object.num_leds // fragments
fragment = 0
colorlist = [
random.randint(0, 256),
random.randint(0, 256),
random.randint(0, 256),
]
color_index = random.randint(0, len(colorlist) - 1)
start_time = time.time()
while time.time() - start_time < duration:
for i in range(
fragment * fragment_size, fragment_size * (fragment + 1)
):
brightness = math.cos(fade + math.pi)
brightness = (brightness + 1) / 2
brightness = int(brightness * 255)
colorlist[color_index] = brightness
led_object.neopixel_list[i] = (
colorlist[0],
colorlist[1],
colorlist[2],
)
NEOPIXEL.ShowNeoPixels(led_object, led_object.neopixel_list)
if fade >= 2 * math.pi:
fade = 0
colorlist = [
random.randint(0, 256),
random.randint(0, 256),
random.randint(0, 256),
]
color_index = random.randint(0, len(colorlist) - 1)
fragment = fragment + 1
fragment = fragment % fragments
# Increment animation variables
fade += fade_increment
# Small delay to control the speed of the animation
time.sleep(speed)
def fifo_fragmented_phase(
led_object,
fragment_amount: int = 2,
fade_speed: float = 0.03,
speed: float = 0.01,
duration: int = 5,
):
"""
White wave effect.
:param led_object: led object
:param int fragment_amount: number of fragments. Default is 2
:param float fade_speed: fade value. Default is 0.03. You can play with the value
For lower values the transition between colors will be smoother. For higher
values the transition will be more abrupt. However this will depend on the
animation speed paramer
:param float speed: speed of the animation. Default is 0.1 seconds.
:param int duration: duration in seconds. Default is 5 seconds
"""
fade = 0
fragment_size = math.floor(led_object.num_leds / fragment_amount)
# Start time
start_time = time.time()
while time.time() - start_time < duration:
for i in range(fragment_amount):
brightness = math.cos(fade + (math.pi // 2 * i))
brightness = (brightness + 1) / 2
brightness = int(brightness * 255)
# Set color for each LED
begin = i * fragment_size
end = min(fragment_size * (i + 1), led_object.num_leds)
for j in range(begin, end):
led_object.neopixel_list[j] = 0, 0, brightness
fade += fade_speed
if fade >= 20:
fade = 0
if fragment_amount >= 8:
fragment_amount = 1
fragment_amount *= 2
fragment_size = math.ceil(led_object.num_leds / fragment_amount)
led_object.fill_all(color=BLACK)
# Increment animation variables
NEOPIXEL.ShowNeoPixels(led_object, led_object.neopixel_list)
# Small delay to control the speed of the animation
time.sleep(speed)
def wave_freq_shrink_and_grow(
led_object,
move_increase: float = 0.2,
freq_increase: float = 0.003,
speed: float = 0.01,
duration: int = 5,
):
"""
White wave effect.
:param led_object: led object
:param float move_increase: move increase value. Default is 0.2. This value will control the
speed of the move
:param float freq_increase: frequency increase value. Default is 0.003. This value will control
the frequency of the wave
:param float speed: speed of the animation. Default is 0.01 seconds. This value will control
how often the animation is updated. You can fin tune animation increase and speed to get the
desired effect
:param int duration: duration in seconds. Default is 5 seconds
"""
move = 0
freq = 0
start_time = time.time()
while time.time() - start_time < duration:
shrinkage = math.sin(freq)
shrinkage = (shrinkage + 1) / 2
for i in range(led_object.num_leds):
saturation = math.sin(move + i * shrinkage)
saturation = (saturation + 1) / 2
saturation = int(saturation * 255)
led_object.neopixel_list[i] = 80, saturation, 80
NEOPIXEL.ShowNeoPixels(led_object, led_object.neopixel_list)
move += move_increase
freq -= freq_increase
# Small delay to control the speed of the animation
time.sleep(speed)
def wave_freq_shrink_and_grow_centered(
led_object,
move_increase: float = 0.2,
frequency: float = 0.003,
speed: float = 0.01,
duration: int = 5,
):
"""
Wave effect with frequency and shrinkage centered.
:param led_object: led object
:param float move_increase: move increase value. Default is 0.2. This value will control the
speed of the move
:param float frequency: frequency value. Default is 0.003. This value will control the
frequency of the wave
:param float speed: speed of the animation. Default is 0.01 seconds. This value will control
how often the animation is updated. You can fin tune animation increase and speed to get the
desired effect
:param int duration: duration in seconds. Default is 5 seconds
"""
move = 0
freq = 0
start_time = time.time()
while time.time() - start_time < duration:
shrinkage = math.sin(freq)
shrinkage = (shrinkage + 1) / 2
midpoint = led_object.num_leds // 2
for i in range(midpoint):
saturation = math.cos(move + i * shrinkage)
saturation = (saturation + 1) / 2
saturation = int(saturation * 255)
led_object.neopixel_list[midpoint + i] = 25, saturation, 80
led_object.neopixel_list[midpoint - i] = 80, saturation, 25
NEOPIXEL.ShowNeoPixels(led_object, led_object.neopixel_list)
move += move_increase
freq += frequency
time.sleep(speed)
def wave_back_and_forth(
led_object,
move_increase: float = 0.06,
speed: float = 0.01,
duration: int = 5,
):
"""
White wave effect.
:param led_object: led object
:param float move_increase: move increase value. Default is 0.06. This value will control the
speed of the move
:param float speed: speed of the animation. Default is 0.01 seconds. This value will control
how often the animation is updated. You can fin tune animation increase and speed to get the
desired effect
:param int duration: duration in seconds. Default is 5 seconds
"""
move = 0
hue = 0
start_time = time.time()
while time.time() - start_time < duration:
for i in range(led_object.num_leds):
hue = int(hue + math.sin(move) * led_object.num_leds)
brigthness = i + math.sin(move) * 20
brigthness = math.sin(brigthness)
brigthness = (brigthness + 1) / 2
brigthness = int(brigthness * 255)
led_object.neopixel_list[i] = hue, 80, brigthness
NEOPIXEL.ShowNeoPixels(led_object, led_object.neopixel_list)
move += move_increase
time.sleep(speed)
def shrink_and_grow(led_object, duration: int = 5):
"""
White wave effect.
:param led_object: led object
:param int duration: duration in seconds. Default is 5 seconds
"""
move = 0
midpoint = led_object.num_leds // 2
spread = midpoint * ((math.sin(move) + 1) / 2)
step = math.pi / spread
# Start time
start_time = time.time()
while time.time() - start_time < duration:
for i in range(int(spread)):
brigthness = math.cos(i + move * step)
brigthness = (brigthness + 1) / 2
brigthness = int(brigthness * 255)
led_object.neopixel_list[midpoint + i] = 60, 60, brigthness
led_object.neopixel_list[midpoint - i] = 60, 60, brigthness