Skip to content

Commit b7305e8

Browse files
committed
Add examples, bugfixes.
1 parent 773c5b7 commit b7305e8

10 files changed

+160
-43
lines changed

adafruit_led_animation/animation/rainbowchase.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,15 @@ class RainbowChase(Chase):
5858
:param size: Number of pixels to turn on in a row.
5959
:param spacing: Number of pixels to turn off in a row.
6060
:param reverse: Reverse direction of movement.
61-
:param wheel_step: How many colors to skip in `colorwheel` per bar (default 8)
61+
:param step: How many colors to skip in `colorwheel` per bar (default 8)
6262
"""
6363

6464
# pylint: disable=too-many-arguments
6565
def __init__(
66-
self,
67-
pixel_object,
68-
speed,
69-
size=2,
70-
spacing=3,
71-
reverse=False,
72-
name=None,
73-
wheel_step=8,
66+
self, pixel_object, speed, size=2, spacing=3, reverse=False, name=None, step=8,
7467
):
75-
self._num_colors = 256 // wheel_step
76-
self._colors = [colorwheel(n % 256) for n in range(0, 512, wheel_step)]
68+
self._num_colors = 256 // step
69+
self._colors = [colorwheel(n % 256) for n in range(0, 512, step)]
7770
self._color_idx = 0
7871
super().__init__(pixel_object, speed, 0, size, spacing, reverse, name)
7972

adafruit_led_animation/animation/rainbowcomet.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class RainbowComet(Comet):
5454
5555
:param pixel_object: The initialised LED object.
5656
:param float speed: Animation speed in seconds, e.g. ``0.1``.
57-
:param color: Animation color in ``(r, g, b)`` tuple, or ``0x000000`` hex format.
5857
:param int tail_length: The length of the comet. Defaults to 10. Cannot exceed the number of
5958
pixels present in the pixel object, e.g. if the strip is 30 pixels
6059
long, the ``tail_length`` cannot exceed 30 pixels.

adafruit_led_animation/animation/rainbowsparkle.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def __init__(
7373
step=1,
7474
name=None,
7575
background_brightness=0.2,
76+
precompute_rainbow=True,
7677
):
7778
self._num_sparkles = num_sparkles
7879
if num_sparkles is None:
@@ -86,7 +87,7 @@ def __init__(
8687
period=period,
8788
step=step,
8889
name=name,
89-
precompute_rainbow=True,
90+
precompute_rainbow=precompute_rainbow,
9091
)
9192

9293
def generate_rainbow(self):

adafruit_led_animation/helper.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,14 @@ def auto_write(self, value):
186186
self._pixels.auto_write = value
187187

188188
@classmethod
189-
def vertical_lines(cls, pixels, width, height, gridmapper):
189+
def vertical_lines(cls, pixel_object, width, height, gridmap):
190190
"""
191191
Generate a PixelMap of horizontal lines on a strip arranged in a grid.
192192
193-
:param pixels: pixel object
193+
:param pixel_object: pixel object
194194
:param width: width of grid
195195
:param height: height of grid
196-
:param gridmapper: a function to map x and y coordinates to the grid
196+
:param gridmap: a function to map x and y coordinates to the grid
197197
see vertical_strip_gridmap and horizontal_strip_gridmap
198198
:return: PixelMap
199199
@@ -205,22 +205,22 @@ def vertical_lines(cls, pixels, width, height, gridmapper):
205205
PixelMap.vertical_lines(pixels, 32, 8, vertical_strip_gridmap(8))
206206
207207
"""
208-
if len(pixels) < width * height:
208+
if len(pixel_object) < width * height:
209209
raise ValueError("number of pixels is less than width x height")
210210
mapping = []
211211
for x in range(width):
212-
mapping.append([gridmapper(x, y) for y in range(height)])
213-
return cls(pixels, mapping, individual_pixels=True)
212+
mapping.append([gridmap(x, y) for y in range(height)])
213+
return cls(pixel_object, mapping, individual_pixels=True)
214214

215215
@classmethod
216-
def horizontal_lines(cls, pixels, width, height, gridmapper):
216+
def horizontal_lines(cls, pixel_object, width, height, gridmap):
217217
"""
218218
Generate a PixelMap of horizontal lines on a strip arranged in a grid.
219219
220-
:param pixels: pixel object
220+
:param pixel_object: pixel object
221221
:param width: width of grid
222222
:param height: height of grid
223-
:param gridmapper: a function to map x and y coordinates to the grid
223+
:param gridmap: a function to map x and y coordinates to the grid
224224
see vertical_strip_gridmap and horizontal_strip_gridmap
225225
:return: PixelMap
226226
@@ -231,20 +231,20 @@ def horizontal_lines(cls, pixels, width, height, gridmapper):
231231
232232
PixelMap.horizontal_lines(pixels, 16, 16, vertical_strip_gridmap(16))
233233
"""
234-
if len(pixels) < width * height:
234+
if len(pixel_object) < width * height:
235235
raise ValueError("number of pixels is less than width x height")
236236
mapping = []
237237
for y in range(height):
238-
mapping.append([gridmapper(x, y) for x in range(width)])
239-
return cls(pixels, mapping, individual_pixels=True)
238+
mapping.append([gridmap(x, y) for x in range(width)])
239+
return cls(pixel_object, mapping, individual_pixels=True)
240240

241241

242242
def vertical_strip_gridmap(height, alternating=True):
243243
"""
244244
Returns a function that determines the pixel number for a grid with strips arranged vertically.
245245
246-
:param height: strip height in pixels
247-
:param alternating: strips alternate directions in a zigzag
246+
:param height: grid height in pixels
247+
:param alternating: Whether or not the lines in the grid run alternate directions in a zigzag
248248
:return: mapper(x, y)
249249
"""
250250

@@ -260,8 +260,8 @@ def horizontal_strip_gridmap(width, alternating=True):
260260
"""
261261
Determines the pixel number for a grid with strips arranged horizontally.
262262
263-
:param width: strip width in pixels
264-
:param alternating: strips alternate directions in a zigzag
263+
:param width: grid width in pixels
264+
:param alternating: Whether or not the lines in the grid run alternate directions in a zigzag
265265
:return: mapper(x, y)
266266
"""
267267

@@ -277,7 +277,7 @@ class PixelSubset:
277277
"""
278278
PixelSubset lets you work with a subset of a pixel object.
279279
280-
:param strip: An object that implements the Neopixel or Dotstar protocol.
280+
:param pixel_object: An object that implements the Neopixel or Dotstar protocol.
281281
:param int start: Starting pixel number.
282282
:param int end: Ending pixel number.
283283
@@ -294,8 +294,8 @@ class PixelSubset:
294294
pixels.show()
295295
"""
296296

297-
def __init__(self, strip, start, end):
298-
self._pixels = strip
297+
def __init__(self, pixel_object, start, end):
298+
self._pixels = pixel_object
299299
self._start = start
300300
self._end = end
301301
self.n = self._end - self._start

adafruit_led_animation/sequence.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def __init__(
103103
name=None
104104
):
105105
if advance_interval and advance_on_cycle_complete:
106-
raise ValueError("Cannot use both advance_interval and auto_clear")
106+
raise ValueError("Cannot use both advance_interval and advance_on_cycle_complete.")
107107
self._members = members
108108
self._advance_interval = (
109109
advance_interval * NANOS_PER_SECOND if advance_interval else None
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
This example displays the basic animations in sequence, at a five 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.solid import Solid
13+
from adafruit_led_animation.animation.colorcycle import ColorCycle
14+
from adafruit_led_animation.animation.blink import Blink
15+
from adafruit_led_animation.animation.comet import Comet
16+
from adafruit_led_animation.animation.chase import Chase
17+
from adafruit_led_animation.animation.pulse import Pulse
18+
from adafruit_led_animation.sequence import AnimationSequence
19+
from adafruit_led_animation.color import (
20+
PURPLE,
21+
WHITE,
22+
AMBER,
23+
JADE,
24+
TEAL,
25+
PINK,
26+
MAGENTA,
27+
ORANGE,
28+
)
29+
30+
# Update to match the pin connected to your NeoPixels
31+
pixel_pin = board.D6
32+
# Update to match the number of NeoPixels you have connected
33+
pixel_num = 32
34+
35+
pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.2, auto_write=False)
36+
37+
solid = Solid(pixels, color=PINK)
38+
colorcycle = ColorCycle(pixels, speed=0.4, colors=[MAGENTA, ORANGE, TEAL])
39+
blink = Blink(pixels, speed=0.5, color=JADE)
40+
comet = Comet(pixels, speed=0.01, color=PURPLE, tail_length=10, bounce=True)
41+
chase = Chase(pixels, speed=0.1, color=WHITE, size=3, spacing=6)
42+
pulse = Pulse(pixels, speed=0.1, color=AMBER, period=3)
43+
44+
45+
animations = AnimationSequence(
46+
solid, comet, blink, chase, colorcycle, pulse, advance_interval=5, auto_clear=True,
47+
)
48+
49+
while True:
50+
animations.animate()

examples/led_animation_gridmap.py renamed to examples/led_animation_pixel_map.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""
2-
This example shows usage of the gridmap helper to easily treat a single strip as a horizontal or
2+
This example shows usage of the PixelMap helper to easily treat a single strip as a horizontal or
33
vertical grid for animation purposes.
44
55
For NeoPixel FeatherWing. Update pixel_pin and pixel_num to match your wiring if using
6-
a different form of NeoPixels.
6+
a different form of NeoPixels. Note that if you are using a number of pixels other than 32, you
7+
will need to alter the PixelMap values as well for this example to work.
78
89
This example does not work on SAMD21 (M0) boards.
910
"""
@@ -19,8 +20,12 @@
1920
from adafruit_led_animation import helper
2021
from adafruit_led_animation.color import PURPLE, JADE, AMBER
2122

