pybricks 4.1.0b3__tar.gz → 4.2.0b1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/PKG-INFO +1 -1
  2. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/pyproject.toml +1 -1
  3. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/pybricks/iodevices.py +152 -25
  4. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/pybricks/tools.py +50 -6
  5. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/AUTHORS.md +0 -0
  6. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/LICENSE +0 -0
  7. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/README.rst +0 -0
  8. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/micropython/__init__.py +0 -0
  9. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/micropython/py.typed +0 -0
  10. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/pybricks/__init__.py +0 -0
  11. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/pybricks/_common.py +0 -0
  12. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/pybricks/ev3devices.py +0 -0
  13. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/pybricks/hubs.py +0 -0
  14. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/pybricks/messaging.py +0 -0
  15. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/pybricks/nxtdevices.py +0 -0
  16. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/pybricks/parameters.py +0 -0
  17. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/pybricks/pupdevices.py +0 -0
  18. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/pybricks/py.typed +0 -0
  19. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/pybricks/robotics.py +0 -0
  20. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/uerrno/__init__.py +0 -0
  21. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/uerrno/py.typed +0 -0
  22. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/uio/__init__.py +0 -0
  23. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/uio/py.typed +0 -0
  24. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/ujson/__init__.py +0 -0
  25. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/umath/__init__.py +0 -0
  26. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/umath/py.typed +0 -0
  27. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/urandom/__init__.py +0 -0
  28. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/urandom/py.typed +0 -0
  29. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/uselect/__init__.py +0 -0
  30. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/uselect/py.typed +0 -0
  31. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/ustruct/__init__.py +0 -0
  32. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/usys/__init__.py +0 -0
  33. {pybricks-4.1.0b3 → pybricks-4.2.0b1}/src/usys/py.typed +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pybricks
3
- Version: 4.1.0b3
3
+ Version: 4.2.0b1
4
4
  Summary: Documentation and user-API stubs for Pybricks MicroPython
5
5
  License: MIT
6
6
  License-File: AUTHORS.md
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "pybricks"
3
- version = "4.1.0b3"
3
+ version = "4.2.0b1"
4
4
  description = "Documentation and user-API stubs for Pybricks MicroPython"
5
5
  authors = ["The Pybricks Authors <team@pybricks.com>"]
6
6
  maintainers = ["Laurens Valk <laurens@pybricks.com>", "David Lechner <david@pybricks.com>" ]
@@ -180,34 +180,57 @@ class AnalogSensor:
180
180
 
181
181
 
182
182
  class I2CDevice:
183
- """Generic or custom I2C device.
183
+ """Generic or custom I2C device."""
184
184
 
185
- Note: Use the ``power_pin`` option at your own risk. Applying power to the
186
- pins can damage your hub or device if you are not careful. When you use
187
- this option, you will be prompted to confirm that you understand the risks.
188
- """
185
+ AUTOMATIC = 1
186
+ CUSTOM_NXT = 8
187
+ CUSTOM_QWIIC = 16
189
188
 
190
189
  def __init__(
191
190
  self,
192
191
  port: Port,
193
192
  address: int,
194
- custom: bool = False,
193
+ mode: int = AUTOMATIC,
195
194
  power_pin: int = 0,
196
- nxt_quirk: bool = False,
195
+ frequency: int | None = None,
196
+ reg_len: int = 1,
197
197
  ):
198
- """I2CDevice(port, address, custom=False, power_pin=0, nxt_quirk=False)
198
+ """I2CDevice(port, address, mode=I2CDevice.AUTOMATIC, power_pin=0, frequency=None, reg_len=1)
199
199
 
200
200
  Arguments:
201
201
  port (Port): Port to which the device is connected.
202
202
  address (int): I2C address of the client device. See
203
203
  :ref:`I2C Addresses <i2caddress>`.
204
- custom (bool): Set to ``True`` if you are using a custom I2C device.
204
+ mode (int): Choose ``I2CDevice.AUTOMATIC`` for a device that can be
205
+ automatically detected. Choose ``I2CDevice.CUSTOM_NXT`` for a
206
+ custom I2C device using the NXT convention or
207
+ ``I2CDevice.CUSTOM_QWIIC`` for the Qwiic convention.
208
+ See the :ref:`pinout <i2cpinout>`.
205
209
  power_pin (int): Power requirements for the device. Use
