Skip to content

Commit a1c9570

Browse files
authored
Create client.py
1 parent 966d2af commit a1c9570

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

BLE_Client_Server/client.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from adafruit_ble.uart_client import UARTClient
2+
from adafruit_ble.scanner import Scanner
3+
from adafruit_bluefruit_connect.packet import Packet
4+
from adafruit_bluefruit_connect.button_packet import ButtonPacket
5+
from adafruit_bluefruit_connect.color_packet import ColorPacket
6+
from neopixel import NeoPixel
7+
from board import NEOPIXEL, SWITCH
8+
from adafruit_debouncer import Debouncer
9+
from digitalio import DigitalInOut, Direction, Pull
10+
from time import sleep
11+
import adafruit_fancyled.adafruit_fancyled as fancy
12+
13+
pin = DigitalInOut(SWITCH) # Set up built-in pushbutton switch
14+
pin.direction = Direction.INPUT
15+
pin.pull = Pull.UP
16+
switch = Debouncer(pin)
17+
18+
pixels = NeoPixel(NEOPIXEL, 1) # Set up built-in NeoPixel
19+
20+
AQUA = const(0x00FFFF) # (0, 255, 255)
21+
GREEN = const(0x00FF00) # (0, 255, 0)
22+
ORANGE = const(0xFF8000) # (255, 128, 0)
23+
RED = const(0xFF0000) # (255, 0, 0)
24+
BLUE = const(0x0000FF) # (0, 0, 255)
25+
26+
gradients = {'Off': [(0.0, RED), (0.75, ORANGE)],
27+
'On': [(0.0, GREEN), (1.0, AQUA)]}
28+
palette = fancy.expand_gradient(gradients['Off'], 30)
29+
30+
gamma_levels = (0.25, 0.3, 0.15)
31+
color_index = 1
32+
fade_direction = 1
33+
34+
TARGET = 'a0:b4:c2:d0:e7:f2' # CHANGE TO YOUR BLE ADDRESS
35+
36+
button_packet = ButtonPacket("1", True) # Transmits pressed button 1
37+
38+
scanner = Scanner() # BLE Scanner
39+
uart_client = UARTClient() # BLE Client
40+
41+
while True:
42+
uart_addresses = []
43+
pixels[0] = BLUE # Blue LED indicates disconnected status
44+
pixels.show()
45+
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
52+
53+
while uart_client.connected: # Connected
54+
switch.update()
55+
if switch.fell: # Check for button press
56+
try:
57+
uart_client.write(button_packet.to_bytes()) # Transmit press
58+
except OSError:
59+
pass
60+
# Check for LED status receipt
61+
if uart_client.in_waiting:
62+
packet = Packet.from_stream(uart_client)
63+
if isinstance(packet, ColorPacket):
64+
if fancy.CRGB(*packet.color).pack() == GREEN: # Color match
65+
# Green indicates on state
66+
palette = fancy.expand_gradient(gradients['On'], 30)
67+
else:
68+
# Otherwise red indicates off
69+
palette = fancy.expand_gradient(gradients['Off'], 30)
70+
71+
# NeoPixel color fading routing
72+
color = fancy.palette_lookup(palette, color_index / 29)
73+
color = fancy.gamma_adjust(color, brightness=gamma_levels)
74+
c = color.pack()
75+
pixels[0] = c
76+
pixels.show()
77+
if color_index == 0 or color_index == 28:
78+
fade_direction *= -1 # Change direction
79+
color_index += fade_direction
80+
81+
sleep(0.02)

0 commit comments

Comments
 (0)