Skip to content

Commit

Permalink
pybricksdev.connections.pybricks: add stdout_observable property
Browse files Browse the repository at this point in the history
This adds a new property for subscribing to each received stdout
notification.

Fixes: https://github.com/orgs/pybricks/discussions/1038
  • Loading branch information
dlech committed Apr 21, 2023
1 parent a08db5f commit 6337ece
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- Added `PybricksHub.stdout_observable` property ([support#1038]).

### Fixed
- Fixed endline in `PybricksHub.write_line()`.

[support#1038]: https://github.com/orgs/pybricks/discussions/1038

## [1.0.0-alpha.44] - 2023-04-20

### Fixed
Expand Down
22 changes: 22 additions & 0 deletions pybricksdev/connections/pybricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from bleak import BleakClient
from bleak.backends.device import BLEDevice
from packaging.version import Version
from reactivex import Observable
from reactivex.subject import BehaviorSubject, Subject
from tqdm.auto import tqdm
from tqdm.contrib.logging import logging_redirect_tqdm
Expand Down Expand Up @@ -80,6 +81,7 @@ class PybricksHub:
def __init__(self):
self.connection_state_observable = BehaviorSubject(ConnectionState.DISCONNECTED)
self.status_observable = BehaviorSubject(StatusFlag(0))
self._stdout_subject = Subject()
self.nus_observable = Subject()
self.print_output = True
self.fw_version = None
Expand Down Expand Up @@ -118,6 +120,13 @@ def __init__(self):
# File handle for logging
self.log_file = None

@property
def stdout_observable(self) -> Observable[bytes]:
"""
Observable used to subscribe to stdout of the hub.
"""
return self._stdout_subject

def _line_handler(self, line: bytes) -> None:
"""
Handles new incoming lines. Handle special actions if needed,
Expand Down Expand Up @@ -201,6 +210,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._stdout_subject.on_next(data)

if self._enable_line_handler:
self._handle_line_data(data)

Expand All @@ -211,6 +222,7 @@ def _pybricks_service_handler(self, _: int, data: bytes) -> None:
self.status_observable.on_next(StatusFlag(flags))
elif data[0] == Event.WRITE_STDOUT:
payload = data[1:]
self._stdout_subject.on_next(payload)

if self._enable_line_handler:
self._handle_line_data(payload)
Expand Down Expand Up @@ -403,7 +415,17 @@ async def read_line(self) -> str:
Returns:
The next line read from stdout (without the newline).
Raises:
RuntimeError:
if line handler is disabled (e.g. :meth:`run` is
called with ``line_handler=False``)
RuntimeError:
if hub becomes disconnected
"""
if not self._enable_line_handler:
raise RuntimeError("line handler is disabled, method would block forever")

return await self.race_disconnect(self._stdout_line_queue.get())

async def start_user_program(self) -> None:
Expand Down

0 comments on commit 6337ece

Please sign in to comment.