Skip to content

use milliseconds instead to support smaller boards #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions adafruit_led_animation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,25 @@ def const(value): # pylint: disable=missing-docstring

try:
from time import monotonic_ns
except ImportError:

monotonic_ns() # Test monotonic_ns in 6.x

def monotonic_ms():
"""
Return monotonic time in milliseconds.
"""
return monotonic_ns() // NANOS_PER_MS


except (ImportError, NotImplementedError):
import time

def monotonic_ns():
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.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)
20 changes: 10 additions & 10 deletions adafruit_led_animation/animation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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_ns()
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
Expand All @@ -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

Expand All @@ -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):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -201,11 +201,11 @@ def speed(self):
"""
The animation speed in fractional seconds.
"""
return self._speed_ns / NANOS_PER_SECOND
return self._speed_ms / MS_PER_SECOND

@speed.setter
def speed(self, seconds):
self._speed_ns = int(seconds * NANOS_PER_SECOND)
self._speed_ms = int(seconds * MS_PER_SECOND)

def on_cycle_complete(self):
"""
Expand Down
8 changes: 4 additions & 4 deletions adafruit_led_animation/animation/rainbow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions adafruit_led_animation/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions adafruit_led_animation/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand All @@ -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()
Expand Down