Skip to content

Commit ab621fe

Browse files
authored
Update BLE code for CircuitPython 8 compatibility
1 parent e426cc1 commit ab621fe

File tree

1 file changed

+57
-44
lines changed

1 file changed

+57
-44
lines changed

BLE_Client_Server/client/code.py

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5-
from time import sleep
6-
from adafruit_ble.uart_client import UARTClient
7-
from adafruit_ble.scanner import Scanner
5+
from adafruit_ble import BLERadio
6+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
7+
from adafruit_ble.services.nordic import UARTService
88
from adafruit_bluefruit_connect.packet import Packet
99
from adafruit_bluefruit_connect.button_packet import ButtonPacket
1010
from adafruit_bluefruit_connect.color_packet import ColorPacket
1111
from neopixel import NeoPixel
12+
from binascii import unhexlify
1213
from board import NEOPIXEL, SWITCH
1314
from adafruit_debouncer import Debouncer
1415
from digitalio import DigitalInOut, Direction, Pull
16+
from time import sleep
1517
import adafruit_fancyled.adafruit_fancyled as fancy
1618

1719
pin = DigitalInOut(SWITCH) # Set up built-in pushbutton switch
@@ -21,11 +23,11 @@
2123

2224
pixels = NeoPixel(NEOPIXEL, 1) # Set up built-in NeoPixel
2325

24-
AQUA = 0x00FFFF # (0, 255, 255)
25-
GREEN = 0x00FF00 # (0, 255, 0)
26-
ORANGE = 0xFF8000 # (255, 128, 0)
27-
RED = 0xFF0000 # (255, 0, 0)
28-
BLUE = 0x0000FF # (0, 0, 255)
26+
AQUA = const(0x00FFFF) # (0, 255, 255)
27+
GREEN = const(0x00FF00) # (0, 255, 0)
28+
ORANGE = const(0xFF8000) # (255, 128, 0)
29+
RED = const(0xFF0000) # (255, 0, 0)
30+
BLUE = const(0x0000FF) # (0, 0, 255)
2931

3032
gradients = {'Off': [(0.0, RED), (0.75, ORANGE)],
3133
'On': [(0.0, GREEN), (1.0, AQUA)]}
@@ -35,51 +37,62 @@
3537
color_index = 1
3638
fade_direction = 1
3739

38-
TARGET = 'a0:b4:c2:d0:e7:f2' # CHANGE TO YOUR BLE ADDRESS
40+
TARGET = 'f0:74:72:60:87:d2' # CHANGE TO YOUR BLE ADDRESS
41+
target_address = TARGET.split(":") # Convert address string to list of bytes
42+
target_address.reverse() # Reverse bytes to match Address class little-endian
43+
target_address = unhexlify(''.join(target_address)) # Convert list to bytes
3944

4045
button_packet = ButtonPacket("1", True) # Transmits pressed button 1
4146

42-
scanner = Scanner() # BLE Scanner
43-
uart_client = UARTClient() # BLE Client
47+
ble = BLERadio()
48+
uart_client = None
4449

4550
while True:
4651
uart_addresses = []
4752
pixels[0] = BLUE # Blue LED indicates disconnected status
4853
pixels.show()
4954

50-
# Keep trying to find target UART peripheral
51-
while not uart_addresses:
52-
uart_addresses = uart_client.scan(scanner)
53-
for address in uart_addresses:
54-
if TARGET in str(address):
55-
uart_client.connect(address, 5) # Connect to target
55+
if not uart_client:
56+
print("Trying to connect to BLE server...")
57+
# Keep trying to find target UART peripheral
58+
for adv in ble.start_scan(ProvideServicesAdvertisement):
59+
print(adv.address.address_bytes) # Print detected addresses
60+
if adv.address.address_bytes == target_address:
61+
uart_client = ble.connect(adv)
62+
print("Connected")
63+
break
64+
ble.stop_scan()
5665

57-
while uart_client.connected: # Connected
58-
switch.update()
59-
if switch.fell: # Check for button press
60-
try:
61-
uart_client.write(button_packet.to_bytes()) # Transmit press
62-
except OSError:
63-
pass
64-
# Check for LED status receipt
65-
if uart_client.in_waiting:
66-
packet = Packet.from_stream(uart_client)
67-
if isinstance(packet, ColorPacket):
68-
if fancy.CRGB(*packet.color).pack() == GREEN: # Color match
69-
# Green indicates on state
70-
palette = fancy.expand_gradient(gradients['On'], 30)
71-
else:
72-
# Otherwise red indicates off
73-
palette = fancy.expand_gradient(gradients['Off'], 30)
66+
if uart_client and uart_client.connected:
67+
uart_service = uart_client[UARTService]
68+
while uart_client and uart_client.connected: # Connected
69+
switch.update()
70+
if switch.fell: # Check for button press
71+
try:
72+
# Transmit press
73+
uart_service.write(button_packet.to_bytes())
74+
except OSError:
75+
pass
76+
# Check for LED status receipt
77+
if uart_service.in_waiting:
78+
packet = Packet.from_stream(uart_service)
79+
if isinstance(packet, ColorPacket):
80+
# Color match
81+
if fancy.CRGB(*packet.color).pack() == GREEN:
82+
# Green indicates on state
83+
palette = fancy.expand_gradient(gradients['On'], 30)
84+
else:
85+
# Otherwise red indicates off
86+
palette = fancy.expand_gradient(gradients['Off'], 30)
7487

75-
# NeoPixel color fading routing
76-
color = fancy.palette_lookup(palette, color_index / 29)
77-
color = fancy.gamma_adjust(color, brightness=gamma_levels)
78-
c = color.pack()
79-
pixels[0] = c
80-
pixels.show()
81-
if color_index in (0, 28):
82-
fade_direction *= -1 # Change direction
83-
color_index += fade_direction
88+
# NeoPixel color fading routing
89+
color = fancy.palette_lookup(palette, color_index / 29)
90+
color = fancy.gamma_adjust(color, brightness=gamma_levels)
91+
c = color.pack()
92+
pixels[0] = c
93+
pixels.show()
94+
if color_index == 0 or color_index == 28:
95+
fade_direction *= -1 # Change direction
96+
color_index += fade_direction
8497

85-
sleep(0.02)
98+
sleep(0.02)

0 commit comments

Comments
 (0)