Skip to content

Instantly share code, notes, and snippets.

@nbogie
Last active October 27, 2019 08:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbogie/0731380a4b5be26cbed45133d59b38cc to your computer and use it in GitHub Desktop.
Save nbogie/0731380a4b5be26cbed45133d59b38cc to your computer and use it in GitHub Desktop.
micro:bit play raw sound file when radio message received
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)
@nbogie
Copy link
Author

nbogie commented Apr 25, 2018

@nbogie
Copy link
Author

nbogie commented Apr 27, 2018

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

@nbogie
Copy link
Author

nbogie commented Apr 27, 2018

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