Skip to content

first commit Circuit Playground Bluefruit Color Remote code client/periph #888

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 1 commit into from
Oct 19, 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
@@ -0,0 +1,48 @@
"""
Demonstration of a Bluefruit BLE Central/client. Connects to the first BLE UART peripheral it finds.
Sends Bluefruit ColorPackets, read from three potentiometers, to the peripheral.
"""

import time

import board
from analogio import AnalogIn

#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

from adafruit_ble.scanner import Scanner
from adafruit_ble.uart_client import UARTClient

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()

a3 = AnalogIn(board.A4)
a4 = AnalogIn(board.A5)
a5 = 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)

while uart_client.connected:
r = scale(a3.value)
g = scale(a4.value)
b = scale(a5.value)

color = (r, g, b)
print(color)
color_packet = ColorPacket(color)
try:
uart_client.write(color_packet.to_bytes())
except OSError:
pass
time.sleep(0.3)
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
Used along with cpb_remote_color_client.py. Receives Bluefruit LE ColorPackets from a central,
and updates NeoPixels to show the history of the received packets.
"""

import board
import neopixel

from adafruit_ble.uart_server import UARTServer
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()

NUM_PIXELS = 10
np = neopixel.NeoPixel(board.NEOPIXEL, NUM_PIXELS, brightness=0.1)
next_pixel = 0

def mod(i):
"""Wrap i to modulus NUM_PIXELS."""
return i % NUM_PIXELS

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

while uart_server.connected:
packet = Packet.from_stream(uart_server)
if isinstance(packet, ColorPacket):
print(packet.color)
np[next_pixel] = packet.color
np[mod(next_pixel + 1)] = (0, 0, 0)
next_pixel = (next_pixel + 1) % NUM_PIXELS