206
- ``0`` (default) for no power on the pins. On NXT and EV3, use ``1``
207
- to apply battery power to pin 1. Other pins are not supported.
208
- nxt_quirk (bool): Set to ``True`` for older NXT I2C sensors that
209
- need slower compatibility timing to communicate reliably,
210
- such as the old NXT Ultrasonic Sensor.
210
+ ``0`` (default) for no power on the pins. On Powered Up hubs,
211
+ use ``1`` or ``2`` for pin 1 or 2, respectively. This will
212
+ apply battery power to the pin, equivalent to powering a motor.
213
+ On NXT and EV3, use ``1`` to apply battery power to pin 1.
214
+ Use at your own risk. Applying power to the
215
+ pins can damage your hub or device if you are not careful. When
216
+ you use this option, you will be prompted to confirm that you
217
+ understand the risks.
218
+ frequency (int, Hz): I2C clock frequency. Choose ``None`` (default)
219
+ for the platform default, which is 10 kHz on EV3 and 100 kHz on
220
+ Powered Up hubs.
221
+ On Powered Up hubs, values from 1000 Hz to 100 kHz are
222
+ supported. The frequency applies to all ports, so
223
+ the most recently created ``I2CDevice`` determines it.
224
+ reg_len (int): Number of bytes used to send the register address:
225
+ ``1`` (default) or ``2``. Use ``2`` for devices with 16-bit
226
+ register addresses. The register is sent most significant
227
+ byte first.
228
+
229
+ Raises:
230
+ ValueError: If ``frequency`` is out of range.
231
+ OSError: If ``frequency`` is not ``None`` on a platform with a
232
+ fixed frequency, or if it is changed while another I2C device
233
+ is busy.
211
234
  """
212
235
 
213
236
  @overload
@@ -227,9 +250,9 @@ class I2CDevice:
227
250
  Reads bytes starting at a given register.
228
251
 
229
252
  Arguments:
230
- reg (int): Register at which to begin reading: 0--255 or
231
- 0x00--0xFF. Use ``None`` to read without writing a register
232
- address first.
253
+ reg (int): Register at which to begin reading: 0x00--0xFF, or
254
+ 0x0000--0xFFFF if ``reg_len`` is ``2``. Use ``None`` to read
255
+ without writing a register address first.
233
256
  length (int): How many bytes to read.
234
257
  map (callable): Optional callable to convert the returned bytes.
235
258
  If given, it is called with the bytes as its argument and its
@@ -248,15 +271,15 @@ class I2CDevice:
248
271
  Writes bytes, optionally starting at a given register.
249
272
 
250
273
  Arguments:
251
- reg (int): Register at which to begin writing: 0--255 or
252
- 0x00--0xFF. Use ``None`` to write without a register prefix.
274
+ reg (int): Register at which to begin writing: 0x00--0xFF, or
275
+ 0x0000--0xFFFF if ``reg_len`` is ``2``. Use ``None`` to write
276
+ without a register prefix.
253
277
  data (bytes): Bytes to be written. Use ``None`` to write nothing
254
278
  after the register.
255
279
 
256
280
  Raises:
257
- ValueError: If ``reg`` is given and ``data`` is more than 32 bytes.
258
- To write more data, omit the ``reg`` argument and include the
259
- register as the first byte of ``data``.
281
+ ValueError: If the register and ``data`` together are more than
282
+ 255 bytes.
260
283
  """
261
284
 
262
285
 
@@ -574,7 +597,7 @@ class XboxController:
574
597
  """
575
598
 
576
599
  def joystick_left(self) -> tuple[int, int]:
577
- """joystick_left() -> tuple
600
+ """joystick_left() -> tuple[int, int]: %
578
601
 
579
602
  Gets the left joystick position as percentages between -100%
580
603
  and 100%. The center position is (0, 0). A square deadzone is applied:
@@ -588,7 +611,7 @@ class XboxController:
588
611
  """
