Skip to content

Commit 33056f4

Browse files
committed
pybricksdev.connections.pybricks: make line handler optional
Long running programs may want to ignore stdout from the hub instead of maintaining an infinite log. We also plan on adding an optional stdout subscription that is an alternative to the line handler. So in these cases, it is desireable to disable the default line handler.
1 parent dd50822 commit 33056f4

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

pybricksdev/connections/pybricks.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ def __init__(self):
8787
self._capability_flags = HubCapabilityFlag(0)
8888
self._max_user_program_size = 0
8989

90+
# whether to enable line handler features or not
91+
self._enable_line_handler = False
92+
9093
# buffered stdout from the hub for splitting into lines
9194
self._stdout_buf = bytearray()
9295

@@ -198,15 +201,19 @@ def _nus_handler(self, sender, data: bytearray) -> None:
198201
# support legacy firmware where the Nordic UART service
199202
# was used for stdio
200203
if self._legacy_stdio:
201-
self._handle_line_data(data)
204+
if self._enable_line_handler:
205+
self._handle_line_data(data)
202206

203207
def _pybricks_service_handler(self, _: int, data: bytes) -> None:
204208
if data[0] == Event.STATUS_REPORT:
205209
# decode the payload
206210
(flags,) = struct.unpack_from("<I", data, 1)
207211
self.status_observable.on_next(StatusFlag(flags))
208212
elif data[0] == Event.WRITE_STDOUT:
209-
self._handle_line_data(data[1:])
213+
payload = data[1:]
214+
215+
if self._enable_line_handler:
216+
self._handle_line_data(payload)
210217

211218
async def connect(self, device: BLEDevice):
212219
"""Connects to a device that was discovered with :meth:`pybricksdev.ble.find_device`
@@ -422,7 +429,11 @@ async def stop_user_program(self) -> None:
422429
)
423430

424431
async def run(
425-
self, py_path: str, wait: bool = True, print_output: bool = True
432+
self,
433+
py_path: str,
434+
wait: bool = True,
435+
print_output: bool = True,
436+
line_handler: bool = True,
426437
) -> None:
427438
"""
428439
Compiles and runs a user program.
@@ -431,6 +442,7 @@ async def run(
431442
py_path: The path to the .py file to compile.
432443
wait: If true, wait for the user program to stop before returning.
433444
print_output: If true, echo stdout of the hub to ``sys.stdout``.
445+
line_handler: If true enable hub stdout line handler features.
434446
"""
435447
if self.connection_state_observable.value != ConnectionState.CONNECTED:
436448
raise RuntimeError("not connected")
@@ -442,6 +454,7 @@ async def run(
442454
self._stdout_line_queue = asyncio.Queue()
443455
self.print_output = print_output
444456
self.script_dir, _ = os.path.split(py_path)
457+
self._enable_line_handler = line_handler
445458

446459
# maintain compatibility with older firmware (Pybricks profile < 1.2.0).
447460
if self._mpy_abi_version:

0 commit comments

Comments
 (0)