Skip to content

Commit 7fd9f41

Browse files
authored
Merge pull request #105 from FoamyGuy/multicolor_comet
adding multicolor comet animation
2 parents b8538c6 + 0629e14 commit 7fd9f41

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed

adafruit_led_animation/animation/comet.py

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class Comet(Animation):
4444
maximum of the length of the ``pixel_object``.
4545
:param bool reverse: Animates the comet in the reverse order. Defaults to ``False``.
4646
:param bool bounce: Comet will bounce back and forth. Defaults to ``False``.
47+
:param Optional[string] name: A human-readable name for the Animation.
48+
Used by the to string function.
4749
:param bool ring: Ring mode. Defaults to ``False``.
4850
"""
4951

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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])
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# SPDX-FileCopyrightText: 2022 Tim Cocks
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""
5+
This example animates a red, yellow, and green gradient comet that bounces
6+
from end to end of the strip.
7+
8+
For QT Py Haxpress and a NeoPixel strip. Update pixel_pin and pixel_num to match your wiring if
9+
using a different board or form of NeoPixels.
10+
11+
This example will run on SAMD21 (M0) Express boards (such as Circuit Playground Express or QT Py
12+
Haxpress), but not on SAMD21 non-Express boards (such as QT Py or Trinket).
13+
"""
14+
import board
15+
import neopixel
16+
from adafruit_led_animation.animation.multicolor_comet import MulticolorComet
17+
18+
# Update to match the pin connected to your NeoPixels
19+
pixel_pin = board.D9
20+
# Update to match the number of NeoPixels you have connected
21+
pixel_num = 96
22+
brightness = 0.02
23+
24+
pixels = neopixel.NeoPixel(
25+
pixel_pin,
26+
pixel_num,
27+
brightness=brightness,
28+
auto_write=True,
29+
pixel_order=neopixel.RGB,
30+
)
31+
32+
comet_colors = [
33+
0xFF0000,
34+
0xFD2000,
35+
0xF93E00,
36+
0xF45B00,
37+
0xEC7500,
38+
0xE28D00,
39+
0xD5A200,
40+
0xC6B500,
41+
0xB5C600,
42+
0xA2D500,
43+
0x8DE200,
44+
0x75EC00,
45+
0x5BF400,
46+
0x3EF900,
47+
0x20FD00,
48+
0x00FF00,
49+
]
50+
51+
52+
comet = MulticolorComet(
53+
pixels,
54+
colors=comet_colors,
55+
speed=0.01,
56+
tail_length=20,
57+
bounce=True,
58+
ring=False,
59+
reverse=False,
60+
)
61+
62+
while True:
63+
comet.animate()

0 commit comments

Comments
 (0)