From 33056f4a0f18e4b3d340edda0d976355dfb8cf3e Mon Sep 17 00:00:00 2001 From: David Lechner Date: Fri, 21 Apr 2023 12:17:14 -0500 Subject: [PATCH] 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. --- pybricksdev/connections/pybricks.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/pybricksdev/connections/pybricks.py b/pybricksdev/connections/pybricks.py index 0bea776..4ea3bfc 100644 --- a/pybricksdev/connections/pybricks.py +++ b/pybricksdev/connections/pybricks.py @@ -87,6 +87,9 @@ def __init__(self): self._capability_flags = HubCapabilityFlag(0) self._max_user_program_size = 0 + # whether to enable line handler features or not + self._enable_line_handler = False + # buffered stdout from the hub for splitting into lines self._stdout_buf = bytearray() @@ -198,7 +201,8 @@ def _nus_handler(self, sender, data: bytearray) -> None: # support legacy firmware where the Nordic UART service # was used for stdio if self._legacy_stdio: - self._handle_line_data(data) + if self._enable_line_handler: + self._handle_line_data(data) def _pybricks_service_handler(self, _: int, data: bytes) -> None: if data[0] == Event.STATUS_REPORT: @@ -206,7 +210,10 @@ def _pybricks_service_handler(self, _: int, data: bytes) -> None: (flags,) = struct.unpack_from(" None: ) async def run( - self, py_path: str, wait: bool = True, print_output: bool = True + self, + py_path: str, + wait: bool = True, + print_output: bool = True, + line_handler: bool = True, ) -> None: """ Compiles and runs a user program. @@ -431,6 +442,7 @@ async def run( py_path: The path to the .py file to compile. wait: If true, wait for the user program to stop before returning. print_output: If true, echo stdout of the hub to ``sys.stdout``. + line_handler: If true enable hub stdout line handler features. """ if self.connection_state_observable.value != ConnectionState.CONNECTED: raise RuntimeError("not connected") @@ -442,6 +454,7 @@ async def run( self._stdout_line_queue = asyncio.Queue() self.print_output = print_output self.script_dir, _ = os.path.split(py_path) + self._enable_line_handler = line_handler # maintain compatibility with older firmware (Pybricks profile < 1.2.0). if self._mpy_abi_version: