Skip to content

Add push button examples for pausing, resuming, and cycling animations #94

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
Feb 11, 2022
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
44 changes: 44 additions & 0 deletions examples/led_animation_cycle_animations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# SPDX-FileCopyrightText: 2021 Alec Delaney
# SPDX-License-Identifier: MIT

"""
This example uses AnimationsSequence along with a connected push button to cycle through
two animations

For NeoPixel FeatherWing. Update pixel_pin and pixel_num to match your wiring if using
a different form of NeoPixels.
"""
import time
import board
import neopixel
from digitalio import DigitalInOut, Direction, Pull

from adafruit_led_animation.animation.solid import Solid
from adafruit_led_animation.sequence import AnimationSequence
from adafruit_led_animation.color import RED, BLUE

# 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

# Update to matchpin connected to button that connect logic high when pushed
button_pin = board.D3

pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.5, auto_write=False)
button = DigitalInOut(button_pin)
button.direction = Direction.INPUT
button.pull = Pull.UP

solid_blue = Solid(pixels, color=BLUE)
solid_red = Solid(pixels, color=RED)
animation_sequence = AnimationSequence(solid_blue, solid_red, auto_clear=True)

while True:
animation_sequence.animate()

# Pressing the button pauses the animation permanently
if not button.value:
animation_sequence.next()
while button.value:
time.sleep(0.1) # Used for button debouncing
38 changes: 38 additions & 0 deletions examples/led_animation_freeze_animation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# SPDX-FileCopyrightText: 2021 Alec Delaney
# SPDX-License-Identifier: MIT

"""
This example uses Pulse animation along with a connected push button to freeze
the animation permanently when pressed

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 digitalio import DigitalInOut, Direction, Pull

from adafruit_led_animation.animation.pulse import Pulse
from adafruit_led_animation.color import RED

# 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

# Update to matchpin connected to button that connect logic high when pushed
button_pin = board.D3

pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.5, auto_write=False)
button = DigitalInOut(button_pin)
button.direction = Direction.INPUT
button.pull = Pull.UP

pulse_animation = Pulse(pixels, speed=0.1, period=1, color=RED)

while True:
pulse_animation.animate()

# Pressing the button pauses the animation permanently
if not button.value:
pulse_animation.freeze()
40 changes: 40 additions & 0 deletions examples/led_animation_resume_animation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# SPDX-FileCopyrightText: 2021 Alec Delaney
# SPDX-License-Identifier: MIT

"""
This example uses Pulse animation along with a connected push button to start
the animation when pressed

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 digitalio import DigitalInOut, Direction, Pull

from adafruit_led_animation.animation.pulse import Pulse
from adafruit_led_animation.color import RED

# 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

# Update to matchpin connected to button that connect logic high when pushed
button_pin = board.D3

pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.5, auto_write=False)
button = DigitalInOut(button_pin)
button.direction = Direction.INPUT
button.pull = Pull.UP

# Create the animation and freeze it afterwards
pulse_animation = Pulse(pixels, speed=0.1, period=1, color=RED)
pulse_animation.freeze()

while True:
pulse_animation.animate()

# Pressing the button resumes (or in this case starts) the animation permanently
if not button.value:
pulse_animation.resume()