Skip to content

Commit 54cd996

Browse files
Merge branch 'master' into ooze-ice
2 parents dbcf882 + 3c8961d commit 54cd996

File tree

2 files changed

+98
-7
lines changed

2 files changed

+98
-7
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
"""
2+
Circuit Playground Bluefruit Ornament Proximity
3+
This demo uses advertising to set the color of scanning devices depending on the strongest broadcast
4+
signal received. Circuit Playgrounds can be switched between advertising and scanning using the
5+
slide switch. The buttons change the color when advertising.
6+
"""
7+
8+
import time
9+
from adafruit_circuitplayground.bluefruit import cpb
10+
11+
from adafruit_ble import BLERadio
12+
from adafruit_ble.advertising.adafruit import AdafruitColor
13+
14+
# The color pickers will cycle through this list with buttons A and B.
15+
color_options = [0x110000,
16+
0x111100,
17+
0x001100,
18+
0x001111,
19+
0x000011,
20+
0x110011,
21+
0x111111,
22+
0x221111,
23+
0x112211,
24+
0x111122]
25+
26+
ble = BLERadio()
27+
28+
i = 0
29+
advertisement = AdafruitColor()
30+
advertisement.color = color_options[i]
31+
cpb.pixels.auto_write = False
32+
cpb.pixels.fill(color_options[i])
33+
while True:
34+
# The first mode is the color selector which broadcasts it's current color to other devices.
35+
if cpb.switch:
36+
print("Broadcasting color")
37+
ble.start_advertising(advertisement)
38+
while cpb.switch:
39+
last_i = i
40+
if cpb.button_a:
41+
i += 1
42+
if cpb.button_b:
43+
i -= 1
44+
i %= len(color_options)
45+
if last_i != i:
46+
color = color_options[i]
47+
cpb.pixels.fill(color)
48+
cpb.pixels.show()
49+
print("New color {:06x}".format(color))
50+
advertisement.color = color
51+
ble.stop_advertising()
52+
ble.start_advertising(advertisement)
53+
time.sleep(0.5)
54+
ble.stop_advertising()
55+
# The second mode listens for color broadcasts and shows the color of the strongest signal.
56+
else:
57+
closest = None
58+
closest_rssi = -80
59+
closest_last_time = 0
60+
print("Scanning for colors")
61+
while not cpb.switch:
62+
for entry in ble.start_scan(AdafruitColor, minimum_rssi=-100, timeout=1):
63+
if cpb.switch:
64+
break
65+
now = time.monotonic()
66+
new = False
67+
if entry.address == closest:
68+
pass
69+
elif entry.rssi > closest_rssi or now - closest_last_time > 0.4:
70+
closest = entry.address
71+
else:
72+
continue
73+
closest_rssi = entry.rssi
74+
closest_last_time = now
75+
discrete_strength = min((100 + entry.rssi) // 5, 10)
76+
cpb.pixels.fill(0x000000)
77+
for i in range(0, discrete_strength):
78+
cpb.pixels[i] = entry.color
79+
cpb.pixels.show()
80+
81+
# Clear the pixels if we haven't heard from anything recently.
82+
now = time.monotonic()
83+
if now - closest_last_time > 1:
84+
cpb.pixels.fill(0x000000)
85+
cpb.pixels.show()
86+
ble.stop_scan()

CircuitPython_BLE_Rover/circuitpython_ble_rover.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
import digitalio
1010

1111
from adafruit_crickit import crickit
12-
from adafruit_ble.uart import UARTServer
12+
13+
from adafruit_ble import BLERadio
14+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
15+
from adafruit_ble.services.nordic import UARTService
1316

1417
from adafruit_bluefruit_connect.packet import Packet
1518
# Only the packet classes that are imported will be known to Packet.
@@ -22,7 +25,9 @@
2225
blue_led.direction = digitalio.Direction.OUTPUT
2326
red_led.direction = digitalio.Direction.OUTPUT
2427

25-
uart_server = UARTServer()
28+
ble = BLERadio()
29+
uart_service = UARTService()
30+
advertisement = ProvideServicesAdvertisement(uart_service)
2631

2732
# motor setup
2833
motor_1 = crickit.dc_motor_1
@@ -46,16 +51,16 @@
4651
print("Use Adafruit Bluefruit app to connect")
4752
while True:
4853
blue_led.value = False
49-
uart_server.start_advertising()
50-
while not uart_server.connected:
54+
ble.start_advertising(advertisement)
55+
while not ble.connected:
5156
# Wait for a connection.
5257
pass
5358
blue_led.value = True # turn on blue LED when connected
54-
while uart_server.connected:
55-
if uart_server.in_waiting:
59+
while ble.connected:
60+
if uart_service.in_waiting:
5661
# Packet is arriving.
5762
red_led.value = False # turn off red LED
58-
packet = Packet.from_stream(uart_server)
63+
packet = Packet.from_stream(uart_service)
5964
if isinstance(packet, ColorPacket):
6065
# Change the color.
6166
color = packet.color

0 commit comments

Comments
 (0)