Merge pull request #38 from rhooper/sequence-one-shot

Add OneShot sequence subclass to make it easy to run a sequence once
This commit is contained in:
Kattni 2020-06-08 20:45:45 -04:00 committed by GitHub
commit 4eefc345ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 9 deletions

View file

@ -99,15 +99,16 @@ class Animation:
for anim in self._peers:
anim.draw()
anim.after_draw()
anim.draw_count += 1
for anim in self._peers:
anim.show()
# Note that the main animation cycle_complete flag is used, not the peer flag.
for anim in self._peers:
if self.cycle_complete:
anim.on_cycle_complete()
if anim.cycle_complete:
anim.cycle_complete = False
anim.on_cycle_complete()
self._next_update = now + self._speed_ns
return True

View file

@ -119,12 +119,14 @@ class Comet(Animation):
if self.bounce:
self.reverse = not self.reverse
self._direction = -self._direction
if self.reverse == self._initial_reverse:
else:
self.reset()
if self.reverse == self._initial_reverse and self.draw_count > 0:
self.cycle_complete = True
def reset(self):
"""
Resets to the first color.
Resets to the first state.
"""
self.reverse = self._initial_reverse
if self.reverse:

View file

@ -351,7 +351,7 @@ def pulse_generator(period: float, animation_object, dotstar_pwm=False):
last_update = now
pos = cycle_position = (cycle_position + time_since_last_draw) % period
if pos < last_pos:
animation_object.on_cycle_complete()
animation_object.cycle_complete = True
last_pos = pos
if pos > half_period:
pos = period - pos

View file

@ -147,7 +147,6 @@ class AnimationSequence:
callback(self)
def _sequence_complete(self, animation): # pylint: disable=unused-argument
self.on_cycle_complete()
if self.advance_on_cycle_complete:
self._advance()
@ -194,10 +193,10 @@ class AnimationSequence:
"""
Jump to the next animation.
"""
current = self._current
if current > self._current:
current = self._current + 1
if current >= len(self._members):
self.on_cycle_complete()
self.activate((self._current + 1) % len(self._members))
self.activate(current % len(self._members))
def random(self):
"""
@ -275,3 +274,49 @@ class AnimationSequence:
Draws the current animation group members.
"""
self.current_animation.show()
class AnimateOnce(AnimationSequence):
"""
Wrapper around AnimationSequence that returns False to animate() until a sequence has completed.
Takes the same arguments as AnimationSequence, but overrides advance_on_cycle_complete=True
and advance_interval=0
Example:
This example animates a comet in one direction then pulses red momentarily
.. code-block:: python
import board
import neopixel
from adafruit_led_animation.animation.comet import Comet
from adafruit_led_animation.animation.pulse import Pulse
from adafruit_led_animation.color import BLUE, RED
from adafruit_led_animation.sequence import AnimateOnce
strip_pixels = neopixel.NeoPixel(board.A1, 30, brightness=0.5, auto_write=False)
comet = Comet(strip_pixels, 0.01, color=BLUE, bounce=False)
pulse = Pulse(strip_pixels, 0.01, color=RED, period=2)
animations = AnimateOnce(comet, pulse)
while animations.animate():
pass
"""
def __init__(self, *members, **kwargs):
kwargs["advance_on_cycle_complete"] = True
kwargs["advance_interval"] = 0
super().__init__(*members, **kwargs)
self._running = True
def on_cycle_complete(self):
super().on_cycle_complete()
self._running = False
def animate(self):
super().animate()
return self._running