25
25
# User input vars
26
26
mode = 0 # 0=audio, 1=rainbow, 2=larsen_scanner, 3=solid
27
27
user_color = (127 ,0 ,0 )
28
- speed = 6.0 # for larsen scanner
29
28
30
29
# Audio meter vars
31
30
PEAK_COLOR = (100 , 0 , 255 )
@@ -100,18 +99,17 @@ def wheel(wheel_pos):
100
99
b = int (255 - wheel_pos * 3 )
101
100
return (r , g , b )
102
101
103
- def rainbow_cycle (wait ):
102
+ def rainbow_cycle (delay ):
104
103
for j in range (255 ):
105
104
for i in range (NUM_PIXELS ):
106
105
pixel_index = (i * 256 // NUM_PIXELS ) + j
107
106
pixels [i ] = wheel (pixel_index & 255 )
108
107
pixels .show ()
109
- time .sleep (wait )
108
+ time .sleep (delay )
110
109
111
- def audio_meter ():
110
+ def audio_meter (new_peak ):
112
111
mic .record (samples , len (samples ))
113
112
magnitude = normalized_rms (samples )
114
- global peak
115
113
116
114
# Compute scaled logarithmic reading in the range 0 to NUM_PIXELS
117
115
c = log_scale (constrain (magnitude , input_floor , input_ceiling ),
@@ -123,13 +121,14 @@ def audio_meter():
123
121
if i < c :
124
122
pixels [i ] = volume_color (i )
125
123
# Light up the peak pixel and animate it slowly dropping.
126
- if c >= peak :
127
- peak = min (c , NUM_PIXELS - 1 )
128
- elif peak > 0 :
129
- peak = peak - 1
130
- if peak > 0 :
131
- pixels [int (peak )] = PEAK_COLOR
124
+ if c >= new_peak :
125
+ new_peak = min (c , NUM_PIXELS - 1 )
126
+ elif new_peak > 0 :
127
+ new_peak = new_peak - 1
128
+ if new_peak > 0 :
129
+ pixels [int (new_peak )] = PEAK_COLOR
132
130
pixels .show ()
131
+ return new_peak
133
132
134
133
pos = 0 # position
135
134
direction = 1 # direction of "eye"
@@ -140,10 +139,9 @@ def larsen_set(index, color):
140
139
else :
141
140
pixels [index ] = color
142
141
143
- def larsen (wait ):
142
+ def larsen (delay ):
144
143
global pos
145
144
global direction
146
-
147
145
color_dark = (int (user_color [0 ]/ 8 ), int (user_color [1 ]/ 8 ),
148
146
int (user_color [2 ]/ 8 ))
149
147
color_med = (int (user_color [0 ]/ 2 ), int (user_color [1 ]/ 2 ),
@@ -159,7 +157,7 @@ def larsen(wait):
159
157
larsen_set (pos + 2 , color_dark )
160
158
161
159
pixels .write ()
162
- time .sleep (wait )
160
+ time .sleep (delay )
163
161
164
162
# Erase all and draw a new one next time
165
163
for j in range (- 2 , 2 ):
@@ -176,24 +174,21 @@ def larsen(wait):
176
174
pos = NUM_PIXELS - 2
177
175
direction = - direction
178
176
179
- def solid ():
180
- global user_color
181
- pixels .fill (user_color )
177
+ def solid (new_color ):
178
+ pixels .fill (new_color )
182
179
pixels .show ()
183
180
184
181
def map_value (value , in_min , in_max , out_min , out_max ):
185
182
out_range = out_max - out_min
186
183
in_range = in_max - in_min
187
184
return out_min + out_range * ((value - in_min ) / in_range )
188
185
189
- def change_speed (val ):
190
- global speed
191
- new_speed = speed + val
192
- if new_speed > 10.0 :
193
- new_speed = 10.0
194
- elif new_speed < 1.0 :
195
- new_speed = 1.0
196
- speed = new_speed
186
+ speed = 6.0
187
+ wait = 0.097
188
+
189
+ def change_speed (mod , old_speed ):
190
+ new_speed = constrain (old_speed + mod , 1.0 , 10.0 )
191
+ return (new_speed , map_value (new_speed , 10.0 , 0.0 , 0.01 , 0.3 ))
197
192
198
193
while True :
199
194
# While BLE is *not* connected
@@ -214,9 +209,9 @@ def change_speed(val):
214
209
elif isinstance (packet , ButtonPacket ):
215
210
if packet .pressed :
216
211
if packet .button == ButtonPacket .UP :
217
- change_speed (1 )
212
+ speed , wait = change_speed (1 , speed )
218
213
elif packet .button == ButtonPacket .DOWN :
219
- change_speed (- 1 )
214
+ speed , wait = change_speed (- 1 , speed )
220
215
elif packet .button == ButtonPacket .BUTTON_1 :
221
216
mode = 0
222
217
elif packet .button == ButtonPacket .BUTTON_2 :
@@ -228,10 +223,10 @@ def change_speed(val):
228
223
229
224
# Determine animation based on mode
230
225
if mode == 0 :
231
- audio_meter ()
226
+ peak = audio_meter (peak )
232
227
elif mode == 1 :
233
228
rainbow_cycle (0.001 )
234
229
elif mode == 2 :
235
- larsen (map_value ( speed , 10.0 , 0.0 , 0.01 , 0.3 ) )
230
+ larsen (wait )
236
231
elif mode == 3 :
237
- solid ()
232
+ solid (user_color )
0 commit comments