-
Notifications
You must be signed in to change notification settings - Fork 9
/
neopixel-xmas-tree-2019.py
2329 lines (1866 loc) · 75.7 KB
/
neopixel-xmas-tree-2019.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
### Sample python code for NeoPixels on Raspberry Pi
### this code is random suggestion from my family, friends, and other examples on the web.
### orginal code: https://github.com/DanStach/rpi-ws2811
import time
import board
import neopixel
import random
import math
import serial
import ctypes
# Choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D18
# NeoPixels must be connected to D10, D12, D18 or D21 to work.
pixel_pin = board.D18
# The number of NeoPixels
num_pixels = 400
# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed!
# For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW.
ORDER = neopixel.RGB
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER)
wait_time = .5
wait_animate = .1
cycleFactor = 1
cred = (255, 0, 0)
cblue = (0, 0, 255)
cgreen = (0, 255, 0)
cyellow = (255, 255, 0)
ccyan = (0, 255, 255)
cpurple = (160, 32, 240)
corange = (255, 165, 0)
cwhite = (255, 255, 255)
cblk = (0, 0, 0)
cltred = (127, 0, 0)
cltblue = (0, 0, 127)
cltgreen = (0, 30, 0)
cltyellow = (127, 127, 0)
cltcyan = (0, 127, 127)
cltpurple = (127, 0, 127)
cltorange = (127, 82, 0)
cltwhite = (127, 127, 127)
colorobj = (cgreen, cwhite, ccyan, cpurple, cyellow, cblue, cred)
xmasColorGroupAll = (cgreen, cwhite, ccyan, cyellow, cblue, cred)
xmasColorGroupDimAll = (cltgreen, cltwhite, cltcyan, cltyellow, cltblue, cltred)
xmasColorGroupRedGreen = (cred, cgreen)
xmasColorGroupRedWhite = (cred, cwhite)
halloweenColorGroupPurpleOrange = ((128,0,128), (250,97,35) )
levels = (43, 73, 103, 135, 160, 188, 213, 236, 255, 272, 286, 295, 300)
#levels = (43, 73, 103, 135, 160, 188, 213, 236, 255, 272, 286, 245, 250)
#levels = (58, 108, 149, 187, 224, 264, 292, 300)
#levels = (110, 200, 270, 300)
### colorAll2Color allows two alternating colors to be shown
#
def colorAll2Color(c1, c2):
for i in range(num_pixels):
if(i % 2 == 0): # even
pixels[i] = c1
else: # odd
pixels[i] = c2
pixels.show()
# colorAllColorGroup(colorObject) allows colors to be
# - colorObject: list of color objects. example ((255, 0, 0), (0, 255, 0))
def colorAllColorGroup(colorObject):
colorCount = len(colorObject)
for i in range(num_pixels):
colorIndex = i % colorCount
pixels[i] = colorObject[colorIndex]
pixels.show()
### wheel(pos) will convert value 0 to 255 to get a color value.
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if pos < 0 or pos > 255:
r = g = b = 0
elif pos < 85:
r = int(pos * 3)
g = int(255 - pos*3)
b = 0
elif pos < 170:
pos -= 85
r = int(255 - pos*3)
g = 0
b = int(pos*3)
else:
pos -= 170
r = 0
g = int(pos*3)
b = int(255 - pos*3)
return (r, g, b) if ORDER == neopixel.RGB or ORDER == neopixel.GRB else (r, g, b, 0)
def wheelBrightLevel(pos, bright):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if pos < 0 or pos > 255:
r = g = b = 0
elif pos < 85:
r = int(pos * 3)
g = int(255 - pos*3)
b = 0
elif pos < 170:
pos -= 85
r = int(255 - pos*3)
g = 0
b = int(pos*3)
else:
pos -= 170
r = 0
g = int(pos*3)
b = int(255 - pos*3)
# bight level logic
color = brightnessRGB(r, g, b, bright)
r = color[0]
g = color[1]
b = color[2]
return color if ORDER == neopixel.RGB or ORDER == neopixel.GRB else (r, g, b, 0)
def brightnessRGB(red, green, blue, bright):
r = (bright/256.0)*red
g = (bright/256.0)*green
b = (bright/256.0)*blue
return (int(r), int(g), int(b))
def colorTransition(color1, color2, percent):
r1 = color1[0]
g1 = color1[1]
b1 = color1[2]
r2 = color2[0]
g2 = color2[1]
b2 = color2[2]
rdelta = color2[0] - color1[0]
gdelta = color2[1] - color1[1]
bdelta = color2[2] - color1[2]
r = int((1-percent)*rdelta) + r1
g = int((1-percent)*gdelta) + g1
b = int((1-percent)*bdelta) + b1
return (int(r), int(g), int(b))
def fadeToBlack(ledNo, fadeValue):
oldColor = pixels[ledNo]
r = oldColor[0]
g = oldColor[1]
b = oldColor[2]
if (r<=10):
r = 0
else:
r = r - ( r * fadeValue / 256 )
if (g<=10):
g = 0
else:
g = g - ( g * fadeValue / 256 )
if (b<=10):
b = 0
else:
b = b - ( b * fadeValue / 256 )
pixels[ledNo] = ( int(r), int(g), int(b) )
def RotateExisting( delay, cycles):
# gather existing colors in strip of pixel
stripExisting = []
for i in range(num_pixels):
stripExisting.append(pixels[i])
for loop in range(cycles):
pixels[0] = pixels[num_pixels - 1]
# rotate pixel positon
for i in range(num_pixels - 1, 0, -1):
pixels[i] = pixels[i-1]
# there is an issue with first 2 pixels are same color
pixels.show()
time.sleep(delay)
def RotateObject(coloreObj, delay, cycles, isDirrectionForward):
totalColorObj = len(coloreObj)
for c in range(cycles): #for each cycle change index number
index = c % num_pixels
if(isDirrectionForward):
for loop in range(num_pixels): # re-write pixels on strand
pos = (index - loop) % totalColorObj
pixels[loop] = coloreObj[pos]
else:
for loop in range(num_pixels): # re-write pixels on strand
pos = (index + loop) % totalColorObj
pixels[loop] = coloreObj[pos]
pixels.show()
time.sleep(delay)
def rainbow_cycle(delay, cycles):
for j in range(cycles):
for i in range(num_pixels):
# " // " this divides and returns the integer value of the quotient.
# It dumps the digits after the decimal
pixel_index = (i * 256 // num_pixels) + j
pixels[i] = wheel(pixel_index & 255)
pixels.show()
time.sleep(delay)
def candycane_custom(c1, c2, thisbright, delay, cycles):
N6 = int(num_pixels/6)
N12 = int(num_pixels/12)
for loop in range(cycles):
cSwap = c1
c1 = c2
c2 = cSwap
for i in range(N6):
j0 = int(( i + num_pixels - N12) % num_pixels)
j1 = int((j0+N6) % num_pixels)
j2 = int((j1+N6) % num_pixels)
j3 = int((j2+N6) % num_pixels)
j4 = int((j3+N6) % num_pixels)
j5 = int((j4+N6) % num_pixels)
pixels[j0] = brightnessRGB(c1[0], c1[1], c1[2], int(thisbright*.75))
pixels[j1] = brightnessRGB(c2[0], c2[1], c2[2], thisbright)
pixels[j2] = brightnessRGB(c1[0], c1[1], c1[2], int(thisbright*.75))
pixels[j3] = brightnessRGB(c2[0], c2[1], c2[2], thisbright)
pixels[j4] = brightnessRGB(c1[0], c1[1], c1[2], int(thisbright*.75))
pixels[j5] = brightnessRGB(c2[0], c2[1], c2[2], thisbright)
# show pixel values
pixels.show()
time.sleep(delay)
def fill_group_random(groupCount, delay, cycles):
pixels.fill((255, 0, 0)) # inital fill red
pixels.show()
wheelPos = 0
for c in range(cycles):
fillColor = wheel(wheelPos)
for i in range(groupCount):
for q in range(0, num_pixels, groupCount):
if i+q < num_pixels:
pixels[i+q] = fillColor
pixels.show()
time.sleep(delay)
wheelPos = random.randint(0, 255)
def fill_group_expand_random(groupCount, delay, cycles):
pixels.fill((255, 0, 0)) # inital fill red
pixels.show()
wheelPos = 0
for c in range(cycles):
fillColor = wheel(wheelPos)
for i in range(int(groupCount/2+1)):
for q in range(0, int(num_pixels), int(groupCount)):
if i+q < num_pixels:
pixels[i+q] = fillColor
if groupCount-i+q < num_pixels:
pixels[groupCount-i+q] = fillColor
pixels.show()
time.sleep(delay)
random.seed()
wheelPos = random.randint(0, 255)
def theaterChaseDot(sectionCount, dotColor, delay, cycles):
pixels.fill(cblk) # inital fill black
pixels.show()
startPos = 0
for c in range(cycles):
for i in range(int(sectionCount)):
for q in range(0, int(num_pixels), int(sectionCount)):
if i+q < num_pixels:
pixels[i+q] = dotColor
if i+q-1 < num_pixels and i+q-1 >= 0:
pixels[i+q-1] = cblk
pixels.show()
time.sleep(delay)
def theaterChaseDotCollection(sectionCount, dotColor, delay, cycles):
pixels.fill(cblk) # inital fill black
pixels.show()
sectionEnd = sectionCount
for c in range(cycles):
for i in range(int(sectionCount)):
if sectionEnd == 0:
pixels.fill(cblk) # inital fill black
sectionEnd = sectionCount
for q in range(0, int(num_pixels), int(sectionCount)):
if i+q < num_pixels:
pixels[i+q] = dotColor
if i+q-1 < num_pixels and i+q-1 >= 0:
if i > 1:
pixels[i+q-1] = cblk
pixels.show()
if i >= sectionEnd-1:
sectionEnd -=1
break
else:
time.sleep(delay)
def theaterChaseDotCollectionMiddle(sectionCount, dotColor, delay, cycles):
pixels.fill(cblk) # inital fill black
pixels.show()
sectionCountHalf= int(sectionCount/2+1)
sectionEnd = sectionCountHalf-1
for c in range(cycles):
for i in range(int(sectionCountHalf)):
for q in range(0, int(num_pixels), int(sectionCount)):
if i+q < num_pixels:
pixels[i+q] = dotColor
if i+q-1 < num_pixels and i+q-1 >= 0:
if i > 1:
pixels[i+q-1] = cblk
if sectionCount-i+q < num_pixels:
pixels[sectionCount-i+q] = dotColor
if sectionCount-i+q+1 < num_pixels and sectionCount-i+q+1 >= 0:
if i < num_pixels:
pixels[sectionCount-i+q+1] = cblk
pixels.show()
if sectionEnd < 0: # fill last dots, and reset
pixels.fill(dotColor) # fill dots
pixels.show()
time.sleep(delay)
pixels.fill(cblk) # inital fill black
pixels.show()
time.sleep(delay)
sectionCountHalf= int(sectionCount/2+1)
sectionEnd = sectionCountHalf-1
if i >= sectionEnd:
sectionEnd -=1
time.sleep(delay)
break
else:
time.sleep(delay)
def theaterChaseGroupCustom(colorobj, colorspace, darkspace, SpeedDelay, cycles):
colorObjCount = len(colorobj)
n = colorspace + darkspace
for j in range(cycles):
for k in range(colorObjCount):
for q in range(n):
for i in range(0, num_pixels, n):
for index in range(0, colorspace, 1):
if i+q+index < num_pixels:
pixels[i+q+index] = colorobj[k]
pixels.show()
time.sleep(SpeedDelay)
pixels.fill(cblk)
for i in range(0, num_pixels, n):
for index in range(0, colorObjCount, 1):
if i+q+index < num_pixels:
pixels[i+q+index] = cblk
def PatternRunningLightsFade(mainColor, mainLength, spaceColor, spaceLength, isDirrectionForward, patternCycles):
stripPattern = []
position = 0
# make pixels for main effect
if isDirrectionForward == True:
start = mainLength
end = 0
increment = -1
else:
start = 0
end = mainLength
increment = 1
for m in range (start, end, increment):
level = int(m/mainLength*128)
stripColor = brightnessRGB(mainColor[0], mainColor[1], mainColor[2], level)
stripPattern.append(stripColor)
position = position + mainLength
# make pixels for space
for i in range(spaceLength):
stripPattern.append(spaceColor)
return stripPattern
def PatternRunningLightsFadeColorObj(colorObj, mainLength, spaceColor, spaceLength, isDirrectionForward, patternCycles):
stripPattern = []
colorObjCount = len(colorObj)
for k in range(colorObjCount):
mainColor = colorobj[k]
# make pixels for main effect
if isDirrectionForward == True:
start = mainLength
end = 0
increment = -1
else:
start = 0
end = mainLength
increment = 1
for m in range (start, end, increment):
level = int(m/mainLength*128)
stripColor = brightnessRGB(mainColor[0], mainColor[1], mainColor[2], level)
stripPattern.append(stripColor)
# make pixels for space
for i in range(spaceLength):
stripPattern.append(spaceColor)
return stripPattern
def PatternRunningLightsWaveColorObj(colorObj, mainLength, spaceColor, spaceLength, isDirrectionForward, patternCycles):
stripPattern = []
colorObjCount = len(colorObj)
halfLength = math.floor(mainLength/2)
for k in range(colorObjCount):
mainColor = colorobj[k]
# make pixel for frist part of wave
for m in range (0, halfLength, 1):
level = int(m/halfLength*128)
stripColor = brightnessRGB(mainColor[0], mainColor[1], mainColor[2], level)
stripPattern.append(stripColor)
#if odd number replace the missing pixel
if mainLength % 2 == 1:
stripPattern.append(mainColor)
# make pixel for second part of wave
for m in range (halfLength, 0, -1):
level = int(m/halfLength*128)
stripColor = brightnessRGB(mainColor[0], mainColor[1], mainColor[2], level)
stripPattern.append(stripColor)
# make pixels for space
for i in range(spaceLength):
stripPattern.append(spaceColor)
return stripPattern
def PatternRunningLightsWaveTrans(colorObj, mainLength, spaceColor, spaceLength, isDirrectionForward, patternCycles):
stripPattern = []
colorObjCount = len(colorObj)
halfLength = math.floor(mainLength/2)
for k in range(colorObjCount):
mainColor = colorobj[k]
# make pixel for frist part of wave
for m in range (0, halfLength, 1):
level = m/halfLength
stripColor = colorTransition(mainColor, spaceColor, level)
stripPattern.append(stripColor)
#if odd number replace the missing pixel
if mainLength % 2 == 1:
stripPattern.append(mainColor)
# make pixel for second part of wave
for m in range (halfLength, 0, -1):
level = m/halfLength
stripColor = colorTransition(mainColor, spaceColor, level)
stripPattern.append(stripColor)
# make pixels for space
for i in range(spaceLength):
stripPattern.append(spaceColor)
return stripPattern
def PatternRunningLightsFadeTrans(colorObj, mainLength, spaceColor, spaceLength, isDirrectionForward, patternCycles):
stripPattern = []
colorObjCount = len(colorObj)
for k in range(colorObjCount):
mainColor = colorobj[k]
# make pixels for main effect
if isDirrectionForward == True:
start = mainLength
end = 0
increment = -1
else:
start = 0
end = mainLength
increment = 1
for m in range (start, end, increment):
level = m/mainLength
stripColor = colorTransition(mainColor, spaceColor, level)
stripPattern.append(stripColor)
# make pixels for space
for i in range(spaceLength):
stripPattern.append(spaceColor)
return stripPattern
def DotCollection(colorObj, sectionCount, spaceColor, delay, cycles):
pixels.fill(spaceColor) # inital fill black
pixels.show()
collectionCount = 0
sectionEnd = sectionCount
#for c in range(cycles):
for c in range(cycles):
dotColor = colorObj[c%len(colorObj)]
#animate dots moving
for i in range(int(sectionCount)):
if sectionEnd == 0:
pixels.fill(spaceColor) # inital fill black
sectionEnd = sectionCount
for q in range(0, int(num_pixels), int(sectionCount)):
# add dotcolor
if i+q < num_pixels:
pixels[i+q] = dotColor
# replace previous dot with space color
if i+q-1 < num_pixels and i+q-1 >= 0:
if i > 0:
pixels[i+q-1] = spaceColor
pixels.show()
if i >= sectionEnd-1:
sectionEnd -=1
break
else:
time.sleep(delay)
def DotCollectionColorChange(colorObj, sectionCount, spaceColor, delay, cycles):
pixels.fill(spaceColor) # inital fill black
pixels.show()
collectionCount = 0
sectionEnd = sectionCount
#for c in range(cycles):
for c in range(cycles):
dotColor = colorObj[c%len(colorObj)]
#animate dots moving
for i in range(int(sectionCount)):
if sectionEnd == 0:
pixels.fill(spaceColor) # inital fill black
sectionEnd = sectionCount
for q in range(0, int(num_pixels), int(sectionCount)):
# add dotcolor
if i+q < num_pixels:
pixels[i+q] = dotColor
# replace previous dot with space color
if i+q-1 < num_pixels and i+q-1 >= 0:
if i > 0 and i < sectionEnd:
pixels[i+q-1] = spaceColor
#check if at end of space section
if i < sectionEnd-1:
pixels.show()
time.sleep(delay)
pixels.show()
sectionEnd -=1
def DotCollectionMiddleColorChange(colorObj, sectionCount, spaceColor, delay, cycles):
pixels.fill(spaceColor) # inital fill black
pixels.show()
collectionCount = 0
sectionCountDouble = sectionCount*2
sectionEnd = sectionCount
#for c in range(cycles):
for c in range(cycles):
dotColor = colorObj[c%len(colorObj)]
#animate dots moving
for i in range(int(sectionCount)):
if sectionEnd == 0:
pixels.fill(spaceColor) # inital fill black
sectionEnd = sectionCount
for q in range(0, int(num_pixels), int(sectionCountDouble)):
# add dotcolor
if i+q < num_pixels:
pixels[i+q] = dotColor
if sectionCountDouble-i+q-1 < num_pixels:
pixels[sectionCountDouble-i+q-1] = dotColor
# replace previous dot with space color
if i+q-1 < num_pixels and i+q-1 >= 0:
if i > 0 and i < sectionEnd:
pixels[i+q-1] = spaceColor
if sectionCountDouble-i+q < num_pixels and sectionCountDouble-i+q >= 0:
if i < num_pixels and i <= sectionEnd-1:
pixels[sectionCountDouble-i+q] = spaceColor
#check if at end of space section
if i < sectionEnd-1:
pixels.show()
time.sleep(delay)
pixels.show()
sectionEnd -=1
def fill_section(colorObj, sectionCount, spaceColor, delay, isDirrectionForward, cycles):
pixels.fill(spaceColor) # inital fill red
pixels.show()
if isDirrectionForward == True:
start = 0
end = sectionCount
increment = 1
else:
start = sectionCount
end = 0
increment = -1
for c in range(cycles):
pixels.fill(spaceColor) # inital fill
pixels.show()
fillColor = colorObj[c%len(colorObj)]
for i in range(start, end, increment):
for q in range(0, num_pixels, sectionCount):
if i+q < num_pixels:
pixels[i+q] = fillColor
pixels.show()
time.sleep(delay)
def fill_section_mid(colorObj, sectionCount, spaceColor, delay, isDirrectionOutward, cycles):
pixels.fill(spaceColor) # inital fill red
pixels.show()
sectionCountDouble = sectionCount * 2
if isDirrectionOutward == True:
start = sectionCount
end = 0
increment = -1
else:
start = 0
end = sectionCount
increment = 1
for c in range(cycles):
pixels.fill(spaceColor) # inital fill
pixels.show()
fillColor = colorObj[c%len(colorObj)]
for i in range(start, end, increment):
for q in range(0, num_pixels, sectionCountDouble):
if i+q < num_pixels:
pixels[i+q] = fillColor
if sectionCountDouble-i+q-1 < num_pixels:
pixels[sectionCountDouble-i+q-1] = fillColor
pixels.show()
time.sleep(delay)
def drain_section(colorObj, sectionCount, spaceColor, delay, isDirrectionForward, cycles):
if isDirrectionForward == True:
start = 0
end = sectionCount
increment = 1
else:
start = sectionCount
end = 0
increment = -1
for c in range(cycles):
fillColor = colorObj[c%len(colorObj)]
pixels.fill(fillColor) # inital fill
pixels.show()
for i in range(start, end, increment):
for q in range(0, num_pixels, sectionCount):
if i+q < num_pixels:
pixels[i+q] = spaceColor
pixels.show()
time.sleep(delay)
def drain_section_mid(colorObj, sectionCount, spaceColor, delay, isDirrectionOutward, cycles):
sectionCountDouble = sectionCount * 2
if isDirrectionOutward == True:
start = 0
end = sectionCount
increment = 1
else:
start = sectionCount
end = 0
increment = -1
for c in range(cycles):
fillColor = colorObj[c%len(colorObj)]
pixels.fill(fillColor) # inital fill
pixels.show()
for i in range(start, end, increment):
for q in range(0, num_pixels, sectionCountDouble):
if i+q < num_pixels:
pixels[i+q] = spaceColor
if sectionCountDouble-i+q-1 < num_pixels:
pixels[sectionCountDouble-i+q-1] = spaceColor
pixels.show()
time.sleep(delay)
def FadeInOutColors(colorObj, spaceColor, incrementPrecent, delay, cycles):
increment = int(incrementPrecent * 100)
for c in range(cycles):
fillColor = colorObj[c%len(colorObj)]
pixels.fill(spaceColor) # inital fill
pixels.show()
for level in range(0, 100, increment):
colorLevel = colorTransition(fillColor, spaceColor, level/100)
pixels.fill(colorLevel)
pixels.show()
time.sleep(delay)
for level in range(100, 0, -increment):
colorLevel = colorTransition(fillColor, spaceColor, level/100)
pixels.fill(colorLevel)
pixels.show()
time.sleep(delay)
def TransColors(colorObj, incrementPrecent, delay, cycles):
increment = int(incrementPrecent * 100)
for c in range(cycles):
fillColor = colorObj[c%len(colorObj)]
fillColor2 = colorObj[(c+1)%len(colorObj)]
for level in range(0, 100, increment):
colorLevel = colorTransition(fillColor, fillColor2, level/100)
pixels.fill(colorLevel)
pixels.show()
time.sleep(delay)
for level in range(100, 0, -increment):
colorLevel = colorTransition(fillColor, fillColor2, level/100)
pixels.fill(colorLevel)
pixels.show()
time.sleep(delay)
def RGBLoop(delay):
for j in range(3):
# Fade IN
for k in range(256):
if j == 0:
pixels.fill((k, 0, 0))
elif j == 1:
pixels.fill((0, k, 0))
elif j == 2:
pixels.fill((0, 0, k))
pixels.show()
time.sleep(delay)
# Fade OUT
for k in range(256):
if j == 2:
pixels.fill((k, 0, 0))
elif j == 1:
pixels.fill((0, k, 0))
elif j == 0:
pixels.fill((0, 0, k))
pixels.show()
time.sleep(delay)
def FadeInOut(red, green, blue, delay):
r = 0
g = 0
b = 0
for k in range(256):
r = (k/256.0)*red
g = (k/256.0)*green
b = (k/256.0)*blue
pixels.fill((int(r), int(g), int(b)))
pixels.show()
time.sleep(delay)
for k in range(256, -1, -1):
r = (k/256.0)*red
g = (k/256.0)*green
b = (k/256.0)*blue
pixels.fill((int(r), int(g), int(b)))
pixels.show()
time.sleep(delay)
def Twinkle(red, green, blue, Count, SpeedDelay, OnlyOne):
pixels.fill(cblk)
for i in range(Count):
pixels[random.randint(0, num_pixels-1)] = (red, green, blue)
pixels.show()
time.sleep(SpeedDelay)
if OnlyOne:
pixels.fill(cblk)
time.sleep(SpeedDelay)
def TwinkleRandom(Count, SpeedDelay, OnlyOne):
pixels.fill(cblk)
for i in range(Count):
pixels[random.randint(0, num_pixels-1)] = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
pixels.show()
time.sleep(SpeedDelay)
if OnlyOne:
pixels.fill(cblk)
time.sleep(SpeedDelay)
def Sparkle(red, green, blue, Count, SpeedDelay):
for i in range(Count):
Pixel = random.randint(0,num_pixels-1)
pixels[Pixel] = (red,green,blue)
pixels.show()
time.sleep(SpeedDelay)
pixels[Pixel] = cblk
def SnowSparkle(red, green, blue, Count, SparkleDelay, SpeedDelay):
pixels.fill((red,green,blue))
for i in range(Count):
Pixel = random.randint(0,num_pixels-1)
pixels[Pixel] = (255,255,255)
pixels.show()
time.sleep(SparkleDelay)
pixels[Pixel] = (red,green,blue)
pixels.show()
time.sleep(SpeedDelay)
def RunningLights(red, green, blue, WaveDelay):
Position = 0
for j in range(num_pixels*2):
Position = Position + 1
for i in range(num_pixels):
# sine wave, 3 offset waves make a rainbow!
# float level = sin(i+Position) * 127 + 128;
# setPixel(i,level,0,0);
# float level = sin(i+Position) * 127 + 128;
level = math.sin(i + Position) * 127 + 128
r = int((level/255)*red)
g = int((level/255)*green)
b = int((level/255)*blue)
pixels[i] = (r,g,b)
pixels.show()
time.sleep(WaveDelay)
def colorWipe(red, green, blue, SpeedDelay):
for i in range(num_pixels):
pixels[i] = (red, green, blue)
pixels.show()
time.sleep(SpeedDelay)
def theaterChase(red, green, blue, cycles, SpeedDelay):
for j in range(cycles):
for q in range(3):
for i in range(0, num_pixels, 3):
if i+q < num_pixels:
# turn every third pixel on
pixels[i+q] = (red, green, blue)
pixels.show()
time.sleep(SpeedDelay)
for i in range(0, num_pixels, 3):
if i+q < num_pixels:
# turn every third pixel off
pixels[i+q] = cblk
def theaterChaseRainbow(SpeedDelay):
# cycle all 256 colors in the wheel
for j in range(256):
for q in range(3):
for i in range(0, num_pixels, 3):
# check that pixel index is not greater than number of pixels
if i+q < num_pixels:
# turn every third pixel on
# c = Wheel( (i+j) % 255);
# setPixel(i+q, *c, *(c+1), *(c+2));
pixels[i+q] = wheel((i+j) % 255)
#print("i", i , "j", j "num_pixels", num_pixels, "pixel_index", (i * 256 // num_pixels) + j)
#pixel_index = (i * 256 // num_pixels) + j
#pixels[i+q] = wheel(pixel_index & 255)
pixels.show()
time.sleep(SpeedDelay)
for i in range(0, num_pixels, 3):
# check that pixel index is not greater than number of pixels
if i+q < num_pixels:
# turn every third pixel off
pixels[i+q] = cblk
### Fix Me - something is broken with the logic. the color doesn't change. and the fire effect seems small
### orginal code; https://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/#LEDStripEffectFire
def Fire(Cooling, Sparking, SpeedDelay, LoopCount):
heat = []
for i in range(num_pixels):
heat.append(0)
for l in range(LoopCount):
cooldown = 0
# Step 1. Cool down every cell a little
for i in range(num_pixels):
#print()
#print()
#print("rand interal" + str(((Cooling * 10) / num_pixels) + 2))
cooldown = random.randint(0, int(((Cooling * 10) / num_pixels) + 2))
#print("cooldown " + str(cooldown))
#print("heat " + str(heat[i]))
if cooldown > heat[i]:
heat[i]=0
else:
heat[i]=heat[i]-cooldown
# Step 2. Heat from each cell drifts 'up' and diffuses a little
for k in range(num_pixels - 1, 2, -1):
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3
# Step 3. Randomly ignite new 'sparks' near the bottom
if random.randint(0,255) < Sparking:
y = random.randint(0,7)
#heat[y] = heat[y] + random.randint(160,255)
heat[y] = random.randint(160,255)
# Step 4. Convert heat to LED colors
#print(heat)
for j in range(num_pixels):
#print(heat[j] )
setPixelHeatColor(j, int(heat[j]) )
pixels.show()
time.sleep(SpeedDelay)
def setPixelHeatColor (Pixel, temperature):