|
1 | 1 | import os
|
2 | 2 | import time
|
3 | 3 | import random
|
4 |
| -import audioio |
5 |
| -from digitalio import DigitalInOut, Direction |
6 |
| -from adafruit_seesaw.seesaw import Seesaw |
7 |
| -from adafruit_seesaw.pwmout import PWMOut |
8 |
| -from adafruit_motor import servo |
9 |
| -from busio import I2C |
10 | 4 | import board
|
| 5 | +import audioio |
| 6 | +from adafruit_crickit import crickit |
11 | 7 |
|
| 8 | +# Sparky automaton |
| 9 | + |
| 10 | +# Find all Wave files on the storage |
12 | 11 | wavefiles = [file for file in os.listdir("/")
|
13 | 12 | if (file.endswith(".wav") and not file.startswith("._"))]
|
14 | 13 | print("Audio files found: ", wavefiles)
|
15 | 14 |
|
16 |
| -# Create seesaw object |
17 |
| -i2c = I2C(board.SCL, board.SDA) |
18 |
| -seesaw = Seesaw(i2c) |
19 |
| - |
20 |
| -led = DigitalInOut(board.D13) |
21 |
| -led.direction = Direction.OUTPUT |
| 15 | +# mouth servo |
| 16 | +mouth_servo = crickit.servo_1 |
| 17 | +# TowerPro servos like 500/2500 pulsewidths |
| 18 | +mouth_servo.set_pulse_width_range(min_pulse=500, max_pulse=2500) |
22 | 19 |
|
23 | 20 | # Servo angles
|
24 | 21 | MOUTH_START = 100
|
25 | 22 | MOUTH_END = 90
|
26 | 23 |
|
27 |
| -# 17 is labeled SERVO 1 on CRICKIT |
28 |
| -pwm = PWMOut(seesaw, 17) |
29 |
| -# must be 50 cannot change |
30 |
| -pwm.frequency = 50 |
31 |
| -my_servo = servo.Servo(pwm) |
32 |
| -# Starting servo locations |
33 |
| -my_servo.angle = MOUTH_START |
| 24 | +# Starting servo location |
| 25 | +mouth_servo.angle = MOUTH_START |
34 | 26 |
|
35 | 27 | # Audio playback object and helper to play a full file
|
36 | 28 | a = audioio.AudioOut(board.A0)
|
37 | 29 |
|
| 30 | +# Play a wave file and move the mouth while its playing! |
38 | 31 | def play_file(wavfile):
|
39 | 32 | print("Playing", wavfile)
|
40 | 33 | with open(wavfile, "rb") as f:
|
41 | 34 | wav = audioio.WaveFile(f)
|
42 | 35 | a.play(wav)
|
43 |
| - while a.playing: |
44 |
| - my_servo.angle = MOUTH_END |
| 36 | + while a.playing: # turn servos, motors, etc. during playback |
| 37 | + mouth_servo.angle = MOUTH_END |
45 | 38 | time.sleep(0.15)
|
46 |
| - my_servo.angle = MOUTH_START |
| 39 | + mouth_servo.angle = MOUTH_START |
47 | 40 | time.sleep(0.15)
|
48 | 41 |
|
49 | 42 | while True:
|
| 43 | + # Play a random quip |
50 | 44 | play_file(random.choice(wavefiles))
|
| 45 | + # then hang out for a few seconds |
51 | 46 | time.sleep(3)
|
0 commit comments