File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed
Crickits/Crickit_Sparky_Automaton Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ # SPDX-FileCopyrightText: 2018 Limor Fried for Adafruit Industries
2+ #
3+ # SPDX-License-Identifier: MIT
4+
5+ import os
6+ import time
7+ import random
8+ import board
9+ import audioio
10+ import audiocore
11+ from adafruit_crickit import crickit
12+
13+ # Sparky automaton
14+
15+ # Find all Wave files on the storage
16+ wavefiles = [file for file in os .listdir ("/" )
17+ if (file .endswith (".wav" ) and not file .startswith ("._" ))]
18+ print ("Audio files found: " , wavefiles )
19+
20+ # mouth servo
21+ mouth_servo = crickit .servo_1
22+ # TowerPro servos like 500/2500 pulsewidths
23+ mouth_servo .set_pulse_width_range (min_pulse = 500 , max_pulse = 2500 )
24+
25+ # Servo angles
26+ MOUTH_START = 100
27+ MOUTH_END = 90
28+
29+ # Starting servo location
30+ mouth_servo .angle = MOUTH_START
31+
32+ # Audio playback object and helper to play a full file
33+ a = audioio .AudioOut (board .A0 )
34+
35+ # Play a wave file and move the mouth while its playing!
36+ def play_file (wavfile ):
37+ print ("Playing" , wavfile )
38+ with open (wavfile , "rb" ) as f :
39+ wav = audiocore .WaveFile (f )
40+ a .play (wav )
41+ while a .playing : # turn servos, motors, etc. during playback
42+ mouth_servo .angle = MOUTH_END
43+ time .sleep (0.15 )
44+ mouth_servo .angle = MOUTH_START
45+ time .sleep (0.15 )
46+
47+ while True :
48+ # Play a random quip
49+ play_file (random .choice (wavefiles ))
50+ # then hang out for a few seconds
51+ time .sleep (3 )
You can’t perform that action at this time.
0 commit comments