2424import audiobusio
2525import board
2626import 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
2832from ulab import numpy as np
2933from rainbowio import colorwheel
3034from adafruit_lsm6ds import lsm6ds33
4650 WHITE ,
4751)
4852
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
5458
5559# Set to the length in seconds for the animations
5660POWER_ON_DURATION = 1.3
7276pixels .show ()
7377
7478
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
7982
83+ # fmt: off
8084#Bottom up along both sides at once
8185pixel_map_reverse = PixelMap (pixels , [
8286 0 , 103 , 1 , 102 , 2 , 101 , 3 , 100 , 4 , 99 , 5 , 98 , 6 , 97 , 7 , 96 , 8 , 95 , 9 , 94 , 10 ,
122126 83 , 22 , 81 , 24 , 79 , 26 , 77 , 29 , 74 , 31 , 72 , 33 , 70 , 35 , 68 , 37 , 66 , 39 , 64 , 41 ,
123127 62 , 43 , 60 , 45 , 58 , 47 , 56 , 49 , 54 , 51 , 52 ,
124128 ], individual_pixels = True )
129+ # fmt: on
125130
126131pixel_map = [
127132 pixel_map_reverse ,
131136 pixel_map_skip ,
132137]
133138
134- #Set up accelerometer & mic
139+ # Set up accelerometer & mic
135140sensor = 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+ )
140144
141145NUM_SAMPLES = 256
142- samples_bit = array .array ('H' , [0 ] * (NUM_SAMPLES + 3 ))
146+ samples_bit = array .array ("H" , [0 ] * (NUM_SAMPLES + 3 ))
147+
143148
144149def power_on (duration ):
145150 """
@@ -152,6 +157,7 @@ def power_on(duration):
152157 break # Stop animating
153158 powerup .animate ()
154159
160+
155161def rockstar_tilt (duration ):
156162 """
157163 Tilt animation - lightning effect with a rotating color
@@ -182,6 +188,7 @@ def rockstar_tilt(duration):
182188 pixels .show ()
183189 time .sleep (0.03 )
184190
191+
185192# Cusomize LED Animations ------------------------------------------------------
186193powerup = RainbowComet (pixel_map [3 ], speed = 0 , tail_length = 25 , bounce = False )
187194rainbow = Rainbow (pixel_map [4 ], speed = 0 , period = 6 , name = "rainbow" , step = 2.4 )
@@ -191,13 +198,13 @@ def rockstar_tilt(duration):
191198rainbow_comet = RainbowComet (pixel_map [2 ], speed = 0 , tail_length = 80 , bounce = True )
192199rainbow_comet2 = RainbowComet (
193200 pixel_map [0 ], speed = 0 , tail_length = 104 , colorwheel_offset = 80 , bounce = True
194- )
201+ )
195202rainbow_comet3 = RainbowComet (
196203 pixel_map [1 ], speed = 0 , tail_length = 25 , colorwheel_offset = 80 , step = 4 , bounce = False
197- )
204+ )
198205strum = RainbowComet (
199206 pixel_map [3 ], speed = 0 , tail_length = 25 , bounce = False , colorwheel_offset = 50 , step = 4
200- )
207+ )
201208lava = Comet (pixel_map [3 ], speed = 0.01 , color = ORANGE , tail_length = 40 , bounce = False )
202209sparkle = Sparkle (pixel_map [4 ], speed = 0.01 , color = BLUE , num_sparkles = 10 )
203210sparkle2 = Sparkle (pixel_map [1 ], speed = 0.05 , color = PURPLE , num_sparkles = 4 )
@@ -214,18 +221,18 @@ def rockstar_tilt(duration):
214221 AnimationGroup (
215222 sparkle ,
216223 strum ,
217- ),
224+ ),
218225 AnimationGroup (
219226 sparkle2 ,
220227 rainbow_comet3 ,
221- ),
228+ ),
222229 auto_clear = True ,
223230 auto_reset = True ,
224231)
225232
226233
227234MODE = 0
228- LASTMODE = 1 # start up in sound reactive mode
235+ LASTMODE = 1 # start up in sound reactive mode
229236i = 0
230237
231238# Main loop
@@ -246,9 +253,9 @@ def rockstar_tilt(duration):
246253 spectrum [1 ] = 0
247254 peak_idx = np .argmax (spectrum )
248255 peak_freq = peak_idx * 16000 / 256
249- # print((peak_idx, peak_freq, spectrum[peak_idx]))
256+ # print((peak_idx, peak_freq, spectrum[peak_idx]))
250257 magnitude = spectrum [peak_idx ]
251- # time.sleep(1)
258+ # time.sleep(1)
252259 if peak_freq == 812.50 and magnitude > SOUND_THRESHOLD :
253260 animations .next ()
254261 time .sleep (1 )
@@ -263,20 +270,20 @@ def rockstar_tilt(duration):
263270 print ("mode = 1" )
264271 LASTMODE = 1
265272 time .sleep (1 )
266- # Read accelerometer
273+ # Read accelerometer
267274 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)
270277 if accel_total > ROCKSTAR_TILT_THRESHOLD :
271278 MODE = 3
272279 print ("Tilted: " , accel_total )
273280 if MODE == 1 :
274281 VOLUME = magnitude / (VOLUME_CALIBRATOR * 100000 )
275282 if VOLUME > MAX_BRIGHTNESS :
276283 VOLUME = MAX_BRIGHTNESS
277- # print(VOLUME)
284+ # print(VOLUME)
278285 pixels .brightness = VOLUME
279- # time.sleep(2)
286+ # time.sleep(2)
280287 animations .animate ()
281288 elif MODE == 2 :
282289 pixels .brightness = NORMAL_BRIGHTNESS
0 commit comments