Skip to content

Commit df0cc26

Browse files
authored
Merge pull request #888 from jedgarpark/circuit-playground-bluefruit-color-remote
first commit Circuit Playground Bluefruit Color Remote code client/periph
2 parents fe2a4dd + 6f3513d commit df0cc26

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
Demonstration of a Bluefruit BLE Central/client. Connects to the first BLE UART peripheral it finds.
3+
Sends Bluefruit ColorPackets, read from three potentiometers, to the peripheral.
4+
"""
5+
6+
import time
7+
8+
import board
9+
from analogio import AnalogIn
10+
11+
#from adafruit_bluefruit_connect.packet import Packet
12+
# Only the packet classes that are imported will be known to Packet.
13+
from adafruit_bluefruit_connect.color_packet import ColorPacket
14+
15+
from adafruit_ble.scanner import Scanner
16+
from adafruit_ble.uart_client import UARTClient
17+
18+
def scale(value):
19+
"""Scale an value from 0-65535 (AnalogIn range) to 0-255 (RGB range)"""
20+
return int(value / 65535 * 255)
21+
22+
scanner = Scanner()
23+
uart_client = UARTClient()
24+
25+
a3 = AnalogIn(board.A4)
26+
a4 = AnalogIn(board.A5)
27+
a5 = AnalogIn(board.A6)
28+
29+
while True:
30+
uart_addresses = []
31+
# Keep trying to find a UART peripheral
32+
while not uart_addresses:
33+
uart_addresses = uart_client.scan(scanner)
34+
uart_client.connect(uart_addresses[0], 5)
35+
36+
while uart_client.connected:
37+
r = scale(a3.value)
38+
g = scale(a4.value)
39+
b = scale(a5.value)
40+
41+
color = (r, g, b)
42+
print(color)
43+
color_packet = ColorPacket(color)
44+
try:
45+
uart_client.write(color_packet.to_bytes())
46+
except OSError:
47+
pass
48+
time.sleep(0.3)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Used along with cpb_remote_color_client.py. Receives Bluefruit LE ColorPackets from a central,
3+
and updates NeoPixels to show the history of the received packets.
4+
"""
5+
6+
import board
7+
import neopixel
8+
9+
from adafruit_ble.uart_server import UARTServer
10+
from adafruit_bluefruit_connect.packet import Packet
11+
# Only the packet classes that are imported will be known to Packet.
12+
from adafruit_bluefruit_connect.color_packet import ColorPacket
13+
14+
uart_server = UARTServer()
15+
16+
NUM_PIXELS = 10
17+
np = neopixel.NeoPixel(board.NEOPIXEL, NUM_PIXELS, brightness=0.1)
18+
next_pixel = 0
19+
20+
def mod(i):
21+
"""Wrap i to modulus NUM_PIXELS."""
22+
return i % NUM_PIXELS
23+
24+
while True:
25+
# Advertise when not connected.
26+
uart_server.start_advertising()
27+
while not uart_server.connected:
28+
pass
29+
30+
while uart_server.connected:
31+
packet = Packet.from_stream(uart_server)
32+
if isinstance(packet, ColorPacket):
33+
print(packet.color)
34+
np[next_pixel] = packet.color
35+
np[mod(next_pixel + 1)] = (0, 0, 0)
36+
next_pixel = (next_pixel + 1) % NUM_PIXELS

0 commit comments

Comments
 (0)