Skip to content

Commit eaf56fa

Browse files
authored
Merge branch 'master' into master
2 parents b82736a + 51679ea commit eaf56fa

File tree

33 files changed

+3387
-139
lines changed

33 files changed

+3387
-139
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

Bluetooth_Luminaries/code.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
import neopixel
66
import touchio
77
import adafruit_fancyled.adafruit_fancyled as fancy
8-
# from adafruit_ble.uart import UARTServer
9-
# for >= CPy 5.0.0
10-
from adafruit_ble.uart_server import UARTServer
118
from adafruit_bluefruit_connect.packet import Packet
129
from adafruit_bluefruit_connect.button_packet import ButtonPacket
1310
from adafruit_bluefruit_connect.color_packet import ColorPacket
1411

12+
13+
from adafruit_ble import BLERadio
14+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
15+
from adafruit_ble.services.nordic import UARTService
16+
17+
18+
1519
NUM_LEDS = 24 # change to reflect your total number of ring LEDs
1620
RING_PIN = board.A1 # change to reflect your wiring
1721
CPX_PIN = board.D8 # CPX Neopixels live on pin D8
@@ -66,7 +70,11 @@
6670
offset_increment = 6
6771
OFFSET_MAX = 1000000
6872

69-
uart_server = UARTServer()
73+
# Setup BLE
74+
ble = BLERadio()
75+
uart = UARTService()
76+
advertisement = ProvideServicesAdvertisement(uart)
77+
7078

7179
def set_palette(palette):
7280
for i in range(NUM_LEDS):
@@ -101,17 +109,17 @@ def set_palette(palette):
101109
set_palette(palette_choice)
102110
offset = (offset + offset_increment) % OFFSET_MAX
103111

104-
if not uart_server.connected and not advertising:
105-
uart_server.start_advertising()
112+
if not ble.connected and not advertising:
113+
ble.start_advertising(advertisement)
106114
advertising = True
107115

108116
# Are we connected via Bluetooth now?
109-
if uart_server.connected:
117+
if ble.connected:
110118
# Once we're connected, we're not advertising any more.
111119
advertising = False
112120
# Have we started to receive a packet?
113-
if uart_server.in_waiting:
114-
packet = Packet.from_stream(uart_server)
121+
if uart.in_waiting:
122+
packet = Packet.from_stream(uart)
115123
if isinstance(packet, ColorPacket):
116124
cycling = False
117125
# Set all the pixels to one color and stay there.
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

CircuitPython_PyPaint/code.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ def set_cursor_bitmap(self, bmp):
146146
self.poke()
147147

148148
################################################################################
149-
150149
class Paint(object):
151150

151+
#pylint:disable=too-many-statements
152152
def __init__(self, display=board.DISPLAY):
153153
self._logger = logging.getLogger("Paint")
154154
self._logger.setLevel(logging.DEBUG)
@@ -196,9 +196,13 @@ def __init__(self, display=board.DISPLAY):
196196
self._splash.append(self._palette)
197197

198198
self._display.show(self._splash)
199-
self._display.refresh_soon()
200-
gc.collect()
201-
self._display.wait_for_frame()
199+
try:
200+
gc.collect()
201+
self._display.refresh(target_frames_per_second=60)
202+
except AttributeError:
203+
self._display.refresh_soon()
204+
gc.collect()
205+
self._display.wait_for_frame()
202206

203207
self._brush = 0
204208
self._cursor_bitmaps = [self._cursor_bitmap_1(), self._cursor_bitmap_3()]
@@ -215,6 +219,7 @@ def __init__(self, display=board.DISPLAY):
215219
self._last_location = None
216220

217221
self._pencolor = 7
222+
#pylint:enable=too-many-statements
218223

219224
def _make_palette(self):
220225
self._palette_bitmap = displayio.Bitmap(self._w // 10, self._h, 5)

CircuitPython_displayio/displayio_display_driver.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,3 @@
3737
group = displayio.Group()
3838
group.append(tile_grid)
3939
display.show(group)
40-
display.refresh_soon()

CircuitPython_displayio/displayio_display_manual.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,3 @@
6464
group = displayio.Group()
6565
group.append(tile_grid)
6666
display.show(group)
67-
display.refresh_soon()

CircuitPython_displayio/displayio_parallelbus.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,3 @@
6262
group = displayio.Group()
6363
group.append(tile_grid)
6464
display.show(group)
65-
display.refresh_soon()

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

DigiKey_IoT_Home/door-closed.png

30.5 KB
Loading

DigiKey_IoT_Home/door-open.png

31.2 KB
Loading
51.6 KB
Loading

0 commit comments

Comments
 (0)