pybricks 4.1.1__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.1 → pybricks-4.2.0b1}/PKG-INFO +1 -1
  2. {pybricks-4.1.1 → pybricks-4.2.0b1}/pyproject.toml +1 -1
  3. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/pybricks/iodevices.py +45 -22
  4. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/pybricks/tools.py +50 -6
  5. {pybricks-4.1.1 → pybricks-4.2.0b1}/AUTHORS.md +0 -0
  6. {pybricks-4.1.1 → pybricks-4.2.0b1}/LICENSE +0 -0
  7. {pybricks-4.1.1 → pybricks-4.2.0b1}/README.rst +0 -0
  8. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/micropython/__init__.py +0 -0
  9. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/micropython/py.typed +0 -0
  10. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/pybricks/__init__.py +0 -0
  11. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/pybricks/_common.py +0 -0
  12. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/pybricks/ev3devices.py +0 -0
  13. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/pybricks/hubs.py +0 -0
  14. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/pybricks/messaging.py +0 -0
  15. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/pybricks/nxtdevices.py +0 -0
  16. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/pybricks/parameters.py +0 -0
  17. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/pybricks/pupdevices.py +0 -0
  18. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/pybricks/py.typed +0 -0
  19. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/pybricks/robotics.py +0 -0
  20. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/uerrno/__init__.py +0 -0
  21. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/uerrno/py.typed +0 -0
  22. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/uio/__init__.py +0 -0
  23. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/uio/py.typed +0 -0
  24. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/ujson/__init__.py +0 -0
  25. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/umath/__init__.py +0 -0
  26. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/umath/py.typed +0 -0
  27. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/urandom/__init__.py +0 -0
  28. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/urandom/py.typed +0 -0
  29. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/uselect/__init__.py +0 -0
  30. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/uselect/py.typed +0 -0
  31. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/ustruct/__init__.py +0 -0
  32. {pybricks-4.1.1 → pybricks-4.2.0b1}/src/usys/__init__.py +0 -0
  33. {pybricks-4.1.1 → 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.1
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.1"
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
 
@@ -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
File without changes
File without changes
File without changes