update to CircuitPython 5.0.0-beta.0 BLE API

This commit is contained in:
Dan Halbert 2019-11-24 17:22:07 -05:00
parent 8fced950b1
commit be2c608ae0
2 changed files with 39 additions and 23 deletions

View file

@ -12,37 +12,48 @@ from analogio import AnalogIn
# Only the packet classes that are imported will be known to Packet.
from adafruit_bluefruit_connect.color_packet import ColorPacket
from adafruit_ble.scanner import Scanner
from adafruit_ble.uart_client import UARTClient
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
def scale(value):
"""Scale an value from 0-65535 (AnalogIn range) to 0-255 (RGB range)"""
return int(value / 65535 * 255)
scanner = Scanner()
uart_client = UARTClient()
ble = BLERadio()
a3 = AnalogIn(board.A4)
a4 = AnalogIn(board.A5)
a5 = AnalogIn(board.A6)
a4 = AnalogIn(board.A4)
a5 = AnalogIn(board.A5)
a6 = AnalogIn(board.A6)
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
while True:
uart_addresses = []
# Keep trying to find a UART peripheral
while not uart_addresses:
uart_addresses = uart_client.scan(scanner)
uart_client.connect(uart_addresses[0], 5)
if not uart_connection:
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:
r = scale(a3.value)
g = scale(a4.value)
b = scale(a5.value)
while uart_connection and uart_connection.connected:
r = scale(a4.value)
g = scale(a5.value)
b = scale(a6.value)
color = (r, g, b)
print(color)
color_packet = ColorPacket(color)
try:
uart_client.write(color_packet.to_bytes())
uart_connection[UARTService].write(color_packet.to_bytes())
except OSError:
pass
time.sleep(0.3)

View file

@ -6,12 +6,17 @@ and updates NeoPixels to show the history of the received packets.
import board
import neopixel
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
# Only the packet classes that are imported will be known to Packet.
from adafruit_bluefruit_connect.color_packet import ColorPacket
uart_server = UARTServer()
ble = BLERadio()
uart = UARTService()
advertisement = ProvideServicesAdvertisement(uart)
NUM_PIXELS = 10
np = neopixel.NeoPixel(board.NEOPIXEL, NUM_PIXELS, brightness=0.1)
@ -23,12 +28,12 @@ def mod(i):
while True:
# Advertise when not connected.
uart_server.start_advertising()
while not uart_server.connected:
ble.start_advertising(advertisement)
while not ble.connected:
pass
while uart_server.connected:
packet = Packet.from_stream(uart_server)
while ble.connected:
packet = Packet.from_stream(uart)
if isinstance(packet, ColorPacket):
print(packet.color)
np[next_pixel] = packet.color