Skip to content

Commit 2d229b6

Browse files
authored
Merge pull request #63 from rhooper/refactor-to-ms
use milliseconds instead to support smaller boards
2 parents c18151f + 7d94b3e commit 2d229b6

File tree

5 files changed

+39
-29
lines changed

5 files changed

+39
-29
lines changed

adafruit_led_animation/__init__.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,25 @@ def const(value): # pylint: disable=missing-docstring
3535

3636
try:
3737
from time import monotonic_ns
38-
except ImportError:
38+
39+
monotonic_ns() # Test monotonic_ns in 6.x
40+
41+
def monotonic_ms():
42+
"""
43+
Return monotonic time in milliseconds.
44+
"""
45+
return monotonic_ns() // NANOS_PER_MS
46+
47+
48+
except (ImportError, NotImplementedError):
3949
import time
4050

41-
def monotonic_ns():
51+
def monotonic_ms():
4252
"""
43-
Implementation of monotonic_ns for platforms without time.monotonic_ns
53+
Implementation of monotonic_ms for platforms without time.monotonic_ns
4454
"""
45-
return int(time.time() * NANOS_PER_SECOND)
55+
return int(time.monotonic() * MS_PER_SECOND)
4656

4757

48-
NANOS_PER_SECOND = const(1000000000)
4958
NANOS_PER_MS = const(1000000)
59+
MS_PER_SECOND = const(1000)

adafruit_led_animation/animation/__init__.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
__version__ = "0.0.0-auto.0"
4747
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation.git"
4848

49-
from adafruit_led_animation import NANOS_PER_SECOND, monotonic_ns
49+
from adafruit_led_animation import MS_PER_SECOND, monotonic_ms
5050

5151

