Skip to content

Commit 15537e2

Browse files
authored
Merge branch 'master' into jerryn_snowglobe
2 parents ba8d446 + 7b2dd56 commit 15537e2

File tree

4 files changed

+66
-41
lines changed

4 files changed

+66
-41
lines changed

BLE_Crickit_Light_Switch/ble_crickit_light_switch.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# BLE Crickit Light Switch
22
# Use with the Adafruit BlueFruit LE Connect app
3-
# Works with CircuitPython 4.0.0-beta.1 and later
3+
# Works with CircuitPython 5.0.0-beta.0 and later
44
# running on an nRF52840 Feather board and Crickit FeatherWing
55
# micro servo, 3D printed switch actuator
66

@@ -10,7 +10,10 @@
1010
import digitalio
1111

1212
from adafruit_crickit import crickit
13-
from adafruit_ble.uart import UARTServer
13+
14+
from adafruit_ble import BLERadio
15+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
16+
from adafruit_ble.services.nordic import UARTService
1417

1518
from adafruit_bluefruit_connect.packet import Packet
1619
# 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
UP_ANGLE = 180
2833
NEUTRAL_ANGLE = 120
@@ -35,17 +40,17 @@
3540
print("Use Adafruit Bluefruit app to connect")
3641
while True:
3742
blue_led.value = False
38-
uart_server.start_advertising()
43+
ble.start_advertising(advertisement)
3944

40-
while not uart_server.connected:
45+
while not ble.connected:
4146
# Wait for a connection.
4247
pass
4348
blue_led.value = True # turn on blue LED when connected
44-
while uart_server.connected:
45-
if uart_server.in_waiting:
49+
while ble.connected:
50+
if uart_service.in_waiting:
4651
# Packet is arriving.
4752
red_led.value = False # turn off red LED
48-
packet = Packet.from_stream(uart_server)
53+
packet = Packet.from_stream(uart_service)
4954
if isinstance(packet, ButtonPacket) and packet.pressed:
5055
red_led.value = True # blink to show a packet has been received
5156
if packet.button == ButtonPacket.UP and angle != UP_ANGLE: # UP button pressed

Circuit_Playground_Bluefruit_Color_Remote/cpb_remote_color_client.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,48 @@
1212
# Only the packet classes that are imported will be known to Packet.
1313
from adafruit_bluefruit_connect.color_packet import ColorPacket
1414

15-
from adafruit_ble.scanner import Scanner
16-
from adafruit_ble.uart_client import UARTClient
15+
from adafruit_ble import BLERadio
16+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
17+
from adafruit_ble.services.nordic import UARTService
1718

1819
def scale(value):
1920
"""Scale an value from 0-65535 (AnalogIn range) to 0-255 (RGB range)"""
2021
return int(value / 65535 * 255)
2122

22-
scanner = Scanner()
23-
uart_client = UARTClient()
23+
ble = BLERadio()
2424

25-
a3 = AnalogIn(board.A4)
26-
a4 = AnalogIn(board.A5)
27-
a5 = AnalogIn(board.A6)
25+
a4 = AnalogIn(board.A4)
26+
a5 = AnalogIn(board.A5)
27+
a6 = AnalogIn(board.A6)
2828

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)
29+
uart_connection = None
30+
31+
# See if any existing connections are providing UARTService.
32+
if ble.connected:
33+
for connection in ble.connections:
34+
if UARTService in connection:
35+
uart_connection = connection
36+
break
3537

36-
while uart_client.connected:
37-
r = scale(a3.value)
38-
g = scale(a4.value)
39-
b = scale(a5.value)
38+
while True:
39+
if not uart_connection:
40+
for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5):
41+
if UARTService in adv.services:
42+
uart_connection = ble.connect(adv)
43+
break
44+
# Stop scanning whether or not we are connected.
45+
ble.stop_scan()
46+
47+
while uart_connection and uart_connection.connected:
48+
r = scale(a4.value)
49+
g = scale(a5.value)
50+
b = scale(a6.value)
4051

