-
Notifications
You must be signed in to change notification settings - Fork 779
/
Copy pathcode.py
48 lines (40 loc) · 1.3 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import os
import random
import time
import board
import audioio
import audiocore
from adafruit_crickit import crickit
# Hal button-and-voice example
# Button connected to Signal pin #1 & ground:
BUTTON = crickit.SIGNAL1
crickit.seesaw.pin_mode(BUTTON, crickit.seesaw.INPUT_PULLUP)
# LED connected to 5V & Drive pin #1:
LED = crickit.drive_1
LED.duty_cycle = 65535
# Find all Wave files in CIRCUITPY storage:
WAVEFILES = [file for file in os.listdir("/")
if (file.endswith(".wav") and not file.startswith("._"))]
print("Audio files found:", WAVEFILES)
# Audio playback object:
AUDIO = audioio.AudioOut(board.A0)
# Function to play a wave file in its entirety:
def play_file(wavfile):
print("Playing", wavfile)
with open(wavfile, "rb") as f:
wav = audiocore.WaveFile(f)
AUDIO.play(wav)
while AUDIO.playing:
LED.duty_cycle = random.randint(5000, 30000)
time.sleep(0.1)
LED.duty_cycle = 65535
while True:
if not crickit.seesaw.digital_read(BUTTON):
# Play a random wave file
play_file(random.choice(WAVEFILES))
# Then wait for button to be released
while not crickit.seesaw.digital_read(BUTTON):
continue