# 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` ================================================================================ TODO * Author(s): Roy Hooper, Kattni Rembor """ 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 10. Cannot exceed the number of pixels present in the pixel object, e.g. if the strip is 30 pixels long, the ``tail_length`` cannot exceed 30 pixels. :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=10, reverse=False, bounce=False, name=None, ): self._tail_length = tail_length + 1 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) 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] self.show() yield cycle_passes += 1 if self.bounce: self.reverse = not self.reverse if not self.bounce or cycle_passes == 2: self.cycle_complete() 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