4152
color = (r, g, b)
4253
print(color)
4354
color_packet = ColorPacket(color)
4455
try:
45-
uart_client.write(color_packet.to_bytes())
56+
uart_connection[UARTService].write(color_packet.to_bytes())
4657
except OSError:
4758
pass
4859
time.sleep(0.3)

Circuit_Playground_Bluefruit_Color_Remote/cpb_remote_color_periph.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
import board
77
import neopixel
88

9-
from adafruit_ble.uart_server import UARTServer
9+
from adafruit_ble import BLERadio
10+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
11+
from adafruit_ble.services.nordic import UARTService
12+
1013
from adafruit_bluefruit_connect.packet import Packet
1114
# Only the packet classes that are imported will be known to Packet.
1215
from adafruit_bluefruit_connect.color_packet import ColorPacket
1316

14-
uart_server = UARTServer()
17+
ble = BLERadio()
18+
uart = UARTService()
19+
advertisement = ProvideServicesAdvertisement(uart)
1520

1621
NUM_PIXELS = 10
1722
np = neopixel.NeoPixel(board.NEOPIXEL, NUM_PIXELS, brightness=0.1)
@@ -23,12 +28,12 @@ def mod(i):
2328

2429
while True:
2530
# Advertise when not connected.
26-
uart_server.start_advertising()
27-
while not uart_server.connected:
31+
ble.start_advertising(advertisement)
32+
while not ble.connected:
2833
pass
2934

30-
while uart_server.connected:
31-
packet = Packet.from_stream(uart_server)
35+
while ble.connected:
36+
packet = Packet.from_stream(uart)
3237
if isinstance(packet, ColorPacket):
3338
print(packet.color)
3439
np[next_pixel] = packet.color

Turtle_BLE_CPB_Crickit_Rover/turtle_ble_cpb_crickit_rover.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
# Circuit Playground Bluefruit Rover
22
# Use with the Adafruit BlueFruit LE Connect app
3-
# Works with CircuitPython 4.0.0-beta.1 and later
3+
# Works with CircuitPython 5.0.0-beta.0 and later
44
# running on an nRF52840 CPB board and Crickit
55

66
import time
77
import board
88
import digitalio
99
import neopixel
1010
from adafruit_crickit import crickit
11-
from adafruit_ble.uart_server import UARTServer
11+
12+
from adafruit_ble import BLERadio
13+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
14+
from adafruit_ble.services.nordic import UARTService
1215

1316
from adafruit_bluefruit_connect.packet import Packet
1417
# Only the packet classes that are imported will be known to Packet.
@@ -19,7 +22,9 @@
1922
red_led = digitalio.DigitalInOut(board.D13)
2023
red_led.direction = digitalio.Direction.OUTPUT
2124

22-
uart_server = UARTServer()
25+
ble = BLERadio()
26+
uart_service = UARTService()
27+
advertisement = ProvideServicesAdvertisement(uart_service)
2328

2429
# motor setup
2530
motor_1 = crickit.dc_motor_1
@@ -42,21 +47,20 @@
4247
print("BLE Turtle Rover")
4348
print("Use Adafruit Bluefruit app to connect")
4449
while True:
45-
# blue_led.value = False
4650
neopixels[0] = BLACK
4751
neopixels.show()
48-
uart_server.start_advertising()
49-
while not uart_server.connected:
52+
ble.start_advertising(advertisement)
53+
while not ble.connected:
5054
# Wait for a connection.
5155
pass
5256
# set a pixel blue when connected
5357
neopixels[0] = BLUE
5458
neopixels.show()
55-
while uart_server.connected:
56-
if uart_server.in_waiting:
59+
while ble.connected:
60+
if uart_service.in_waiting:
5761
# Packet is arriving.
5862
red_led.value = False # turn off red LED
59-
packet = Packet.from_stream(uart_server)
63+
packet = Packet.from_stream(uart_service)
6064
if isinstance(packet, ColorPacket):
6165
# Change the color.
6266
color = packet.color

0 commit comments

Comments
 (0)