|
| 1 | +# SPDX-FileCopyrightText: 2022 Tim Cocks |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +""" |
| 6 | +`adafruit_led_animation.animation.multicolor_comet` |
| 7 | +================================================================================ |
| 8 | +
|
| 9 | +Multi-color Comet animation for CircuitPython helper library for LED animations. |
| 10 | +
|
| 11 | +* Author(s): Kattni Rembor, Tim Cocks |
| 12 | +
|
| 13 | +Implementation Notes |
| 14 | +-------------------- |
| 15 | +
|
| 16 | +**Hardware:** |
| 17 | +
|
| 18 | +* `Adafruit NeoPixels <https://www.adafruit.com/category/168>`_ |
| 19 | +* `Adafruit DotStars <https://www.adafruit.com/category/885>`_ |
| 20 | +
|
| 21 | +**Software and Dependencies:** |
| 22 | +
|
| 23 | +* Adafruit CircuitPython firmware for the supported boards: |
| 24 | + https://circuitpython.org/downloads |
| 25 | +
|
| 26 | +
|
| 27 | +""" |
| 28 | +from adafruit_led_animation.animation.comet import Comet |
| 29 | +from adafruit_led_animation.color import BLACK |
| 30 | + |
| 31 | + |
| 32 | +class MulticolorComet(Comet): |
| 33 | + """ |
| 34 | + A multi-color comet animation. |
| 35 | +
|
| 36 | + :param pixel_object: The initialised LED object. |
| 37 | + :param float speed: Animation speed in seconds, e.g. ``0.1``. |
| 38 | + :param colors: Animation colors in a list or tuple of entries in |
| 39 | + ``(r, g, b)`` tuple, or ``0x000000`` hex format. |
| 40 | + :param int tail_length: The length of the comet. Defaults to 25% of the length of the |
| 41 | + ``pixel_object``. Automatically compensates for a minimum of 2 and a |
| 42 | + maximum of the length of the ``pixel_object``. |
| 43 | + :param bool reverse: Animates the comet in the reverse order. Defaults to ``False``. |
| 44 | + :param bool bounce: Comet will bounce back and forth. Defaults to ``True``. |
| 45 | + :param Optional[string] name: A human-readable name for the Animation. |
| 46 | + Used by the to string function. |
| 47 | + :param bool ring: Ring mode. Defaults to ``False``. |
| 48 | + :param bool off_pixels: Turn pixels off after the animation passes them. Defaults to ``True``. |
| 49 | + Setting to False will result in all pixels not currently in the comet |
| 50 | + to remain on and set to a color after the comet passes. |
| 51 | + """ |
| 52 | + |
| 53 | + # pylint: disable=too-many-arguments,too-many-instance-attributes |
| 54 | + def __init__( |
| 55 | + self, |
| 56 | + pixel_object, |
| 57 | + speed, |
| 58 | + colors, |
| 59 | + *, |
| 60 | + tail_length=0, |
| 61 | + reverse=False, |
| 62 | + bounce=False, |
| 63 | + name=None, |
| 64 | + ring=False, |
| 65 | + off_pixels=True, |
| 66 | + ): |
| 67 | + if tail_length == 0: |
| 68 | + tail_length = len(pixel_object) // 4 |
| 69 | + if bounce and ring: |
| 70 | + raise ValueError("Cannot combine bounce and ring mode") |
| 71 | + self.bounce = bounce |
| 72 | + self._reverse = reverse |
| 73 | + self._initial_reverse = reverse |
| 74 | + self._tail_length = tail_length |
| 75 | + |
| 76 | + self._comet_colors = None |
| 77 | + |
| 78 | + self._num_pixels = len(pixel_object) |
| 79 | + self._direction = -1 if reverse else 1 |
| 80 | + self._left_side = -self._tail_length |
| 81 | + self._right_side = self._num_pixels |
| 82 | + self._tail_start = 0 |
| 83 | + self._ring = ring |
| 84 | + self._colors = colors |
| 85 | + if colors is None or len(colors) < 2: |
| 86 | + raise ValueError("Must pass at least two colors.") |
| 87 | + |
| 88 | + self._off_pixels = off_pixels |
| 89 | + if ring: |
| 90 | + self._left_side = 0 |
| 91 | + self.reset() |
| 92 | + super().__init__( |
| 93 | + pixel_object, |
| 94 | + speed, |
| 95 | + 0x0, |
| 96 | + name=name, |
| 97 | + tail_length=tail_length, |
| 98 | + bounce=bounce, |
| 99 | + ring=ring, |
| 100 | + reverse=reverse, |
| 101 | + ) |
| 102 | + |
| 103 | + on_cycle_complete_supported = True |
| 104 | + |
| 105 | + def _set_color(self, color): |
| 106 | + if self._off_pixels: |
| 107 | + self._comet_colors = [BLACK] |
| 108 | + else: |
| 109 | + self._comet_colors = [] |
| 110 | + |
| 111 | + for n in range(self._tail_length): |
| 112 | + _float_index = ((len(self._colors)) / self._tail_length) * n |
| 113 | + _color_index = int(_float_index) |
| 114 | + self._comet_colors.append(self._colors[_color_index]) |
0 commit comments