|
# This script requires a Raspberry Pi 2, 3 or Zero. Circuit Python must |
|
# be installed and it is strongly recommended that you use the latest |
|
# release of Raspbian. |
|
|
|
import os |
|
import board |
|
import digitalio |
|
import pygame |
|
from tasko import Loop |
|
|
|
dirname = os.path.dirname(os.path.abspath(__file__)) |
|
|
|
button_pins = ['D23', 'D24', 'D25', 'D16', 'D26'] |
|
led_pins = ['D17', 'D27', 'D22', 'D5', 'D6'] |
|
sounds = ['walk', 'play', 'toby', 'potty', 'food'] |
|
buttons = [{} for i in range(len(button_pins))] |
|
|
|
for i, button in enumerate(buttons): |
|
pin = digitalio.DigitalInOut(getattr(board, button_pins[i])) |
|
pin.direction = digitalio.Direction.INPUT |
|
pin.pull = digitalio.Pull.UP |
|
button['button'] = pin |
|
|
|
pin = digitalio.DigitalInOut(getattr(board, led_pins[i])) |
|
pin.direction = digitalio.Direction.OUTPUT |
|
button['led'] = pin |
|
|
|
button['sound'] = sounds[i] |
|
|
|
pygame.mixer.init() |
|
pygame.mixer.music.load(dirname + "/hello.mp3") |
|
speakerVolume = "1" |
|
pygame.mixer.music.set_volume(float(speakerVolume)) |
|
pygame.mixer.music.play() |
|
|
|
async def blink(led): |
|
for n in range (5): |
|
led.value = True |
|
await loop.sleep(0.5) |
|
led.value = False |
|
await loop.sleep(0.5) |
|
|
|
def play(button): |
|
filename = button['sound'] + '.mp3' |
|
led = button['led'] |
|
|
|
pygame.mixer.music.load(os.path.join(dirname, filename)) |
|
pygame.mixer.music.play() |
|
loop.add_task(blink(led)) |
|
|
|
async def read_sensors(): |
|
for button in buttons: |
|
if not button['button'].value: |
|
play(button) |
|
|
|
loop = Loop() |
|
loop.schedule(hz=100, coroutine_function=read_sensors) |
|
loop.run() |