1515import audiobusio
1616import board
1717import neopixel
18- from adafruit_ble .uart_server import UARTServer
18+
19+ from adafruit_ble import BLERadio
20+ from adafruit_ble .advertising .standard import ProvideServicesAdvertisement
21+ from adafruit_ble .services .nordic import UARTService
1922from adafruit_bluefruit_connect .packet import Packet
2023from adafruit_bluefruit_connect .color_packet import ColorPacket
2124from adafruit_bluefruit_connect .button_packet import ButtonPacket
2225
23- uart_server = UARTServer ()
26+ ble = BLERadio ()
27+ uart_service = UARTService ()
28+ advertisement = ProvideServicesAdvertisement (uart_service )
2429
2530# User input vars
2631mode = 0 # 0=audio, 1=rainbow, 2=larsen_scanner, 3=solid
2934# Audio meter vars
3035PEAK_COLOR = (100 , 0 , 255 )
3136NUM_PIXELS = 10
37+ NEOPIXEL_PIN = board .A1
38+ # Use this instead if you want to use the NeoPixels on the Circuit Playground Bluefruit.
39+ # NEOPIXEL_PIN = board.NEOPIXEL
3240CURVE = 2
3341SCALE_EXPONENT = math .pow (10 , CURVE * - 0.1 )
3442NUM_SAMPLES = 160
@@ -62,7 +70,7 @@ def volume_color(volume):
6270 return 200 , volume * (255 // NUM_PIXELS ), 0
6371
6472# Set up NeoPixels and turn them all off.
65- pixels = neopixel .NeoPixel (board . A1 , NUM_PIXELS , brightness = 0.1 , auto_write = False )
73+ pixels = neopixel .NeoPixel (NEOPIXEL_PIN , NUM_PIXELS , brightness = 0.1 , auto_write = False )
6674pixels .fill (0 )
6775pixels .show ()
6876
@@ -191,15 +199,18 @@ def change_speed(mod, old_speed):
191199 return (new_speed , map_value (new_speed , 10.0 , 0.0 , 0.01 , 0.3 ))
192200
193201while True :
194- # While BLE is *not* connected
195- if not uart_server .connected :
196- # OK to call again even if already advertising
197- uart_server .start_advertising ()
202+ ble .start_advertising (advertisement )
203+ while not ble .connected :
204+ pass
198205
199206 # While BLE is connected
200- else :
201- if uart_server .in_waiting :
202- packet = Packet .from_stream (uart_server )
207+ while ble .connected :
208+ if uart_service .in_waiting :
209+ try :
210+ packet = Packet .from_stream (uart_service )
211+ # Ignore malformed packets.
212+ except ValueError :
213+ continue
203214
204215 # Received ColorPacket
205216 if isinstance (packet , ColorPacket ):
@@ -221,12 +232,12 @@ def change_speed(mod, old_speed):
221232 elif packet .button == ButtonPacket .BUTTON_4 :
222233 mode = 3
223234
224- # Determine animation based on mode
225- if mode == 0 :
226- peak = audio_meter (peak )
227- elif mode == 1 :
228- rainbow_cycle (0.001 )
229- elif mode == 2 :
230- larsen (wait )
231- elif mode == 3 :
232- solid (user_color )
235+ # Determine animation based on mode
236+ if mode == 0 :
237+ peak = audio_meter (peak )
238+ elif mode == 1 :
239+ rainbow_cycle (0.001 )
240+ elif mode == 2 :
241+ larsen (wait )
242+ elif mode == 3 :
243+ solid (user_color )
0 commit comments