Skip to content

Commit 8fced95

Browse files
authored
Merge pull request #934 from dhalbert/cpy5-circuspython
CircusPython_BLE: update for CircuitPython 5.0.0-beta.0
2 parents b3b8ad4 + 27437dc commit 8fced95

File tree

1 file changed

+35
-37
lines changed

1 file changed

+35
-37
lines changed

CircusPython_BLE/circus.py

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
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

55
import random
66
import time
77

88
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
1012

1113
from 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.
3034
DELAY = 0.0
@@ -36,41 +40,35 @@ def sparkle():
3640
# slightly as necessary so you don't bump into the ring.
3741
DOWN_ANGLE = 2
3842

39-
advertising_now = False
4043
crickit.servo_1.angle = UP_ANGLE
4144
angle = UP_ANGLE
4245

4346
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

Comments
 (0)