Last active
March 27, 2020 18:38
-
-
Save kwalseth/17ed484f0d572d44bc29f8bdbe98489a to your computer and use it in GitHub Desktop.
firetruck bed
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
import random | |
import time | |
import board | |
import neopixel | |
import touchio | |
import digitalio | |
from audioio import WaveFile | |
from audioio import AudioOut | |
touch_pad = board.A3 | |
touch = touchio.TouchIn(touch_pad) | |
spkrenable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) | |
spkrenable.direction = digitalio.Direction.OUTPUT | |
spkrenable.value = True | |
audiofiles = ["rimshot.wav", "siren.wav", "firetruck.wav"] | |
def fireeffect(): | |
pixel1[random.randrange(45)] = (255, 0, 0) | |
pixel1[random.randrange(45)] = color2 | |
pixel1[random.randrange(45)] = color2 | |
pixel1[random.randrange(45)] = color2 | |
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.8) | |
pixel1 = neopixel.NeoPixel(board.A1, 45, brightness=0.8) | |
color2 = (50, 0, 0) | |
fire = 0 | |
def play_file(filename): | |
wave_file = open(filename, "rb") | |
with WaveFile(wave_file) as wave: | |
with AudioOut(board.SPEAKER) as audio: | |
audio.play(wave) | |
while audio.playing: | |
pass | |
wave = WaveFile(open(audiofiles[1], "rb")) | |
audio = AudioOut(board.SPEAKER) | |
time_started = 0 | |
while True: | |
now = time.monotonic() | |
if touch.value: | |
if not audio.playing: | |
audio.play(wave, loop=True) | |
time_started = now | |
if now - time_started < 12: | |
fireeffect() | |
elif audio.playing: | |
audio.stop() | |
pixel1.fill((0, 0, 0)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment