Skip to content

Commit be2c608

Browse files
committed
update to CircuitPython 5.0.0-beta.0 BLE API
1 parent 8fced95 commit be2c608

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

Circuit_Playground_Bluefruit_Color_Remote/cpb_remote_color_client.py

+29-18
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,48 @@
1212
# Only the packet classes that are imported will be known to Packet.
1313
from adafruit_bluefruit_connect.color_packet import ColorPacket
1414

15-
from adafruit_ble.scanner import Scanner
16-
from adafruit_ble.uart_client import UARTClient
15+
from adafruit_ble import BLERadio
16+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
17+
from adafruit_ble.services.nordic import UARTService
1718

1819
def scale(value):
1920
"""Scale an value from 0-65535 (AnalogIn range) to 0-255 (RGB range)"""
2021
return int(value / 65535 * 255)
2122

22-
scanner = Scanner()
23-
uart_client = UARTClient()
23+
ble = BLERadio()
2424

25-
a3 = AnalogIn(board.A4)
26-
a4 = AnalogIn(board.A5)
27-
a5 = AnalogIn(board.A6)
25+
a4 = AnalogIn(board.A4)
26+
a5 = AnalogIn(board.A5)
27+
a6 = AnalogIn(board.A6)
2828

29-
while True:
30-
uart_addresses = []
31-
# Keep trying to find a UART peripheral
32-
while not uart_addresses:
33-
uart_addresses = uart_client.scan(scanner)
34-
uart_client.connect(uart_addresses[0], 5)
29+
uart_connection = None
30+
31+
# See if any existing connections are providing UARTService.
32+
if ble.connected:
33+
for connection in ble.connections:
34+
if UARTService in connection:
35+
uart_connection = connection
36+
break
3537

36-
while uart_client.connected:
37-
r = scale(a3.value)
38-
g = scale(a4.value)
39-
b = scale(a5.value)
38+
while True:
39+
if not uart_connection:
40+
for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5):
41+
if UARTService in adv.services:
42+
uart_connection = ble.connect(adv)
43+
break
44+
# Stop scanning whether or not we are connected.
45+
ble.stop_scan()
46+
47+
while uart_connection and uart_connection.connected:
48+
r = scale(a4.value)
49+
g = scale(a5.value)
50+
b = scale(a6.value)
4051

4152
color = (r, g, b)
4253
print(color)
4354
color_packet = ColorPacket(color)
4455
try:
45-
uart_client.write(color_packet.to_bytes())
56+
uart_connection[UARTService].write(color_packet.to_bytes())
4657
except OSError:
4758
pass
4859
time.sleep(0.3)

Circuit_Playground_Bluefruit_Color_Remote/cpb_remote_color_periph.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
import board
77
import neopixel
88

9-
from adafruit_ble.uart_server import UARTServer
9+
from adafruit_ble import BLERadio
10+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
11+
from adafruit_ble.services.nordic import UARTService
12+
1013
from adafruit_bluefruit_connect.packet import Packet
1114
# Only the packet classes that are imported will be known to Packet.
1215
from adafruit_bluefruit_connect.color_packet import ColorPacket
1316

14-
uart_server = UARTServer()
17+
ble = BLERadio()
18+
uart = UARTService()
19+
advertisement = ProvideServicesAdvertisement(uart)
1520

1621
NUM_PIXELS = 10
1722
np = neopixel.NeoPixel(board.NEOPIXEL, NUM_PIXELS, brightness=0.1)
@@ -23,12 +28,12 @@ def mod(i):
2328

2429
while True:
2530
# Advertise when not connected.
26-
uart_server.start_advertising()
27-
while not uart_server.connected:
31+
ble.start_advertising(advertisement)
32+
while not ble.connected:
2833
pass
2934

30-
while uart_server.connected:
31-
packet = Packet.from_stream(uart_server)
35+
while ble.connected:
36+
packet = Packet.from_stream(uart)
3237
if isinstance(packet, ColorPacket):
3338
print(packet.color)
3439
np[next_pixel] = packet.color

0 commit comments

Comments
 (0)