|
2 | 2 | # |
3 | 3 | # SPDX-License-Identifier: MIT |
4 | 4 |
|
| 5 | +from binascii import unhexlify |
5 | 6 | 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 |
8 | 12 | from adafruit_bluefruit_connect.packet import Packet |
9 | 13 | from adafruit_bluefruit_connect.button_packet import ButtonPacket |
10 | 14 | from adafruit_bluefruit_connect.color_packet import ColorPacket |
|
21 | 25 |
|
22 | 26 | pixels = NeoPixel(NEOPIXEL, 1) # Set up built-in NeoPixel |
23 | 27 |
|
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) |
29 | 33 |
|
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) |
33 | 36 |
|
34 | 37 | gamma_levels = (0.25, 0.3, 0.15) |
35 | 38 | color_index = 1 |
36 | 39 | fade_direction = 1 |
37 | 40 |
|
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 |
39 | 45 |
|
40 | 46 | button_packet = ButtonPacket("1", True) # Transmits pressed button 1 |
41 | 47 |
|
42 | | -scanner = Scanner() # BLE Scanner |
43 | | -uart_client = UARTClient() # BLE Client |
| 48 | +ble = BLERadio() |
| 49 | +uart_client = None |
44 | 50 |
|
45 | 51 | while True: |
46 | 52 | uart_addresses = [] |
47 | 53 | pixels[0] = BLUE # Blue LED indicates disconnected status |
48 | 54 | pixels.show() |
49 | 55 |
|
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() |
56 | 66 |
|
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) |
74 | 88 |
|
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 |
84 | 98 |
|
85 | | - sleep(0.02) |
| 99 | + sleep(0.02) |
0 commit comments