Merge pull request #2265 from rdagger/patch-4

Update BLE code for CircuitPython 8 compatibility
This commit is contained in:
Dan Halbert 2024-04-09 16:20:33 -04:00 committed by GitHub
commit b31cb65f4f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,9 +2,13 @@
# #
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
from binascii import unhexlify
from time import sleep from time import sleep
from adafruit_ble.uart_client import UARTClient
from adafruit_ble.scanner import Scanner from micropython import const
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
from adafruit_bluefruit_connect.packet import Packet from adafruit_bluefruit_connect.packet import Packet
from adafruit_bluefruit_connect.button_packet import ButtonPacket from adafruit_bluefruit_connect.button_packet import ButtonPacket
from adafruit_bluefruit_connect.color_packet import ColorPacket from adafruit_bluefruit_connect.color_packet import ColorPacket
@ -21,65 +25,75 @@ switch = Debouncer(pin)
pixels = NeoPixel(NEOPIXEL, 1) # Set up built-in NeoPixel pixels = NeoPixel(NEOPIXEL, 1) # Set up built-in NeoPixel
AQUA = 0x00FFFF # (0, 255, 255) AQUA = const(0x00FFFF) # (0, 255, 255)
GREEN = 0x00FF00 # (0, 255, 0) GREEN = const(0x00FF00) # (0, 255, 0)
ORANGE = 0xFF8000 # (255, 128, 0) ORANGE = const(0xFF8000) # (255, 128, 0)
RED = 0xFF0000 # (255, 0, 0) RED = const(0xFF0000) # (255, 0, 0)
BLUE = 0x0000FF # (0, 0, 255) BLUE = const(0x0000FF) # (0, 0, 255)
gradients = {'Off': [(0.0, RED), (0.75, ORANGE)], gradients = {"Off": [(0.0, RED), (0.75, ORANGE)], "On": [(0.0, GREEN), (1.0, AQUA)]}
'On': [(0.0, GREEN), (1.0, AQUA)]} palette = fancy.expand_gradient(gradients["Off"], 30)
palette = fancy.expand_gradient(gradients['Off'], 30)
gamma_levels = (0.25, 0.3, 0.15) gamma_levels = (0.25, 0.3, 0.15)
color_index = 1 color_index = 1
fade_direction = 1 fade_direction = 1
TARGET = 'a0:b4:c2:d0:e7:f2' # CHANGE TO YOUR BLE ADDRESS TARGET = "f0:74:72:60:87:d2" # CHANGE TO YOUR BLE ADDRESS
target_address = TARGET.split(":") # Convert address string to list of bytes
target_address.reverse() # Reverse bytes to match Address class little-endian
target_address = unhexlify("".join(target_address)) # Convert list to bytes
button_packet = ButtonPacket("1", True) # Transmits pressed button 1 button_packet = ButtonPacket("1", True) # Transmits pressed button 1
scanner = Scanner() # BLE Scanner ble = BLERadio()
uart_client = UARTClient() # BLE Client uart_client = None
while True: while True:
uart_addresses = [] uart_addresses = []
pixels[0] = BLUE # Blue LED indicates disconnected status pixels[0] = BLUE # Blue LED indicates disconnected status
pixels.show() pixels.show()
# Keep trying to find target UART peripheral if not uart_client:
while not uart_addresses: print("Trying to connect to BLE server...")
uart_addresses = uart_client.scan(scanner) # Keep trying to find target UART peripheral
for address in uart_addresses: for adv in ble.start_scan(ProvideServicesAdvertisement):
if TARGET in str(address): print(adv.address.address_bytes) # Print detected addresses
uart_client.connect(address, 5) # Connect to target if adv.address.address_bytes == target_address:
uart_client = ble.connect(adv)
print("Connected")
break
ble.stop_scan()
while uart_client.connected: # Connected if uart_client and uart_client.connected:
switch.update() uart_service = uart_client[UARTService]
if switch.fell: # Check for button press while uart_client and uart_client.connected: # Connected
try: switch.update()
uart_client.write(button_packet.to_bytes()) # Transmit press if switch.fell: # Check for button press
except OSError: try:
pass # Transmit press
# Check for LED status receipt uart_service.write(button_packet.to_bytes())
if uart_client.in_waiting: except OSError:
packet = Packet.from_stream(uart_client) pass
if isinstance(packet, ColorPacket): # Check for LED status receipt
if fancy.CRGB(*packet.color).pack() == GREEN: # Color match if uart_service.in_waiting:
# Green indicates on state packet = Packet.from_stream(uart_service)
palette = fancy.expand_gradient(gradients['On'], 30) if isinstance(packet, ColorPacket):
else: # Color match
# Otherwise red indicates off if fancy.CRGB(*packet.color).pack() == GREEN:
palette = fancy.expand_gradient(gradients['Off'], 30) # Green indicates on state
palette = fancy.expand_gradient(gradients["On"], 30)
else:
# Otherwise red indicates off
palette = fancy.expand_gradient(gradients["Off"], 30)
# NeoPixel color fading routing # NeoPixel color fading routing
color = fancy.palette_lookup(palette, color_index / 29) color = fancy.palette_lookup(palette, color_index / 29)
color = fancy.gamma_adjust(color, brightness=gamma_levels) color = fancy.gamma_adjust(color, brightness=gamma_levels)
c = color.pack() c = color.pack()
pixels[0] = c pixels[0] = c
pixels.show() pixels.show()
if color_index in (0, 28): if color_index in (0, 28):
fade_direction *= -1 # Change direction fade_direction *= -1 # Change direction
color_index += fade_direction color_index += fade_direction
sleep(0.02) sleep(0.02)