-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfade_neopixles.py
109 lines (96 loc) · 2.63 KB
/
fade_neopixles.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
from machine import Pin
from neopixel import NeoPixel
import urandom
import time
NUM_PIXELS = 64
NUM_PANELS = 1
PANELS = [64]
STEPS = 100
PAUSE = 0.1
pin = Pin(2, Pin.OUT) # D4 on nodemcu
# \o/ looks like number on the silkscreen match up to GPIO numbers on the ESP32 boards I've got
np = NeoPixel(pin, NUM_PIXELS)
np[0] = (0, 128, 0)
np.write()
for i in range(1, NUM_PIXELS):
np[i-1] = (0,0,0)
np[i] = (0, 128, 0)
np.write()
time.sleep(.05)
# from colorsys.py
def hsv_to_rgb(h, s, v):
if s == 0.0:
return v, v, v
i = int(h*6.0) # XXX assume int() truncates!
f = (h*6.0) - i
p = v*(1.0 - s)
q = v*(1.0 - s*f)
t = v*(1.0 - s*(1.0-f))
i = i%6
if i == 0:
return v, t, p
if i == 1:
return q, v, p
if i == 2:
return p, v, t
if i == 3:
return p, q, v
if i == 4:
return t, p, v
if i == 5:
return v, p, q
# Cannot get here
def randc():
r,g,b = hsv_to_rgb(urandom.getrandbits(8)/255.0, 1, 1.0)
return [int(r*255), int(g*255), int(b*255)]
def fade_each_px():
cur = [randc() for x in range(NUM_PIXELS)]
nxt = [randc() for x in range(NUM_PIXELS)]
while True:
for s in range(STEPS):
for p in range(NUM_PIXELS):
np[p] = [int(cur[p][c]+(s*(nxt[p][c]-cur[p][c])/STEPS)) for c in range(3)]
np.write()
time.sleep(PAUSE)
cur = nxt
nxt = [randc() for x in range(NUM_PIXELS)]
time.sleep(1000*PAUSE)
print("Next colour.")
def fade_each():
cur = [randc() for x in range(NUM_PANELS)]
nxt = [randc() for x in range(NUM_PANELS)]
while True:
for i in range(STEPS):
for p in range(NUM_PANELS):
for q in range(PANELS[p]):
np[sum(PANELS[:p])+q] = [int(cur[p][j]+(i*(nxt[p][j]-cur[p][j])/STEPS)) for j in range(3)]
np.write()
time.sleep(PAUSE)
cur = nxt
nxt = [randc() for x in range(NUM_PANELS)]
time.sleep(100*PAUSE)
print("Next colour.")
def jmp_each():
while True:
for i in range(NUM_PIXELS):
np[i] = [int(v) for v in randc()]
np.write()
time.sleep(0.5)
def fade_all():
cur = randc()
nxt = randc()
while True:
for i in range(STEPS):
val = [int(cur[j]+(i*(nxt[j]-cur[j])/STEPS)) for j in range(3)]
for p in range(NUM_PIXELS):
np[p] = val
np.write()
time.sleep(PAUSE)
cur = nxt
nxt = randc()
time.sleep(PAUSE*20)
print("Next colour")
fade_each()
# jmp_each()
# fade_each_px()
# fade_all()