Skip to content

Commit 3274cef

Browse files
authored
pybricks.common: Add new BLE class.
This adds a new BLE class that is used for connectionless broadcasting/ observing on hubs with built-in Bluetooth Low Energy. Also see pybricks/pybricks-micropython#158.
1 parent b4ff3af commit 3274cef

16 files changed

+413
-12
lines changed

doc/common/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ def on_missing_reference(
325325
# warnings when Sphinx tries to cross reference units like deg/s. For
326326
# consistency, we also treat units without special characters this way.
327327
for unit in [
328+
"dBm",
328329
"deg",
329330
"deg/s",
330331
"deg/s²",

doc/main/hubs/cityhub.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ City Hub
1919

2020
.. automethod:: pybricks.hubs::CityHub.light.animate
2121

22+
.. rubric:: Using connectionless Bluetooth messaging
23+
24+
``ble.broadcast()`` does not work on ``CityHub`` due to a bug in the
25+
Bluetooth chip firmware.
26+
27+
.. automethod:: pybricks.hubs::CityHub.ble.observe
28+
29+
.. automethod:: pybricks.hubs::CityHub.ble.signal_strength
30+
31+
.. automethod:: pybricks.hubs::CityHub.ble.version
32+
2233
.. rubric:: Using the battery
2334

2435
.. automethod:: pybricks.hubs::CityHub.battery.voltage
@@ -70,6 +81,17 @@ Creating light animations
7081
.. literalinclude::
7182
../../../examples/pup/hub_common/build/light_animate_cityhub.py
7283

84+
85+
Bluetooth examples
86+
------------------
87+
88+
Observing data from other hubs
89+
******************************
90+
91+
.. literalinclude::
92+
../../../examples/pup/hub_common/build/ble_observe_cityhub.py
93+
94+
7395
Button and system examples
7496
----------------------------------
7597

doc/main/hubs/essentialhub.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ Essential Hub
4747

4848
.. automethod:: pybricks.hubs::EssentialHub.imu.settings
4949

50+
.. rubric:: Using connectionless Bluetooth messaging
51+
52+
.. automethod:: pybricks.hubs::EssentialHub.ble.broadcast
53+
54+
.. automethod:: pybricks.hubs::EssentialHub.ble.observe
55+
56+
.. automethod:: pybricks.hubs::EssentialHub.ble.signal_strength
57+
58+
.. automethod:: pybricks.hubs::EssentialHub.ble.version
59+
5060
.. rubric:: Using the battery
5161

5262
.. automethod:: pybricks.hubs::EssentialHub.battery.voltage
@@ -136,6 +146,23 @@ Reading acceleration and angular velocity on one axis
136146
.. literalinclude::
137147
../../../examples/pup/hub_common/build/imu_read_scalar_essentialhub.py
138148

149+
150+
Bluetooth examples
151+
------------------
152+
153+
Broadcasting data to other hubs
154+
*******************************
155+
156+
.. literalinclude::
157+
../../../examples/pup/hub_common/build/ble_broadcast_essentialhub.py
158+
159+
Observing data from other hubs
160+
******************************
161+
162+
.. literalinclude::
163+
../../../examples/pup/hub_common/build/ble_observe_essentialhub.py
164+
165+
139166
System examples
140167
----------------------------------
141168

doc/main/hubs/movehub.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ Move Hub
3131

3232
Changed acceleration units from m/s² to mm/s².
3333

34+
.. rubric:: Using connectionless Bluetooth messaging
35+
36+
.. automethod:: pybricks.hubs::MoveHub.ble.broadcast
37+
38+
.. automethod:: pybricks.hubs::MoveHub.ble.observe
39+
40+
.. automethod:: pybricks.hubs::MoveHub.ble.signal_strength
41+
42+
.. automethod:: pybricks.hubs::MoveHub.ble.version
43+
3444
.. rubric:: Using the battery
3545

3646
.. automethod:: pybricks.hubs::MoveHub.battery.voltage
@@ -85,6 +95,23 @@ Reading acceleration
8595
.. literalinclude::
8696
../../../examples/pup/hub_movehub/imu_read_acceleration.py
8797

98+
99+
Bluetooth examples
100+
------------------
101+
102+
Broadcasting data to other hubs
103+
*******************************
104+
105+
.. literalinclude::
106+
../../../examples/pup/hub_common/build/ble_broadcast_movehub.py
107+
108+
Observing data from other hubs
109+
******************************
110+
111+
.. literalinclude::
112+
../../../examples/pup/hub_common/build/ble_observe_movehub.py
113+
114+
88115
Button and system examples
89116
----------------------------------
90117

doc/main/hubs/primehub.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ Prime Hub / Inventor Hub
8989

9090
.. automethod:: pybricks.hubs::PrimeHub.speaker.play_notes
9191

92+
.. rubric:: Using connectionless Bluetooth messaging
93+
94+
.. automethod:: pybricks.hubs::PrimeHub.ble.broadcast
95+
96+
.. automethod:: pybricks.hubs::PrimeHub.ble.observe
97+
98+
.. automethod:: pybricks.hubs::PrimeHub.ble.signal_strength
99+
100+
.. automethod:: pybricks.hubs::PrimeHub.ble.version
101+
92102
.. rubric:: Using the battery
93103

94104
.. automethod:: pybricks.hubs::PrimeHub.battery.voltage
@@ -247,6 +257,23 @@ Reading acceleration and angular velocity on one axis
247257
.. literalinclude::
248258
../../../examples/pup/hub_common/build/imu_read_scalar_primehub.py
249259

260+
261+
Bluetooth examples
262+
------------------
263+
264+
Broadcasting data to other hubs
265+
*******************************
266+
267+
.. literalinclude::
268+
../../../examples/pup/hub_common/build/ble_broadcast_primehub.py
269+
270+
Observing data from other hubs
271+
******************************
272+
273+
.. literalinclude::
274+
../../../examples/pup/hub_common/build/ble_observe_primehub.py
275+
276+
250277
System examples
251278
----------------------------------
252279

doc/main/hubs/technichub.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ Technic Hub
4343

4444
.. automethod:: pybricks.hubs::TechnicHub.imu.settings
4545

46+
.. rubric:: Using connectionless Bluetooth messaging
47+
48+
.. automethod:: pybricks.hubs::TechnicHub.ble.broadcast
49+
50+
.. automethod:: pybricks.hubs::TechnicHub.ble.observe
51+
52+
.. automethod:: pybricks.hubs::TechnicHub.ble.signal_strength
53+
54+
.. automethod:: pybricks.hubs::TechnicHub.ble.version
55+
4656
.. rubric:: Using the battery
4757

4858
.. automethod:: pybricks.hubs::TechnicHub.battery.voltage
@@ -128,6 +138,23 @@ Reading acceleration and angular velocity on one axis
128138
.. literalinclude::
129139
../../../examples/pup/hub_common/build/imu_read_scalar_technichub.py
130140

141+
142+
Bluetooth examples
143+
------------------
144+
145+
Broadcasting data to other hubs
146+
*******************************
147+
148+
.. literalinclude::
149+
../../../examples/pup/hub_common/build/ble_broadcast_technichub.py
150+
151+
Observing data from other hubs
152+
******************************
153+
154+
.. literalinclude::
155+
../../../examples/pup/hub_common/build/ble_observe_technichub.py
156+
157+
131158
Button and system examples
132159
----------------------------------
133160

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# ThisHub = MoveHub CityHub TechnicHub PrimeHub EssentialHub
2+
from pybricks.hubs import ThisHub
3+
from pybricks.pupdevices import Motor
4+
from pybricks.parameters import Port
5+
from pybricks.tools import wait
6+
7+
# Initialize the hub.
8+
hub = ThisHub(broadcast_channel=1)
9+
10+
# Initialize the motors.
11+
left_motor = Motor(Port.A)
12+
right_motor = Motor(Port.B)
13+
14+
while True:
15+
# Read the motor angles to be sent to the other hub.
16+
left_angle = left_motor.angle()
17+
right_angle = right_motor.angle()
18+
19+
# Set the broadcast data and start broadcasting if not already doing so.
20+
hub.ble.broadcast(left_angle, right_angle)
21+
22+
# Broadcasts are only sent every 100 milliseconds, so there is no reason
23+
# to call the broadcast() method more often than that.
24+
wait(100)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# ThisHub = MoveHub CityHub TechnicHub PrimeHub EssentialHub
2+
from pybricks.hubs import ThisHub
3+
from pybricks.pupdevices import Motor
4+
from pybricks.parameters import Color, Port
5+
from pybricks.tools import wait
6+
7+
# Initialize the hub.
8+
hub = ThisHub(observe_channels=[1])
9+
10+
# Initialize the motors.
11+
left_motor = Motor(Port.A)
12+
right_motor = Motor(Port.B)
13+
14+
while True:
15+
# Receive broadcast from the other hub.
16+
17+
data = hub.ble.observe(1)
18+
19+
if data is None:
20+
# No data has been received in the last 1 second.
21+
hub.light.on(Color.RED)
22+
else:
23+
# Data was received and is less that one second old.
24+
hub.light.on(Color.GREEN)
25+
26+
# *data* contains the same values in the same order
27+
# that were passed to hub.ble.broadcast() on the
28+
# other hub.
29+
left_angle = data[0]
30+
right_angle = data[1]
31+
32+
# Make the motors on this hub mirror the position of the
33+
# motors on the other hub.
34+
left_motor.track_target(left_angle)
35+
right_motor.track_target(right_angle)
36+
37+
# Broadcasts are only sent every 100 milliseconds, so there is
38+
# no reason to call the observe() method more often than that.
39+
wait(100)

jedi/tests/test_complete_city_hub.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def test_hub_dot():
3333
completions: list[CompletionItem] = json.loads(complete(code, 3, len(line) + 1))
3434
assert [c["insertText"] for c in completions] == [
3535
"battery",
36+
"ble",
3637
"button",
3738
"light",
3839
"system",

jedi/tests/test_complete_essential_hub.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def test_hub_dot():
3333
completions: list[CompletionItem] = json.loads(complete(code, 3, len(line) + 1))
3434
assert [c["insertText"] for c in completions] == [
3535
"battery",
36+
"ble",
3637
"button",
3738
"charger",
3839
"imu",

0 commit comments

Comments
 (0)