Skip to content

Commit 0d92928

Browse files
committed
address some code review concerns
1 parent f6da086 commit 0d92928

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

adafruit_led_animation/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""
2+
Adafruit LED Animation library.
3+
"""
4+
5+
NANOS_PER_SECOND = 1000000000

adafruit_led_animation/animation.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
4444
"""
4545

46+
import random
47+
from math import ceil, sin, radians
48+
49+
from . import NANOS_PER_SECOND
50+
from .color import BLACK, RAINBOW
4651
try:
4752
from time import monotonic_ns
4853
except ImportError:
@@ -52,11 +57,8 @@ def monotonic_ns():
5257
"""
5358
Implementation of monotonic_ns for platforms without time.monotonic_ns
5459
"""
55-
return int(time.time() * 1000000000)
60+
return int(time.time() * NANOS_PER_SECOND)
5661

57-
import random
58-
from math import ceil, sin, radians
59-
from .color import BLACK, RAINBOW
6062

6163
__version__ = "0.0.0-auto.0"
6264
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation.git"
@@ -69,15 +71,15 @@ class Animation:
6971
# pylint: disable=too-many-arguments
7072
def __init__(self, pixel_object, speed, color, peers=None, paused=False):
7173
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()
7674
self.pixel_object.auto_write = False
77-
self.color = color
7875
self.peers = peers if peers else []
76+
self._speed_ns = 0
77+
self._color = None
7978
self._paused = paused
79+
self._next_update = monotonic_ns()
8080
self._time_left_at_pause = 0
81+
self.speed = speed # sets _speed_ns
82+
self.color = color # Triggers _recompute_color
8183

8284
def animate(self):
8385
"""
@@ -147,6 +149,8 @@ def color(self):
147149

148150
@color.setter
149151
def color(self, color):
152+
if self._color == color:
153+
return
150154
if isinstance(color, int):
151155
color = (color >> 16 & 0xff, color >> 8 & 0xff, color & 0xff)
152156
self._color = color
@@ -157,13 +161,17 @@ def speed(self):
157161
"""
158162
The animation speed in fractional seconds.
159163
"""
160-
return self._speed_ns / 1000000000
164+
return self._speed_ns / NANOS_PER_SECOND
161165

162166
@speed.setter
163167
def speed(self, seconds):
164-
self._speed_ns = int(seconds * 1000000000)
168+
self._speed_ns = int(seconds * NANOS_PER_SECOND)
165169

166170
def _recompute_color(self, color):
171+
"""
172+
Called if the color is changed, which includes at initialization.
173+
Override as needed.
174+
"""
167175
pass
168176

169177

@@ -245,7 +253,10 @@ def __init__(self, pixel_object, speed, color, tail_length=10, reverse=False, bo
245253
self._reverse_comet_colors = None
246254
self.reverse = reverse
247255
self.bounce = bounce
256+
# Super is called late because it needs ._color to be initialized.
248257
super(Comet, self).__init__(pixel_object, speed, color)
258+
# _recompute_color needs calling before creating the generator, so setup the generator
259+
# afterwards
249260
self._generator = self._comet_generator()
250261

251262
def _recompute_color(self, color):
@@ -294,6 +305,8 @@ class Sparkle(Animation):
294305
:param color: Animation color in ``(r, g, b)`` tuple, or ``0x000000`` hex format.
295306
"""
296307
def __init__(self, pixel_object, speed, color):
308+
if len(pixel_object) < 2:
309+
raise ValueError("Sparkle needs at least 2 pixels")
297310
self._half_color = None
298311
self._dim_color = None
299312
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
344357

345358
def draw(self):
346359
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
348361
self._last_update = now
349362
self._cycle_position = (self._cycle_position + time_since_last_draw) % self._period
350363
intensity = self.min_intensity + (
@@ -438,7 +451,7 @@ class AnimationSequence:
438451
"""
439452
def __init__(self, *members, advance_interval=None, auto_clear=False):
440453
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
442455
self._last_advance = monotonic_ns()
443456
self._current = 0
444457
self._auto_clear = auto_clear

0 commit comments

Comments
 (0)