Skip to content

Commit 9179e7c

Browse files
committed
adding multicolor comet animation
1 parent 5e3a68c commit 9179e7c

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 bool ring: Ring mode. Defaults to ``False``.
46+
:param bool off_pixels: Turn pixels off after the animation passes them. Defaults to ``True``.
47+
Setting to False will result in all pixels not currently in the comet
48+
to remain on and set to a color after the comet passes.
49+
"""
50+
51+
# pylint: disable=too-many-arguments,too-many-instance-attributes
52+
def __init__(
53+
self,
54+
pixel_object,
55+
speed,
56+
colors,
57+
tail_length=0,
58+
reverse=False,
59+
bounce=False,
60+
name=None,
61+
ring=False,
62+
off_pixels=True,
63+
):
64+
if tail_length == 0:
65+
tail_length = len(pixel_object) // 4
66+
if bounce and ring:
67+
raise ValueError("Cannot combine bounce and ring mode")
68+
self.bounce = bounce
69+
self._reverse = reverse
70+
self._initial_reverse = reverse
71+
self._tail_length = tail_length
72+
73+
self._comet_colors = None
74+
75+
self._num_pixels = len(pixel_object)
76+
self._direction = -1 if reverse else 1
77+
self._left_side = -self._tail_length
78+
self._right_side = self._num_pixels
79+
self._tail_start = 0
80+
self._ring = ring
81+
self._colors = colors
82+
if colors is None or len(colors) < 2:
83+
raise ValueError("Must pass at least two colors.")
84+
85+
self._off_pixels = off_pixels
86+
if ring:
87+
self._left_side = 0
88+
self.reset()
89+
super().__init__(
90+
pixel_object,
91+
speed,
92+
0x0,
93+
name=name,
94+
tail_length=tail_length,
95+
bounce=bounce,
96+
ring=ring,
97+
reverse=reverse,
98+
)
99+
100+
on_cycle_complete_supported = True
101+
102+
def _set_color(self, color):
103+
if self._off_pixels:
104+
self._comet_colors = [BLACK]
105+
else:
106+
self._comet_colors = []
107+
108+
for n in range(self._tail_length):
109+
_float_index = ((len(self._colors)) / self._tail_length) * n
110+
_color_index = int(_float_index)
111+
self._comet_colors.append(self._colors[_color_index])
Lines changed: 63 additions & 0 deletions
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)