-
Notifications
You must be signed in to change notification settings - Fork 0
/
tl9.py
executable file
·213 lines (178 loc) · 4.46 KB
/
tl9.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
#!/usr/bin/env python3
import sys
import board
import neopixel
import time
import random
import asyncio
if len(sys.argv) > 1:
num_lights = int(sys.argv[1])
else:
num_lights = 50
print(f'num_lights is: {num_lights}')
seg_length = 10
# transform between RGB color and GBR
# goes both ways since it just swaps the first two of the tuple
def t(c):
return (c[1], c[0], c[2])
black = (0, 0, 0)
white = (255, 255, 255)
def sleep(seconds):
time.sleep(seconds)
async def slowOn(x):
c = randomColor(x)
delay = .020
for y in range(256):
#print(f'y is {y}')
pc = float(y+1) / 256.0
#print(f'pc is {pc}')
#c2 = (int(pc * c[0]), int(pc * c[1]), int(pc * c[2]))
c2 = (pc * c[0], pc * c[1], pc * c[2])
#print(f'c2 is {c2}')
await asyncio.sleep(delay * (1-pc))
pixels[x] = c2
str[x] = c2
def setLights(str):
for n in range(len(str)):
pixels[n] = str[n]
async def blink(n):
c = str[n]
str[n] = black
pixels[n] = black
await asyncio.sleep(0.25) # was .05
str[n] = c
pixels[n] = c
async def blinks(which):
print(f'len(which) is: {len(which)}')
count = 0
while (True):
# await blink(which[count])
r = random.randint(0, len(which) - 1)
print(f'blinks: r is: {r}')
await blink(which[r])
count = (count + 1) % len(which)
await asyncio.sleep(0.15) # was 1
async def fade(n):
# c = str[n]
await slowOn(n)
async def fades(which):
count = 0
while (True):
r = random.randint(0, len(which) - 1)
print(f'fades: r is: {r}')
# await fade(which[count])
await fade(which[r])
count = (count + 1) % len(which)
await asyncio.sleep(1)
# program 50 lights with the default brightness 1.0, and autoWrite true
#pixels = neopixel.NeoPixel(board.D18, num_lights)
# following did not do what I thought it would
#pixels = neopixel.NeoPixel(board.D18, num_lights, pixel_order=neopixel.RGBW)
pixels = neopixel.NeoPixel(board.D18, num_lights)
# set first LED to green
#pixels[0] = (255, 0, 0, 0)
#pixels.fill(black)
#print('red')
#pixels[0] = t((255, 0, 0))
#sleep(1)
#print('green')
#pixels[0] = t((0, 255, 0))
#sleep(1)
#print('blue')
#pixels[0] = t((0, 0, 255))
#sleep(1)
#pixels.fill(black)
# light 20 bright green
#pixels[19] = t((0,255,0))
# light all pixels red
#pixels.fill(t((255,0,0)))
# turn off neopixels
pixels.fill(black)
# green, red, blue
# red, green, blue
# Colors must be supplied to pixels as grb colors,
# but I think in rgb.
# green, red, blue colors translated from rgb
black = t(( 0, 0, 0))
blue = t((000, 000, 255))
red = t((255, 0, 0))
magenta = t((255, 0, 255))
green = t(( 0, 255, 0))
cyan = t(( 0, 255, 255))
yellow = t((255, 255, 000))
white = t((255, 255, 255))
purple = t((75, 0, 130))
str = []
#pixels.fill(black)
#print('red')
#pixels[0] = red
#sleep(1)
#print('green')
#pixels[0] = green
#sleep(1)
#print('blue')
#pixels[0] = blue
#sleep(1)
#pixels.fill(black)
colors = [
white,
red,
green,
blue,
yellow,
purple,
magenta,
cyan,
black
]
def randomColor(x):
#return (colors[0])
r = random.randint(0,5)
#print (f'r is: {r}')
return colors[r] # "christmas light colors"
#not reached at the moment
if (x % 2 == 0):
return colors[random.randint(0,5)]
return (random.randint(0,255),random.randint(0,255),random.randint(0,255))
#for x in range(1000):
# pixels.fill(randomColor(x))
# sleep(.010)
print(type(str))
for n in range(num_lights):
print(f'n is: {n}')
rc = randomColor(n)
print(f'rc is: {rc}')
str.append(rc)
setLights(str)
loop = asyncio.get_event_loop()
l = []
for i in range(int(num_lights/2)):
l.append(2*i)
# loop.create_task(blinks([1,3,5]))
print(l)
loop.create_task(blinks(l))
l = []
for i in range(int(num_lights/2)):
l.append(2*i+1)
#loop.create_task(fades([0,2,4]))
print(l)
loop.create_task(fades(l))
loop.run_forever()
loop.close()
for n in range(100):
r = random.randint(0,len(str)-1)
slowOn(r)
pixels.fill(black)
delay = 0.04
while True:
for x in range(num_lights+seg_length):
# print(f'setting light {x} to a color')
if (x < num_lights):
pixels[x] = randomColor(x)
# print(f'x is {x}')
if ((x >= num_lights) or (x >= seg_length)):
# print(f'setting light {x-seg_length} to black')
pixels[x-seg_length] = black
sleep(delay)
sleep(1)
pixels.fill((0,0,0))