Skip to content

update Circuit_Playground_Bluefruit_Color_Remote to CircuitPython 5.0.0-beta.0 BLE API #940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,48 @@
# 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)

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)
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 uart_client.connected:
r = scale(a3.value)
g = scale(a4.value)
b = scale(a5.value)
while True:
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_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)
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
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)
Expand All @@ -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
Expand Down