589
612
 
590
613
  def joystick_right(self) -> tuple[int, int]:
591
- """joystick_right() -> tuple
614
+ """joystick_right() -> tuple[int, int]: %
592
615
 
593
616
  Gets the right joystick position as percentages between -100%
594
617
  and 100%. The center position is (0, 0). A square deadzone is applied:
@@ -602,7 +625,7 @@ class XboxController:
602
625
  """
603
626
 
604
627
  def triggers(self) -> tuple[int, int]:
605
- """triggers() -> tuple
628
+ """triggers() -> tuple[int, int]: %
606
629
 
607
630
  Gets the left and right trigger positions as percentages between 0%
608
631
  and 100%.
@@ -683,6 +706,110 @@ class XboxController:
683
706
  """
684
707
 
685
708
 
709
+ class PlayStationController:
710
+ """Use a Sony® PlayStation® controller as a sensor in your projects to
711
+ control them remotely.
712
+
713
+ This has been tested on the DUALSHOCK®4 wireless controller for
714
+ PlayStation®4 and the DualSense™ wireless controller for PlayStation®5, but
715
+ similar gamepads may also work.
716
+
717
+ Pair the controller in the hub menu first. When your program
718
+ runs, it uses the controller that is currently connected.
719
+
720
+ For tips on connectivity and pairing, see
721
+ :ref:`below <playstation-controller-pairing>`.
722
+
723
+ .. versionadded:: 4.1
724
+ """
725
+
726
+ buttons = _common.Keypad([])
727
+
728
+ def __init__(self, joystick_deadzone: int = 10):
729
+ """__init__(joystick_deadzone=10)
730
+
731
+ Arguments:
732
+ joystick_deadzone (Number, %): Joystick deadzone (0 to 100). Values
733
+ below this threshold in both axes will be reported as 0 to
734
+ prevent stick drift.
735
+
736
+ Raises:
737
+ OSError: If the controller is not connected.
738
+ """
739
+
740
+ def name(self) -> str | None:
741
+ """name() -> str | None
742
+
743
+ Gets the Bluetooth name of the connected controller.
744
+
745
+ Returns:
746
+ Bluetooth name of the controller, or ``None`` if it is not known.
747
+
748
+ Raises:
749
+ OSError: If the controller is not connected.
750
+ """
751
+
752
+ def joystick_left(self) -> tuple[int, int]:
753
+ """joystick_left() -> tuple[int, int]: %
754
+
755
+ Gets the left joystick position as percentages between -100%
756
+ and 100%. The center position is (0, 0). A square deadzone is applied:
757
+ if both axes are within the deadzone, both are reported as 0.
758
+
759
+ Returns:
760
+ Tuple of X (horizontal) and Y (vertical) position.
761
+
762
+ Raises:
763
+ OSError: If the controller is not connected.
764
+ """
765
+
766
+ def joystick_right(self) -> tuple[int, int]:
767
+ """joystick_right() -> tuple[int, int]: %
768
+
769
+ Gets the right joystick position as percentages between -100%
770
+ and 100%. The center position is (0, 0). A square deadzone is applied:
771
+ if both axes are within the deadzone, both are reported as 0.
772
+
773
+ Returns:
774
+ Tuple of X (horizontal) and Y (vertical) position.
775
+
776
+ Raises:
777
+ OSError: If the controller is not connected.
778
+ """
779
+
780
+ def triggers(self) -> tuple[int, int]:
781
+ """triggers() -> tuple[int, int]: %
782
+
783
+ Gets the left (L2) and right (R2) trigger positions as percentages
784
+ between 0% and 100%.
785
+
786
+ Returns:
787
+ Tuple of left and right trigger positions.
788
+
789
+ Raises:
790
+ OSError: If the controller is not connected.
791
+ """
792
+
793
+ def dpad(self) -> int:
794
+ """dpad() -> int
795
+
796
+ Gets the direction-pad value. ``1`` is up, ``2`` is up-right, ``3``
797
+ is right, ``4`` is down-right, ``5`` is down, ``6`` is down-left,
798
+ ``7`` is left, ``8`` is up-left, and ``0`` is not pressed.
799
+
800
+ This is essentially the same as reading the state of the
801
+ ``Button.UP``, ``Button.RIGHT``, ``Button.DOWN``, and ``Button.LEFT``
802
+ buttons, but this method conveniently returns a number that indicates
803
+ a direction.
804
+
805
+ Returns:
806
+ Direction-pad position, indicating a direction.
807
+
808
+ Raises:
809
+ OSError: If the controller is not connected.
810
+ """
811
+
812
+
686
813
  # Hide type-only names from jedi completions in the module namespace.
