48
48
49
49
from . import NANOS_PER_SECOND
50
50
from .color import BLACK , RAINBOW
51
+
51
52
try :
52
53
from time import monotonic_ns
53
54
except ImportError :
@@ -68,6 +69,7 @@ class Animation:
68
69
"""
69
70
Base class for animations.
70
71
"""
72
+
71
73
# pylint: disable=too-many-arguments
72
74
def __init__ (self , pixel_object , speed , color , peers = None , paused = False ):
73
75
self .pixel_object = pixel_object
@@ -152,7 +154,7 @@ def color(self, color):
152
154
if self ._color == color :
153
155
return
154
156
if isinstance (color , int ):
155
- color = (color >> 16 & 0xff , color >> 8 & 0xff , color & 0xff )
157
+ color = (color >> 16 & 0xFF , color >> 8 & 0xFF , color & 0xFF )
156
158
self ._color = color
157
159
self ._recompute_color (color )
158
160
@@ -183,6 +185,7 @@ class ColorCycle(Animation):
183
185
:param colors: A list of colors to cycle through in ``(r, g, b)`` tuple, or ``0x000000`` hex
184
186
format. Defaults to a rainbow color cycle.
185
187
"""
188
+
186
189
def __init__ (self , pixel_object , speed , colors = RAINBOW ):
187
190
self .colors = colors
188
191
super (ColorCycle , self ).__init__ (pixel_object , speed , colors [0 ])
@@ -209,6 +212,7 @@ class Blink(ColorCycle):
209
212
:param int speed: Animation speed in seconds, e.g. ``0.1``.
210
213
:param color: Animation color in ``(r, g, b)`` tuple, or ``0x000000`` hex format.
211
214
"""
215
+
212
216
def __init__ (self , pixel_object , speed , color ):
213
217
super (Blink , self ).__init__ (pixel_object , speed , [color , BLACK ])
214
218
@@ -223,6 +227,7 @@ class Solid(ColorCycle):
223
227
:param pixel_object: The initialised LED object.
224
228
:param color: Animation color in ``(r, g, b)`` tuple, or ``0x000000`` hex format.
225
229
"""
230
+
226
231
def __init__ (self , pixel_object , color ):
227
232
super (Solid , self ).__init__ (pixel_object , speed = 1 , colors = [color ])
228
233
@@ -243,8 +248,11 @@ class Comet(Animation):
243
248
:param bool reverse: Animates the comet in the reverse order. Defaults to ``False``.
244
249
:param bool bounce: Comet will bounce back and forth. Defaults to ``True``.
245
250
"""
251
+
246
252
# pylint: disable=too-many-arguments
247
- def __init__ (self , pixel_object , speed , color , tail_length = 10 , reverse = False , bounce = False ):
253
+ def __init__ (
254
+ self , pixel_object , speed , color , tail_length = 10 , reverse = False , bounce = False
255
+ ):
248
256
self ._tail_length = tail_length + 1
249
257
self ._color_step = 0.9 / tail_length
250
258
self ._color_offset = 0.1
@@ -260,9 +268,11 @@ def __init__(self, pixel_object, speed, color, tail_length=10, reverse=False, bo
260
268
261
269
def _recompute_color (self , color ):
262
270
self ._comet_colors = [BLACK ] + [
263
- [int (color [rgb ] * ((n * self ._color_step ) + self ._color_offset ))
264
- for rgb in range (len (color ))
265
- ] for n in range (self ._tail_length - 1 )
271
+ [
272
+ int (color [rgb ] * ((n * self ._color_step ) + self ._color_offset ))
273
+ for rgb in range (len (color ))
274
+ ]
275
+ for n in range (self ._tail_length - 1 )
266
276
]
267
277
self ._reverse_comet_colors = list (reversed (self ._comet_colors ))
268
278
@@ -283,9 +293,11 @@ def _comet_generator(self):
283
293
end = num_pixels - start
284
294
if start <= 0 :
285
295
num_visible = self ._tail_length + start
286
- self .pixel_object [0 :num_visible ] = colors [self ._tail_length - num_visible :]
296
+ self .pixel_object [0 :num_visible ] = colors [
297
+ self ._tail_length - num_visible :
298
+ ]
287
299
else :
288
- self .pixel_object [start : start + end ] = colors [0 :end ]
300
+ self .pixel_object [start : start + end ] = colors [0 :end ]
289
301
self .show ()
290
302
yield
291
303
if self .bounce :
@@ -303,6 +315,7 @@ class Sparkle(Animation):
303
315
:param int speed: Animation speed in seconds, e.g. ``0.1``.
304
316
:param color: Animation color in ``(r, g, b)`` tuple, or ``0x000000`` hex format.
305
317
"""
318
+
306
319
def __init__ (self , pixel_object , speed , color ):
307
320
if len (pixel_object ) < 2 :
308
321
raise ValueError ("Sparkle needs at least 2 pixels" )
@@ -343,7 +356,9 @@ class Pulse(Animation):
343
356
"""
344
357
345
358
# pylint: disable=too-many-arguments
346
- def __init__ (self , pixel_object , speed , color , period = 5 , max_intensity = 1 , min_intensity = 0 ):
359
+ def __init__ (
360
+ self , pixel_object , speed , color , period = 5 , max_intensity = 1 , min_intensity = 0
361
+ ):
347
362
self .max_intensity = max_intensity
348
363
self .min_intensity = min_intensity
349
364
self ._period = period
@@ -359,10 +374,14 @@ def draw(self):
359
374
now = monotonic_ns ()
360
375
time_since_last_draw = (now - self ._last_update ) / NANOS_PER_SECOND
361
376
self ._last_update = now
362
- pos = self ._cycle_position = (self ._cycle_position + time_since_last_draw ) % self ._period
377
+ pos = self ._cycle_position = (
378
+ self ._cycle_position + time_since_last_draw
379
+ ) % self ._period
363
380
if pos > self ._half_period :
364
381
pos = self ._period - pos
365
- intensity = self .min_intensity + (pos * self ._intensity_delta * self ._position_factor )
382
+ intensity = self .min_intensity + (
383
+ pos * self ._intensity_delta * self ._position_factor
384
+ )
366
385
color = [int (self .color [n ] * intensity ) for n in range (self ._bpp )]
367
386
self .fill (color )
368
387
self .show ()
@@ -408,8 +427,10 @@ def draw(self):
408
427
self .pixel_object .fill ((0 , 0 , 0 ))
409
428
for i in range (self ._size ):
410
429
n = (self ._n + i ) % self ._repeat_width
411
- num = len (self .pixel_object [n ::self ._repeat_width ])
412
- self .pixel_object [n ::self ._repeat_width ] = [self .group_color (n ) for n in range (num )]
430
+ num = len (self .pixel_object [n :: self ._repeat_width ])
431
+ self .pixel_object [n :: self ._repeat_width ] = [
432
+ self .group_color (n ) for n in range (num )
433
+ ]
413
434
self ._n = (self ._n + self ._direction ) % self ._repeat_width
414
435
self .show ()
415
436
@@ -449,9 +470,12 @@ class AnimationSequence:
449
470
while True:
450
471
animations.animate()
451
472
"""
473
+
452
474
def __init__ (self , * members , advance_interval = None , auto_clear = False ):
453
475
self ._members = members
454
- self ._advance_interval = advance_interval * NANOS_PER_SECOND if advance_interval else None
476
+ self ._advance_interval = (
477
+ advance_interval * NANOS_PER_SECOND if advance_interval else None
478
+ )
455
479
self ._last_advance = monotonic_ns ()
456
480
self ._current = 0
457
481
self ._auto_clear = auto_clear
@@ -545,6 +569,7 @@ class AnimationGroup:
545
569
first member of the group. Defaults to ``False``.
546
570
547
571
"""
572
+
548
573
def __init__ (self , * members , sync = False ):
549
574
self ._members = members
550
575
self ._sync = sync
@@ -584,16 +609,16 @@ def fill(self, color):
584
609
"""
585
610
Fills all pixel objects in the group with a color.
586
611
"""
587
- self ._for_all (' fill' , color )
612
+ self ._for_all (" fill" , color )
588
613
589
614
def freeze (self ):
590
615
"""
591
616
Freeze all animations in the group.
592
617
"""
593
- self ._for_all (' freeze' )
618
+ self ._for_all (" freeze" )
594
619
595
620
def resume (self ):
596
621
"""
597
622
Resume all animations in the group.
598
623
"""
599
- self ._for_all (' resume' )
624
+ self ._for_all (" resume" )
0 commit comments