update crickit

This commit is contained in:
ladyada 2018-07-08 21:22:27 -04:00
parent 6b5c5d431d
commit 942f6cc118

View file

@ -1,51 +1,46 @@
import os import os
import time import time
import random import random
import audioio
from digitalio import DigitalInOut, Direction
from adafruit_seesaw.seesaw import Seesaw
from adafruit_seesaw.pwmout import PWMOut
from adafruit_motor import servo
from busio import I2C
import board import board
import audioio
from adafruit_crickit import crickit
# Sparky automaton
# Find all Wave files on the storage
wavefiles = [file for file in os.listdir("/") wavefiles = [file for file in os.listdir("/")
if (file.endswith(".wav") and not file.startswith("._"))] if (file.endswith(".wav") and not file.startswith("._"))]
print("Audio files found: ", wavefiles) print("Audio files found: ", wavefiles)
# Create seesaw object # mouth servo
i2c = I2C(board.SCL, board.SDA) mouth_servo = crickit.servo_1
seesaw = Seesaw(i2c) # TowerPro servos like 500/2500 pulsewidths
mouth_servo.set_pulse_width_range(min_pulse=500, max_pulse=2500)
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT
# Servo angles # Servo angles
MOUTH_START = 100 MOUTH_START = 100
MOUTH_END = 90 MOUTH_END = 90
# 17 is labeled SERVO 1 on CRICKIT # Starting servo location
pwm = PWMOut(seesaw, 17) mouth_servo.angle = MOUTH_START
# must be 50 cannot change
pwm.frequency = 50
my_servo = servo.Servo(pwm)
# Starting servo locations
my_servo.angle = MOUTH_START
# Audio playback object and helper to play a full file # Audio playback object and helper to play a full file
a = audioio.AudioOut(board.A0) a = audioio.AudioOut(board.A0)
# Play a wave file and move the mouth while its playing!
def play_file(wavfile): def play_file(wavfile):
print("Playing", wavfile) print("Playing", wavfile)
with open(wavfile, "rb") as f: with open(wavfile, "rb") as f:
wav = audioio.WaveFile(f) wav = audioio.WaveFile(f)
a.play(wav) a.play(wav)
while a.playing: while a.playing: # turn servos, motors, etc. during playback
my_servo.angle = MOUTH_END mouth_servo.angle = MOUTH_END
time.sleep(0.15) time.sleep(0.15)
my_servo.angle = MOUTH_START mouth_servo.angle = MOUTH_START
time.sleep(0.15) time.sleep(0.15)
while True: while True:
# Play a random quip
play_file(random.choice(wavefiles)) play_file(random.choice(wavefiles))
# then hang out for a few seconds
time.sleep(3) time.sleep(3)