687
814
  if TYPE_CHECKING:
688
815
  del MaybeAwaitable
@@ -8,12 +8,14 @@ from __future__ import annotations
8
8
  from typing import TYPE_CHECKING, Self, overload
9
9
 
10
10
  if TYPE_CHECKING:
11
- from collections.abc import Coroutine, Sequence
12
- from typing import Any
11
+ from collections.abc import Callable, Coroutine, Sequence
12
+ from typing import Any, TypeVar
13
13
 
14
14
  from ._common import MaybeAwaitable, MaybeAwaitableTuple
15
15
  from .parameters import Number
16
16
 
17
+ T = TypeVar("T")
18
+
17
19
 
18
20
  def wait(time: Number) -> MaybeAwaitable:
19
21
  """wait(time)
@@ -275,9 +277,18 @@ def multitask(*coroutines: Coroutine, race=False) -> MaybeAwaitableTuple:
275
277
  """
276
278
 
277
279
 
278
- def run_task(coroutine: Coroutine) -> bool | None:
280
+ @overload
281
+ def run_task() -> bool: ...
282
+
283
+
284
+ @overload
285
+ def run_task(coroutine: Coroutine[Any, Any, T]) -> T: ...
286
+
287
+
288
+ def run_task(coroutine=None):
279
289
  """
280
- run_task(coroutine) -> bool | None
290
+ run_task(coroutine) -> Any
291
+ run_task() -> bool
281
292
 
282
293
  Runs a coroutine from start to finish while blocking the rest of the
283
294
  program. This is used primarily to run the main coroutine of a program.
@@ -288,16 +299,49 @@ def run_task(coroutine: Coroutine) -> bool | None:
288
299
  coroutine (coroutine): The main coroutine to run.
289
300
 
290
301
  Returns:
291
- If no ``coroutine`` is given, this function returns whether the
292
- run loop is currently active (``True``) or not (``False``).
302
+ The value returned by the coroutine. If no ``coroutine`` is
303
+ given, this function instead returns whether the run loop is
304
+ currently active (``True``) or not (``False``).
305
+ """
306
+
307
+
308
+ def maybe_async(
309
+ func: Callable[..., Coroutine[Any, Any, T]],
310
+ ) -> Callable[..., T | Coroutine[Any, Any, T]]:
293
311
  """
312
+ maybe_async(func) -> Callable
313
+
314
+ Decorator that makes your own ``async`` function or method usable
315
+ with and without multitasking, just like built-in methods prefixed by
316
+ ``await``.
317
+
318
+ If the run loop is active (see :func:`run_task`), calling the decorated
319
+ function gives a coroutine that you can ``await``. Otherwise, it runs
320
+ to completion and returns the result directly.
321
+
322
+ Arguments:
323
+ func (callable): The ``async`` function or method to wrap.
324
+
325
+ Returns:
326
+ The wrapped function.
327
+ """
328
+
329
+ def wrapper(*args, **kwargs):
330
+ coroutine = func(*args, **kwargs)
331
+ if run_task():
332
+ return coroutine
333
+ return run_task(coroutine)
334
+
335
+ return wrapper
294
336
 
295
337
 
296
338
  # Hide type-only names from jedi completions in the module namespace.
297
339
  if TYPE_CHECKING:
298
340
  del Any
341
+ del Callable
299
342
  del Coroutine
300
343
  del MaybeAwaitable
301
344
  del MaybeAwaitableTuple
302
345
  del Number
303
346
  del Sequence
347
+ del T
File without changes
File without changes
File without changes
File without changes
File without changes