Skip to content

Commit 053f9c6

Browse files
committed
Adding code and audio for adabot
Adding code and Wav files for the Adabot learn guide
1 parent c37fce1 commit 053f9c6

File tree

9 files changed

+148
-0
lines changed

9 files changed

+148
-0
lines changed
59.3 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
40.9 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import os
6+
import random
7+
import board
8+
import audiocore
9+
import audiobusio
10+
import audiomixer
11+
import pwmio
12+
import neopixel
13+
import adafruit_lis3dh
14+
from adafruit_ticks import ticks_ms, ticks_add, ticks_diff
15+
from digitalio import DigitalInOut, Direction, Pull
16+
from adafruit_motor import servo
17+
from adafruit_led_animation.animation.comet import Comet
18+
from adafruit_led_animation.animation.pulse import Pulse
19+
from adafruit_led_animation.color import RED, BLUE, BLACK
20+
21+
# enable external power pin
22+
# provides power to the external components
23+
external_power = DigitalInOut(board.EXTERNAL_POWER)
24+
external_power.direction = Direction.OUTPUT
25+
external_power.value = True
26+
27+
i2c = board.I2C()
28+
int1 = DigitalInOut(board.ACCELEROMETER_INTERRUPT)
29+
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1)
30+
lis3dh.range = adafruit_lis3dh.RANGE_2_G
31+
32+
switch = DigitalInOut(board.EXTERNAL_BUTTON)
33+
switch.direction = Direction.INPUT
34+
switch.pull = Pull.UP
35+
switch_state = False
36+
37+
wavs = []
38+
for filename in os.listdir('/WAVs'):
39+
if filename.lower().endswith('.wav') and not filename.startswith('.'):
40+
wavs.append("/WAVs/"+filename)
41+
42+
audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA)
43+
mixer = audiomixer.Mixer(voice_count=1, sample_rate=22050, channel_count=1,
44+
bits_per_sample=16, samples_signed=True, buffer_size=32768)
45+
46+
mixer.voice[0].level = 1
47+
track_number = 0
48+
wav_filename = wavs[track_number]
49+
# pylint: disable=consider-using-with
50+
wav_file = open(wav_filename, "rb")
51+
wave = audiocore.WaveFile(wav_file)
52+
audio.play(mixer)
53+
mixer.voice[0].play(wave)
54+
55+
def open_audio(num):
56+
n = wavs[num]
57+
f = open(n, "rb")
58+
w = audiocore.WaveFile(f)
59+
return w
60+
61+
PIXEL_PIN = board.EXTERNAL_NEOPIXELS
62+
SERVO_PIN = board.EXTERNAL_SERVO
63+
NUM_PIXELS = 8
64+
ORDER = neopixel.GRB
65+
BRIGHTNESS = 0.6
66+
67+
PWM = pwmio.PWMOut(SERVO_PIN, duty_cycle=2 ** 15, frequency=50)
68+
SERVO = servo.Servo(PWM)
69+
70+
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
71+
pixel.brightness = 1
72+
73+
PIXELS = neopixel.NeoPixel(PIXEL_PIN, NUM_PIXELS, auto_write=False,
74+
pixel_order=ORDER)
75+
LARSON = Comet(PIXELS, bounce=True, speed=0.6/NUM_PIXELS,
76+
tail_length=NUM_PIXELS//2,
77+
color=(RED[0] * BRIGHTNESS,
78+
RED[1] * BRIGHTNESS,
79+
RED[2] * BRIGHTNESS))
80+
pulse = Pulse(PIXELS, speed=0.1, color=BLUE, period=3)
81+
82+
SERVO.angle = POSITION = NEXT_POSITION = 90
83+
MOVING = False
84+
START_TIME = ticks_ms()
85+
DURATION = 1000
86+
87+
adabot_talk = False
88+
89+
clock = ticks_ms()
90+
prop_time = 1000
91+
adabot_nap = False
92+
93+
mixer.voice[0].play(wave)
94+
while mixer.playing:
95+
LARSON.animate()
96+
97+
while True:
98+
if ticks_diff(ticks_ms(), clock) >= prop_time:
99+
x, y, z = [
100+
value / adafruit_lis3dh.STANDARD_GRAVITY for value in lis3dh.acceleration
101+
]
102+
if z > 0.9:
103+
adabot_nap = True
104+
SERVO.angle = POSITION = NEXT_POSITION = 90
105+
LARSON.color=(BLUE[0] * BRIGHTNESS,
106+
BLUE[1] * BRIGHTNESS,
107+
BLUE[2] * BRIGHTNESS)
108+
else:
109+
adabot_nap = False
110+
LARSON.color=(RED[0] * BRIGHTNESS,
111+
RED[1] * BRIGHTNESS,
112+
RED[2] * BRIGHTNESS)
113+
if not adabot_nap:
114+
MOVING = not MOVING
115+
if MOVING:
116+
POSITION = NEXT_POSITION
117+
while abs(POSITION - NEXT_POSITION) < 10:
118+
NEXT_POSITION = random.uniform(0, 180)
119+
DURATION = 0.2 + 0.6 * abs(POSITION - NEXT_POSITION) / 180
120+
else:
121+
SERVO.angle = NEXT_POSITION
122+
DURATION = random.uniform(0.5, 2.5)
123+
clock = ticks_add(clock, prop_time)
124+
if MOVING:
125+
FRACTION = 0.0 / DURATION
126+
FRACTION = (3 * FRACTION ** 2) - (2 * FRACTION ** 3)
127+
SERVO.angle = POSITION + (NEXT_POSITION - POSITION) * FRACTION
128+
if adabot_talk:
129+
wave = open_audio(random.randint(1, 7))
130+
mixer.voice[0].play(wave)
131+
while mixer.playing:
132+
LARSON.animate()
133+
if not mixer.playing:
134+
adabot_talk = False
135+
PIXELS.fill(BLACK)
136+
PIXELS.show()
137+
elif adabot_nap:
138+
LARSON.animate()
139+
else:
140+
pulse.animate()
141+
142+
if not switch.value and switch_state is False:
143+
PIXELS.fill(BLACK)
144+
PIXELS.show()
145+
adabot_talk = True
146+
switch_state = True
147+
if switch.value and switch_state is True:
148+
switch_state = False

0 commit comments

Comments
 (0)