Skip to content

Commit 1498b66

Browse files
authored
Merge pull request #1 from adafruit/master
pull from adafruit master
2 parents fed9ec0 + 67ccb54 commit 1498b66

12 files changed

+185
-20
lines changed

adafruit_led_animation/animation/__init__.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def __init__(self, pixel_object, speed, color, peers=None, paused=False, name=No
6868
self._time_left_at_pause = 0
6969
self._also_notify = []
7070
self.speed = speed # sets _speed_ns
71-
self.color = color # Triggers _recompute_color
71+
self.color = color # Triggers _set_color
7272
self.name = name
7373
self.cycle_complete = False
7474
self.notify_cycles = 1
@@ -173,6 +173,7 @@ def fill(self, color):
173173
Fills the pixel object with a color.
174174
"""
175175
self.pixel_object.fill(color)
176+
self.pixel_object.show()
176177

177178
@property
178179
def color(self):
@@ -187,8 +188,14 @@ def color(self, color):
187188
return
188189
if isinstance(color, int):
189190
color = (color >> 16 & 0xFF, color >> 8 & 0xFF, color & 0xFF)
191+
self._set_color(color)
192+
193+
def _set_color(self, color):
194+
"""
195+
Called after the color is changed, which includes at initialization.
196+
Override as needed.
197+
"""
190198
self._color = color
191-
self._recompute_color(color)
192199

193200
@property
194201
def speed(self):
@@ -201,12 +208,6 @@ def speed(self):
201208
def speed(self, seconds):
202209
self._speed_ns = int(seconds * NANOS_PER_SECOND)
203210

204-
def _recompute_color(self, color):
205-
"""
206-
Called if the color is changed, which includes at initialization.
207-
Override as needed.
208-
"""
209-
210211
def on_cycle_complete(self):
211212
"""
212213
Called by some animations when they complete an animation cycle.

adafruit_led_animation/animation/blink.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,5 @@ class Blink(ColorCycle):
6060
def __init__(self, pixel_object, speed, color, name=None):
6161
super().__init__(pixel_object, speed, [color, BLACK], name=name)
6262

63-
def _recompute_color(self, color):
63+
def _set_color(self, color):
6464
self.colors = [color, BLACK]

adafruit_led_animation/animation/comet.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,7 @@ def __init__(
9292

9393
on_cycle_complete_supported = True
9494

95-
def _recompute_color(self, color):
96-
self._comet_recompute_color(color)
97-
98-
def _comet_recompute_color(self, color):
95+
def _set_color(self, color):
9996
self._comet_colors = [BLACK]
10097
for n in range(self._tail_length):
10198
self._comet_colors.append(
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()

adafruit_led_animation/animation/rainbowcomet.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def __init__(
8282
self._colorwheel_offset = colorwheel_offset
8383
super().__init__(pixel_object, speed, 0, tail_length, reverse, bounce, name)
8484

85-
def _comet_recompute_color(self, color):
85+
def _set_color(self, color):
8686
self._comet_colors = [BLACK]
8787
for n in range(self._tail_length):
8888
invert = self._tail_length - n - 1

adafruit_led_animation/animation/solid.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ class Solid(ColorCycle):
5858
def __init__(self, pixel_object, color, name=None):
5959
super().__init__(pixel_object, speed=1, colors=[color], name=name)
6060

61-
def _recompute_color(self, color):
61+
def _set_color(self, color):
6262
self.colors = [color]

adafruit_led_animation/animation/sparkle.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def __init__(self, pixel_object, speed, color, num_sparkles=1, name=None):
7171
self._pixels = []
7272
super().__init__(pixel_object, speed, color, name=name)
7373

74-
def _recompute_color(self, color):
74+
def _set_color(self, color):
7575
half_color = tuple(color[rgb] // 4 for rgb in range(len(color)))
7676
dim_color = tuple(color[rgb] // 10 for rgb in range(len(color)))
7777
for pixel in range(len(self.pixel_object)):

adafruit_led_animation/animation/sparklepulse.py

+3
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ def __init__(
8080
)
8181
self._generator = pulse_generator(self._period, self, dotstar_pwm=dotstar)
8282

83+
def _set_color(self, color):
84+
self._color = color
85+
8386
def draw(self):
8487
self._sparkle_color = next(self._generator)
8588
super().draw()

adafruit_led_animation/group.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
__version__ = "0.0.0-auto.0"
4848
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation.git"
4949

50+
from adafruit_led_animation.animation import Animation
51+
5052

5153
class AnimationGroup:
5254
"""
@@ -158,7 +160,14 @@ def animate(self, show=True):
158160
if self._sync:
159161
result = self._members[0].animate(show=False)
160162
if result and show:
161-
self._members[0].show()
163+
last_strip = None
164+
for member in self._members:
165+
if isinstance(member, Animation):
166+
if last_strip != member.pixel_object:
167+
member.pixel_object.show()
168+
last_strip = member.pixel_object
169+
else:
170+
member.show()
162171
return result
163172

164173
return any([item.animate(show) for item in self._members])

adafruit_led_animation/sequence.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ def _advance(self):
172172
self.current_animation.reset()
173173
if self.auto_clear:
174174
self.current_animation.fill(self.clear_color)
175-
self.current_animation.show()
176175
if self._random:
177176
self.random()
178177
else:
@@ -317,6 +316,6 @@ def on_cycle_complete(self):
317316
super().on_cycle_complete()
318317
self._running = False
319318

320-
def animate(self):
321-
super().animate()
319+
def animate(self, show=True):
320+
super().animate(show)
322321
return self._running

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)