Skip to content

Commit f59513c

Browse files
committed
pybricksdev.connections.pybricks: add user program ID to start_user_program()
Add an optional user program ID argument to the start_user_program() method of PybricksHub to allow starting built-in programs.
1 parent 364ba4b commit f59513c

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Added
10+
- Added optional user program ID arg to `PybricksHub.start_user_program()`.
11+
912
### Fixed
1013
- Fixed calling `PybricksHub.write()` methods.
1114

pybricksdev/ble/pybricks.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,43 @@ class HubCapabilityFlag(IntFlag):
314314
"""
315315

316316

317+
class UserProgramId(IntEnum):
318+
"""
319+
User program identifiers.
320+
"""
321+
322+
FIRST_SLOT = 0
323+
"""
324+
The first user program slot.
325+
326+
.. availability:: Since Pybricks protocol v1.4.0.
327+
"""
328+
LAST_SLOT = 127
329+
"""
330+
The last user program slot.
331+
332+
.. availability:: Since Pybricks protocol v1.4.0.
333+
"""
334+
REPL = 128
335+
"""
336+
The REPL builtin program slot.
337+
338+
.. availability:: Since Pybricks protocol v1.4.0.
339+
"""
340+
PORT_VIEW = 129
341+
"""
342+
The port view builtin program slot.
343+
344+
.. availability:: Since Pybricks protocol v1.4.0.
345+
"""
346+
IMU_CALIBRATION = 130
347+
"""
348+
The IMU calibration builtin program slot.
349+
350+
.. availability:: Since Pybricks protocol v1.4.0.
351+
"""
352+
353+
317354
def unpack_hub_capabilities(data: bytes) -> Tuple[int, HubCapabilityFlag, int]:
318355
"""
319356
Unpacks the value read from the hub capabilities characteristic.

pybricksdev/connections/pybricks.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
Event,
3737
HubCapabilityFlag,
3838
StatusFlag,
39+
UserProgramId,
3940
unpack_hub_capabilities,
4041
unpack_pnp_id,
4142
)
@@ -471,18 +472,29 @@ async def download_user_program(self, program: bytes) -> None:
471472
response=True,
472473
)
473474

474-
async def start_user_program(self) -> None:
475+
async def start_user_program(self, slot: UserProgramId | int | None = None) -> None:
475476
"""
476477
Starts the user program that is already in RAM on the hub.
477478
479+
Args:
480+
slot: The user program slot to start. If None, the currently selected slot is used.
481+
478482
Requires hub with Pybricks Profile >= v1.2.0.
483+
484+
User program ID requires hub with Pybricks Profile >= v1.4.0.
479485
"""
486+
487+
if slot is None:
488+
slot = self._selected_slot
489+
490+
# TODO: add fallback for starting REPL on older firmware
491+
480492
await self.write_gatt_char(
481493
PYBRICKS_COMMAND_EVENT_UUID,
482494
(
483495
struct.pack("<B", Command.START_USER_PROGRAM)
484496
if self._num_of_slots == 0
485-
else struct.pack("<BB", Command.START_USER_PROGRAM, self._selected_slot)
497+
else struct.pack("<BB", Command.START_USER_PROGRAM, slot)
486498
),
487499
response=True,
488500
)

0 commit comments

Comments
 (0)