Skip to content

Commit a8eae25

Browse files
committed
update for CPy 5.0.0-beta.0; not tested
1 parent 8fced95 commit a8eae25

File tree

2 files changed

+41
-27
lines changed

2 files changed

+41
-27
lines changed

BLE_Client_Server/client.py

+29-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from time import sleep
2-
from adafruit_ble.uart_client import UARTClient
3-
from adafruit_ble.scanner import Scanner
2+
from adafruit_ble import BLERadio
3+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
4+
from adafruit_ble.services.nordic import UARTService
45
from adafruit_bluefruit_connect.packet import Packet
56
from adafruit_bluefruit_connect.button_packet import ButtonPacket
67
from adafruit_bluefruit_connect.color_packet import ColorPacket
@@ -35,31 +36,38 @@
3536

3637
button_packet = ButtonPacket("1", True) # Transmits pressed button 1
3738

38-
scanner = Scanner() # BLE Scanner
39-
uart_client = UARTClient() # BLE Client
39+
ble = BLERadio()
4040

41-
while True:
42-
uart_addresses = []
43-
pixels[0] = BLUE # Blue LED indicates disconnected status
44-
pixels.show()
41+
uart_connection = None
42+
# See if any existing connections are providing UARTService.
43+
if ble.connected:
44+
for connection in ble.connections:
45+
if UARTService in connection:
46+
uart_connection = connection
47+
break
4548

46-
# Keep trying to find target UART peripheral
47-
while not uart_addresses:
48-
uart_addresses = uart_client.scan(scanner)
49-
for address in uart_addresses:
50-
if TARGET in str(address):
51-
uart_client.connect(address, 5) # Connect to target
49+
while True:
50+
if not uart_connection:
51+
pixels[0] = BLUE # Blue LED indicates disconnected status
52+
pixels.show()
53+
for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5):
54+
if UARTService in adv.services:
55+
uart_connection = ble.connect(adv)
56+
break
57+
# Stop scanning whether or not we are connected.
58+
ble.stop_scan()
5259

53-
while uart_client.connected: # Connected
60+
while uart_connection and uart_connection.connected:
5461
switch.update()
5562
if switch.fell: # Check for button press
5663
try:
57-
uart_client.write(button_packet.to_bytes()) # Transmit press
64+
uart_connection.write(button_packet.to_bytes()) # Transmit press
5865
except OSError:
59-
pass
66+
uart_connection = None
67+
continue
6068
# Check for LED status receipt
61-
if uart_client.in_waiting:
62-
packet = Packet.from_stream(uart_client)
69+
if uart_connection.in_waiting:
70+
packet = Packet.from_stream(uart_connection)
6371
if isinstance(packet, ColorPacket):
6472
if fancy.CRGB(*packet.color).pack() == GREEN: # Color match
6573
# Green indicates on state
@@ -79,3 +87,5 @@
7987
color_index += fade_direction
8088

8189
sleep(0.02)
90+
91+
uart_connection = None

BLE_Client_Server/server.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from time import sleep
2-
from adafruit_ble.uart_server import UARTServer
2+
from adafruit_ble import BLERadio
3+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
4+
from adafruit_ble.services.nordic import UARTService
35
from adafruit_bluefruit_connect.packet import Packet
46
from adafruit_bluefruit_connect.button_packet import ButtonPacket
57
from adafruit_bluefruit_connect.color_packet import ColorPacket
@@ -13,17 +15,19 @@
1315
solenoid.direction = Direction.OUTPUT
1416
solenoid.value = False
1517

16-
uart_server = UARTServer()
18+
ble = BLERadio()
19+
uart_service = UARTService()
20+
advertisement = ProvideServicesAdvertisement(uart_service)
1721

1822
while True:
19-
uart_server.start_advertising() # Advertise when not connected.
23+
ble.start_advertising(advertisement) # Advertise when not connected.
2024

21-
while not uart_server.connected: # Wait for connection
25+
while not ble.connected: # Wait for connection
2226
pass
2327

24-
while uart_server.connected: # Connected
25-
if uart_server.in_waiting: # Check BLE commands
26-
packet = Packet.from_stream(uart_server)
28+
while uart_service.connected: # Connected
29+
if uart_service.in_waiting: # Check BLE commands
30+
packet = Packet.from_stream(uart_service)
2731
if isinstance(packet, ButtonPacket):
2832
if packet.button == '1' and packet.pressed:
2933
solenoid.value = True # Activate solenoid for 1 second
@@ -35,7 +39,7 @@
3539
# Color: red = off, green = on
3640
color_packet = ColorPacket((255 * int(not led_on), 255 * led_on, 0))
3741
try:
38-
uart_server.write(color_packet.to_bytes()) # Transmit state color
42+
uart_service.write(color_packet.to_bytes()) # Transmit state color
3943
except OSError:
4044
pass
4145

0 commit comments

Comments
 (0)