Last active
December 22, 2023 06:05
-
-
Save dglaude/0ad2d2bb37798f4c83ba471b3aac7ea4 to your computer and use it in GitHub Desktop.
Playing an wav audio file on button press with "Feather RP2040 Prop-Maker" (update Debounce)
This file contains hidden or 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
import supervisor | |
supervisor.set_next_code_file(None, reload_on_error=True) | |
import board | |
import audiocore | |
import audiobusio | |
import audiomixer | |
from digitalio import DigitalInOut, Direction, Pull | |
from adafruit_debouncer import Debouncer | |
# enable external power pin to provides power to the external components | |
external_power = DigitalInOut(board.EXTERNAL_POWER) | |
external_power.direction = Direction.OUTPUT | |
external_power.value = True | |
# i2s playback | |
level_play = 0.5 | |
wave_file = open("Merry-Xmas-Piano.wav", "rb") | |
wave = audiocore.WaveFile(wave_file) | |
audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA) | |
mixer = audiomixer.Mixer(voice_count=1, sample_rate=22050, channel_count=1, bits_per_sample=16, samples_signed=True) | |
mixer.voice[0].level = 0 | |
audio.play(mixer) | |
# servo control | |
play_ringtone = False | |
# boot button | |
switch = DigitalInOut(board.BUTTON) | |
switch.direction = Direction.INPUT | |
switch.pull = Pull.UP | |
# external button | |
switch_ext_ = DigitalInOut(board.EXTERNAL_BUTTON) | |
switch_ext_.direction = Direction.INPUT | |
switch_ext_.pull = Pull.UP | |
# Make the external switch a debounced switch with minimum 50 ms | |
switch_ext = Debouncer(switch_ext_, interval=0.05) | |
switch_state = False | |
while True: | |
if play_ringtone: | |
if not(mixer.voice[0].playing): | |
play_ringtone = False | |
mixer.voice[0].level = 0.0 | |
print("Stopped playing") | |
else: | |
if (not switch.value or not switch_ext.value) and switch_state is False: | |
play_ringtone = True | |
switch_state = True | |
mixer.voice[0].play(wave, loop=False) | |
mixer.voice[0].level = level_play | |
print("Starting playing") | |
if (switch.value or switch_ext.value) and switch_state is True: | |
switch_state = False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On multiple occasion, the Xmas sound started, so I added a 50ms debounce on the external button.
I don't know yet how effective it is, should it be shorter or longer???