From ea7d2e1cdca1e978107b625ae824bafe005238c0 Mon Sep 17 00:00:00 2001 From: Roy Hooper Date: Wed, 12 Aug 2020 10:26:18 -0400 Subject: [PATCH 1/4] use milliseconds instead to support smaller boards --- adafruit_led_animation/__init__.py | 11 ++++++++--- adafruit_led_animation/animation/__init__.py | 14 +++++++------- adafruit_led_animation/animation/rainbow.py | 8 ++++---- adafruit_led_animation/helper.py | 8 ++++---- adafruit_led_animation/sequence.py | 12 ++++++------ 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/adafruit_led_animation/__init__.py b/adafruit_led_animation/__init__.py index fbe8130..db2f419 100644 --- a/adafruit_led_animation/__init__.py +++ b/adafruit_led_animation/__init__.py @@ -35,15 +35,20 @@ def const(value): # pylint: disable=missing-docstring try: from time import monotonic_ns + + def monotonic_ms(): + return monotonic_ns() // NANOS_PER_MS + + except ImportError: import time - def monotonic_ns(): + def monotonic_ms(): """ Implementation of monotonic_ns for platforms without time.monotonic_ns """ - return int(time.time() * NANOS_PER_SECOND) + return int(time.monotonic() * MS_PER_SECOND) -NANOS_PER_SECOND = const(1000000000) NANOS_PER_MS = const(1000000) +MS_PER_SECOND = const(1000) diff --git a/adafruit_led_animation/animation/__init__.py b/adafruit_led_animation/animation/__init__.py index 12a22ba..231e877 100644 --- a/adafruit_led_animation/animation/__init__.py +++ b/adafruit_led_animation/animation/__init__.py @@ -46,7 +46,7 @@ __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation.git" -from adafruit_led_animation import NANOS_PER_SECOND, monotonic_ns +from adafruit_led_animation import MS_PER_SECOND, monotonic_ms class Animation: @@ -64,7 +64,7 @@ def __init__(self, pixel_object, speed, color, peers=None, paused=False, name=No self._speed_ns = 0 self._color = None self._paused = paused - self._next_update = monotonic_ns() + self._next_update = monotonic_ms() self._time_left_at_pause = 0 self._also_notify = [] self.speed = speed # sets _speed_ns @@ -93,7 +93,7 @@ def animate(self, show=True): if self._paused: return False - now = monotonic_ns() + now = monotonic_ms() if now < self._next_update: return False @@ -157,13 +157,13 @@ def freeze(self): Stops the animation until resumed. """ self._paused = True - self._time_left_at_pause = max(0, monotonic_ns() - self._next_update) + self._time_left_at_pause = max(0, monotonic_ms() - self._next_update) def resume(self): """ Resumes the animation. """ - self._next_update = monotonic_ns() + self._time_left_at_pause + self._next_update = monotonic_ms() + self._time_left_at_pause self._time_left_at_pause = 0 self._paused = False @@ -201,11 +201,11 @@ def speed(self): """ The animation speed in fractional seconds. """ - return self._speed_ns / NANOS_PER_SECOND + return self._speed_ns / MS_PER_SECOND @speed.setter def speed(self, seconds): - self._speed_ns = int(seconds * NANOS_PER_SECOND) + self._speed_ns = int(seconds * MS_PER_SECOND) def on_cycle_complete(self): """ diff --git a/adafruit_led_animation/animation/rainbow.py b/adafruit_led_animation/animation/rainbow.py index ef83a4c..0130708 100644 --- a/adafruit_led_animation/animation/rainbow.py +++ b/adafruit_led_animation/animation/rainbow.py @@ -45,7 +45,7 @@ from adafruit_led_animation.animation import Animation from adafruit_led_animation.color import BLACK, colorwheel -from adafruit_led_animation import NANOS_PER_SECOND, monotonic_ns +from adafruit_led_animation import MS_PER_SECOND, monotonic_ms __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation.git" @@ -88,15 +88,15 @@ def generate_rainbow(self): on_cycle_complete_supported = True def _color_wheel_generator(self): - period = int(self._period * NANOS_PER_SECOND) + period = int(self._period * MS_PER_SECOND) num_pixels = len(self.pixel_object) - last_update = monotonic_ns() + last_update = monotonic_ms() cycle_position = 0 last_pos = 0 while True: cycle_completed = False - now = monotonic_ns() + now = monotonic_ms() time_since_last_draw = now - last_update last_update = now pos = cycle_position = (cycle_position + time_since_last_draw) % period diff --git a/adafruit_led_animation/helper.py b/adafruit_led_animation/helper.py index 40ea32d..fea729e 100644 --- a/adafruit_led_animation/helper.py +++ b/adafruit_led_animation/helper.py @@ -44,7 +44,7 @@ import math -from . import NANOS_PER_SECOND, monotonic_ns +from . import MS_PER_SECOND, monotonic_ms from .color import calculate_intensity @@ -339,14 +339,14 @@ def pulse_generator(period: float, animation_object, dotstar_pwm=False): :param animation_object: An animation object to interact with. :param dotstar_pwm: Whether to use the dostar per pixel PWM value for brightness control. """ - period = int(period * NANOS_PER_SECOND) + period = int(period * MS_PER_SECOND) half_period = period // 2 - last_update = monotonic_ns() + last_update = monotonic_ms() cycle_position = 0 last_pos = 0 while True: - now = monotonic_ns() + now = monotonic_ms() time_since_last_draw = now - last_update last_update = now pos = cycle_position = (cycle_position + time_since_last_draw) % period diff --git a/adafruit_led_animation/sequence.py b/adafruit_led_animation/sequence.py index bdf8eff..eb9ad91 100644 --- a/adafruit_led_animation/sequence.py +++ b/adafruit_led_animation/sequence.py @@ -46,7 +46,7 @@ import random from adafruit_led_animation.color import BLACK -from . import NANOS_PER_SECOND, monotonic_ns +from . import MS_PER_SECOND, monotonic_ms __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation.git" @@ -108,9 +108,9 @@ def __init__( ) self._members = members self._advance_interval = ( - advance_interval * NANOS_PER_SECOND if advance_interval else None + advance_interval * MS_PER_SECOND if advance_interval else None ) - self._last_advance = monotonic_ns() + self._last_advance = monotonic_ms() self._current = 0 self.auto_clear = auto_clear self.auto_reset = auto_reset @@ -162,7 +162,7 @@ def add_cycle_complete_receiver(self, callback): def _auto_advance(self): if not self._advance_interval: return - now = monotonic_ns() + now = monotonic_ms() if now - self._last_advance > self._advance_interval: self._last_advance = now self._advance() @@ -247,7 +247,7 @@ def freeze(self): if self._paused: return self._paused = True - self._paused_at = monotonic_ns() + self._paused_at = monotonic_ms() self.current_animation.freeze() def resume(self): @@ -257,7 +257,7 @@ def resume(self): if not self._paused: return self._paused = False - now = monotonic_ns() + now = monotonic_ms() self._last_advance += now - self._paused_at self._paused_at = 0 self.current_animation.resume() From 5606528a37d8dfb87f74fbf92ed02078c621f8ef Mon Sep 17 00:00:00 2001 From: Roy Hooper Date: Wed, 12 Aug 2020 11:22:51 -0400 Subject: [PATCH 2/4] add docstring --- adafruit_led_animation/__init__.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/adafruit_led_animation/__init__.py b/adafruit_led_animation/__init__.py index db2f419..a29f852 100644 --- a/adafruit_led_animation/__init__.py +++ b/adafruit_led_animation/__init__.py @@ -37,6 +37,9 @@ def const(value): # pylint: disable=missing-docstring from time import monotonic_ns def monotonic_ms(): + """ + Return monotonic time in milliseconds. + """ return monotonic_ns() // NANOS_PER_MS @@ -45,7 +48,7 @@ def monotonic_ms(): def monotonic_ms(): """ - Implementation of monotonic_ns for platforms without time.monotonic_ns + Implementation of monotonic_ms for platforms without time.monotonic_ns """ return int(time.monotonic() * MS_PER_SECOND) From 78020595b0f73f4a90d279d6333deedc4d128fb6 Mon Sep 17 00:00:00 2001 From: Roy Hooper Date: Wed, 12 Aug 2020 12:21:51 -0400 Subject: [PATCH 3/4] fix references to _ns --- adafruit_led_animation/__init__.py | 2 +- adafruit_led_animation/animation/__init__.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/adafruit_led_animation/__init__.py b/adafruit_led_animation/__init__.py index a29f852..51915dc 100644 --- a/adafruit_led_animation/__init__.py +++ b/adafruit_led_animation/__init__.py @@ -34,7 +34,7 @@ def const(value): # pylint: disable=missing-docstring try: - from time import monotonic_ns + from time import monotonic_ms def monotonic_ms(): """ diff --git a/adafruit_led_animation/animation/__init__.py b/adafruit_led_animation/animation/__init__.py index 231e877..6dd53de 100644 --- a/adafruit_led_animation/animation/__init__.py +++ b/adafruit_led_animation/animation/__init__.py @@ -61,13 +61,13 @@ def __init__(self, pixel_object, speed, color, peers=None, paused=False, name=No self.pixel_object = pixel_object self.pixel_object.auto_write = False self._peers = [self] + peers if peers is not None else [self] - self._speed_ns = 0 + self._speed_ms = 0 self._color = None self._paused = paused self._next_update = monotonic_ms() self._time_left_at_pause = 0 self._also_notify = [] - self.speed = speed # sets _speed_ns + self.speed = speed # sets _speed_ms self.color = color # Triggers _set_color self.name = name self.cycle_complete = False @@ -113,7 +113,7 @@ def animate(self, show=True): anim.cycle_complete = False anim.on_cycle_complete() - self._next_update = now + self._speed_ns + self._next_update = now + self._speed_ms return True def draw(self): @@ -201,11 +201,11 @@ def speed(self): """ The animation speed in fractional seconds. """ - return self._speed_ns / MS_PER_SECOND + return self._speed_ms / MS_PER_SECOND @speed.setter def speed(self, seconds): - self._speed_ns = int(seconds * MS_PER_SECOND) + self._speed_ms = int(seconds * MS_PER_SECOND) def on_cycle_complete(self): """ From 7d94b3e48f807a5479612f0a54caea6ee2562b86 Mon Sep 17 00:00:00 2001 From: Roy Hooper Date: Wed, 12 Aug 2020 12:25:40 -0400 Subject: [PATCH 4/4] fix for 6.x - montonic_ns returns NotImplementedError --- adafruit_led_animation/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/adafruit_led_animation/__init__.py b/adafruit_led_animation/__init__.py index 51915dc..b27ba9a 100644 --- a/adafruit_led_animation/__init__.py +++ b/adafruit_led_animation/__init__.py @@ -34,7 +34,9 @@ def const(value): # pylint: disable=missing-docstring try: - from time import monotonic_ms + from time import monotonic_ns + + monotonic_ns() # Test monotonic_ns in 6.x def monotonic_ms(): """ @@ -43,7 +45,7 @@ def monotonic_ms(): return monotonic_ns() // NANOS_PER_MS -except ImportError: +except (ImportError, NotImplementedError): import time def monotonic_ms():