Skip to content

Commit 4eefc34

Browse files
authored
Merge pull request adafruit#38 from rhooper/sequence-one-shot
Add OneShot sequence subclass to make it easy to run a sequence once
2 parents a07c8ac + 307665a commit 4eefc34

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed

adafruit_led_animation/animation/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,16 @@ def animate(self):
9999
for anim in self._peers:
100100
anim.draw()
101101
anim.after_draw()
102+
anim.draw_count += 1
102103

103104
for anim in self._peers:
104105
anim.show()
105106

106107
# Note that the main animation cycle_complete flag is used, not the peer flag.
107108
for anim in self._peers:
108-
if self.cycle_complete:
109-
anim.on_cycle_complete()
109+
if anim.cycle_complete:
110110
anim.cycle_complete = False
111+
anim.on_cycle_complete()
111112

112113
self._next_update = now + self._speed_ns
113114
return True

adafruit_led_animation/animation/comet.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,14 @@ def draw(self):
119119
if self.bounce:
120120
self.reverse = not self.reverse
121121
self._direction = -self._direction
122-
if self.reverse == self._initial_reverse:
122+
else:
123+
self.reset()
124+
if self.reverse == self._initial_reverse and self.draw_count > 0:
123125
self.cycle_complete = True
124126

125127
def reset(self):
126128
"""
127-
Resets to the first color.
129+
Resets to the first state.
128130
"""
129131
self.reverse = self._initial_reverse
130132
if self.reverse:

adafruit_led_animation/helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ def pulse_generator(period: float, animation_object, dotstar_pwm=False):
351351
last_update = now
352352
pos = cycle_position = (cycle_position + time_since_last_draw) % period
353353
if pos < last_pos:
354-
animation_object.on_cycle_complete()
354+
animation_object.cycle_complete = True
355355
last_pos = pos
356356
if pos > half_period:
357357
pos = period - pos

adafruit_led_animation/sequence.py

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ def on_cycle_complete(self):
147147
callback(self)
148148

149149
def _sequence_complete(self, animation): # pylint: disable=unused-argument
150-
self.on_cycle_complete()
151150
if self.advance_on_cycle_complete:
152151
self._advance()
153152

@@ -194,10 +193,10 @@ def next(self):
194193
"""
195194
Jump to the next animation.
196195
"""
197-
current = self._current
198-
if current > self._current:
196+
current = self._current + 1
197+
if current >= len(self._members):
199198
self.on_cycle_complete()
200-
self.activate((self._current + 1) % len(self._members))
199+
self.activate(current % len(self._members))
201200

202201
def random(self):
203202
"""
@@ -275,3 +274,49 @@ def show(self):
275274
Draws the current animation group members.
276275
"""
277276
self.current_animation.show()
277+
278+
279+
class AnimateOnce(AnimationSequence):
280+
"""
281+
Wrapper around AnimationSequence that returns False to animate() until a sequence has completed.
282+
Takes the same arguments as AnimationSequence, but overrides advance_on_cycle_complete=True
283+
and advance_interval=0
284+
285+
Example:
286+
287+
This example animates a comet in one direction then pulses red momentarily
288+
289+
.. code-block:: python
290+
291+
import board
292+
import neopixel
293+
from adafruit_led_animation.animation.comet import Comet
294+
from adafruit_led_animation.animation.pulse import Pulse
295+
from adafruit_led_animation.color import BLUE, RED
296+
from adafruit_led_animation.sequence import AnimateOnce
297+
298+
strip_pixels = neopixel.NeoPixel(board.A1, 30, brightness=0.5, auto_write=False)
299+
300+
comet = Comet(strip_pixels, 0.01, color=BLUE, bounce=False)
301+
pulse = Pulse(strip_pixels, 0.01, color=RED, period=2)
302+
303+
animations = AnimateOnce(comet, pulse)
304+
305+
while animations.animate():
306+
pass
307+
308+
"""
309+
310+
def __init__(self, *members, **kwargs):
311+
kwargs["advance_on_cycle_complete"] = True
312+
kwargs["advance_interval"] = 0
313+
super().__init__(*members, **kwargs)
314+
self._running = True
315+
316+
def on_cycle_complete(self):
317+
super().on_cycle_complete()
318+
self._running = False
319+
320+
def animate(self):
321+
super().animate()
322+
return self._running

0 commit comments

Comments
 (0)