11# CircusPython!
22# For use with the Adafruit BlueFruit LE Connect app.
3- # Works with CircuitPython 4 .0.0-beta.1 and later running on an nRF52840 board.
3+ # Works with CircuitPython 5 .0.0-beta.0 and later running on an nRF52840 board.
44
55import random
66import time
77
88from adafruit_crickit import crickit
9- from adafruit_ble .uart import UARTServer
9+ from adafruit_ble import BLERadio
10+ from adafruit_ble .advertising .standard import ProvideServicesAdvertisement
11+ from adafruit_ble .services .nordic import UARTService
1012
1113from adafruit_bluefruit_connect .packet import Packet
1214# Only the packet classes that are imported will be known to Packet.
@@ -24,7 +26,9 @@ def sparkle():
2426 crickit .neopixel [random .randrange (24 )] = color
2527 crickit .neopixel [random .randrange (24 )] = color
2628
27- uart_server = UARTServer ()
29+ ble = BLERadio ()
30+ uart_service = UARTService ()
31+ advertisement = ProvideServicesAdvertisement (uart_service )
2832
2933# Increase this to slow down movement of the servo arm.
3034DELAY = 0.0
@@ -36,41 +40,35 @@ def sparkle():
3640# slightly as necessary so you don't bump into the ring.
3741DOWN_ANGLE = 2
3842
39- advertising_now = False
4043crickit .servo_1 .angle = UP_ANGLE
4144angle = UP_ANGLE
4245
4346while True :
44- sparkle ()
45- if not uart_server .connected :
46- if not advertising_now :
47- uart_server .start_advertising ()
48- advertising_now = True
49- continue
50-
51- # Connected, so no longer advertising.
52- advertising_now = False
53-
54- if uart_server .in_waiting :
55- packet = Packet .from_stream (uart_server )
56- if isinstance (packet , ColorPacket ):
57- # Change the fire color.
58- color = packet .color
59- elif isinstance (packet , ButtonPacket ):
60- if packet .pressed :
61- if packet .button == '5' and angle != UP_ANGLE :
62- # The Up button was pressed.
63- for a in range (angle , UP_ANGLE + 1 , 1 ):
64- crickit .servo_1 .angle = a
65- # Sparkle while moving.
66- sparkle ()
67- time .sleep (DELAY )
68- angle = UP_ANGLE
69- elif packet .button == '6' and angle != DOWN_ANGLE :
70- # The Down button was pressed.
71- for a in range (angle , DOWN_ANGLE - 1 , - 1 ):
72- crickit .servo_1 .angle = a
73- # Sparkle while moving.
74- sparkle ()
75- time .sleep (DELAY )
76- angle = DOWN_ANGLE
47+ ble .start_advertising (advertisement )
48+ while not ble .connected :
49+ sparkle ()
50+ while ble .connected :
51+ sparkle ()
52+ if uart_service .in_waiting :
53+ packet = Packet .from_stream (uart_service )
54+ if isinstance (packet , ColorPacket ):
55+ # Change the fire color.
56+ color = packet .color
57+ elif isinstance (packet , ButtonPacket ):
58+ if packet .pressed :
59+ if packet .button == '5' and angle != UP_ANGLE :
60+ # The Up button was pressed.
61+ for a in range (angle , UP_ANGLE + 1 , 1 ):
62+ crickit .servo_1 .angle = a
63+ # Sparkle while moving.
64+ sparkle ()
65+ time .sleep (DELAY )
66+ angle = UP_ANGLE
67+ elif packet .button == '6' and angle != DOWN_ANGLE :
68+ # The Down button was pressed.
69+ for a in range (angle , DOWN_ANGLE - 1 , - 1 ):
70+ crickit .servo_1 .angle = a
71+ # Sparkle while moving.
72+ sparkle ()
73+ time .sleep (DELAY )
74+ angle = DOWN_ANGLE
0 commit comments