Skip to content

Commit cd2b7dd

Browse files
committed
Add and update examples
1 parent 579c116 commit cd2b7dd

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

adafruit_ble/uart_client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ def scan(scanner=None, scan_time=2):
101101
:param float scan_time: scan for this many seconds.
102102
:return list of Address objects, or an empty list, if none found
103103
"""
104+
if not scanner:
105+
scanner = Scanner()
106+
104107
return [se.address for se in
105108
ScanEntry.with_service_uuid(scanner.scan_unique(scan_time), NUS_SERVICE_UUID)]
106109

examples/ble_demo_central.py

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. 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+
uart_addresses = []
25+
26+
# Keep trying to find a UART peripheral
27+
while not uart_addresses:
28+
uart_addresses = uart_client.scan(scanner)
29+
30+
a0 = AnalogIn(board.A0)
31+
a1 = AnalogIn(board.A1)
32+
a2 = AnalogIn(board.A2)
33+
34+
while True:
35+
uart_client.connect(uart_addresses[0], 5)
36+
while uart_client.connected:
37+
r = scale(a0.value)
38+
g = scale(a1.value)
39+
b = scale(a2.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)

examples/ble_demo_periph.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Used with ble_demo_central.py. Receives Bluefruit LE ColorPackets from a central,
3+
and updates a NeoPixel FeatherWing 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 = 32
17+
np = neopixel.NeoPixel(board.D10, 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

examples/ble_uart_echo_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from adafruit_ble.uart import UARTServer
1+
from adafruit_ble.uart_server import UARTServer
22

33
uart = UARTServer()
44

0 commit comments

Comments
 (0)