Skip to content

Commit b31cb65

Browse files
authored
Merge pull request #2265 from rdagger/patch-4
Update BLE code for CircuitPython 8 compatibility
2 parents 389d364 + 2d34f12 commit b31cb65

File tree

1 file changed

+60
-46
lines changed

1 file changed

+60
-46
lines changed

BLE_Client_Server/client/code.py

Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5+
from binascii import unhexlify
56
from time import sleep
6-
from adafruit_ble.uart_client import UARTClient
7-
from adafruit_ble.scanner import Scanner
7+
8+
from micropython import const
9+
from adafruit_ble import BLERadio
10+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
11+
from adafruit_ble.services.nordic import UARTService
812
from adafruit_bluefruit_connect.packet import Packet
913
from adafruit_bluefruit_connect.button_packet import ButtonPacket
1014
from adafruit_bluefruit_connect.color_packet import ColorPacket
@@ -21,65 +25,75 @@
2125

2226
pixels = NeoPixel(NEOPIXEL, 1) # Set up built-in NeoPixel
2327

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)
28+
AQUA = const(0x00FFFF) # (0, 255, 255)
29+
GREEN = const(0x00FF00) # (0, 255, 0)
30+
ORANGE = const(0xFF8000) # (255, 128, 0)
31+
RED = const(0xFF0000) # (255, 0, 0)
32+
BLUE = const(0x0000FF) # (0, 0, 255)
2933

30-
gradients = {'Off': [(0.0, RED), (0.75, ORANGE)],
31-
'On': [(0.0, GREEN), (1.0, AQUA)]}
32-
palette = fancy.expand_gradient(gradients['Off'], 30)
34+
gradients = {"Off": [(0.0, RED), (0.75, ORANGE)], "On": [(0.0, GREEN), (1.0, AQUA)]}
35+
palette = fancy.expand_gradient(gradients["Off"], 30)
3336

3437
gamma_levels = (0.25, 0.3, 0.15)
3538
color_index = 1
3639
fade_direction = 1
3740

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

4046
button_packet = ButtonPacket("1", True) # Transmits pressed button 1
4147

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

4551
while True:
4652
uart_addresses = []
4753
pixels[0] = BLUE # Blue LED indicates disconnected status
4854
pixels.show()
4955

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
56+
if not uart_client:
57+
print("Trying to connect to BLE server...")
58+
# Keep trying to find target UART peripheral
59+
for adv in ble.start_scan(ProvideServicesAdvertisement):
60+
print(adv.address.address_bytes) # Print detected addresses
61+
if adv.address.address_bytes == target_address:
62+
uart_client = ble.connect(adv)
63+
print("Connected")
64+
break
65+
ble.stop_scan()
5666

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)
67+
if uart_client and uart_client.connected:
68+
uart_service = uart_client[UARTService]
69+
while uart_client and uart_client.connected: # Connected
70+
switch.update()
71+
if switch.fell: # Check for button press
72+
try:
73+
# Transmit press
74+
uart_service.write(button_packet.to_bytes())
75+
except OSError:
76+
pass
77+
# Check for LED status receipt
78+
if uart_service.in_waiting:
79+
packet = Packet.from_stream(uart_service)
80+
if isinstance(packet, ColorPacket):
81+
# Color match
82+
if fancy.CRGB(*packet.color).pack() == GREEN:
83+
# Green indicates on state
84+
palette = fancy.expand_gradient(gradients["On"], 30)
85+
else:
86+
# Otherwise red indicates off
87+
palette = fancy.expand_gradient(gradients["Off"], 30)
7488

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
89+
# NeoPixel color fading routing
90+
color = fancy.palette_lookup(palette, color_index / 29)
91+
color = fancy.gamma_adjust(color, brightness=gamma_levels)
92+
c = color.pack()
93+
pixels[0] = c
94+
pixels.show()
95+
if color_index in (0, 28):
96+
fade_direction *= -1 # Change direction
97+
color_index += fade_direction
8498

85-
sleep(0.02)
99+
sleep(0.02)

0 commit comments

Comments
 (0)