Skip to content

Commit 81a3174

Browse files
committed
debounce pause/resume for sequences and ensure the pause continues where it left off.
1 parent a3e3b75 commit 81a3174

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

adafruit_led_animation/animation.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def __init__(self, pixel_object, speed, color, peers=None, paused=False):
7676
self.color = color
7777
self.peers = peers if peers else []
7878
self._paused = paused
79+
self._time_left_at_pause = 0
7980

8081
def animate(self):
8182
"""
@@ -117,11 +118,14 @@ def freeze(self):
117118
Stops the animation until resumed.
118119
"""
119120
self._paused = True
121+
self._time_left_at_pause = max(0, monotonic_ns() - self._next_update)
120122

121123
def resume(self):
122124
"""
123125
Resumes the animation.
124126
"""
127+
self._next_update = monotonic_ns() + self._time_left_at_pause
128+
self._time_left_at_pause = 0
125129
self._paused = False
126130

127131
def fill(self, color):
@@ -350,6 +354,7 @@ def __init__(self, *members, advance_interval=None, auto_clear=False):
350354
self._auto_clear = auto_clear
351355
self.clear_color = BLACK
352356
self._paused = False
357+
self._paused_at = 0
353358

354359
def _auto_advance(self):
355360
if not self._advance_interval:
@@ -392,17 +397,26 @@ def freeze(self):
392397
Freeze the current animation in the sequence.
393398
Also stops auto_advance.
394399
"""
400+
if self._paused:
401+
return
395402
self._paused = True
403+
self._paused_at = monotonic_ns()
396404
self.current_animation.freeze()
397405

398406
def resume(self):
399407
"""
400408
Resume the current animation in the sequence and resumes auto advance
401409
if enabled.
402410
"""
411+
if not self._paused:
412+
return
403413
self._paused = False
414+
now = monotonic_ns()
415+
self._last_advance += now - self._paused_at
416+
self._paused_at = 0
404417
self.current_animation.resume()
405418

419+
406420
class AnimationGroup:
407421
"""
408422
A group of animations that are active together, such as a strip of

0 commit comments

Comments
 (0)