|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to deal |
| 5 | +# in the Software without restriction, including without limitation the rights |
| 6 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +# copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +# THE SOFTWARE. |
| 20 | +""" |
| 21 | +`adafruit_led_animation.timedsequence` |
| 22 | +================================================================================ |
| 23 | +
|
| 24 | +Animation timed sequence helper for CircuitPython helper library for LED animations. |
| 25 | +
|
| 26 | +
|
| 27 | +* Author(s): Mark Komus |
| 28 | +
|
| 29 | +Implementation Notes |
| 30 | +-------------------- |
| 31 | +
|
| 32 | +**Hardware:** |
| 33 | +
|
| 34 | +* `Adafruit NeoPixels <https://www.adafruit.com/category/168>`_ |
| 35 | +* `Adafruit DotStars <https://www.adafruit.com/category/885>`_ |
| 36 | +
|
| 37 | +**Software and Dependencies:** |
| 38 | +
|
| 39 | +* Adafruit CircuitPython firmware for the supported boards: |
| 40 | + https://circuitpython.org/downloads |
| 41 | +
|
| 42 | +""" |
| 43 | + |
| 44 | +from adafruit_led_animation.sequence import AnimationSequence |
| 45 | +from . import MS_PER_SECOND |
| 46 | + |
| 47 | + |
| 48 | +class TimedAnimationSequence(AnimationSequence): |
| 49 | + """ |
| 50 | + A sequence of Animations to run in succession, each animation running for an |
| 51 | + individual amount of time. |
| 52 | + :param members: The animation objects or groups followed by how long the animation |
| 53 | + should run in seconds. |
| 54 | + :param bool auto_clear: Clear the pixels between animations. If ``True``, the current animation |
| 55 | + will be cleared from the pixels before the next one starts. |
| 56 | + Defaults to ``False``. |
| 57 | + :param bool random_order: Activate the animations in a random order. Defaults to ``False``. |
| 58 | + :param bool auto_reset: Automatically call reset() on animations when changing animations. |
| 59 | + .. code-block:: python |
| 60 | + import board |
| 61 | + import neopixel |
| 62 | + from adafruit_led_animation.timedsequence import TimedAnimationSequence |
| 63 | + import adafruit_led_animation.animation.comet as comet_animation |
| 64 | + import adafruit_led_animation.animation.sparkle as sparkle_animation |
| 65 | + import adafruit_led_animation.animation.blink as blink_animation |
| 66 | + import adafruit_led_animation.color as color |
| 67 | + strip_pixels = neopixel.NeoPixel(board.A1, 30, brightness=1, auto_write=False) |
| 68 | + blink = blink_animation.Blink(strip_pixels, 0.2, color.RED) |
| 69 | + comet = comet_animation.Comet(strip_pixels, 0.1, color.BLUE) |
| 70 | + sparkle = sparkle_animation.Sparkle(strip_pixels, 0.05, color.GREEN) |
| 71 | + animations = AnimationSequence(blink, 5, comet, 3, sparkle, 7) |
| 72 | + while True: |
| 73 | + animations.animate() |
| 74 | + """ |
| 75 | + |
| 76 | + # pylint: disable=too-many-instance-attributes |
| 77 | + def __init__( |
| 78 | + self, *members, auto_clear=True, random_order=False, auto_reset=False, name=None |
| 79 | + ): |
| 80 | + self._animation_members = [] |
| 81 | + self._animation_timings = [] |
| 82 | + for x, item in enumerate(members): |
| 83 | + if not x % 2: |
| 84 | + self._animation_members.append(item) |
| 85 | + else: |
| 86 | + self._animation_timings.append(item) |
| 87 | + |
| 88 | + super().__init__( |
| 89 | + *self._animation_members, |
| 90 | + auto_clear=auto_clear, |
| 91 | + random_order=random_order, |
| 92 | + auto_reset=auto_reset, |
| 93 | + advance_on_cycle_complete=False, |
| 94 | + name=name |
| 95 | + ) |
| 96 | + self._advance_interval = self._animation_timings[self._current] * MS_PER_SECOND |
| 97 | + |
| 98 | + def activate(self, index): |
| 99 | + super().activate(index) |
| 100 | + self._advance_interval = self._animation_timings[self._current] * MS_PER_SECOND |
0 commit comments