Skip to content

Commit 3dbe521

Browse files
authored
Merge pull request #51 from cjsieh/customcolorchase
Customcolorschase
2 parents 56230fb + a8ad8eb commit 3dbe521

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2019-2020 Roy Hooper
4+
# Copyright (c) 2020 Kattni Rembor for Adafruit Industries
5+
# Copyright (c) 2020 Connie Sieh
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in
15+
# all copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
# THE SOFTWARE.
24+
"""
25+
`adafruit_led_animation.animation.customcolorchase`
26+
================================================================================
27+
28+
Custom color chase animation for CircuitPython helper library for LED animations.
29+
30+
* Author(s): Roy Hooper, Kattni Rembor, Connie Sieh
31+
32+
Implementation Notes
33+
--------------------
34+
35+
**Hardware:**
36+
37+
* `Adafruit NeoPixels <https://www.adafruit.com/category/168>`_
38+
* `Adafruit DotStars <https://www.adafruit.com/category/885>`_
39+
40+
**Software and Dependencies:**
41+
42+
* Adafruit CircuitPython firmware for the supported boards:
43+
https://circuitpython.org/downloads
44+
45+
46+
"""
47+
48+
from adafruit_led_animation.animation.chase import Chase
49+
from adafruit_led_animation.color import RAINBOW
50+
51+
52+
class CustomColorChase(Chase):
53+
"""
54+
Chase pixels in one direction, like a theater marquee with Custom Colors
55+
56+
:param pixel_object: The initialised LED object.
57+
:param float speed: Animation speed rate in seconds, e.g. ``0.1``.
58+
:param colors: Animation colors in list of `(r, g, b)`` tuple, or ``0x000000`` hex format
59+
:param size: Number of pixels to turn on in a row.
60+
:param spacing: Number of pixels to turn off in a row.
61+
:param reverse: Reverse direction of movement.
62+
"""
63+
64+
# pylint: disable=too-many-arguments
65+
def __init__(
66+
self,
67+
pixel_object,
68+
speed,
69+
size=2,
70+
spacing=3,
71+
reverse=False,
72+
name=None,
73+
colors=RAINBOW,
74+
):
75+
self._num_colors = len(colors)
76+
self._colors = colors
77+
self._color_idx = 0
78+
super().__init__(pixel_object, speed, 0, size, spacing, reverse, name)
79+
80+
def bar_color(self, n, pixel_no=0):
81+
return self._colors[self._color_idx - (n % len(self._colors))]
82+
83+
def on_cycle_complete(self):
84+
self._color_idx = (self._color_idx + self._direction) % len(self._colors)
85+
super().on_cycle_complete()

examples/led_animation_all_animations.py

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from adafruit_led_animation.animation.solid import Solid
2222
from adafruit_led_animation.animation.colorcycle import ColorCycle
2323
from adafruit_led_animation.animation.rainbow import Rainbow
24+
from adafruit_led_animation.animation.customcolorchase import CustomColorChase
2425
from adafruit_led_animation.sequence import AnimationSequence
2526
from adafruit_led_animation.color import PURPLE, WHITE, AMBER, JADE, MAGENTA, ORANGE
2627

@@ -43,6 +44,9 @@
4344
rainbow_comet = RainbowComet(pixels, speed=0.1, tail_length=7, bounce=True)
4445
rainbow_chase = RainbowChase(pixels, speed=0.1, size=3, spacing=2, step=8)
4546
rainbow_sparkle = RainbowSparkle(pixels, speed=0.1, num_sparkles=15)
47+
custom_color_chase = CustomColorChase(
48+
pixels, speed=0.1, size=2, spacing=3, colors=[ORANGE, WHITE, JADE]
49+
)
4650

4751

4852
animations = AnimationSequence(
@@ -57,6 +61,7 @@
5761
rainbow_comet,
5862
sparkle_pulse,
5963
rainbow_chase,
64+
custom_color_chase,
6065
advance_interval=5,
6166
auto_clear=True,
6267
)
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
This example displays custom color chase animations in sequence, at a six second interval.
3+
4+
For NeoPixel FeatherWing. Update pixel_pin and pixel_num to match your wiring if using
5+
a different form of NeoPixels.
6+
7+
This example may not work on SAMD21 (M0) boards.
8+
"""
9+
import board
10+
import neopixel
11+
12+
from adafruit_led_animation.animation.customcolorchase import CustomColorChase
13+
from adafruit_led_animation.sequence import AnimationSequence
14+
15+
# colorwheel only needed for rainbowchase example
16+
from adafruit_led_animation.color import colorwheel
17+
18+
# Colors for customcolorchase examples
19+
from adafruit_led_animation.color import PINK, GREEN, RED, BLUE
20+
21+
# Update to match the pin connected to your NeoPixels
22+
pixel_pin = board.D5
23+
# Update to match the number of NeoPixels you have connected
24+
pixel_num = 30
25+
brightness = 0.3
26+
27+
pixels = neopixel.NeoPixel(
28+
pixel_pin, pixel_num, brightness=brightness, auto_write=False
29+
)
30+
31+
# colors default to RAINBOW as defined in color.py
32+
custom_color_chase_rainbow = CustomColorChase(pixels, speed=0.1, size=2, spacing=3)
33+
custom_color_chase_rainbow_r = CustomColorChase(
34+
pixels, speed=0.1, size=3, spacing=3, reverse=True
35+
)
36+
37+
# Example with same colors as RainbowChase
38+
steps = 30
39+
# This was taken from rainbowchase.py
40+
rainbow_colors = [colorwheel(n % 256) for n in range(0, 512, steps)]
41+
# Now use rainbow_colors with CustomColorChase
42+
custom_color_chase_rainbowchase = CustomColorChase(
43+
pixels, speed=0.1, colors=rainbow_colors, size=2, spacing=3
44+
)
45+
46+
custom_color_chase_bgp = CustomColorChase(
47+
pixels, speed=0.1, colors=[BLUE, GREEN, PINK], size=3, spacing=2
48+
)
49+
50+
# Can use integer values for color, 0 is black
51+
custom_color_chase_br = CustomColorChase(
52+
pixels, speed=0.1, colors=[BLUE, 0, RED, 0], size=2, spacing=0
53+
)
54+
55+
animations = AnimationSequence(
56+
custom_color_chase_rainbow,
57+
custom_color_chase_rainbow_r,
58+
custom_color_chase_rainbowchase,
59+
custom_color_chase_bgp,
60+
custom_color_chase_br,
61+
advance_interval=6,
62+
auto_clear=True,
63+
)
64+
65+
while True:
66+
animations.animate()

0 commit comments

Comments
 (0)