Skip to content

Commit b6d1eef

Browse files
committed
pybricks.iodevices.LWP3Device: add api and docs
This adds docs and API stubs for pybricks.iodevices.LWP3Device. Fixes: pybricks/support#534
1 parent 20cb5fd commit b6d1eef

File tree

5 files changed

+87
-1
lines changed

5 files changed

+87
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
### Added
88
- Added `ColorLightMatrix` class.
9+
- Added `LWP3Device` class.
910

1011
## 1.6.0 - 2021-08-30
1112

doc/main/iodevices/index.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
:hidden:
1111

1212
pupdevice
13+
lwp3device
1314

1415
This module has classes for generic input/output devices.
1516

@@ -18,3 +19,9 @@ This module has classes for generic input/output devices.
1819
.. figure:: ../../main/images/sensor_pup.png
1920
:width: 70 %
2021
:target: pupdevice.html
22+
23+
.. pybricks-classlink:: LWP3Device
24+
25+
.. figure:: ../../main/images/powereduphubs.png
26+
:width: 70 %
27+
:target: lwp3device.html

doc/main/iodevices/lwp3device.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
LEGO Wireless Protocol v3 device
2+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3+
4+
.. warning::
5+
6+
This is an experimental class. It has not been well tested and may be
7+
changed in future.
8+
9+
.. figure:: ../../main/images/powereduphubs.png
10+
:width: 25 %
11+
12+
.. autoclass:: pybricks.iodevices.LWP3Device

src/pybricks/iodevices.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,59 @@ def waiting(self):
244244
def clear(self):
245245
"""Empties the buffer."""
246246
pass
247+
248+
249+
class LWP3Device:
250+
"""
251+
Connects to a remote hub running official LEGO firmware using the the
252+
`LEGO Wireless Protocol v3`_
253+
254+
.. _`LEGO Wireless Protocol v3`:
255+
https://lego.github.io/lego-ble-wireless-protocol-docs/
256+
"""
257+
258+
def __init__(self, hub_kind, name=None, timeout=10000):
259+
"""
260+
261+
Arguments:
262+
hub_kind (int):
263+
The `hub type identifier`_ of the hub to connect to.
264+
name (str):
265+
The name of the hub to connect to or ``None`` to connect to any
266+
hub.
267+
timeout (int):
268+
The time, in milliseconds, to wait for a connection before
269+
raising an exception.
270+
271+
272+
.. _`hub type identifier`:
273+
https://github.com/pybricks/technical-info/blob/master/assigned-numbers.md#hub-type-ids
274+
"""
275+
276+
def name(self, name=None):
277+
"""Gets or sets the Bluetooth name of the remote.
278+
279+
If no name is given, this method returns the current name.
280+
281+
Arguments:
282+
name (str): New Bluetooth name of the remote.
283+
"""
284+
285+
def write(self, buf):
286+
"""
287+
Sends a message to the remote hub.
288+
289+
Arguments:
290+
buf (bytes): The raw binary message to send.
291+
"""
292+
293+
def read(self):
294+
"""
295+
Retrieves the most recent message received from the remote hub.
296+
297+
If a message has not been received since the last read, the method will
298+
block until a message is received.
299+
300+
Returns:
301+
bytes: The raw binary message.
302+
"""

src/pybricks/iodevices.pyi

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SPDX-License-Identifier: MIT
22
# Copyright (c) 2020 The Pybricks Authors
33

4-
from typing import Dict, Optional, Tuple
4+
from typing import Dict, Optional, Tuple, overload
55

66
from .parameters import Port
77

@@ -40,3 +40,13 @@ class UARTDevice:
4040
def write(self, data: bytes) -> None: ...
4141
def waiting(self) -> int: ...
4242
def clear(self) -> None: ...
43+
44+
class LWP3Device:
45+
def __init__(self, hub_kind: int, name: str = None, timeout: int = 10000): ...
46+
@overload
47+
def name(self) -> str: ...
48+
@overload
49+
def name(self, name: str) -> None: ...
50+
def name(self, *args): ...
51+
def read(self) -> bytes: ...
52+
def write(self, buf: bytes) -> None: ...

0 commit comments

Comments
 (0)