Created
January 29, 2024 23:59
-
-
Save todbot/e31f46e5036fbab2709be3d603d49380 to your computer and use it in GitHub Desktop.
Neopixel LED animations + playing audio on Pico RP2040
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# led animations + audio on RP2040 Pico | |
# | |
import os, time, random | |
import board, keypad | |
import audiomixer, audiocore | |
import audiopwmio | |
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 | |
leds = neopixel.NeoPixel(board.GP6, 64, brightness=0.1, auto_write=False) | |
keys = keypad.Keys( (board.GP12,), value_when_pressed=False, pull=True) | |
audio = audiopwmio.PWMAudioOut(board.GP21) # RP2040 Pico example | |
mixer = audiomixer.Mixer(voice_count=1, sample_rate=22050, channel_count=1, | |
bits_per_sample=16, samples_signed=True, buffer_size=4096) | |
audio.play(mixer) # attach mixer to audio playback | |
rainbow = Rainbow(leds, speed=0.1, period=4) | |
rainbow_chase = RainbowChase(leds, speed=0.1, size=4, spacing=3) | |
rainbow_comet = RainbowComet(leds, speed=0.1, tail_length=12, bounce=True) | |
rainbow_sparkle = RainbowSparkle(leds, speed=0.1, num_sparkles=15) | |
animations = AnimationSequence( | |
rainbow, | |
rainbow_chase, | |
rainbow_comet, | |
rainbow_sparkle, | |
advance_interval=3, | |
auto_clear=False, | |
random_order=True) | |
dir_path = "/wav" | |
wav_files = [] | |
wavs = [] | |
wav_index = 0 | |
for path in os.listdir(dir_path): | |
if path.endswith('.wav'): | |
fullpath = dir_path + "/" + path | |
print('-', fullpath) | |
wav_files.append(fullpath) | |
wavs.append( audiocore.WaveFile(open(fullpath, "rb")) ) | |
while True: | |
animations.animate() | |
key = keys.events.get() | |
if key: | |
print("key", key) | |
if key.pressed: | |
print("playing WAV:", wav_files[wav_index]) | |
mixer.voice[0].play( wavs[ wav_index] ) | |
wav_index = (wav_index + 1) % len(wav_files) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want the LEDs to turn on and animate only while audio is playing, make the
while
loop look something like this: