3
3
# SPDX-License-Identifier: MIT
4
4
5
5
import os
6
- import time
7
6
import random
8
7
import board
9
8
import audiocore
12
11
import pwmio
13
12
import neopixel
14
13
import adafruit_lis3dh
15
- from random import randint
16
14
from adafruit_ticks import ticks_ms , ticks_add , ticks_diff
17
15
from digitalio import DigitalInOut , Direction , Pull
18
16
from adafruit_motor import servo
32
30
lis3dh = adafruit_lis3dh .LIS3DH_I2C (i2c , int1 = int1 )
33
31
lis3dh .range = adafruit_lis3dh .RANGE_2_G
34
32
35
- # Setup button switch
36
33
switch = DigitalInOut (board .EXTERNAL_BUTTON )
37
34
switch .direction = Direction .INPUT
38
35
switch .pull = Pull .UP
56
53
mixer .voice [0 ].play (wave )
57
54
58
55
def open_audio (num ):
59
- wav_filename = wavs [num ]
60
- wav_file = open (wav_filename , "rb" )
61
- wave = audiocore .WaveFile (wav_file )
62
- return wave
56
+ n = wavs [num ]
57
+ f = open (n , "rb" )
58
+ w = audiocore .WaveFile (f )
59
+ return w
63
60
64
61
PIXEL_PIN = board .EXTERNAL_NEOPIXELS
65
62
SERVO_PIN = board .EXTERNAL_SERVO
66
63
NUM_PIXELS = 8
67
64
ORDER = neopixel .GRB
68
65
BRIGHTNESS = 0.3
69
66
70
- # Initialize servo
71
67
PWM = pwmio .PWMOut (SERVO_PIN , duty_cycle = 2 ** 15 , frequency = 50 )
72
68
SERVO = servo .Servo (PWM )
73
69
74
- # Initialize NeoPixels and animations
75
70
pixel = neopixel .NeoPixel (board .NEOPIXEL , 1 )
76
71
pixel .brightness = 1
77
72
78
73
PIXELS = neopixel .NeoPixel (PIXEL_PIN , NUM_PIXELS , auto_write = False ,
79
74
pixel_order = ORDER )
80
- LARSON = Comet (PIXELS , bounce = True , speed = 0.07 ,
75
+ LARSON = Comet (PIXELS , bounce = True , speed = 0.7 ,
81
76
tail_length = NUM_PIXELS // 2 ,
82
- color = (BLUE [0 ] * BRIGHTNESS , # This is a little faster than
83
- BLUE [1 ] * BRIGHTNESS , # using the NeoPixel brightness
84
- BLUE [2 ] * BRIGHTNESS )) # setting.
77
+ color = (BLUE [0 ] * BRIGHTNESS ,
78
+ BLUE [1 ] * BRIGHTNESS ,
79
+ BLUE [2 ] * BRIGHTNESS ))
85
80
pulse = Pulse (PIXELS , speed = 0.05 ,
86
- color = (BLUE [0 ] * BRIGHTNESS , # This is a little faster than
87
- BLUE [1 ] * BRIGHTNESS , # using the NeoPixel brightness
88
- BLUE [2 ] * BRIGHTNESS ), period = 3 )
81
+ color = (BLUE [0 ] * BRIGHTNESS ,
82
+ BLUE [1 ] * BRIGHTNESS ,
83
+ BLUE [2 ] * BRIGHTNESS ), period = 3 )
89
84
sparkle = Sparkle (PIXELS , speed = 0.2 ,
90
- color = (RED [0 ] * BRIGHTNESS , # This is a little faster than
91
- RED [1 ] * BRIGHTNESS , # using the NeoPixel brightness
92
- RED [2 ] * BRIGHTNESS ), num_sparkles = 10 )
85
+ color = (RED [0 ] * BRIGHTNESS ,
86
+ RED [1 ] * BRIGHTNESS ,
87
+ RED [2 ] * BRIGHTNESS ), num_sparkles = 10 )
93
88
94
89
SERVO .angle = POSITION = NEXT_POSITION = 90
95
- MOVING = False # Initial state = paused
96
- START_TIME = ticks_ms () # Initial time
97
- DURATION = 1000 # Hold initial position for 1 sec
90
+ MOVING = False
91
+ START_TIME = ticks_ms ()
92
+ DURATION = 1000
98
93
99
94
adabot_talk = False
100
95
@@ -106,43 +101,37 @@ def open_audio(num):
106
101
while mixer .playing :
107
102
LARSON .animate ()
108
103
109
- while True : # Loop forever...
104
+ while True :
110
105
if ticks_diff (ticks_ms (), clock ) >= prop_time :
111
106
x , y , z = [
112
107
value / adafruit_lis3dh .STANDARD_GRAVITY for value in lis3dh .acceleration
113
108
]
114
109
if z > 0.9 :
115
110
adabot_nap = True
116
111
SERVO .angle = POSITION = NEXT_POSITION = 90
117
- pulse .color = (BLUE [0 ] * BRIGHTNESS , # This is a little faster than
118
- BLUE [1 ] * BRIGHTNESS , # using the NeoPixel brightness
119
- BLUE [2 ] * BRIGHTNESS )
120
112
else :
121
113
adabot_nap = False
122
- LARSON .color = (BLUE [0 ] * BRIGHTNESS , # This is a little faster than
123
- BLUE [1 ] * BRIGHTNESS , # using the NeoPixel brightness
124
- BLUE [2 ] * BRIGHTNESS )
125
114
if not adabot_nap :
126
115
MOVING = not MOVING
127
- if MOVING : # Switching from paused to moving
116
+ if MOVING :
128
117
POSITION = NEXT_POSITION
129
- while abs (POSITION - NEXT_POSITION ) < 10 : # Min +/- 10 degrees
130
- NEXT_POSITION = random .uniform (0 , 180 ) # Try, try again
118
+ while abs (POSITION - NEXT_POSITION ) < 10 :
119
+ NEXT_POSITION = random .uniform (0 , 180 )
131
120
DURATION = 0.2 + 0.6 * abs (POSITION - NEXT_POSITION ) / 180
132
- else : # Switching from moving to paused
133
- SERVO .angle = NEXT_POSITION # Move to end of sweep
134
- DURATION = random .uniform (0.5 , 2.5 ) # Pause time
121
+ else :
122
+ SERVO .angle = NEXT_POSITION
123
+ DURATION = random .uniform (0.5 , 2.5 )
135
124
clock = ticks_add (clock , prop_time )
136
125
if MOVING :
137
- FRACTION = 0.0 / DURATION # Linear 0 to 1
138
- FRACTION = (3 * FRACTION ** 2 ) - (2 * FRACTION ** 3 ) # Ease in/out
126
+ FRACTION = 0.0 / DURATION
127
+ FRACTION = (3 * FRACTION ** 2 ) - (2 * FRACTION ** 3 )
139
128
SERVO .angle = POSITION + (NEXT_POSITION - POSITION ) * FRACTION
140
129
if adabot_talk :
141
- wave = open_audio (randint (1 , 17 ))
130
+ wave = open_audio (random . randint (1 , 7 ))
142
131
mixer .voice [0 ].play (wave )
143
132
while mixer .playing :
144
133
sparkle .animate ()
145
- else :
134
+ if not mixer . playing :
146
135
adabot_talk = False
147
136
PIXELS .fill (BLACK )
148
137
PIXELS .show ()
0 commit comments