5252
class Animation:
@@ -61,13 +61,13 @@ def __init__(self, pixel_object, speed, color, peers=None, paused=False, name=No
6161
self.pixel_object = pixel_object
6262
self.pixel_object.auto_write = False
6363
self._peers = [self] + peers if peers is not None else [self]
64-
self._speed_ns = 0
64+
self._speed_ms = 0
6565
self._color = None
6666
self._paused = paused
67-
self._next_update = monotonic_ns()
67+
self._next_update = monotonic_ms()
6868
self._time_left_at_pause = 0
6969
self._also_notify = []
70-
self.speed = speed # sets _speed_ns
70+
self.speed = speed # sets _speed_ms
7171
self.color = color # Triggers _set_color
7272
self.name = name
7373
self.cycle_complete = False
@@ -93,7 +93,7 @@ def animate(self, show=True):
9393
if self._paused:
9494
return False
9595

96-
now = monotonic_ns()
96+
now = monotonic_ms()
9797
if now < self._next_update:
9898
return False
9999

@@ -113,7 +113,7 @@ def animate(self, show=True):
113113
anim.cycle_complete = False
114114
anim.on_cycle_complete()
115115

116-
self._next_update = now + self._speed_ns
116+
self._next_update = now + self._speed_ms
117117
return True
118118

119119
def draw(self):
@@ -157,13 +157,13 @@ def freeze(self):
157157
Stops the animation until resumed.
158158
"""
159159
self._paused = True
160-
self._time_left_at_pause = max(0, monotonic_ns() - self._next_update)
160+
self._time_left_at_pause = max(0, monotonic_ms() - self._next_update)
161161

162162
def resume(self):
163163
"""
164164
Resumes the animation.
165165
"""
166-
self._next_update = monotonic_ns() + self._time_left_at_pause
166+
self._next_update = monotonic_ms() + self._time_left_at_pause
167167
self._time_left_at_pause = 0
168168
self._paused = False
169169

@@ -201,11 +201,11 @@ def speed(self):
201201
"""
202202
The animation speed in fractional seconds.
203203
"""
204-
return self._speed_ns / NANOS_PER_SECOND
204+
return self._speed_ms / MS_PER_SECOND
205205

206206
@speed.setter
207207
def speed(self, seconds):
208-
self._speed_ns = int(seconds * NANOS_PER_SECOND)
208+
self._speed_ms = int(seconds * MS_PER_SECOND)
209209

210210
def on_cycle_complete(self):
211211
"""

adafruit_led_animation/animation/rainbow.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
from adafruit_led_animation.animation import Animation
4747
from adafruit_led_animation.color import BLACK, colorwheel
48-
from adafruit_led_animation import NANOS_PER_SECOND, monotonic_ns
48+
from adafruit_led_animation import MS_PER_SECOND, monotonic_ms
4949

5050
__version__ = "0.0.0-auto.0"
5151
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation.git"
@@ -88,15 +88,15 @@ def generate_rainbow(self):
8888
on_cycle_complete_supported = True
8989

9090
def _color_wheel_generator(self):
91-
period = int(self._period * NANOS_PER_SECOND)
91+
period = int(self._period * MS_PER_SECOND)
9292

9393
num_pixels = len(self.pixel_object)
94-
last_update = monotonic_ns()
94+
last_update = monotonic_ms()
9595
cycle_position = 0
9696
last_pos = 0
9797
while True:
9898
cycle_completed = False
99-
now = monotonic_ns()
99+
now = monotonic_ms()
100100
time_since_last_draw = now - last_update
101101
last_update = now
102102
pos = cycle_position = (cycle_position + time_since_last_draw) % period

adafruit_led_animation/helper.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
import math
4646

47-
from . import NANOS_PER_SECOND, monotonic_ns
47+
from . import MS_PER_SECOND, monotonic_ms
4848
from .color import calculate_intensity
4949

5050

@@ -339,14 +339,14 @@ def pulse_generator(period: float, animation_object, dotstar_pwm=False):
339339
:param animation_object: An animation object to interact with.
340340
:param dotstar_pwm: Whether to use the dostar per pixel PWM value for brightness control.
341341
"""
342-
period = int(period * NANOS_PER_SECOND)
342+
period = int(period * MS_PER_SECOND)
343343
half_period = period // 2
344344

345-
last_update = monotonic_ns()
345+
last_update = monotonic_ms()
346346
cycle_position = 0
347347
last_pos = 0
348348
while True:
349-
now = monotonic_ns()
349+
now = monotonic_ms()
350350
time_since_last_draw = now - last_update
351351
last_update = now
352352
pos = cycle_position = (cycle_position + time_since_last_draw) % period

adafruit_led_animation/sequence.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
import random
4848
from adafruit_led_animation.color import BLACK
49-
from . import NANOS_PER_SECOND, monotonic_ns
49+
from . import MS_PER_SECOND, monotonic_ms
5050

5151
__version__ = "0.0.0-auto.0"
5252
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation.git"
@@ -108,9 +108,9 @@ def __init__(
108108
)
109109
self._members = members
110110
self._advance_interval = (
111-
advance_interval * NANOS_PER_SECOND if advance_interval else None
111+
advance_interval * MS_PER_SECOND if advance_interval else None
112112
)
113-
self._last_advance = monotonic_ns()
113+
self._last_advance = monotonic_ms()
114114
self._current = 0
115115
self.auto_clear = auto_clear
116116
self.auto_reset = auto_reset
@@ -162,7 +162,7 @@ def add_cycle_complete_receiver(self, callback):
162162
def _auto_advance(self):
163163
if not self._advance_interval:
164164
return
165-
now = monotonic_ns()
165+
now = monotonic_ms()
166166
if now - self._last_advance > self._advance_interval:
167167
self._last_advance = now
168168
self._advance()
@@ -247,7 +247,7 @@ def freeze(self):
247247
if self._paused:
248248
return
249249
self._paused = True
250-
self._paused_at = monotonic_ns()
250+
self._paused_at = monotonic_ms()
251251
self.current_animation.freeze()
252252

253253
def resume(self):
@@ -257,7 +257,7 @@ def resume(self):
257257
if not self._paused:
258258
return
259259
self._paused = False
260-
now = monotonic_ns()
260+
now = monotonic_ms()
261261
self._last_advance += now - self._paused_at
262262
self._paused_at = 0
263263
self.current_animation.resume()

0 commit comments

Comments
 (0)