43
43
44
44
"""
45
45
46
+ import random
47
+ from math import ceil , sin , radians
48
+
49
+ from . import NANOS_PER_SECOND
50
+ from .color import BLACK , RAINBOW
46
51
try :
47
52
from time import monotonic_ns
48
53
except ImportError :
@@ -52,11 +57,8 @@ def monotonic_ns():
52
57
"""
53
58
Implementation of monotonic_ns for platforms without time.monotonic_ns
54
59
"""
55
- return int (time .time () * 1000000000 )
60
+ return int (time .time () * NANOS_PER_SECOND )
56
61
57
- import random
58
- from math import ceil , sin , radians
59
- from .color import BLACK , RAINBOW
60
62
61
63
__version__ = "0.0.0-auto.0"
62
64
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation.git"
@@ -69,15 +71,15 @@ class Animation:
69
71
# pylint: disable=too-many-arguments
70
72
def __init__ (self , pixel_object , speed , color , peers = None , paused = False ):
71
73
self .pixel_object = pixel_object
72
- self ._speed_ns = 0
73
- self .speed = speed # sets _speed_ns
74
- self ._color = color
75
- self ._next_update = monotonic_ns ()
76
74
self .pixel_object .auto_write = False
77
- self .color = color
78
75
self .peers = peers if peers else []
76
+ self ._speed_ns = 0
77
+ self ._color = None
79
78
self ._paused = paused
79
+ self ._next_update = monotonic_ns ()
80
80
self ._time_left_at_pause = 0
81
+ self .speed = speed # sets _speed_ns
82
+ self .color = color # Triggers _recompute_color
81
83
82
84
def animate (self ):
83
85
"""
@@ -147,6 +149,8 @@ def color(self):
147
149
148
150
@color .setter
149
151
def color (self , color ):
152
+ if self ._color == color :
153
+ return
150
154
if isinstance (color , int ):
151
155
color = (color >> 16 & 0xff , color >> 8 & 0xff , color & 0xff )
152
156
self ._color = color
@@ -157,13 +161,17 @@ def speed(self):
157
161
"""
158
162
The animation speed in fractional seconds.
159
163
"""
160
- return self ._speed_ns / 1000000000
164
+ return self ._speed_ns / NANOS_PER_SECOND
161
165
162
166
@speed .setter
163
167
def speed (self , seconds ):
164
- self ._speed_ns = int (seconds * 1000000000 )
168
+ self ._speed_ns = int (seconds * NANOS_PER_SECOND )
165
169
166
170
def _recompute_color (self , color ):
171
+ """
172
+ Called if the color is changed, which includes at initialization.
173
+ Override as needed.
174
+ """
167
175
pass
168
176
169
177
@@ -245,7 +253,10 @@ def __init__(self, pixel_object, speed, color, tail_length=10, reverse=False, bo
245
253
self ._reverse_comet_colors = None
246
254
self .reverse = reverse
247
255
self .bounce = bounce
256
+ # Super is called late because it needs ._color to be initialized.
248
257
super (Comet , self ).__init__ (pixel_object , speed , color )
258
+ # _recompute_color needs calling before creating the generator, so setup the generator
259
+ # afterwards
249
260
self ._generator = self ._comet_generator ()
250
261
251
262
def _recompute_color (self , color ):
@@ -294,6 +305,8 @@ class Sparkle(Animation):
294
305
:param color: Animation color in ``(r, g, b)`` tuple, or ``0x000000`` hex format.
295
306
"""
296
307
def __init__ (self , pixel_object , speed , color ):
308
+ if len (pixel_object ) < 2 :
309
+ raise ValueError ("Sparkle needs at least 2 pixels" )
297
310
self ._half_color = None
298
311
self ._dim_color = None
299
312
super (Sparkle , self ).__init__ (pixel_object , speed , color )
@@ -344,7 +357,7 @@ def __init__(self, pixel_object, speed, color, period=5, max_intensity=1, min_in
344
357
345
358
def draw (self ):
346
359
now = monotonic_ns ()
347
- time_since_last_draw = (now - self ._last_update ) / 1000000000
360
+ time_since_last_draw = (now - self ._last_update ) / NANOS_PER_SECOND
348
361
self ._last_update = now
349
362
self ._cycle_position = (self ._cycle_position + time_since_last_draw ) % self ._period
350
363
intensity = self .min_intensity + (
@@ -438,7 +451,7 @@ class AnimationSequence:
438
451
"""
439
452
def __init__ (self , * members , advance_interval = None , auto_clear = False ):
440
453
self ._members = members
441
- self ._advance_interval = advance_interval * 1000000000 if advance_interval else None
454
+ self ._advance_interval = advance_interval * NANOS_PER_SECOND if advance_interval else None
442
455
self ._last_advance = monotonic_ns ()
443
456
self ._current = 0
444
457
self ._auto_clear = auto_clear
0 commit comments