1
1
# CircusPython!
2
2
# 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.
4
4
5
5
import random
6
6
import time
7
7
8
8
from 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
10
12
11
13
from adafruit_bluefruit_connect .packet import Packet
12
14
# Only the packet classes that are imported will be known to Packet.
@@ -24,7 +26,9 @@ def sparkle():
24
26
crickit .neopixel [random .randrange (24 )] = color
25
27
crickit .neopixel [random .randrange (24 )] = color
26
28
27
- uart_server = UARTServer ()
29
+ ble = BLERadio ()
30
+ uart_service = UARTService ()
31
+ advertisement = ProvideServicesAdvertisement (uart_service )
28
32
29
33
# Increase this to slow down movement of the servo arm.
30
34
DELAY = 0.0
@@ -36,41 +40,35 @@ def sparkle():
36
40
# slightly as necessary so you don't bump into the ring.
37
41
DOWN_ANGLE = 2
38
42
39
- advertising_now = False
40
43
crickit .servo_1 .angle = UP_ANGLE
41
44
angle = UP_ANGLE
42
45
43
46
while 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