24
24
import audiobusio
25
25
import board
26
26
import neopixel
27
- from ulab .scipy .signal import spectrogram
27
+
28
+ try :
29
+ from ulab .utils import spectrogram
30
+ except ImportError :
31
+ from ulab .scipy .signal import spectrogram
28
32
from ulab import numpy as np
29
33
from rainbowio import colorwheel
30
34
from adafruit_lsm6ds import lsm6ds33
46
50
WHITE ,
47
51
)
48
52
49
- MAX_BRIGHTNESS = 0.3 # set max brightness for sound reactive mode
50
- NORMAL_BRIGHTNESS = 0.1 # set brightness for non-reactive mode
51
- VOLUME_CALIBRATOR = 50 # multiplier for brightness mapping
52
- ROCKSTAR_TILT_THRESHOLD = 200 # shake threshold
53
- SOUND_THRESHOLD = 430000 # main strum or pluck threshold
53
+ MAX_BRIGHTNESS = 0.3 # set max brightness for sound reactive mode
54
+ NORMAL_BRIGHTNESS = 0.1 # set brightness for non-reactive mode
55
+ VOLUME_CALIBRATOR = 50 # multiplier for brightness mapping
56
+ ROCKSTAR_TILT_THRESHOLD = 200 # shake threshold
57
+ SOUND_THRESHOLD = 430000 # main strum or pluck threshold
54
58
55
59
# Set to the length in seconds for the animations
56
60
POWER_ON_DURATION = 1.3
72
76
pixels .show ()
73
77
74
78
75
- #PIXEL MAPS: Used for reordering pixels so the animations can run in different configurations.
76
- #My LED strips inside the neck are accidentally swapped left-right,
77
- #so these maps also correct for that
78
-
79
+ # PIXEL MAPS: Used for reordering pixels so the animations can run in different configurations.
80
+ # My LED strips inside the neck are accidentally swapped left-right,
81
+ # so these maps also correct for that
79
82
83
+ # fmt: off
80
84
#Bottom up along both sides at once
81
85
pixel_map_reverse = PixelMap (pixels , [
82
86
0 , 103 , 1 , 102 , 2 , 101 , 3 , 100 , 4 , 99 , 5 , 98 , 6 , 97 , 7 , 96 , 8 , 95 , 9 , 94 , 10 ,
122
126
83 , 22 , 81 , 24 , 79 , 26 , 77 , 29 , 74 , 31 , 72 , 33 , 70 , 35 , 68 , 37 , 66 , 39 , 64 , 41 ,
123
127
62 , 43 , 60 , 45 , 58 , 47 , 56 , 49 , 54 , 51 , 52 ,
124
128
], individual_pixels = True )
129
+ # fmt: on
125
130
126
131
pixel_map = [
127
132
pixel_map_reverse ,
131
136
pixel_map_skip ,
132
137
]
133
138
134
- #Set up accelerometer & mic
139
+ # Set up accelerometer & mic
135
140
sensor = lsm6ds33 .LSM6DS33 (i2c )
136
- mic = audiobusio .PDMIn (board .MICROPHONE_CLOCK ,
137
- board .MICROPHONE_DATA ,
138
- sample_rate = 16000 ,
139
- bit_depth = 16 )
141
+ mic = audiobusio .PDMIn (
142
+ board .MICROPHONE_CLOCK , board .MICROPHONE_DATA , sample_rate = 16000 , bit_depth = 16
143
+ )
140
144
141
145
NUM_SAMPLES = 256
142
- samples_bit = array .array ('H' , [0 ] * (NUM_SAMPLES + 3 ))
146
+ samples_bit = array .array ("H" , [0 ] * (NUM_SAMPLES + 3 ))
147
+
143
148
144
149
def power_on (duration ):
145
150
"""
@@ -152,6 +157,7 @@ def power_on(duration):
152
157
break # Stop animating
153
158
powerup .animate ()
154
159
160
+
155
161
def rockstar_tilt (duration ):
156
162
"""
157
163
Tilt animation - lightning effect with a rotating color
@@ -182,6 +188,7 @@ def rockstar_tilt(duration):
182
188
pixels .show ()
183
189
time .sleep (0.03 )
184
190
191
+
185
192
# Cusomize LED Animations ------------------------------------------------------
186
193
powerup = RainbowComet (pixel_map [3 ], speed = 0 , tail_length = 25 , bounce = False )
187
194
rainbow = Rainbow (pixel_map [4 ], speed = 0 , period = 6 , name = "rainbow" , step = 2.4 )
@@ -191,13 +198,13 @@ def rockstar_tilt(duration):
191
198
rainbow_comet = RainbowComet (pixel_map [2 ], speed = 0 , tail_length = 80 , bounce = True )
192
199
rainbow_comet2 = RainbowComet (
193
200
pixel_map [0 ], speed = 0 , tail_length = 104 , colorwheel_offset = 80 , bounce = True
194
- )
201
+ )
195
202
rainbow_comet3 = RainbowComet (
196
203
pixel_map [1 ], speed = 0 , tail_length = 25 , colorwheel_offset = 80 , step = 4 , bounce = False
197
- )
204
+ )
198
205
strum = RainbowComet (
199
206
pixel_map [3 ], speed = 0 , tail_length = 25 , bounce = False , colorwheel_offset = 50 , step = 4
200
- )
207
+ )
201
208
lava = Comet (pixel_map [3 ], speed = 0.01 , color = ORANGE , tail_length = 40 , bounce = False )
202
209
sparkle = Sparkle (pixel_map [4 ], speed = 0.01 , color = BLUE , num_sparkles = 10 )
203
210
sparkle2 = Sparkle (pixel_map [1 ], speed = 0.05 , color = PURPLE , num_sparkles = 4 )
@@ -214,18 +221,18 @@ def rockstar_tilt(duration):
214
221
AnimationGroup (
215
222
sparkle ,
216
223
strum ,
217
- ),
224
+ ),
218
225
AnimationGroup (
219
226
sparkle2 ,
220
227
rainbow_comet3 ,
221
- ),
228
+ ),
222
229
auto_clear = True ,
223
230
auto_reset = True ,
224
231
)
225
232
226
233
227
234
MODE = 0
228
- LASTMODE = 1 # start up in sound reactive mode
235
+ LASTMODE = 1 # start up in sound reactive mode
229
236
i = 0
230
237
231
238
# Main loop
@@ -246,9 +253,9 @@ def rockstar_tilt(duration):
246
253
spectrum [1 ] = 0
247
254
peak_idx = np .argmax (spectrum )
248
255
peak_freq = peak_idx * 16000 / 256
249
- # print((peak_idx, peak_freq, spectrum[peak_idx]))
256
+ # print((peak_idx, peak_freq, spectrum[peak_idx]))
250
257
magnitude = spectrum [peak_idx ]
251
- # time.sleep(1)
258
+ # time.sleep(1)
252
259
if peak_freq == 812.50 and magnitude > SOUND_THRESHOLD :
253
260
animations .next ()
254
261
time .sleep (1 )
@@ -263,20 +270,20 @@ def rockstar_tilt(duration):
263
270
print ("mode = 1" )
264
271
LASTMODE = 1
265
272
time .sleep (1 )
266
- # Read accelerometer
273
+ # Read accelerometer
267
274
x , y , z = sensor .acceleration
268
- accel_total = x * x + y * y # x=tilt, y=rotate
269
- # print (accel_total)
275
+ accel_total = x * x + y * y # x=tilt, y=rotate
276
+ # print (accel_total)
270
277
if accel_total > ROCKSTAR_TILT_THRESHOLD :
271
278
MODE = 3
272
279
print ("Tilted: " , accel_total )
273
280
if MODE == 1 :
274
281
VOLUME = magnitude / (VOLUME_CALIBRATOR * 100000 )
275
282
if VOLUME > MAX_BRIGHTNESS :
276
283
VOLUME = MAX_BRIGHTNESS
277
- # print(VOLUME)
284
+ # print(VOLUME)
278
285
pixels .brightness = VOLUME
279
- # time.sleep(2)
286
+ # time.sleep(2)
280
287
animations .animate ()
281
288
elif MODE == 2 :
282
289
pixels .brightness = NORMAL_BRIGHTNESS
0 commit comments