1717from tqdm .contrib .logging import logging_redirect_tqdm
1818
1919from .ble import BLEConnection
20+ from .ble .lwp3 .bytecodes import HubKind
2021from .ble .nus import NUS_RX_UUID , NUS_TX_UUID
2122from .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)
2831from .compile import compile_file
2932from .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