23+
# Update to match the pin connected to your NeoPixels
24+
pixel_pin = board.D6
25+
# Update to match the number of NeoPixels you have connected
26+
pixel_num = 32
2227

23-
pixels = neopixel.NeoPixel(board.D6, 32, brightness=0.2, auto_write=False)
28+
pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.2, auto_write=False)
2429

2530
pixel_wing_vertical = helper.PixelMap.vertical_lines(
2631
pixels, 8, 4, helper.horizontal_strip_gridmap(8, alternating=False)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
This example uses AnimationsSequence to display multiple animations in sequence, at a five second
3+
interval.
4+
5+
For NeoPixel FeatherWing. Update pixel_pin and pixel_num to match your wiring if using
6+
a different form of NeoPixels.
7+
8+
This example does not work on SAMD21 (M0) boards.
9+
"""
10+
import board
11+
import neopixel
12+
13+
from adafruit_led_animation.animation.rainbow import Rainbow
14+
from adafruit_led_animation.animation.rainbowchase import RainbowChase
15+
from adafruit_led_animation.animation.rainbowcomet import RainbowComet
16+
from adafruit_led_animation.animation.rainbowsparkle import RainbowSparkle
17+
from adafruit_led_animation.sequence import AnimationSequence
18+
19+
# Update to match the pin connected to your NeoPixels
20+
pixel_pin = board.D6
21+
# Update to match the number of NeoPixels you have connected
22+
pixel_num = 32
23+
24+
pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.2, auto_write=False)
25+
26+
rainbow = Rainbow(pixels, speed=0.1, period=2)
27+
rainbow_chase = RainbowChase(pixels, speed=0.1, size=5, spacing=3)
28+
rainbow_comet = RainbowComet(pixels, speed=0.1, tail_length=7, bounce=True)
29+
rainbow_sparkle = RainbowSparkle(pixels, speed=0.1, num_sparkles=15)
30+
31+
32+
animations = AnimationSequence(
33+
rainbow,
34+
rainbow_chase,
35+
rainbow_comet,
36+
rainbow_sparkle,
37+
advance_interval=5,
38+
auto_clear=True,
39+
)
40+
41+
while True:
42+
animations.animate()

examples/led_animation_sequence.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@
44
55
For NeoPixel FeatherWing. Update pixel_pin and pixel_num to match your wiring if using
66
a different form of NeoPixels.
7-
8-
This example does not work on SAMD21 (M0) boards.
97
"""
108
import board
119
import neopixel
1210

1311
from adafruit_led_animation.animation.blink import Blink
1412
from adafruit_led_animation.animation.comet import Comet
1513
from adafruit_led_animation.animation.chase import Chase
16-
from adafruit_led_animation.animation.pulse import Pulse
1714
from adafruit_led_animation.sequence import AnimationSequence
18-
from adafruit_led_animation.color import PURPLE, WHITE, AMBER, JADE
15+
from adafruit_led_animation.color import PURPLE, AMBER, JADE
1916

2017
# Update to match the pin connected to your NeoPixels
2118
pixel_pin = board.D6
@@ -26,12 +23,11 @@
2623

2724
blink = Blink(pixels, speed=0.5, color=JADE)
2825
comet = Comet(pixels, speed=0.01, color=PURPLE, tail_length=10, bounce=True)
29-
chase = Chase(pixels, speed=0.1, size=3, spacing=6, color=WHITE)
30-
pulse = Pulse(pixels, speed=0.1, period=3, color=AMBER)
26+
chase = Chase(pixels, speed=0.1, size=3, spacing=6, color=AMBER)
3127

3228

3329
animations = AnimationSequence(
34-
comet, blink, chase, pulse, advance_interval=5, auto_clear=True,
30+
comet, blink, chase, advance_interval=5, auto_clear=True, random_order=True,
3531
)
3632

3733
while True:
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
This example uses AnimationsSequence to display multiple animations in sequence, at a five second
3+
interval.
4+
5+
For NeoPixel FeatherWing. Update pixel_pin and pixel_num to match your wiring if using
6+
a different form of NeoPixels.
7+
"""
8+
import board
9+
import neopixel
10+
11+
from adafruit_led_animation.animation.sparkle import Sparkle
12+
from adafruit_led_animation.animation.sparklepulse import SparklePulse
13+
from adafruit_led_animation.sequence import AnimationSequence
14+
from adafruit_led_animation.color import AMBER, JADE
15+
16+
# Update to match the pin connected to your NeoPixels
17+
pixel_pin = board.D6
18+
# Update to match the number of NeoPixels you have connected
19+
pixel_num = 32
20+
21+
pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.2, auto_write=False)
22+
23+
sparkle = Sparkle(pixels, speed=0.05, color=AMBER, num_sparkles=10)
24+
sparkle_pulse = SparklePulse(pixels, speed=0.05, period=3, color=JADE)
25+
26+
animations = AnimationSequence(
27+
sparkle, sparkle_pulse, advance_interval=5, auto_clear=True,
28+
)
29+
30+
while True:
31+
animations.animate()

0 commit comments

Comments
 (0)