Skip to content

Commit

Permalink
update for CPy 5.0.0-beta.0; not tested
Browse files Browse the repository at this point in the history
  • Loading branch information
dhalbert committed Nov 21, 2019
1 parent 8fced95 commit 80a9f38
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
46 changes: 28 additions & 18 deletions BLE_Client_Server/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from time import sleep
from adafruit_ble.uart_client import UARTClient
from adafruit_ble.scanner import Scanner
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.button_packet import ButtonPacket
from adafruit_bluefruit_connect.color_packet import ColorPacket
Expand Down Expand Up @@ -35,30 +36,37 @@

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

scanner = Scanner() # BLE Scanner
uart_client = UARTClient() # BLE Client
ble = BLERadio()

while True:
uart_addresses = []
pixels[0] = BLUE # Blue LED indicates disconnected status
pixels.show()
uart_connection = None
# See if any existing connections are providing UARTService.
if ble.connected:
for connection in ble.connections:
if UARTService in connection:
uart_connection = connection
break

# Keep trying to find target UART peripheral
while not uart_addresses:
uart_addresses = uart_client.scan(scanner)
for address in uart_addresses:
if TARGET in str(address):
uart_client.connect(address, 5) # Connect to target
while True:
if not uart_connection:
pixels[0] = BLUE # Blue LED indicates disconnected status
pixels.show()
for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5):
if UARTService in adv.services:
uart_connection = ble.connect(adv)
break
# Stop scanning whether or not we are connected.
ble.stop_scan()

while uart_client.connected: # Connected
while uart_connection and uart_connection.connected:
switch.update()
if switch.fell: # Check for button press
try:
uart_client.write(button_packet.to_bytes()) # Transmit press
uart_connection.write(button_packet.to_bytes()) # Transmit press
except OSError:
pass
uart_connection = None
continue
# Check for LED status receipt
if uart_client.in_waiting:
if uart_connection.in_waiting:
packet = Packet.from_stream(uart_client)
if isinstance(packet, ColorPacket):
if fancy.CRGB(*packet.color).pack() == GREEN: # Color match
Expand All @@ -79,3 +87,5 @@
color_index += fade_direction

sleep(0.02)

uart_connection = None
20 changes: 12 additions & 8 deletions BLE_Client_Server/server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from time import sleep
from adafruit_ble.uart_server import UARTServer
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.button_packet import ButtonPacket
from adafruit_bluefruit_connect.color_packet import ColorPacket
Expand All @@ -13,17 +15,19 @@
solenoid.direction = Direction.OUTPUT
solenoid.value = False

uart_server = UARTServer()
ble = BLERadio()
uart_service = UARTService()
advertisement = ProvideServicesAdvertisement(uart_service)

while True:
uart_server.start_advertising() # Advertise when not connected.
ble.start_advertising(advertisement) # Advertise when not connected.

while not uart_server.connected: # Wait for connection
while not ble.connected: # Wait for connection
pass

while uart_server.connected: # Connected
if uart_server.in_waiting: # Check BLE commands
packet = Packet.from_stream(uart_server)
while uart_service.connected: # Connected
if uart_service.in_waiting: # Check BLE commands
packet = Packet.from_stream(uart_service)
if isinstance(packet, ButtonPacket):
if packet.button == '1' and packet.pressed:
solenoid.value = True # Activate solenoid for 1 second
Expand All @@ -35,7 +39,7 @@
# Color: red = off, green = on
color_packet = ColorPacket((255 * int(not led_on), 255 * led_on, 0))
try:
uart_server.write(color_packet.to_bytes()) # Transmit state color
uart_service.write(color_packet.to_bytes()) # Transmit state color
except OSError:
pass

Expand Down

0 comments on commit 80a9f38

Please sign in to comment.