Last active
October 27, 2019 08:15
-
-
Save nbogie/0731380a4b5be26cbed45133d59b38cc to your computer and use it in GitHub Desktop.
micro:bit play raw sound file when radio message received
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
from microbit import display, sleep, button_a, button_b | |
import audio, radio, os | |
def frames_from_file(sndfile, frame): | |
while(sndfile.readinto(frame, 32) > 0): | |
yield frame | |
def play_snd(fname): | |
file_size = os.size(fname) | |
frame = audio.AudioFrame() | |
with open(fname, 'rb') as sndfile: | |
audio.play(frames_from_file(sndfile, frame),wait=False) | |
sleep(file_size / 7.8125) | |
audio.stop() | |
del frame | |
def roar(): | |
play_snd('clip.raw') | |
radio.config(group=138) | |
radio.on() | |
display.scroll('138') | |
display.show(')') | |
while True: | |
incoming = radio.receive() | |
if incoming is not None: | |
display.show("!") | |
roar() | |
roar() | |
if button_a.is_pressed(): | |
display.show('p') | |
roar() | |
if button_b.is_pressed(): | |
radio.send("roar") | |
display.show(">") | |
sleep(500) | |
sleep(200) |
modded to trigger on ANY radio message on that group, to make it easier to send from a makecode-programmed micro:bit, as those messages have extra headers on them.
See
https://support.microbit.org/support/solutions/articles/19000053168-receiving-radio-data-from-the-javascript-blocks-editor-within-python
modded to use a different radio group rather than group zero, to make it less likely that another nearby project will trigger it (given it now currently doesn't care WHAT message it gets)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
simpler one is here https://gist.github.com/nbogie/4b2e68e1e4217862b358f232b9c38ece