Skip to content

Commit 3bc2f35

Browse files
committed
pybricks.iodevices: Added missing functions and parameters.
* Added missing functions and parameters to documentation. - AnalogSensor: Missing parameter "custom". - I2CDevice: Missing parameters "port", "address", "custom", "powered", and "nxt_quirk" with attempted explanation of parameters. - UARTDevice: Missing default parameter value. - LWP3Device: Missing "connect" parameter. - Added missing connect and disconnnect methods. - XboxController: Missing documentation in constructor and missing methods. * Minor changes to the documentation Made it more clear and less verbose when to skip connect to bluetooth devices.
1 parent 61578b5 commit 3bc2f35

1 file changed

Lines changed: 70 additions & 9 deletions

File tree

src/pybricks/iodevices.py

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,13 @@ def read(self, mode: str) -> MaybeAwaitableTuple:
117117
class AnalogSensor:
118118
"""Generic or custom analog sensor."""
119119

120-
def __init__(self, port: _Port):
121-
"""AnalogSensor(port)
120+
def __init__(self, port: _Port, custom: bool = False):
121+
"""AnalogSensor(port, custom=False)
122122
123123
Arguments:
124124
port (Port): Port to which the sensor is connected.
125+
custom (bool): Set to ``True`` if you are using a custom analog
126+
sensor, such as a passive RCX sensor.
125127
"""
126128

127129
def voltage(self) -> int:
@@ -173,13 +175,18 @@ def passive(self) -> None:
173175
class I2CDevice:
174176
"""Generic or custom I2C device."""
175177

176-
def __init__(self, port: _Port, address: int):
177-
"""I2CDevice(port, address)
178+
def __init__(self, port: _Port, address: int, custom: bool = False, powered: bool = False, nxt_quirk: bool = False):
179+
"""I2CDevice(port, address, custom=False, powered=False, nxt_quirk=False)
178180
179181
Arguments:
180182
port (Port): Port to which the device is connected.
181183
address(int): I2C address of the client device. See
182184
:ref:`I2C Addresses <i2caddress>`.
185+
custom (bool): Set to ``True`` if you are using a custom I2C device.
186+
powered (bool): Set to ``True`` to power the I2C device.
187+
nxt_quirk (bool): Set to ``True`` for older NXT I2C sensors that
188+
need slower compatibility timing to communicate reliably,
189+
such as the old NXT Ultrasonic Sensor.
183190
"""
184191

185192
def read(self, reg: Optional[int], length: Optional[int] = 1) -> bytes:
@@ -211,8 +218,8 @@ def write(self, reg: Optional[int], data: Optional[bytes] = None) -> None:
211218
class UARTDevice:
212219
"""Generic UART device."""
213220

214-
def __init__(self, port: _Port, baudrate: int, timeout: Optional[int] = None):
215-
"""UARTDevice(port, baudrate, timeout=None)
221+
def __init__(self, port: _Port, baudrate: int = 115200, timeout: Optional[int] = None):
222+
"""UARTDevice(port, baudrate=115200, timeout=None)
216223
217224
Arguments:
218225
port (Port): Port to which the device is connected.
@@ -287,8 +294,9 @@ def __init__(
287294
timeout: int = 10000,
288295
pair: bool = False,
289296
num_notifications: int = 8,
297+
connect: bool = True,
290298
):
291-
"""LWP3Device(hub_kind, name=None, timeout=10000, pair=False, num_notifications=8)
299+
"""LWP3Device(hub_kind, name=None, timeout=10000, pair=False, num_notifications=8, connect=True)
292300
293301
Arguments:
294302
hub_kind (int):
@@ -303,6 +311,8 @@ def __init__(
303311
This is required for some newer hubs.
304312
num_notifications (int): Number of incoming messages from the remote
305313
hub to store before discarding older messages.
314+
connect (bool): Choose ``False`` to skip connecting.
315+
``connect()`` can be called later to connect.
306316
307317
.. versionchanged:: 3.6
308318
@@ -316,6 +326,13 @@ def __init__(
316326
https://github.com/pybricks/technical-info/blob/master/assigned-numbers.md#hub-type-ids
317327
"""
318328

329+
def connect(self) -> MaybeAwaitable:
330+
"""connect()
331+
332+
Connects to the remote LWP3Device. Only needed if you initialized the
333+
device with ``connect=False``.
334+
"""
335+
319336
@overload
320337
def name(self, name: str) -> MaybeAwaitable: ...
321338

@@ -377,8 +394,52 @@ class XboxController:
377394

378395
buttons = _common.Keypad([])
379396

380-
def __init__(self):
381-
""""""
397+
def __init__(self, joystick_deadzone: int = 10, name: Optional[str] = None, timeout: int = 10000, connect: bool = True):
398+
"""__init__(joystick_deadzone=10, name=None, timeout=10000, connect=True)
399+
400+
Arguments:
401+
joystick_deadzone (Number, %): Joystick deadzone (0 to 100). Values
402+
below this threshold will be reported as 0.
403+
name (str): The Bluetooth name of the Xbox controller to connect to,
404+
or ``None`` to connect to any available controller.
405+
timeout (Number, ms): How long to wait for a connection before
406+
giving up.
407+
connect (bool): Choose ``False`` to skip connecting to the controller.
408+
``connect()`` can be called later to connect.
409+
"""
410+
411+
def connect(self) -> MaybeAwaitable:
412+
"""connect()
413+
414+
Connects to the Xbox controller. Only needed if you initialized the
415+
controller with ``connect=False``.
416+
"""
417+
418+
def disconnect(self) -> MaybeAwaitable:
419+
"""disconnect()
420+
421+
Disconnects the Xbox controller.
422+
"""
423+
424+
def name(self) -> str:
425+
"""name() -> str
426+
427+
Gets the Bluetooth name of the connected controller.
428+
429+
Returns:
430+
Bluetooth name of the controller.
431+
"""
432+
433+
def state(self) -> Tuple:
434+
"""state() -> Tuple
435+
436+
Gets all raw controller input values as a single tuple. This gives
437+
access to values not exposed by the other methods.
438+
439+
Returns:
440+
Tuple of ``(x, y, z, rz, left_trigger, right_trigger, dpad,
441+
buttons, upload, profile, trigger_switches, paddles)``.
442+
"""
382443

383444
def joystick_left(self) -> Tuple[int, int]:
384445
"""joystick_left() -> Tuple

0 commit comments

Comments
 (0)