# The MIT License (MIT) # # Copyright (c) 2019-2020 Roy Hooper # Copyright (c) 2020 Kattni Rembor for Adafruit Industries # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. """ `adafruit_led_animation.animation.comet` ================================================================================ Comet animation for CircuitPython helper library for LED animations. * Author(s): Roy Hooper, Kattni Rembor Implementation Notes -------------------- **Hardware:** * `Adafruit NeoPixels `_ * `Adafruit DotStars `_ **Software and Dependencies:** * Adafruit CircuitPython firmware for the supported boards: https://circuitpython.org/downloads """ from adafruit_led_animation.animation import Animation from adafruit_led_animation.color import BLACK class Comet(Animation): """ A comet animation. :param pixel_object: The initialised LED object. :param float speed: Animation speed in seconds, e.g. ``0.1``. :param color: Animation color in ``(r, g, b)`` tuple, or ``0x000000`` hex format. :param int tail_length: The length of the comet. Defaults to 25% of the length of the ``pixel_object``. Automatically compensates for a minimum of 2 and a maximum of the length of the ``pixel_object``. :param bool reverse: Animates the comet in the reverse order. Defaults to ``False``. :param bool bounce: Comet will bounce back and forth. Defaults to ``True``. """ # pylint: disable=too-many-arguments def __init__( self, pixel_object, speed, color, tail_length=0, reverse=False, bounce=False, name=None, ): if tail_length == 0: tail_length = len(pixel_object) // 4 else: tail_length = max(2, min(tail_length, len(pixel_object))) self._tail_length = tail_length self._color_step = 0.9 / tail_length self._color_offset = 0.1 self._comet_colors = None self._reverse_comet_colors = None self._initial_reverse = reverse self.reverse = reverse self.bounce = bounce self._computed_color = color self._generator = self._comet_generator() super().__init__(pixel_object, speed, color, name=name) on_cycle_complete_supported = True def _recompute_color(self, color): pass def __recompute_color(self, color): self._comet_colors = [BLACK] + [ [ int(color[rgb] * ((n * self._color_step) + self._color_offset)) for rgb in range(len(color)) ] for n in range(self._tail_length - 1) ] self._reverse_comet_colors = list(reversed(self._comet_colors)) self._computed_color = color def _get_range(self, num_pixels): if self.reverse: return range(num_pixels, -self._tail_length - 1, -1) return range(-self._tail_length, num_pixels + 1) def _comet_generator(self): num_pixels = len(self.pixel_object) cycle_passes = 0 while True: if self._color != self._computed_color or not self._comet_colors: self.__recompute_color(self._color) colors = self._reverse_comet_colors if self.reverse else self._comet_colors for start in self._get_range(num_pixels): if start + self._tail_length < num_pixels: end = self._tail_length else: end = num_pixels - start if start <= 0: num_visible = self._tail_length + start self.pixel_object[0:num_visible] = colors[ self._tail_length - num_visible : ] else: self.pixel_object[start : start + end] = colors[0:end] yield cycle_passes += 1 if self.bounce: self.reverse = not self.reverse if not self.bounce or cycle_passes == 2: self.cycle_complete = True cycle_passes = 0 def draw(self): next(self._generator) def reset(self): """ Resets to the first color. """ self._generator = self._comet_generator() self.reverse = self._initial_reverse