Skip to content

Commit 9303ca0

Browse files
committed
connections: fix running programs on Move hub
Move Hub has a fixed MTU at 23 bytes, so we have to divide the 100 byte chunks into 20 byte chunks when writing to the BLE characteristic. Fixes: #19
1 parent 139393f commit 9303ca0

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010
- Added support for Pybricks Protocol v1.1.0.
1111

12+
### Fixed
13+
- Fixed `pybricksdev ble run` not working with BOOST Move hub.
14+
1215
## [1.0.0-alpha.10] - 2021-06-27
1316
### Added
1417
- Support for Python 3.9.

pybricksdev/connections.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
from tqdm.contrib.logging import logging_redirect_tqdm
1818

1919
from .ble import BLEConnection
20+
from .ble.lwp3.bytecodes import HubKind
2021
from .ble.nus import NUS_RX_UUID, NUS_TX_UUID
2122
from .ble.pybricks import (
2223
PYBRICKS_CONTROL_UUID,
2324
PYBRICKS_PROTOCOL_VERSION,
2425
SW_REV_UUID,
26+
PNP_ID_UUID,
2527
Event,
2628
Status,
29+
unpack_pnp_id,
2730
)
2831
from .compile import compile_file
2932
from .tools.checksum import xor_bytes
@@ -601,6 +604,9 @@ def __init__(self):
601604
# used to notify when the user program has ended
602605
self.user_program_stopped = asyncio.Event()
603606

607+
self.hub_kind: HubKind
608+
self.hub_variant: int
609+
604610
# File handle for logging
605611
self.log_file = None
606612

@@ -722,13 +728,18 @@ def disconnected_handler(self, _: BleakClient):
722728
logger.info("Connected successfully!")
723729
protocol_version = await self.client.read_gatt_char(SW_REV_UUID)
724730
protocol_version = semver.VersionInfo.parse(protocol_version.decode())
731+
725732
if (
726733
protocol_version < PYBRICKS_PROTOCOL_VERSION
727734
or protocol_version >= PYBRICKS_PROTOCOL_VERSION.bump_major()
728735
):
729736
raise RuntimeError(
730737
f"Unsupported Pybricks protocol version: {protocol_version}"
731738
)
739+
740+
pnp_id = await self.client.read_gatt_char(PNP_ID_UUID)
741+
_, _, self.hub_kind, self.hub_variant = unpack_pnp_id(pnp_id)
742+
732743
await self.client.start_notify(NUS_TX_UUID, self.nus_handler)
733744
await self.client.start_notify(
734745
PYBRICKS_CONTROL_UUID, self.pybricks_service_handler
@@ -749,7 +760,15 @@ async def send_block(self, data):
749760
self.checksum_ready.clear()
750761
self.expected_checksum = xor_bytes(data, 0)
751762
try:
752-
await self.client.write_gatt_char(NUS_RX_UUID, bytearray(data), False)
763+
if self.hub_kind == HubKind.BOOST:
764+
# BOOST Move hub has fixed MTU of 23 so we can only send 20
765+
# bytes at a time
766+
for i in range(0, len(data), 20):
767+
await self.client.write_gatt_char(
768+
NUS_RX_UUID, data[i : i + 20], False
769+
)
770+
else:
771+
await self.client.write_gatt_char(NUS_RX_UUID, data, False)
753772
await asyncio.wait_for(self.checksum_ready.wait(), timeout=0.5)
754773
except: # noqa: E722
755774
# normally self.expected_checksum = -1 will be called in nus_handler()

0 commit comments

Comments
 (0)