-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunction_library.py
853 lines (798 loc) · 34.3 KB
/
function_library.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
#This is the function library. This is imported by main.py
import time
import math
import random
from rpi_ws281x import *
from array import *
from progmem import *
import argparse
import signal
import sys
import datetime
def signal_handler(signal, frame):
colorWipe(strip, Color(0,0,0))
sys.exit(0)
def opt_parse():
parser = argparse.ArgumentParser()
parser.add_argument('-c', action='store_true', help='clear the display on exit')
args = parser.parse_args()
if args.c:
signal.signal(signal.SIGINT, signal_handler)
#LED strip configuration:
MATRIX_COUNT = 256 # Number of LED pixels.
LED_COUNT = 32 # Number of LED pixels.
MATRIX_PIN = 13 # GPIO pin connected to the pixels (18 uses PWM!).
LED_PIN = 12 # GPIO pin connected to the pixels (18 uses PWM!).
#LED_PIN = 10 # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 10 # DMA channel to use for generating signal (try 10)
LED_BRIGHTNESS = 127 # Set to 0 for darkest and 255 for brightest
MATRIX_BRIGHTNESS = 10 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53
MATRIX_CHANNEL = 1 # set to '1' for GPIOs 13, 19, 41, 45 or 53
LED_STRIP = ws.WS2811_STRIP_GRB # Strip type and colour ordering
#Define functions which animate LEDs in various ways.
def SetAll(strip, color):
"""Wipe color across display a pixel at a time."""
for i in range(strip.numPixels()):
strip.setPixelColor(i, color)
# Call progmem character animations by calling DigDug, Qbert, or BombJack
def CharAnimation(strip, gamename, loops=3, sleep=0.5):
frames = play(gamename)
sequence = Sequences[gamename]
for l in range(loops):
for i in range(len(sequence)):
for j in range(256):
strip.setPixelColorRGB(j, frames[sequence[i]][j][0], frames[sequence[i]][j][1], frames[sequence[i]][j][2])
strip.show()
time.sleep(sleep)
def FadeRGB(strip):
for i in range(0, 3):
#Fade In.
for j in range (0, 256):
if i == 0:
SetAll(strip, Color(j, 0, 0))
elif i == 1:
SetAll(strip, Color(0, j, 0))
elif i == 2:
SetAll(strip, Color(0, 0, j))
strip.show()
#Fade Out.
for j in range (256, 0, -1):
if i == 0:
SetAll(strip, Color(j, 0, 0))
elif i == 1:
SetAll(strip, Color(0, j, 0))
elif i == 2:
SetAll(strip, Color(0, 0, j))
strip.show()
def FadeInOut(strip, red, green, blue):
#Fade In.
for i in range (0, 256):
r = int(math.floor((i / 256.0) * red))
g = int(math.floor((i / 256.0) * green))
b = int(math.floor((i / 256.0) * blue))
SetAll(strip, Color(r, g, b))
strip.show()
#Fade Out.
for i in range (256, 0, -1):
r = int(math.floor((i / 256.0) * red))
g = int(math.floor((i / 256.0) * green))
b = int(math.floor((i / 256.0) * blue))
SetAll(strip, Color(r, g, b))
strip.show()
def Heartbeat(strip, red, green, blue, fade_in_speed=0.02, fade_out_speed=0.02):
#Fade In.
for i in range (0, 256):
r = int(math.floor((i / 256.0) * red))
g = int(math.floor((i / 256.0) * green))
b = int(math.floor((i / 256.0) * blue))
SetAll(strip, Color(r, g, b))
strip.show()
time.sleep(fade_in_speed) # Adjust the sleep time for the desired speed of the heartbeat.
#Fade Out.
for i in range (256, 0, -1):
r = int(math.floor((i / 256.0) * red))
g = int(math.floor((i / 256.0) * green))
b = int(math.floor((i / 256.0) * blue))
SetAll(strip, Color(r, g, b))
strip.show()
time.sleep(fade_out_speed) # Adjust the sleep time for the desired speed of the heartbeat.
def Strobe(strip, red, green, blue, StrobeCount, FlashDelay, EndPause):
for i in range (0, StrobeCount):
SetAll(strip, Color(red, green, blue))
strip.show()
time.sleep(FlashDelay)
SetAll(strip, Color(0, 0, 0))
strip.show()
time.sleep(FlashDelay)
time.sleep(EndPause)
def Cylon(strip, red, green, blue, EyeSize, SpeedDelay, ReturnDelay):
for i in range (0, (LED_COUNT - EyeSize - 2)):
SetAll(strip, Color(0, 0, 0))
strip.setPixelColor(i, Color(int(math.floor(red / 10)), int(math.floor(green / 10)), int(math.floor(blue / 10))))
for j in range(1, (EyeSize + 1)):
strip.setPixelColor(i + j, Color(red, green, blue))
strip.setPixelColor(i + EyeSize + 1, Color(int(math.floor(red / 10)), int(math.floor(green / 10)), int(math.floor(blue / 10))))
strip.show()
time.sleep(SpeedDelay)
time.sleep(ReturnDelay)
for i in range ((LED_COUNT - EyeSize - 2), 0, -1):
SetAll(strip, Color(0, 0, 0))
strip.setPixelColor(i, Color(int(math.floor(red / 10)), int(math.floor(green / 10)), int(math.floor(blue / 10))))
for j in range(1, (EyeSize + 1)):
strip.setPixelColor(i + j, Color(red, green, blue))
strip.setPixelColor(i + EyeSize + 1, Color(int(math.floor(red / 10)), int(math.floor(green / 10)), int(math.floor(blue / 10))))
strip.show()
time.sleep(SpeedDelay)
time.sleep(ReturnDelay)
SetAll(strip, Color(0, 0, 0))
strip.show()
def Twinkle(strip, red, green, blue, Count, SpeedDelay, OnlyOne):
SetAll(strip, Color(0, 0, 0))
for i in range (0, Count):
strip.setPixelColor(random.randrange(0, LED_COUNT), Color(red, green, blue))
strip.show()
time.sleep(SpeedDelay)
if OnlyOne:
SetAll(strip, Color(0, 0, 0))
time.sleep(SpeedDelay)
def TwinkleRandom(strip, Count, SpeedDelay, OnlyOne):
SetAll(strip, Color(0, 0, 0))
for i in range (0, Count):
strip.setPixelColor(random.randrange(0, LED_COUNT), Color(random.randrange(0, 256), random.randrange(0, 256), random.randrange(0, 256)))
strip.show()
time.sleep(SpeedDelay)
if OnlyOne:
SetAll(strip, Color(0, 0, 0))
time.sleep(SpeedDelay)
def Sparkle(strip, red, green, blue, SpeedDelay):
pixel=random.randrange(0, LED_COUNT)
strip.setPixelColor(pixel, Color(red, green, blue))
strip.show()
time.sleep(SpeedDelay)
strip.setPixelColor(pixel, Color(0, 0, 0))
def SnowSparkle(strip, red, green, blue, SparkleDelay, SpeedDelay):
SetAll(strip, Color(red, green, blue))
pixel=random.randrange(0, LED_COUNT)
strip.setPixelColor(pixel, Color(255, 255, 255))
strip.show()
time.sleep(SparkleDelay)
strip.setPixelColor(pixel, Color(red, green, blue))
strip.show()
time.sleep(SpeedDelay)
def RunningLights(strip, red, green, blue, WaveDelay):
Position=0
for i in range (0, (LED_COUNT * 2)):
Position = Position + 1
for i in range (0, LED_COUNT):
strip.setPixelColor(i, Color(int(math.floor(((math.sin(i+Position) * 127 + 128) / 255) * red)), int(math.floor(((math.sin(i+Position) * 127 + 128) / 255) * green)), int(math.floor(((math.sin(i+Position) * 127 + 128) / 255) * blue))))
strip.show()
time.sleep(WaveDelay)
def RunningLightsOther(strip, red, green, blue, WaveDelay):
Position=0
for i in range ((LED_COUNT * 2), 0, -1):
Position = Position - 1
for i in range (0, LED_COUNT):
strip.setPixelColor(i, Color(int(math.floor(((math.sin(i+Position) * 127 + 128) / 255) * red)), int(math.floor(((math.sin(i+Position) * 127 + 128) / 255) * green)), int(math.floor(((math.sin(i+Position) * 127 + 128) / 255) * blue))))
strip.show()
time.sleep(WaveDelay)
def ColorWipe(strip, red, green, blue, SpeedDelay):
for i in range (0, LED_COUNT):
strip.setPixelColor(i, Color(red, green, blue))
strip.show()
time.sleep(SpeedDelay)
def ColorWipeReverse(strip, red, green, blue, SpeedDelay):
for i in range (LED_COUNT, 0, -1):
strip.setPixelColor(i, Color(red, green, blue))
strip.show()
time.sleep(SpeedDelay)
def Wheel(WheelPosition):
#Generate rainbow colors across 0-255 positions.
if WheelPosition < 85:
return Color(WheelPosition * 3, 255 - WheelPosition * 3, 0)
elif WheelPosition < 170:
WheelPosition -= 85
return Color(255 - WheelPosition * 3, 0, WheelPosition * 3)
else:
WheelPosition -= 170
return Color(0, WheelPosition * 3, 255 - WheelPosition * 3)
#def Rainbow(strip, SpeedDelay):
# for i in range(0, 256):
# #print("I = ",i)
# for j in range(0, LED_COUNT):
# #print("J = ",j)
# strip.setPixelColor(j, Wheel((j + i) & 255))
# strip.show()
# #print("Speed Delay = ",SpeedDelay, "I = ",i, "J = ",j)
# time.sleep(SpeedDelay)
def Rainbow(strip, wait_ms=20, iterations=5):
"""Draw rainbow that fades across all pixels at once."""
for j in range(256 * iterations):
for i in range(strip.numPixels()):
strip.setPixelColor(i, Wheel((i + j) & 255))
strip.show()
time.sleep(wait_ms / 1000.0)
def RainbowCycle(strip, Iterations, SpeedDelay):
for i in range (0, 256 * Iterations):
for j in range (0, LED_COUNT):
strip.setPixelColor(j, Wheel((int(j * 256 / LED_COUNT) + i) & 255))
strip.show()
time.sleep(SpeedDelay)
SetAll(strip, Color(0, 0, 0))
strip.show()
def RainbowCycleSlide(strip, Iterations, SpeedDelay):
# Initialize a circular buffer to store the color values of each LED
buffer = [Color(0, 0, 0)] * LED_COUNT
for i in range (0, 256 * Iterations):
# Shift the circular buffer by one position
buffer.insert(0, buffer.pop())
# Set the color of each LED in the strip using the values in the circular buffer
for j in range (0, LED_COUNT):
strip.setPixelColor(j, buffer[j])
# Update the LED strip
strip.show()
# Introduce a delay between iterations
time.sleep(SpeedDelay)
# Turn off all LEDs
SetAll(strip, Color(0, 0, 0))
strip.show()
def ColorChase(strip, red, green, blue, SpeedDelay):
for i in range(LED_COUNT):
strip.setPixelColor(i, Color(red, green, blue))
strip.show()
time.sleep(SpeedDelay)
strip.setPixelColor(i, Color(0, 0, 0))
strip.show()
def ColorChaseReverse(strip, red, green, blue, SpeedDelay):
for i in range(LED_COUNT, 0, -1):
strip.setPixelColor(i, Color(red, green, blue))
strip.show()
time.sleep(SpeedDelay)
strip.setPixelColor(i, Color(0, 0, 0))
strip.show()
def TheaterChase(strip, red, green, blue, SpeedDelay, Iterations):
for i in range(0, Iterations):
for j in range(0, 3):
for k in range(0, LED_COUNT, 3):
strip.setPixelColor(k + j, Color(red, green, blue))
strip.show()
time.sleep(SpeedDelay)
for k in range(0, LED_COUNT, 3):
strip.setPixelColor(k + j, Color(0, 0, 0))
def TheaterChaseRainbow(strip, SpeedDelay):
#Rainbow movie theater light style chaser animation.
for i in range(256):
for j in range(3):
for k in range(0, strip.numPixels(), 3):
strip.setPixelColor(k + j, Wheel((k + i) % 255))
strip.show()
time.sleep(SpeedDelay)
for k in range(0, strip.numPixels(), 3):
strip.setPixelColor(k + j, Color(0, 0, 0))
strip.setPixelColor(i, Color(0, 0, 0))
strip.show()
def MeteorRain(strip, red, green, blue, MeteorSize, MeteorTrailDecay, MeteorRandomDecay, SpeedDelay):
SetAll(strip, Color(0, 0, 0))
for i in range (0, LED_COUNT + LED_COUNT):
# Fade brightness all LEDs one step
for j in range (0, LED_COUNT):
if ((not MeteorRandomDecay) or ((random.randint(0, 10)>5))):
FadeToBlack(strip, j, MeteorTrailDecay)
# Draw meteor
for j in range (0, MeteorSize):
if (((i - j) < LED_COUNT) and ((i - j) >= 0)):
strip.setPixelColor(i - j, Color(red, green, blue))
strip.show()
time.sleep(SpeedDelay)
#Used by MeteorRain
def FadeToBlack(strip, Position, FadeValue):
OldColor = strip.getPixelColor(Position)
r = (OldColor & 0x00ff0000) >> 16
g = (OldColor & 0x0000ff00) >> 8
b = (OldColor & 0x000000ff)
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)
strip.setPixelColor(Position, Color(r, g, b))
def NewKitt(strip, red, green, blue, EyeSize, SpeedDelay, ReturnDelay):
RightToLeft(strip, red, green, blue, EyeSize, SpeedDelay, ReturnDelay)
LeftToRight(strip, red, green, blue, EyeSize, SpeedDelay, ReturnDelay)
OutsideToCenter(strip, red, green, blue, EyeSize, SpeedDelay, ReturnDelay)
CenterToOutside(strip, red, green, blue, EyeSize, SpeedDelay, ReturnDelay)
RightToLeft(strip, red, green, blue, EyeSize, SpeedDelay, ReturnDelay)
LeftToRight(strip, red, green, blue, EyeSize, SpeedDelay, ReturnDelay)
OutsideToCenter(strip, red, green, blue, EyeSize, SpeedDelay, ReturnDelay)
CenterToOutside(strip, red, green, blue, EyeSize, SpeedDelay, ReturnDelay)
#Used by NewKitt
def CenterToOutside(strip, red, green, blue, EyeSize, SpeedDelay, ReturnDelay):
for i in range(int(math.floor((LED_COUNT-EyeSize)/2)), 0, -1):
SetAll(strip, Color(0, 0, 0))
strip.setPixelColor(i, Color(int(math.floor(red/10)), int(math.floor(green/10)), int(math.floor(blue/10))))
for j in range(1, EyeSize + 1):
strip.setPixelColor(i + j, Color(red, green, blue))
strip.setPixelColor(i + EyeSize + 1, Color(int(math.floor(red/10)), int(math.floor(green/10)), int(math.floor(blue/10))))
strip.setPixelColor(LED_COUNT - i, Color(int(math.floor(red/10)), int(math.floor(green/10)), int(math.floor(blue/10))))
for j in range(1, EyeSize + 1):
strip.setPixelColor(LED_COUNT - i - j, Color(red, green, blue))
strip.setPixelColor(LED_COUNT - i - EyeSize - 1, Color(int(math.floor(red/10)), int(math.floor(green/10)), int(math.floor(blue/10))))
strip.show()
time.sleep(SpeedDelay)
time.sleep(ReturnDelay)
#Used by NewKitt
def OutsideToCenter(strip, red, green, blue, EyeSize, SpeedDelay, ReturnDelay):
for i in range(0, int(math.floor((LED_COUNT-EyeSize)/2))):
SetAll(strip, Color(0, 0, 0))
strip.setPixelColor(i, Color(int(math.floor(red/10)), int(math.floor(green/10)), int(math.floor(blue/10))))
for j in range(1, EyeSize + 1):
strip.setPixelColor(i + j, Color(red, green, blue))
strip.setPixelColor(i + EyeSize + 1, Color(int(math.floor(red/10)), int(math.floor(green/10)), int(math.floor(blue/10))))
strip.setPixelColor(LED_COUNT - i, Color(int(math.floor(red/10)), int(math.floor(green/10)), int(math.floor(blue/10))))
for j in range(1, EyeSize + 1):
strip.setPixelColor(LED_COUNT - i - j, Color(red, green, blue))
strip.setPixelColor(LED_COUNT - i - EyeSize - 1, Color(int(math.floor(red/10)), int(math.floor(green/10)), int(math.floor(blue/10))))
strip.show()
time.sleep(SpeedDelay)
time.sleep(ReturnDelay)
#Used by NewKitt
def LeftToRight(strip, red, green, blue, EyeSize, SpeedDelay, ReturnDelay):
for i in range(0, LED_COUNT - EyeSize - 2):
SetAll(strip, Color(0, 0, 0))
strip.setPixelColor(i, Color(int(math.floor(red/10)), int(math.floor(green/10)), int(math.floor(blue/10))))
for j in range(1, EyeSize+1):
strip.setPixelColor(i + j, Color(red, green, blue))
strip.setPixelColor(i + EyeSize + 1, Color(int(math.floor(red/10)), int(math.floor(green/10)), int(math.floor(blue/10))))
strip.show()
time.sleep(SpeedDelay)
time.sleep(ReturnDelay)
#Used by NewKitt
def RightToLeft(strip, red, green, blue, EyeSize, SpeedDelay, ReturnDelay):
for i in range(LED_COUNT - EyeSize - 2, 0, -1):
SetAll(strip, Color(0, 0, 0))
strip.setPixelColor(i, Color(int(math.floor(red/10)), int(math.floor(green/10)), int(math.floor(blue/10))))
for j in range(1, EyeSize + 1):
strip.setPixelColor(i + j, Color(red, green, blue))
strip.setPixelColor(i + EyeSize + 1, Color(int(math.floor(red/10)), int(math.floor(green/10)), int(math.floor(blue/10))))
strip.show()
time.sleep(SpeedDelay)
time.sleep(ReturnDelay)
#The Heat array needs to be declaired globally. You can put it in the main section (but not in the While loop), but not in the Fire function...
Heat = [0] * LED_COUNT
def Fire(strip, Heat, Cooling, Sparking, SpeedDelay):
#Step 1. Cool down every cell a little
for i in range(0, LED_COUNT):
CoolDown = random.randint(0, (int(math.floor((Cooling * 10) / LED_COUNT)) + 2))
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 i in range((LED_COUNT - 1), 2, -1):
Heat[i] = int(math.floor((Heat[i - 1] + Heat[i - 2] + Heat[i - 2]) / 3))
#Step 3. Randomly ignite new 'sparks' near the bottom
if (random.randint(0, 255) < Sparking):
y=random.randint(0, 7)
#There are 2 different ways to do this part, the commented out line is the alternate
Heat[y] = Heat[y] + random.randrange(160, 255)
#Added check to original code so that the heat never exceeds 255
if (Heat[y]>255): Heat[y]=255
#Heat[y] = random.randrange(160, 255)
#Step 4. Convert heat to LED colors
for i in range(0, LED_COUNT):
SetPixelHeatColor(strip, i, Heat[i])
#Step 5. Display
strip.show()
time.sleep(SpeedDelay)
#Used by Fire
def SetPixelHeatColor(strip, Pixel, Temperature):
#Scale 'heat' down from 0-255 to 0-191
t192 = int(math.floor((Temperature / 255.0) * 191))
#calculate ramp up from
HeatRamp = t192 & 0x3F #0..63
HeatRamp <<= 2 #scale up to 0..252
#figure out which third of the spectrum we're in:
if (t192 > 0x80):
strip.setPixelColor(Pixel, Color(255, 255, HeatRamp))
elif(t192 > 0x40):
strip.setPixelColor(Pixel, Color(255, HeatRamp, 0))
else:
strip.setPixelColor(Pixel, Color(HeatRamp, 0, 0))
#The Freeze array needs to be declaired globally. You can put it in the main section (but not in the While loop), but not in the Ice function...
Freeze = [0] * LED_COUNT
def Ice(strip, Freeze, Cooling, Sparking, SpeedDelay):
#Step 1. Cool down every cell a little
for i in range(0, LED_COUNT):
CoolDown = random.randint(0, (int(math.floor((Cooling * 10) / LED_COUNT)) + 2))
if (CoolDown > Freeze[i]):
Heat[i] = 0
else:
Freeze[i] = Freeze[i] - CoolDown
#Step 2. Heat from each cell drifts 'up' and diffuses a little
for i in range((LED_COUNT - 1), 2, -1):
Freeze[i] = int(math.floor((Freeze[i - 1] + Freeze[i - 2] + Freeze[i - 2]) / 3))
#Step 3. Randomly ignite new 'sparks' near the bottom
if (random.randint(0, 255) < Sparking):
y=random.randint(0, 7)
#There are 2 different ways to do this part, the commented out line is the alternate
Freeze[y] = Freeze[y] + random.randrange(160, 255)
#Added check to original code so that the heat never exceeds 255
if (Freeze[y]>255): Freeze[y]=255
#Freeze[y] = random.randrange(160, 255)
#Step 4. Convert heat to LED colors
for i in range(0, LED_COUNT):
SetPixelFreezeColor(strip, i, Freeze[i])
#Step 5. Display
strip.show()
time.sleep(SpeedDelay)
#Used by Ice
def SetPixelFreezeColor(strip, Pixel, Temperature):
#Scale 'heat' down from 0-255 to 0-191
t192 = int(math.floor((Temperature / 255.0) * 191))
#calculate ramp up from
FreezeRamp = t192 & 0x3F #0..63
FreezeRamp <<= 2 #scale up to 0..252
#figure out which third of the spectrum we're in:
if (t192 > 0x80):
strip.setPixelColor(Pixel, Color(FreezeRamp, 255, 255))
elif(t192 > 0x40):
strip.setPixelColor(Pixel, Color(0, FreezeRamp, 255))
else:
strip.setPixelColor(Pixel, Color(0, 0, FreezeRamp))
#The Herb array needs to be declaired globally. You can put it in the main section (but not in the While loop), but not in the Safe function...
Herb = [0] * LED_COUNT
def Safe(strip, Herb, Cooling, Sparking, SpeedDelay):
#Step 1. Cool down every cell a little
for i in range(0, LED_COUNT):
CoolDown = random.randint(0, (int(math.floor((Cooling * 10) / LED_COUNT)) + 2))
if (CoolDown > Herb[i]):
Heat[i] = 0
else:
Herb[i] = Herb[i] - CoolDown
#Step 2. Heat from each cell drifts 'up' and diffuses a little
for i in range((LED_COUNT - 1), 2, -1):
Herb[i] = int(math.floor((Herb[i - 1] + Herb[i - 2] + Herb[i - 2]) / 3))
#Step 3. Randomly ignite new 'sparks' near the bottom
if (random.randint(0, 255) < Sparking):
y=random.randint(0, 7)
#There are 2 different ways to do this part, the commented out line is the alternate
Herb[y] = Herb[y] + random.randrange(160, 255)
#Added check to original code so that the heat never exceeds 255
if (Herb[y]>255): Herb[y]=255
#Herb[y] = random.randrange(160, 255)
#Step 4. Convert heat to LED colors
for i in range(0, LED_COUNT):
SetPixelHerbColor(strip, i, Herb[i])
#Step 5. Display
strip.show()
time.sleep(SpeedDelay)
#Used by Safe
def SetPixelHerbColor(strip, Pixel, Temperature):
#Scale 'heat' down from 0-255 to 0-191
t192 = int(math.floor((Temperature / 255.0) * 191))
#calculate ramp up from
HerbRamp = t192 & 0x3F #0..63
HerbRamp <<= 2 #scale up to 0..252
#figure out which third of the spectrum we're in:
if (t192 > 0x80):
strip.setPixelColor(Pixel, Color(0, HerbRamp, 255))
elif(t192 > 0x40):
strip.setPixelColor(Pixel, Color(0, HerbRamp, 128))
else:
strip.setPixelColor(Pixel, Color(0, HerbRamp, 0))
#Used by BouncingBalls and BouncingBallsRGB
def GetMillis():
return (time.time() * 1000)
def BouncingBalls(strip, red, green, blue, BallCount):
Gravity = -9.81
StartHeight = 1
Height = [0] * BallCount
ImpactVelocityStart = math.sqrt(-2 * Gravity * StartHeight)
ImpactVelocity = [0] * BallCount
TimeSinceLastBounce = [0] * BallCount
Position = [0] * BallCount
ClockTimeSinceLastBounce = [0] * BallCount
Dampening = [0] * BallCount
for i in range(0, BallCount):
ClockTimeSinceLastBounce[i] = GetMillis()
Height[i] = StartHeight
Position[i] = 0
ImpactVelocity[i] = ImpactVelocityStart
TimeSinceLastBounce[i] = 0
Dampening[i] = 0.90 - (i / math.pow(BallCount,2))
for i in range(0, BallCount):
TimeSinceLastBounce[i] = GetMillis() - ClockTimeSinceLastBounce[i]
Height[i] = 0.5 * Gravity * math.pow((TimeSinceLastBounce[i] / 1000), 2.0) + ImpactVelocity[i] * (TimeSinceLastBounce[i] / 1000)
if (Height[i] < 0):
Height[i] = 0
ImpactVelocity[i] = Dampening[i] * ImpactVelocity[i]
ClockTimeSinceLastBounce[i] = GetMillis()
if (ImpactVelocity[i] < 0.01):
ImpactVelocity[i] = ImpactVelocityStart
Position[i] = int(math.floor((Height[i] * (LED_COUNT - 1)) / StartHeight))
for i in range(0, BallCount):
strip.setPixelColor(Position[i], Color(red, green, blue))
strip.show()
SetAll(strip, Color(0, 0, 0))
def BouncingBallsRGB(strip, BallCount, BallColors):
Gravity = -9.81
StartHeight = 1
Height = [0] * BallCount
ImpactVelocityStart = math.sqrt(-2 * Gravity * StartHeight)
ImpactVelocity = [0] * BallCount
TimeSinceLastBounce = [0] * BallCount
Position = [0] * BallCount
ClockTimeSinceLastBounce = [0] * BallCount
Dampening = [0] * BallCount
for i in range(0, BallCount):
ClockTimeSinceLastBounce[i] = GetMillis()
Height[i] = StartHeight
Position[i] = 0
ImpactVelocity[i] = ImpactVelocityStart
TimeSinceLastBounce[i] = 0
Dampening[i] = 0.90 - (i / math.pow(BallCount,2))
for i in range(0, BallCount):
TimeSinceLastBounce[i] = GetMillis() - ClockTimeSinceLastBounce[i]
Height[i] = 0.5 * Gravity * math.pow((TimeSinceLastBounce[i] / 1000), 2.0) + ImpactVelocity[i] * (TimeSinceLastBounce[i] / 1000)
if (Height[i] < 0):
Height[i] = 0
ImpactVelocity[i] = Dampening[i] * ImpactVelocity[i]
ClockTimeSinceLastBounce[i] = GetMillis()
if (ImpactVelocity[i] < 0.01):
ImpactVelocity[i] = ImpactVelocityStart
Position[i] = int(math.floor((Height[i] * (LED_COUNT - 1)) / StartHeight))
for i in range(0, BallCount):
strip.setPixelColor(Position[i], Color(BallColors[i][i], BallColors[i][1], BallColors[i][2]))
strip.show()
SetAll(strip, Color(0, 0, 0))
def HalloweenEyes(strip, red, green, blue, EyeWidth, EyeSpace, Fade, Steps, FadeDelay, EndPause):
StartPoint = random.randint(0, LED_COUNT - (2 * EyeWidth) - EyeSpace)
Start2ndEye = StartPoint + EyeWidth + EyeSpace
for i in range(0, EyeWidth):
strip.setPixelColor(StartPoint + i, Color(red, green, blue))
strip.setPixelColor(Start2ndEye + i, Color(red, green, blue))
strip.show()
if (Fade):
for i in range(Steps, -1, -1):
r = i * int(math.floor(red / Steps))
g = i * int(math.floor(green / Steps))
b = i * int(math.floor(blue / Steps))
for j in range(0, EyeWidth):
strip.setPixelColor(StartPoint, Color(r, g, b))
strip.setPixelColor(Start2ndEye, Color(r, g, b))
strip.show()
time.sleep(FadeDelay)
SetAll(strip, Color(0, 0, 0))
time.sleep(EndPause)
#Morse Code dictionary
MORSE_CODE_DICT = {'A':'.-', 'B':'-...', 'C':'-.-.', 'D':'-..', 'E':'.', 'F':'..-.', 'G':'--.', 'H':'....', 'I':'..', 'J':'.---', 'K':'-.-', 'L':'.-..', 'M':'--', 'N':'-.', 'O':'---', 'P':'.--.', 'Q':'--.-', 'R':'.-.', 'S':'...', 'T':'-', 'U':'..-', 'V':'...-', 'W':'.--', 'X':'-..-', 'Y':'-.--', 'Z':'--..', '1':'.----', '2':'..---', '3':'...--', '4':'....-', '5':'.....', '6':'-....', '7':'--...', '8':'---..', '9':'----.', '0':'-----', ', ':'--..--', '.':'.-.-.-', '?':'..--..', '/':'-..-.', '-':'-....-', '(':'-.--.', ')':'-.--.-'}
def MorseCode(strip, red, green, blue, Message, DotLength):
Message = Message.upper()
MessageArray=list(Message)
cipher = ''
#Fill cipher with converted MessageArray values
for i in range(0, len(MessageArray)):
if MessageArray[i] != ' ':
if i < (len(MessageArray)-1):
if MessageArray[i+1] == ' ':
cipher += MORSE_CODE_DICT[MessageArray[i]]
else:
cipher += MORSE_CODE_DICT[MessageArray[i]] + ' '
else:
cipher += MORSE_CODE_DICT[MessageArray[i]]
else:
cipher += '='
"""
Dot length = Dot length x 1
Dash length = Dot length x 3
Pause between elements = Dot length x 1
Pause between characters = Dot length x 3
Pause between words = Dot length x 7
"""
for element in cipher:
if element == '.':
SetAll(strip, Color(red, green, blue))
strip.show()
time.sleep(DotLength)
#Include Pause between elements
SetAll(strip, Color(0, 0, 0))
strip.show()
time.sleep(DotLength)
elif element == '-':
SetAll(strip, Color(red, green, blue))
strip.show()
time.sleep(DotLength * 3)
#Include Pause between elements
SetAll(strip, Color(0, 0, 0))
strip.show()
time.sleep(DotLength)
elif element == ' ':
#Since we have already included an element Pause, only pause between characters for 2 dot lengths
SetAll(strip, Color(0, 0, 0))
strip.show()
time.sleep(DotLength * 2)
elif element == '=':
#Since we have already included an element Pause, only pause between words for 6 dot lengths
SetAll(strip, Color(0, 0, 0))
strip.show()
time.sleep(DotLength * 6)
#Give word pause at end of message. Since we have already included an element Pause, only pause between words for 6 dot lengths
SetAll(strip, Color(0, 0, 0))
strip.show()
time.sleep(DotLength * 6)
def Clock1(strip):
#This function is geared towards my 60 LED strip; you'll need to modify it if you have something else.
#Get the current Time
CurrentTime=datetime.datetime.now()
Hours = int(CurrentTime.hour)
Minutes = int(CurrentTime.minute)
Seconds = int(CurrentTime.second)
Microseconds = int(CurrentTime.microsecond) #range=1,000,000
#Convert from 24h to 12h
if (Hours>12):
AMPM="PM"
Hours=Hours-12
else:
AMPM="AM"
#Output = LED - Notes
#Hours = 0-11 - Divided into 4 red, 4 green, and 4 blue for easier counting
#AMPM = 12 - AM = solid white, PM = blinking white
#Seperator = 13
#Minutes = 14-43 - 30 pixels - blinks the last pixel if it is the first half of the 2 minutes, solid for the 2nd half of the 2 minutes
#Seperator = 44
#Seconds = 45-59 - 15 pixels - each pixel represents 4 seconds, the last pixel increases brightness for each second
SetAll(strip, Color(0, 0, 0))
#Hours
for i in range(1,4+1):
if (i>Hours):
strip.setPixelColor(i-1, Color(0, 0, 0))
else:
strip.setPixelColor(i-1, Color(255, 0, 0))
for i in range(5,8+1):
if (i>Hours):
strip.setPixelColor(i-1, Color(0, 0, 0))
else:
strip.setPixelColor(i-1, Color(0, 255, 0))
for i in range(9,12+1):
if (i>Hours):
strip.setPixelColor(i-1, Color(0, 0, 0))
else:
strip.setPixelColor(i-1, Color(0, 0, 255))
#AMPM
if (AMPM=='AM'):
strip.setPixelColor(12, Color(255, 255, 255))
else:
#Blink the PM
if (Microseconds<500000):
strip.setPixelColor(12, Color(0, 0, 0))
else:
strip.setPixelColor(12, Color(255, 255, 255))
#Seperator
strip.setPixelColor(13, Color(128, 0, 255))
#Minutes
for i in range(0,30):
if Minutes>((i*2)+1):
strip.setPixelColor(i+14, Color(255, 255, 0))
elif Minutes==(i*2) or Minutes==((i*2)+1):
if Minutes==(i*2):
if (Microseconds<500000):
strip.setPixelColor(12, Color(0, 0, 0))
else:
strip.setPixelColor(i+14, Color(255, 255, 0))
else:
strip.setPixelColor(i+14, Color(255, 255, 0))
else:
strip.setPixelColor(i+14, Color(0, 0, 0))
#Seperator
strip.setPixelColor(44, Color(128, 0, 255))
#Seconds
for i in range(0,15):
if Seconds>((i*4)+3):
strip.setPixelColor(i+45, Color(0, 255, 255))
elif (i*4) <= Seconds <= ((i*4)+3):
if Seconds==((i*4)+0):
strip.setPixelColor(i+45, Color(0, 51, 51))
elif Seconds==((i*4)+1):
strip.setPixelColor(i+45, Color(0, 102, 102))
elif Seconds==((i*4)+2):
strip.setPixelColor(i+45, Color(0, 153, 153))
else:
strip.setPixelColor(i+45, Color(0, 204, 204))
else:
strip.setPixelColor(i+45, Color(0, 0, 0))
#Show Results
strip.show()
def Clock2(strip, IncludeHours):
#This function is geared towards my 60 LED strip; you'll need to modify it if you have something else.
#Based on this: https://www.youtube.com/watch?v=YTbRFqJvVOE
#Get the current Time
CurrentTime=datetime.datetime.now()
Hours = int(CurrentTime.hour)
Minutes = int(CurrentTime.minute)
Seconds = int(CurrentTime.second)
#Convert from 24h to 12h
if (Hours>12):
AMPM="PM"
Hours=Hours-12
else:
AMPM="AM"
#Output
SetAll(strip, Color(0, 0, 0))
if IncludeHours:
for i in range (0, 60):
if i%5==0:
strip.setPixelColor(i, Color(64, 64, 64))
HourPosition = Hours * 5
if HourPosition==60:
HourPosition=0
strip.setPixelColor(HourPosition, Color(255, 0, 0))
strip.setPixelColor(Minutes, Color(0, 255, 0))
strip.setPixelColor(Seconds, Color(0, 0, 255))
#Intersections
if Minutes==Seconds:
strip.setPixelColor(Seconds, Color(0, 255, 255))
if Seconds==HourPosition:
strip.setPixelColor(Seconds, Color(255, 0, 255))
if Minutes==HourPosition:
strip.setPixelColor(Seconds, Color(255, 255, 0))
if Seconds==HourPosition and Minutes==HourPosition:
strip.setPixelColor(Seconds, Color(255, 255, 255))
strip.show()
def FillDownRandom(strip, SpeedDelay, DisplayDelay, PauseDelay, FlushDelay):
SetAll(strip, Color(0, 0, 0))
#Fill down with random colors
for i in range(0, LED_COUNT):
r=random.randint(0, 255)
g=random.randint(0, 255)
b=random.randint(0, 255)
for j in range(0,LED_COUNT-i):
strip.setPixelColor(j, Color(r, g, b))
if j>0:
strip.setPixelColor(j-1, Color(0, 0, 0))
strip.show()
time.sleep(SpeedDelay)
time.sleep(DisplayDelay)
time.sleep(PauseDelay)
#"Flush" results
for i in range(LED_COUNT-1, -1, -1):
for j in range(LED_COUNT-1, 0, -1):
OldColor = strip.getPixelColor(j-1)
strip.setPixelColor(j, OldColor)
strip.setPixelColor(i-LED_COUNT+1, Color(0, 0, 0))
strip.show()
time.sleep(FlushDelay)
time.sleep(PauseDelay)
def RandomColors(strip, SpeedDelay):
SetAll(strip, Color(0, 0, 0))
while True:
for i in range(0, LED_COUNT):
r=random.randint(0, 255)
g=random.randint(0, 255)
b=random.randint(0, 255)
strip.setPixelColor(i, Color(r, g, b))
strip.show()
time.sleep(SpeedDelay)
def Blank(strip):
SetAll(strip, Color(0, 0, 0))
strip.show()
def Cops(strip, StrobeCount, FlashDelay, EndPause):
for i in range (0, StrobeCount):
SetAll(strip, Color(0, 0, 64))
strip.show()
time.sleep(FlashDelay)
SetAll(strip, Color(64, 0, 0))
strip.show()
time.sleep(FlashDelay)
# time.sleep(EndPause)
"""
Done
"""