Merge pull request #63 from rhooper/refactor-to-ms
use milliseconds instead to support smaller boards
This commit is contained in:
commit
2d229b66ef
5 changed files with 39 additions and 29 deletions
|
|
@ -35,15 +35,25 @@ except ImportError:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from time import monotonic_ns
|
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
|
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)
|
NANOS_PER_MS = const(1000000)
|
||||||
|
MS_PER_SECOND = const(1000)
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ Implementation Notes
|
||||||
__version__ = "0.0.0-auto.0"
|
__version__ = "0.0.0-auto.0"
|
||||||
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation.git"
|
__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:
|
class Animation:
|
||||||
|
|
@ -61,13 +61,13 @@ class Animation:
|
||||||
self.pixel_object = pixel_object
|
self.pixel_object = pixel_object
|
||||||
self.pixel_object.auto_write = False
|
self.pixel_object.auto_write = False
|
||||||
self._peers = [self] + peers if peers is not None else [self]
|
self._peers = [self] + peers if peers is not None else [self]
|
||||||
self._speed_ns = 0
|
self._speed_ms = 0
|
||||||
self._color = None
|
self._color = None
|
||||||
self._paused = paused
|
self._paused = paused
|
||||||
self._next_update = monotonic_ns()
|
self._next_update = monotonic_ms()
|
||||||
self._time_left_at_pause = 0
|
self._time_left_at_pause = 0
|
||||||
self._also_notify = []
|
self._also_notify = []
|
||||||
self.speed = speed # sets _speed_ns
|
self.speed = speed # sets _speed_ms
|
||||||
self.color = color # Triggers _set_color
|
self.color = color # Triggers _set_color
|
||||||
self.name = name
|
self.name = name
|
||||||
self.cycle_complete = False
|
self.cycle_complete = False
|
||||||
|
|
@ -93,7 +93,7 @@ class Animation:
|
||||||
if self._paused:
|
if self._paused:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
now = monotonic_ns()
|
now = monotonic_ms()
|
||||||
if now < self._next_update:
|
if now < self._next_update:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
@ -113,7 +113,7 @@ class Animation:
|
||||||
anim.cycle_complete = False
|
anim.cycle_complete = False
|
||||||
anim.on_cycle_complete()
|
anim.on_cycle_complete()
|
||||||
|
|
||||||
self._next_update = now + self._speed_ns
|
self._next_update = now + self._speed_ms
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def draw(self):
|
def draw(self):
|
||||||
|
|
@ -157,13 +157,13 @@ class Animation:
|
||||||
Stops the animation until resumed.
|
Stops the animation until resumed.
|
||||||
"""
|
"""
|
||||||
self._paused = True
|
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):
|
def resume(self):
|
||||||
"""
|
"""
|
||||||
Resumes the animation.
|
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._time_left_at_pause = 0
|
||||||
self._paused = False
|
self._paused = False
|
||||||
|
|
||||||
|
|
@ -201,11 +201,11 @@ class Animation:
|
||||||
"""
|
"""
|
||||||
The animation speed in fractional seconds.
|
The animation speed in fractional seconds.
|
||||||
"""
|
"""
|
||||||
return self._speed_ns / NANOS_PER_SECOND
|
return self._speed_ms / MS_PER_SECOND
|
||||||
|
|
||||||
@speed.setter
|
@speed.setter
|
||||||
def speed(self, seconds):
|
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):
|
def on_cycle_complete(self):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ Implementation Notes
|
||||||
|
|
||||||
from adafruit_led_animation.animation import Animation
|
from adafruit_led_animation.animation import Animation
|
||||||
from adafruit_led_animation.color import BLACK, colorwheel
|
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"
|
__version__ = "0.0.0-auto.0"
|
||||||
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation.git"
|
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation.git"
|
||||||
|
|
@ -88,15 +88,15 @@ class Rainbow(Animation):
|
||||||
on_cycle_complete_supported = True
|
on_cycle_complete_supported = True
|
||||||
|
|
||||||
def _color_wheel_generator(self):
|
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)
|
num_pixels = len(self.pixel_object)
|
||||||
last_update = monotonic_ns()
|
last_update = monotonic_ms()
|
||||||
cycle_position = 0
|
cycle_position = 0
|
||||||
last_pos = 0
|
last_pos = 0
|
||||||
while True:
|
while True:
|
||||||
cycle_completed = False
|
cycle_completed = False
|
||||||
now = monotonic_ns()
|
now = monotonic_ms()
|
||||||
time_since_last_draw = now - last_update
|
time_since_last_draw = now - last_update
|
||||||
last_update = now
|
last_update = now
|
||||||
pos = cycle_position = (cycle_position + time_since_last_draw) % period
|
pos = cycle_position = (cycle_position + time_since_last_draw) % period
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ Implementation Notes
|
||||||
|
|
||||||
import math
|
import math
|
||||||
|
|
||||||
from . import NANOS_PER_SECOND, monotonic_ns
|
from . import MS_PER_SECOND, monotonic_ms
|
||||||
from .color import calculate_intensity
|
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 animation_object: An animation object to interact with.
|
||||||
:param dotstar_pwm: Whether to use the dostar per pixel PWM value for brightness control.
|
: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
|
half_period = period // 2
|
||||||
|
|
||||||
last_update = monotonic_ns()
|
last_update = monotonic_ms()
|
||||||
cycle_position = 0
|
cycle_position = 0
|
||||||
last_pos = 0
|
last_pos = 0
|
||||||
while True:
|
while True:
|
||||||
now = monotonic_ns()
|
now = monotonic_ms()
|
||||||
time_since_last_draw = now - last_update
|
time_since_last_draw = now - last_update
|
||||||
last_update = now
|
last_update = now
|
||||||
pos = cycle_position = (cycle_position + time_since_last_draw) % period
|
pos = cycle_position = (cycle_position + time_since_last_draw) % period
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ Implementation Notes
|
||||||
|
|
||||||
import random
|
import random
|
||||||
from adafruit_led_animation.color import BLACK
|
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"
|
__version__ = "0.0.0-auto.0"
|
||||||
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation.git"
|
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation.git"
|
||||||
|
|
@ -108,9 +108,9 @@ class AnimationSequence:
|
||||||
)
|
)
|
||||||
self._members = members
|
self._members = members
|
||||||
self._advance_interval = (
|
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._current = 0
|
||||||
self.auto_clear = auto_clear
|
self.auto_clear = auto_clear
|
||||||
self.auto_reset = auto_reset
|
self.auto_reset = auto_reset
|
||||||
|
|
@ -162,7 +162,7 @@ class AnimationSequence:
|
||||||
def _auto_advance(self):
|
def _auto_advance(self):
|
||||||
if not self._advance_interval:
|
if not self._advance_interval:
|
||||||
return
|
return
|
||||||
now = monotonic_ns()
|
now = monotonic_ms()
|
||||||
if now - self._last_advance > self._advance_interval:
|
if now - self._last_advance > self._advance_interval:
|
||||||
self._last_advance = now
|
self._last_advance = now
|
||||||
self._advance()
|
self._advance()
|
||||||
|
|
@ -247,7 +247,7 @@ class AnimationSequence:
|
||||||
if self._paused:
|
if self._paused:
|
||||||
return
|
return
|
||||||
self._paused = True
|
self._paused = True
|
||||||
self._paused_at = monotonic_ns()
|
self._paused_at = monotonic_ms()
|
||||||
self.current_animation.freeze()
|
self.current_animation.freeze()
|
||||||
|
|
||||||
def resume(self):
|
def resume(self):
|
||||||
|
|
@ -257,7 +257,7 @@ class AnimationSequence:
|
||||||
if not self._paused:
|
if not self._paused:
|
||||||
return
|
return
|
||||||
self._paused = False
|
self._paused = False
|
||||||
now = monotonic_ns()
|
now = monotonic_ms()
|
||||||
self._last_advance += now - self._paused_at
|
self._last_advance += now - self._paused_at
|
||||||
self._paused_at = 0
|
self._paused_at = 0
|
||||||
self.current_animation.resume()
|
self.current_animation.resume()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue