Skip to content

Example updates and bugfixes. #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion adafruit_led_animation/animation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def peers(self, peer_list):
:param list peer_list: List of peer animations.
"""
if peer_list is not None:
self._peers = [self] + peer_list
self._peers = [self] + list(peer_list)

def freeze(self):
"""
Expand Down
15 changes: 4 additions & 11 deletions adafruit_led_animation/animation/rainbowchase.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,15 @@ class RainbowChase(Chase):
:param size: Number of pixels to turn on in a row.
:param spacing: Number of pixels to turn off in a row.
:param reverse: Reverse direction of movement.
:param wheel_step: How many colors to skip in `colorwheel` per bar (default 8)
:param step: How many colors to skip in `colorwheel` per bar (default 8)
"""

# pylint: disable=too-many-arguments
def __init__(
self,
pixel_object,
speed,
size=2,
spacing=3,
reverse=False,
name=None,
wheel_step=8,
self, pixel_object, speed, size=2, spacing=3, reverse=False, name=None, step=8,
):
self._num_colors = 256 // wheel_step
self._colors = [colorwheel(n % 256) for n in range(0, 512, wheel_step)]
self._num_colors = 256 // step
self._colors = [colorwheel(n % 256) for n in range(0, 512, step)]
self._color_idx = 0
super().__init__(pixel_object, speed, 0, size, spacing, reverse, name)

Expand Down
1 change: 0 additions & 1 deletion adafruit_led_animation/animation/rainbowcomet.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class RainbowComet(Comet):

:param pixel_object: The initialised LED object.
:param float speed: Animation speed in seconds, e.g. ``0.1``.
:param color: Animation color in ``(r, g, b)`` tuple, or ``0x000000`` hex format.
:param int tail_length: The length of the comet. Defaults to 10. Cannot exceed the number of
pixels present in the pixel object, e.g. if the strip is 30 pixels
long, the ``tail_length`` cannot exceed 30 pixels.
Expand Down
3 changes: 2 additions & 1 deletion adafruit_led_animation/animation/rainbowsparkle.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def __init__(
step=1,
name=None,
background_brightness=0.2,
precompute_rainbow=True,
):
self._num_sparkles = num_sparkles
if num_sparkles is None:
Expand All @@ -86,7 +87,7 @@ def __init__(
period=period,
step=step,
name=name,
precompute_rainbow=True,
precompute_rainbow=precompute_rainbow,
)

def generate_rainbow(self):
Expand Down
38 changes: 19 additions & 19 deletions adafruit_led_animation/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,14 @@ def auto_write(self, value):
self._pixels.auto_write = value

@classmethod
def vertical_lines(cls, pixels, width, height, gridmapper):
def vertical_lines(cls, pixel_object, width, height, gridmap):
"""
Generate a PixelMap of horizontal lines on a strip arranged in a grid.

:param pixels: pixel object
:param pixel_object: pixel object
:param width: width of grid
:param height: height of grid
:param gridmapper: a function to map x and y coordinates to the grid
:param gridmap: a function to map x and y coordinates to the grid
see vertical_strip_gridmap and horizontal_strip_gridmap
:return: PixelMap

Expand All @@ -205,22 +205,22 @@ def vertical_lines(cls, pixels, width, height, gridmapper):
PixelMap.vertical_lines(pixels, 32, 8, vertical_strip_gridmap(8))

"""
if len(pixels) < width * height:
if len(pixel_object) < width * height:
raise ValueError("number of pixels is less than width x height")
mapping = []
for x in range(width):
mapping.append([gridmapper(x, y) for y in range(height)])
return cls(pixels, mapping, individual_pixels=True)
mapping.append([gridmap(x, y) for y in range(height)])
return cls(pixel_object, mapping, individual_pixels=True)

@classmethod
def horizontal_lines(cls, pixels, width, height, gridmapper):
def horizontal_lines(cls, pixel_object, width, height, gridmap):
"""
Generate a PixelMap of horizontal lines on a strip arranged in a grid.

:param pixels: pixel object
:param pixel_object: pixel object
:param width: width of grid
:param height: height of grid
:param gridmapper: a function to map x and y coordinates to the grid
:param gridmap: a function to map x and y coordinates to the grid
see vertical_strip_gridmap and horizontal_strip_gridmap
:return: PixelMap

Expand All @@ -231,20 +231,20 @@ def horizontal_lines(cls, pixels, width, height, gridmapper):

PixelMap.horizontal_lines(pixels, 16, 16, vertical_strip_gridmap(16))
"""
if len(pixels) < width * height:
if len(pixel_object) < width * height:
raise ValueError("number of pixels is less than width x height")
mapping = []
for y in range(height):
mapping.append([gridmapper(x, y) for x in range(width)])
return cls(pixels, mapping, individual_pixels=True)
mapping.append([gridmap(x, y) for x in range(width)])
return cls(pixel_object, mapping, individual_pixels=True)


def vertical_strip_gridmap(height, alternating=True):
"""
Returns a function that determines the pixel number for a grid with strips arranged vertically.

:param height: strip height in pixels
:param alternating: strips alternate directions in a zigzag
:param height: grid height in pixels
:param alternating: Whether or not the lines in the grid run alternate directions in a zigzag
:return: mapper(x, y)
"""

Expand All @@ -260,8 +260,8 @@ def horizontal_strip_gridmap(width, alternating=True):
"""
Determines the pixel number for a grid with strips arranged horizontally.

:param width: strip width in pixels
:param alternating: strips alternate directions in a zigzag
:param width: grid width in pixels
:param alternating: Whether or not the lines in the grid run alternate directions in a zigzag
:return: mapper(x, y)
"""

Expand All @@ -277,7 +277,7 @@ class PixelSubset:
"""
PixelSubset lets you work with a subset of a pixel object.

:param strip: An object that implements the Neopixel or Dotstar protocol.
:param pixel_object: An object that implements the Neopixel or Dotstar protocol.
:param int start: Starting pixel number.
:param int end: Ending pixel number.

Expand All @@ -294,8 +294,8 @@ class PixelSubset:
pixels.show()
"""

def __init__(self, strip, start, end):
self._pixels = strip
def __init__(self, pixel_object, start, end):
self._pixels = pixel_object
self._start = start
self._end = end
self.n = self._end - self._start
Expand Down
4 changes: 3 additions & 1 deletion adafruit_led_animation/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ def __init__(
name=None
):
if advance_interval and advance_on_cycle_complete:
raise ValueError("Cannot use both advance_interval and auto_clear")
raise ValueError(
"Cannot use both advance_interval and advance_on_cycle_complete."
)
self._members = members
self._advance_interval = (
advance_interval * NANOS_PER_SECOND if advance_interval else None
Expand Down
50 changes: 50 additions & 0 deletions examples/led_animation_basic_animations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
This example displays the basic animations in sequence, at a five second interval.

For NeoPixel FeatherWing. Update pixel_pin and pixel_num to match your wiring if using
a different form of NeoPixels.

This example may not work on SAMD21 (M0) boards.
"""
import board
import neopixel

from adafruit_led_animation.animation.solid import Solid
from adafruit_led_animation.animation.colorcycle import ColorCycle
from adafruit_led_animation.animation.blink import Blink
from adafruit_led_animation.animation.comet import Comet
from adafruit_led_animation.animation.chase import Chase
from adafruit_led_animation.animation.pulse import Pulse
from adafruit_led_animation.sequence import AnimationSequence
from adafruit_led_animation.color import (
PURPLE,
WHITE,
AMBER,
JADE,
TEAL,
PINK,
MAGENTA,
ORANGE,
)

# Update to match the pin connected to your NeoPixels
pixel_pin = board.D6
# Update to match the number of NeoPixels you have connected
pixel_num = 32

pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.2, auto_write=False)

solid = Solid(pixels, color=PINK)
blink = Blink(pixels, speed=0.5, color=JADE)
colorcycle = ColorCycle(pixels, speed=0.4, colors=[MAGENTA, ORANGE, TEAL])
chase = Chase(pixels, speed=0.1, color=WHITE, size=3, spacing=6)
comet = Comet(pixels, speed=0.01, color=PURPLE, tail_length=10, bounce=True)
pulse = Pulse(pixels, speed=0.1, color=AMBER, period=3)


animations = AnimationSequence(
solid, blink, colorcycle, chase, comet, pulse, advance_interval=5, auto_clear=True,
)

while True:
animations.animate()
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""
This example shows usage of the gridmap helper to easily treat a single strip as a horizontal or
This example shows usage of the PixelMap helper to easily treat a single strip as a horizontal or
vertical grid for animation purposes.

For NeoPixel FeatherWing. Update pixel_pin and pixel_num to match your wiring if using
a different form of NeoPixels.
a different form of NeoPixels. Note that if you are using a number of pixels other than 32, you
will need to alter the PixelMap values as well for this example to work.

This example does not work on SAMD21 (M0) boards.
"""
Expand All @@ -19,8 +20,12 @@
from adafruit_led_animation import helper
from adafruit_led_animation.color import PURPLE, JADE, AMBER

# Update to match the pin connected to your NeoPixels
pixel_pin = board.D6
# Update to match the number of NeoPixels you have connected
pixel_num = 32

pixels = neopixel.NeoPixel(board.D6, 32, brightness=0.2, auto_write=False)
pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.2, auto_write=False)

pixel_wing_vertical = helper.PixelMap.vertical_lines(
pixels, 8, 4, helper.horizontal_strip_gridmap(8, alternating=False)
Expand All @@ -35,7 +40,7 @@
comet_v = Comet(pixel_wing_vertical, speed=0.1, color=AMBER, tail_length=6, bounce=True)
chase_h = Chase(pixel_wing_horizontal, speed=0.1, size=3, spacing=6, color=JADE)
rainbow_chase_v = RainbowChase(
pixel_wing_vertical, speed=0.1, size=3, spacing=2, wheel_step=8
pixel_wing_vertical, speed=0.1, size=3, spacing=2, step=8
)
rainbow_comet_v = RainbowComet(
pixel_wing_vertical, speed=0.1, tail_length=7, bounce=True
Expand Down
42 changes: 42 additions & 0 deletions examples/led_animation_rainbow_animations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
This example uses AnimationsSequence to display multiple animations in sequence, at a five second
interval.

For NeoPixel FeatherWing. Update pixel_pin and pixel_num to match your wiring if using
a different form of NeoPixels.

This example does not work on SAMD21 (M0) boards.
"""
import board
import neopixel

from adafruit_led_animation.animation.rainbow import Rainbow
from adafruit_led_animation.animation.rainbowchase import RainbowChase
from adafruit_led_animation.animation.rainbowcomet import RainbowComet
from adafruit_led_animation.animation.rainbowsparkle import RainbowSparkle
from adafruit_led_animation.sequence import AnimationSequence

# Update to match the pin connected to your NeoPixels
pixel_pin = board.D6
# Update to match the number of NeoPixels you have connected
pixel_num = 32

pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.2, auto_write=False)

rainbow = Rainbow(pixels, speed=0.1, period=2)
rainbow_chase = RainbowChase(pixels, speed=0.1, size=5, spacing=3)
rainbow_comet = RainbowComet(pixels, speed=0.1, tail_length=7, bounce=True)
rainbow_sparkle = RainbowSparkle(pixels, speed=0.1, num_sparkles=15)


animations = AnimationSequence(
rainbow,
rainbow_chase,
rainbow_comet,
rainbow_sparkle,
advance_interval=5,
auto_clear=True,
)

while True:
animations.animate()
14 changes: 4 additions & 10 deletions examples/led_animation_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,29 @@

For NeoPixel FeatherWing. Update pixel_pin and pixel_num to match your wiring if using
a different form of NeoPixels.

This example does not work on SAMD21 (M0) boards.
"""
import board
import neopixel

from adafruit_led_animation.animation.blink import Blink
from adafruit_led_animation.animation.comet import Comet
from adafruit_led_animation.animation.chase import Chase
from adafruit_led_animation.animation.pulse import Pulse
from adafruit_led_animation.sequence import AnimationSequence
from adafruit_led_animation.color import PURPLE, WHITE, AMBER, JADE
from adafruit_led_animation.color import PURPLE, AMBER, JADE

# Update to match the pin connected to your NeoPixels
pixel_pin = board.D6
# Update to match the number of NeoPixels you have connected
pixel_num = 32

pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.2, auto_write=False)
pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.1, auto_write=False)

blink = Blink(pixels, speed=0.5, color=JADE)
comet = Comet(pixels, speed=0.01, color=PURPLE, tail_length=10, bounce=True)
chase = Chase(pixels, speed=0.1, size=3, spacing=6, color=WHITE)
pulse = Pulse(pixels, speed=0.1, period=3, color=AMBER)
chase = Chase(pixels, speed=0.1, size=3, spacing=6, color=AMBER)


animations = AnimationSequence(
comet, blink, chase, pulse, advance_interval=5, auto_clear=True,
)
animations = AnimationSequence(blink, comet, chase, advance_interval=3, auto_clear=True)

while True:
animations.animate()
31 changes: 31 additions & 0 deletions examples/led_animation_sparkle_animations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
This example uses AnimationsSequence to display multiple animations in sequence, at a five second
interval.

For NeoPixel FeatherWing. Update pixel_pin and pixel_num to match your wiring if using
a different form of NeoPixels.
"""
import board
import neopixel

from adafruit_led_animation.animation.sparkle import Sparkle
from adafruit_led_animation.animation.sparklepulse import SparklePulse
from adafruit_led_animation.sequence import AnimationSequence
from adafruit_led_animation.color import AMBER, JADE

# Update to match the pin connected to your NeoPixels
pixel_pin = board.D6
# Update to match the number of NeoPixels you have connected
pixel_num = 32

pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.2, auto_write=False)

sparkle = Sparkle(pixels, speed=0.05, color=AMBER, num_sparkles=10)
sparkle_pulse = SparklePulse(pixels, speed=0.05, period=3, color=JADE)

animations = AnimationSequence(
sparkle, sparkle_pulse, advance_interval=5, auto_clear=True,
)

while True:
animations.animate()