-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstrip_animations.py
510 lines (412 loc) · 16.1 KB
/
strip_animations.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
from bibliopixel import LEDStrip
import bibliopixel.colors as colors
from bibliopixel.animation import BaseStripAnim
import math
import time
import random
class Rainbow(BaseStripAnim):
"""Generate rainbow distributed over 256 pixels.
If you want the full rainbow to fit in the number of pixels you
are using, use RainbowCycle instead
"""
def __init__(self, led, start=0, end=-1):
super(Rainbow, self).__init__(led, start, end)
def step(self, amt = 1):
for i in range(self._size):
h = (i + self._step) % 255
self._led.set(self._start + i, colors.hue2rgb_rainbow(h))
self._step += amt
overflow = self._step - 256
if overflow >= 0:
self._step = overflow
class RainbowCycle(BaseStripAnim):
"""Generate rainbow wheel equally distributed over strip."""
def __init__(self, led, start=0, end=-1):
super(RainbowCycle, self).__init__(led, start, end)
def step(self, amt = 1):
for i in range(self._size):
c = colors.hue_helper(i, self._size, self._step)
self._led.set(self._start + i, c)
self._step += amt
overflow = self._step - 256
if overflow >= 0:
self._step = overflow
class ColorPattern(BaseStripAnim):
"""Fill the dots progressively along the strip with alternating colors."""
def __init__(self, led, colors, width, dir = True, start=0, end=-1):
super(ColorPattern, self).__init__(led, start, end)
self._colors = colors
self._colorCount = len(colors)
self._width = width
self._total_width = self._width * self._colorCount;
self._dir = dir
def step(self, amt = 1):
for i in range(self._size):
cIndex = ((i+self._step) % self._total_width) / self._width;
self._led.set(self._start + i, self._colors[cIndex])
if self._dir:
self._step += amt
overflow = (self._start + self._step) - self._end
if overflow >= 0:
self._step = overflow
else:
self._step -= amt
if self._step < 0:
self._step = self._end + self._step
class ColorWipe(BaseStripAnim):
"""Fill the dots progressively along the strip."""
def __init__(self, led, color, start=0, end=-1):
super(ColorWipe, self).__init__(led, start, end)
self._color = color
def step(self, amt = 1):
if self._step == 0:
self._led.all_off()
for i in range(amt):
self._led.set(self._start + self._step - i, self._color)
self._step += amt
overflow = (self._start + self._step) - self._end
if overflow >= 0:
self._step = overflow
class ColorFade(BaseStripAnim):
"""Fill the dots progressively along the strip."""
def wave_range(self, start, peak, step):
main = [i for i in range(start, peak+1, step)]
return main + [i for i in reversed(main[0:len(main)-1])]
def __init__(self, led, colors, step = 5, start=0, end=-1):
super(ColorFade, self).__init__(led, start, end)
self._colors = colors
self._levels = self.wave_range(30, 255, step)
self._level_count = len(self._levels)
self._color_count = len(colors)
def step(self, amt = 1):
if self._step > self._level_count * self._color_count:
self._step = 0
c_index = (self._step / self._level_count) % self._color_count
l_index = (self._step % self._level_count)
color = self._colors[c_index];
self._led.fill(colors.color_scale(color, self._levels[l_index]), self._start, self._end)
self._step += amt
class ColorChase(BaseStripAnim):
"""Chase one pixel down the strip."""
def __init__(self, led, color, width=1, start=0, end=-1):
super(ColorChase, self).__init__(led, start, end)
self._color = color
self._width = width
def step(self, amt = 1):
self._led.all_off() #because I am lazy
for i in range(self._width):
self._led.set(self._start + self._step + i, self._color)
self._step += amt
overflow = (self._start + self._step) - self._end
if overflow >= 0:
self._step = overflow
class PartyMode(BaseStripAnim):
"""Stobe Light Effect."""
def __init__(self, led, colors, start=0, end=-1):
super(PartyMode, self).__init__(led, start, end)
self._colors = colors
self._color_count = len(colors)
def step(self, amt = 1):
amt = 1 #anything other than 1 would be just plain silly
if self._step > (self._color_count * 2) - 1:
self._step = 0
if self._step % 2 == 0:
self._led.fill(self._colors[self._step / 2], self._start, self._end)
else:
self._led.all_off()
self._step += amt
class FireFlies(BaseStripAnim):
"""Stobe Light Effect."""
def __init__(self, led, colors, width = 1, count = 1, start=0, end=-1):
super(FireFlies, self).__init__(led, start, end)
self._colors = colors
self._color_count = len(colors)
self._width = width
self._count = count
def step(self, amt = 1):
amt = 1 #anything other than 1 would be just plain silly
if self._step > self._led.numLEDs:
self._step = 0
self._led.all_off();
for i in range(self._count):
pixel = random.randint(0, self._led.numLEDs - 1)
color = self._colors[random.randint(0, self._color_count - 1)]
for i in range(self._width):
if pixel + i < self._led.numLEDs:
self._led.set(pixel + i, color)
self._step += amt
class LarsonScanner(BaseStripAnim):
"""Larson scanner (i.e. Cylon Eye or K.I.T.T.)."""
def __init__(self, led, color, tail=2, start=0, end=-1):
super(LarsonScanner, self).__init__(led, start, end)
self._color = color
self._tail = tail + 1 # makes tail math later easier
if self._tail >= self._size / 2:
self._tail = (self._size / 2) - 1
self._direction = -1
self._last = 0
self._fadeAmt = 256 / self._tail
def step(self, amt = 1):
self._led.all_off()
self._last = self._start + self._step
self._led.set(self._last, self._color)
for i in range(self._tail):
self._led.set(self._last - i, colors.color_scale(self._color, 255 - (self._fadeAmt * i)))
self._led.set(self._last + i, colors.color_scale(self._color, 255 - (self._fadeAmt * i)))
if self._start + self._step >= self._end:
self._direction = -self._direction
elif self._step <= 0:
self._direction = -self._direction
self._step += self._direction * amt
class LarsonRainbow(LarsonScanner):
"""Larson scanner (i.e. Cylon Eye or K.I.T.T.) but Rainbow."""
def __init__(self, led, tail=2, start=0, end=-1):
super(LarsonRainbow, self).__init__(
led, colors.Off, tail, start, end)
def step(self, amt = 1):
self._color = colors.hue_helper(0, self._size, self._step)
#self._color = colors.hue2rgb_rainbow((self._step * (256 / self._size)) % 256)
super(LarsonRainbow, self).step(amt)
class Wave(BaseStripAnim):
"""Sine wave animation."""
def __init__(self, led, color, cycles, start=0, end=-1):
super(Wave, self).__init__(led, start, end)
self._color = color
self._cycles = cycles
def step(self, amt = 1):
for i in range(self._size):
y = math.sin(
math.pi *
float(self._cycles) *
float(self._step * i) /
float(self._size))
if y >= 0.0:
# Peaks of sine wave are white
y = 1.0 - y # Translate Y to 0.0 (top) to 1.0 (center)
r, g, b = self._color
c2 = (int(255 - float(255 - r) * y), int(255 - float(255 - g) * y), int(255 - float(255 - b) * y))
else:
# Troughs of sine wave are black
y += 1.0 # Translate Y to 0.0 (bottom) to 1.0 (center)
r, g, b = self._color
c2 = (int(float(r) * y),
int(float(g) * y),
int(float(b) * y))
self._led.set(self._start + i, c2)
self._step += amt
class WaveMove(BaseStripAnim):
"""Sine wave animation."""
def __init__(self, led, color, cycles, start=0, end=-1):
super(WaveMove, self).__init__(led, start, end)
self._color = color
self._cycles = cycles
self._moveStep = 0
def step(self, amt = 1):
for i in range(self._size):
y = math.sin(
(math.pi *
float(self._cycles) *
float(i) /
float(self._size))
+ self._moveStep)
if y >= 0.0:
# Peaks of sine wave are white
y = 1.0 - y # Translate Y to 0.0 (top) to 1.0 (center)
r, g, b = self._color
c2 = (int(255 - float(255 - r) * y), int(255 - float(255 - g) * y), int(255 - float(255 - b) * y))
else:
# Troughs of sine wave are black
y += 1.0 # Translate Y to 0.0 (bottom) to 1.0 (center)
r, g, b = self._color
c2 = (int(float(r) * y),
int(float(g) * y),
int(float(b) * y))
self._led.set(self._start + i, c2)
self._step += amt
self._moveStep += 1
if(self._moveStep >= self._size):
self._moveStep = 0
import time
class RGBClock(BaseStripAnim):
"""RGB Clock done with RGB LED strip(s)"""
def __init__(self, led, hStart, hEnd, mStart, mEnd, sStart, sEnd):
super(RGBClock, self).__init__(led, 0, -1)
if hEnd < hStart:
hEnd = hStart + 1
if mEnd < mStart:
mEnd = mStart + 1
if sEnd < sStart:
sEnd = sStart + 1
self._hStart = hStart
self._hEnd = hEnd
self._mStart = mStart
self._mEnd = mEnd
self._sStart = sStart
self._sEnd = sEnd
def step(self, amt = 1):
t = time.localtime()
r, g, b = colors.hue2rgb_rainbow(t.tm_hour * (256/24))
self._led.fillRGB(r,g,b,self._hStart,self._hEnd)
r, g, b = colors.hue2rgb_rainbow(t.tm_min * (256/60))
self._led.fillRGB(r,g,b,self._mStart,self._mEnd)
r, g, b = colors.hue2rgb_rainbow(t.tm_sec * (256/60))
self._led.fillRGB(r,g,b,self._sStart,self._sEnd)
self._step = 0
class PingPong(BaseStripAnim):
def __init__(self, led, duration=100, start=0, end=-1):
super(PingPong, self).__init__(led, start, end)
self.duration = duration
self.reverse = False
self.goal_size = self._led.numLEDs / 8
self.blue_goal = [int(i) for i in range(0, self.goal_size)]
self.blue_min = int(self.goal_size * (.6))
self.red_goal = [int(i) for i in range(self._led.numLEDs - self.goal_size, self._led.numLEDs)]
self.red_min = int(self._led.numLEDs - (self.goal_size * (.6)))
self.last_idx = self.blue_min
self.speed = 0.05
def step(self, amt=1):
for i in self.blue_goal:
self._led.set(i, colors.Blue)
for i in self.red_goal:
self._led.set(i, colors.Red)
if self.reverse:
i = self.last_idx - amt
else:
i = self.last_idx + amt
if i in self.red_goal:
color = colors.Red
elif i in self.blue_goal:
color = colors.Blue
else:
color = colors.White
self._led.set(i, color)
if i not in self.blue_goal and i not in self.red_goal:
self._led.setOff(self.last_idx)
if i == self.blue_min or i == self.red_min:
self.reverse = not self.reverse
self.last_idx = i
time.sleep(self.speed)
class BinarySearch(BaseStripAnim):
def __init__(self, led):
super(BinarySearch, self).__init__(led, 0, -1)
self.color = colors.Blue
self.sleeptime = 0.1
def random_color(self):
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
return (r,g,b)
def split(self, minled, maxled):
# Find the middle, light that sheite up
diff = (maxled - minled)
# Unless they're next to each other
if diff <= 1:
self._led.set(maxled, self.color)
self._led.set(minled, self.color)
self._led.update()
return
center = int(minled + (diff/2))
self._led.set(center, self.color)
self._led.update()
# Otherwise split again on both our halves
time.sleep(self.sleeptime)
self.split(minled, center)
self.split(center, maxled)
def step(self, amt=1):
self.color = self.random_color()
self.split(0, self._led.numLEDs-1)
time.sleep(self.sleeptime)
self._led.all_off()
def rgb2hsv(rgb):
"""
Compute Hue, Saturation, Value for a given RGB tuple.
Based on these formulas: http://www.rapidtables.com/convert/color/rgb-to-hsv.htm
:param rgb: red, green, blue tuple
:return: hue, saturation, value tuple
"""
red = rgb[0]/255.0
green = rgb[1]/255.0
blue = rgb[2]/255.0
color_max = max(red, green, blue)
color_min = min(red, green, blue)
delta = color_max - color_min
if color_max == 0:
saturation = 0
else:
saturation = delta / color_max
# hue calculation
if delta == 0:
hue = 0
elif red == color_max:
hue = (green - blue) / delta
elif green == color_max:
hue = 2.0 + (blue - red) / delta
elif blue == color_max:
hue = 4.0 + (red - green) / delta
hue = 60.0 * hue
value = color_max
return tuple(int(round(v)) for v in (hue, saturation, value))
class BubbleSort(BaseStripAnim):
"""
Generate a random array of colors, sort them into a rainbow.
"""
SORTING = 0
FADING = 1
INITING = 2
SLEEP_SORT = 0.05
SLEEP_INIT = 0.1
SLEEP_FADE = 0.001
def __init__(self, led, start=0, end=-1):
super(BubbleSort, self).__init__(led, start, end)
self.init()
self.state = self.INITING
self.fade_step = 0
self.init_step = 0
def init(self):
self.items = [colors.hue_helper(x, self._size, 0) for x in range(self._size)]
random.shuffle(self.items)
def step(self, amt = 1):
for i, item in enumerate(self.items):
self._led.set(i, item)
if self.state == self.INITING:
if self.init_step == 0:
# let the blank linger
time.sleep(1)
# print('init')
if self.init_step > 10:
self.state = self.SORTING
self.init_step = 0
else:
self.init_step += 1
time.sleep(self.SLEEP_INIT)
elif self.state == self.SORTING:
# print('sorting')
last = self.items[0]
swaps = False
for i, item in enumerate(self.items):
hue_item = rgb2hsv(item)[0]
hue_last = rgb2hsv(last)[0]
if hue_item < hue_last:
swaps = True
self.items[i-1] = item
self._led.set(i-1, item)
self.items[i] = last
self._led.set(i, last)
time.sleep(self.SLEEP_SORT)
break
last = self.items[i]
if not swaps:
self.state = self.FADING
else:
# print('fading')
self.fade_step += 1
if self.fade_step > 200:
self._led.all_off()
# print('done')
self.init()
self.state = self.INITING
self.fade_step = 0
else:
for i, item in enumerate(self.items):
self._led.set(i, colors.color_scale(item, 255 - (self.fade_step)))
time.sleep(self.SLEEP_FADE)