-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdatecolors.py
More file actions
executable file
·463 lines (424 loc) · 15.3 KB
/
updatecolors.py
File metadata and controls
executable file
·463 lines (424 loc) · 15.3 KB
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
#!/usr/bin/env python3
# NeoPixel library strandtest example
# Author: Tony DiCola (tony@tonydicola.com)
#
# Direct port of the Arduino NeoPixel library strandtest example. Showcases
# various animations on a strip of NeoPixels.
import time
import math
from rpi_ws281x import *
import argparse
import requests
import json
import os
import random
import threading
API_KEY = os.getenv("YAKKO_API_KEY", "")
SERVER_URL = os.getenv("YAKKO_URL", "http://yakko.cs.wmich.edu:8878")
# LED strip configuration:
LED_COUNT = 300 # Number of LED pixels.
LED_PIN = 18 # 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 = 255 # 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
# request thread
STATUS = {}
def interrupted():
"""Check if the animation type has changed, allowing faster mode switching."""
return STATUS.get('_last_type') != STATUS.get('type')
# Define functions which animate LEDs in various ways.
def colorWipe(strip, color, wait_ms=25):
"""Wipe color across display a pixel at a time."""
for i in range(strip.numPixels()):
if interrupted():
return
strip.setPixelColor(i, color)
strip.show()
time.sleep(wait_ms/1000.0)
def theaterChase(strip, color, wait_ms=100, iterations=50):
"""Movie theater light style chaser animation."""
for j in range(iterations):
if interrupted():
return
for q in range(3):
for i in range(0, strip.numPixels(), 3):
strip.setPixelColor(i+q, color)
strip.show()
time.sleep(wait_ms/1000.0)
for i in range(0, strip.numPixels(), 3):
strip.setPixelColor(i+q, 0)
def wheel(pos):
"""Generate rainbow colors across 0-255 positions."""
if pos < 85:
return Color(pos * 3, 255 - pos * 3, 0)
elif pos < 170:
pos -= 85
return Color(255 - pos * 3, 0, pos * 3)
else:
pos -= 170
return Color(0, pos * 3, 255 - pos * 3)
def rainbow(strip, wait_ms=20, iterations=1):
"""Draw rainbow that fades across all pixels at once."""
for j in range(256*iterations):
if interrupted():
return
for i in range(strip.numPixels()):
strip.setPixelColor(i, wheel((i+j) & 255))
strip.show()
time.sleep(wait_ms/1000.0)
def rainbowCycle(strip, wait_ms=20, iterations=5):
"""Draw rainbow that uniformly distributes itself across all pixels."""
for j in range(256*iterations):
if interrupted():
return
for i in range(strip.numPixels()):
strip.setPixelColor(i, wheel((int(i * 256 / strip.numPixels()) + j) & 255))
strip.show()
time.sleep(wait_ms/1000.0)
def theaterChaseRainbow(strip, wait_ms=50):
"""Rainbow movie theater light style chaser animation."""
for j in range(256):
if interrupted():
return
for q in range(3):
for i in range(0, strip.numPixels(), 3):
strip.setPixelColor(i+q, wheel((i+j) % 255))
strip.show()
time.sleep(wait_ms/1000.0)
for i in range(0, strip.numPixels(), 3):
strip.setPixelColor(i+q, 0)
# --- New animation modes ---
def solid(strip, color):
"""Set all pixels instantly to the color."""
for i in range(strip.numPixels()):
strip.setPixelColor(i, color)
strip.show()
# Hold until mode changes
while not interrupted():
time.sleep(0.5)
def breathe(strip, color, cycles=10):
"""Smooth fade in/out of the current color (breathing/pulse effect)."""
r = (color >> 16) & 0xFF
g = (color >> 8) & 0xFF
b = color & 0xFF
for _ in range(cycles):
# Fade in and out using a sine curve
for step in range(200):
if interrupted():
return
brightness = (math.sin(step * math.pi / 100) ** 2)
cr = int(r * brightness)
cg = int(g * brightness)
cb = int(b * brightness)
c = Color(cr, cg, cb)
for i in range(strip.numPixels()):
strip.setPixelColor(i, c)
strip.show()
time.sleep(0.015)
def strobe(strip, color, flashes=50, on_ms=50, off_ms=50):
"""Fast on/off flashing."""
off = Color(0, 0, 0)
for _ in range(flashes):
if interrupted():
return
for i in range(strip.numPixels()):
strip.setPixelColor(i, color)
strip.show()
time.sleep(on_ms / 1000.0)
for i in range(strip.numPixels()):
strip.setPixelColor(i, off)
strip.show()
time.sleep(off_ms / 1000.0)
def fire(strip, cooling=55, sparking=120, iterations=500):
"""Flickering fire simulation (red/orange/yellow)."""
num = strip.numPixels()
heat = [0] * num
for _ in range(iterations):
if interrupted():
return
# Cool down every cell
for i in range(num):
heat[i] = max(0, heat[i] - random.randint(0, ((cooling * 10) // num) + 2))
# Heat diffuses upward
for i in range(num - 1, 1, -1):
heat[i] = (heat[i - 1] + heat[i - 2] + heat[i - 2]) // 3
# Randomly ignite sparks near bottom
if random.randint(0, 255) < sparking:
y = random.randint(0, min(7, num - 1))
heat[y] = min(255, heat[y] + random.randint(160, 255))
# Map heat to color
for i in range(num):
t = heat[i]
if t > 170:
# hot: white-yellow
strip.setPixelColor(i, Color(255, 255, min(255, (t - 170) * 3)))
elif t > 85:
# medium: orange
strip.setPixelColor(i, Color(255, min(255, (t - 85) * 3), 0))
else:
# cool: red
strip.setPixelColor(i, Color(min(255, t * 3), 0, 0))
strip.show()
time.sleep(0.02)
def meteor(strip, color, meteor_size=10, decay=64, iterations=3):
"""Shooting light with fading tail."""
r = (color >> 16) & 0xFF
g = (color >> 8) & 0xFF
b = color & 0xFF
num = strip.numPixels()
for _ in range(iterations):
if interrupted():
return
# Clear strip
for i in range(num):
strip.setPixelColor(i, Color(0, 0, 0))
strip.show()
for j in range(num + meteor_size):
if interrupted():
return
# Fade all pixels
for i in range(num):
cr = (strip.getPixelColor(i) >> 16) & 0xFF
cg = (strip.getPixelColor(i) >> 8) & 0xFF
cb = strip.getPixelColor(i) & 0xFF
cr = max(0, cr - decay) if random.randint(0, 10) > 3 else cr
cg = max(0, cg - decay) if random.randint(0, 10) > 3 else cg
cb = max(0, cb - decay) if random.randint(0, 10) > 3 else cb
strip.setPixelColor(i, Color(cr, cg, cb))
# Draw meteor
for k in range(meteor_size):
if 0 <= j - k < num:
strip.setPixelColor(j - k, Color(r, g, b))
strip.show()
time.sleep(0.01)
def scanner(strip, color, iterations=5):
"""Knight Rider bouncing light (Larson scanner)."""
r = (color >> 16) & 0xFF
g = (color >> 8) & 0xFF
b = color & 0xFF
num = strip.numPixels()
eye_size = max(1, num // 30)
for _ in range(iterations):
if interrupted():
return
# Forward
for j in range(num - eye_size - 1):
if interrupted():
return
for i in range(num):
strip.setPixelColor(i, Color(0, 0, 0))
# Dim leading pixel
strip.setPixelColor(j, Color(r // 10, g // 10, b // 10))
# Eye
for i in range(eye_size):
strip.setPixelColor(j + 1 + i, Color(r, g, b))
# Dim trailing pixel
strip.setPixelColor(j + eye_size + 1, Color(r // 10, g // 10, b // 10))
strip.show()
time.sleep(0.005)
# Backward
for j in range(num - eye_size - 1, 0, -1):
if interrupted():
return
for i in range(num):
strip.setPixelColor(i, Color(0, 0, 0))
strip.setPixelColor(j, Color(r // 10, g // 10, b // 10))
for i in range(eye_size):
strip.setPixelColor(j + 1 + i, Color(r, g, b))
strip.setPixelColor(j + eye_size + 1, Color(r // 10, g // 10, b // 10))
strip.show()
time.sleep(0.005)
def sparkle(strip, color, iterations=300):
"""Random twinkling pixels over base color."""
num = strip.numPixels()
# Set base color
for i in range(num):
strip.setPixelColor(i, color)
strip.show()
for _ in range(iterations):
if interrupted():
return
# Light a random pixel white
pixel = random.randint(0, num - 1)
strip.setPixelColor(pixel, Color(255, 255, 255))
strip.show()
time.sleep(0.05)
# Restore base color
strip.setPixelColor(pixel, color)
strip.show()
time.sleep(0.05)
def police(strip, flashes=50):
"""Red/blue alternating flash."""
num = strip.numPixels()
half = num // 2
red = Color(255, 0, 0)
blue = Color(0, 0, 255)
off = Color(0, 0, 0)
for _ in range(flashes):
if interrupted():
return
# Red left, blue right
for i in range(half):
strip.setPixelColor(i, red)
for i in range(half, num):
strip.setPixelColor(i, blue)
strip.show()
time.sleep(0.1)
# Off
for i in range(num):
strip.setPixelColor(i, off)
strip.show()
time.sleep(0.05)
# Blue left, red right
for i in range(half):
strip.setPixelColor(i, blue)
for i in range(half, num):
strip.setPixelColor(i, red)
strip.show()
time.sleep(0.1)
for i in range(num):
strip.setPixelColor(i, off)
strip.show()
time.sleep(0.05)
def gradient(strip, color):
"""Smooth gradient from the set color to its complement."""
r = (color >> 16) & 0xFF
g = (color >> 8) & 0xFF
b = color & 0xFF
# Complement color
cr, cg, cb = 255 - r, 255 - g, 255 - b
num = strip.numPixels()
for i in range(num):
ratio = i / max(1, num - 1)
pr = int(r + (cr - r) * ratio)
pg = int(g + (cg - g) * ratio)
pb = int(b + (cb - b) * ratio)
strip.setPixelColor(i, Color(pr, pg, pb))
strip.show()
# Hold until mode changes
while not interrupted():
time.sleep(0.5)
def wave(strip, color, iterations=100):
"""Sinusoidal brightness wave moving along the strip."""
r = (color >> 16) & 0xFF
g = (color >> 8) & 0xFF
b = color & 0xFF
num = strip.numPixels()
for offset in range(iterations * num):
if interrupted():
return
for i in range(num):
brightness = (math.sin((i + offset) * 2 * math.pi / 60) + 1) / 2
strip.setPixelColor(i, Color(
int(r * brightness),
int(g * brightness),
int(b * brightness)
))
strip.show()
time.sleep(0.02)
def candy(strip, color, segment_len=15, iterations=300):
"""Alternating colored segments that scroll."""
r = (color >> 16) & 0xFF
g = (color >> 8) & 0xFF
b = color & 0xFF
num = strip.numPixels()
for offset in range(iterations):
if interrupted():
return
for i in range(num):
pos = (i + offset) % (segment_len * 2)
if pos < segment_len:
strip.setPixelColor(i, Color(r, g, b))
else:
strip.setPixelColor(i, Color(255, 255, 255))
strip.show()
time.sleep(0.05)
def off(strip):
"""Turn all LEDs off."""
for i in range(strip.numPixels()):
strip.setPixelColor(i, Color(0, 0, 0))
strip.show()
# Hold until mode changes
while not interrupted():
time.sleep(0.5)
def get_status():
global STATUS
while 1:
try:
r = requests.get(SERVER_URL, headers={"Authorization": "Bearer " + API_KEY})
except:
print("Failed connection.")
time.sleep(2)
continue
print("GET: " + str(r.text))
STATUS = json.loads(r.text)
time.sleep(1)
# Main program logic follows:
if __name__ == '__main__':
# Process arguments
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--clear', action='store_true', help='clear the display on exit')
args = parser.parse_args()
# Create NeoPixel object with appropriate configuration.
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
# Intialize the library (must be called once before other functions).
strip.begin()
print ('Press Ctrl-C to quit.')
if not args.clear:
print('Use "-c" argument to clear LEDs on exit')
# start requests thread
t = threading.Thread(target=get_status)
t.daemon = True
t.start()
# wait for first request
time.sleep(3)
try:
while True:
status = STATUS
print (status)
# Track which type we're running so interrupted() works
STATUS['_last_type'] = status.get('type', 'color')
color = Color(random.randint(16, 128), random.randint(16, 128), random.randint(16, 128))
if status.get('type') != "random":
color = Color(int(status.get('red', 0)), int(status.get('green', 0)), int(status.get('blue', 0)))
rand = random.randint(1, 3)
mode = status.get('type', 'color')
if mode == "off":
off(strip)
elif mode == "solid":
solid(strip, color)
elif mode == "breathe":
breathe(strip, color)
elif mode == "strobe":
strobe(strip, color)
elif mode == "fire":
fire(strip)
elif mode == "meteor":
meteor(strip, color)
elif mode == "scanner":
scanner(strip, color)
elif mode == "sparkle":
sparkle(strip, color)
elif mode == "police":
police(strip)
elif mode == "gradient":
gradient(strip, color)
elif mode == "wave":
wave(strip, color)
elif mode == "candy":
candy(strip, color)
elif mode == "rainbow" or (mode == "random" and rand == 1):
rainbowCycle(strip)
elif mode == "chase" or (mode == "random" and rand == 2):
theaterChase(strip, color)
colorWipe(strip, color, 1)
colorWipe(strip, Color(0,0,0), 1)
else:
colorWipe(strip, color)
colorWipe(strip, Color(0,0,0), 5)
except KeyboardInterrupt:
if args.clear:
colorWipe(strip, Color(0,0,0), 10)