Last active
March 30, 2020 15:59
-
-
Save kwalseth/2fcbd70eee546972ba84e71b99f4e1eb to your computer and use it in GitHub Desktop.
emergency sounds/lights
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", ""] | |
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 | |
audio = AudioOut(board.SPEAKER) | |
wave = WaveFile(open(audiofiles[1], "rb")) | |
time_started = 0 | |
while True: | |
now = time.monotonic() | |
if touch.value: | |
if not audio.playing: | |
audio.play(wave, loop=True) | |
fireeffect() | |
time_started = now | |
if now - time_started < 5: | |
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