Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Adafruit_Learning_System_Guides/HAL-9000/code.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
48 lines (40 sloc)
1.3 KB
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
# 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 |