pulse.py
 1  # SPDX-FileCopyrightText: 2020 Kattni Rembor for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  """
 6  `adafruit_led_animation.animation.pulse`
 7  ================================================================================
 8  
 9  Pulse animation for CircuitPython helper library for LED animations.
10  
11  * Author(s): Kattni Rembor
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  
29  from adafruit_led_animation.animation import Animation
30  
31  
32  class Pulse(Animation):
33      """
34      Pulse all pixels a single color.
35  
36      :param pixel_object: The initialised LED object.
37      :param float speed: Animation refresh rate in seconds, e.g. ``0.1``.
38      :param color: Animation color in ``(r, g, b)`` tuple, or ``0x000000`` hex format.
39      :param period: Period to pulse the LEDs over.  Default 5.
40      """
41  
42      # pylint: disable=too-many-arguments
43      def __init__(self, pixel_object, speed, color, period=5, name=None):
44          super().__init__(pixel_object, speed, color, name=name)
45          self._period = period
46          self._generator = None
47          self.reset()
48  
49      on_cycle_complete_supported = True
50  
51      def draw(self):
52          color = next(self._generator)
53          self.pixel_object.fill(color)
54  
55      def reset(self):
56          """
57          Resets the animation.
58          """
59          dotstar = len(self.pixel_object[0]) == 4 and isinstance(
60              self.pixel_object[0][-1], float
61          )
62          from adafruit_led_animation.helper import (  # pylint: disable=import-outside-toplevel
63              pulse_generator,
64          )
65  
66          self._generator = pulse_generator(self._period, self, dotstar_pwm=dotstar)