pybricks.iodevices: Add XboxController.

This commit is contained in:
Laurens Valk
2024-02-11 14:26:03 +01:00
parent fae24a6ef0
commit d3ca8ee719
19 changed files with 10086 additions and 85 deletions
+43 -65
View File
@@ -6,7 +6,16 @@ speakers, and batteries."""
from __future__ import annotations
from typing import Union, Iterable, overload, Optional, Tuple, Collection, TYPE_CHECKING
from typing import (
Union,
Iterable,
overload,
Optional,
Tuple,
Collection,
Set,
TYPE_CHECKING,
)
from .tools import Matrix
from .parameters import Axis, Direction, Stop, Button, Port, Color, Side
@@ -18,24 +27,18 @@ if TYPE_CHECKING:
_T_co = TypeVar("_T_co", covariant=True)
class MaybeAwaitable(None, Awaitable[None]):
...
class MaybeAwaitable(None, Awaitable[None]): ...
# HACK: Cannot subclass bool, so using Any instead.
class MaybeAwaitableBool(Any, Awaitable[bool]):
...
class MaybeAwaitableBool(Any, Awaitable[bool]): ...
class MaybeAwaitableFloat(float, Awaitable[float]):
...
class MaybeAwaitableFloat(float, Awaitable[float]): ...
class MaybeAwaitableInt(int, Awaitable[int]):
...
class MaybeAwaitableInt(int, Awaitable[int]): ...
class MaybeAwaitableTuple(Tuple[_T_co], Awaitable[Tuple[_T_co]]):
...
class MaybeAwaitableTuple(Tuple[_T_co], Awaitable[Tuple[_T_co]]): ...
class MaybeAwaitableColor(Color, Awaitable[Color]):
...
class MaybeAwaitableColor(Color, Awaitable[Color]): ...
class System:
@@ -92,12 +95,10 @@ class System:
"""
@overload
def storage(self, offset: int, *, read: int) -> bytes:
...
def storage(self, offset: int, *, read: int) -> bytes: ...
@overload
def storage(self, offset: int, *, write: bytes) -> None:
...
def storage(self, offset: int, *, write: bytes) -> None: ...
def storage(self, offset, read=None, write=None):
"""
@@ -168,12 +169,10 @@ class DCMotor:
is generated while the motor is still moving."""
@overload
def settings(self, max_voltage: Number) -> None:
...
def settings(self, max_voltage: Number) -> None: ...
@overload
def settings(self) -> Tuple[int]:
...
def settings(self) -> Tuple[int]: ...
def settings(self, *args):
"""
@@ -206,12 +205,10 @@ class Control:
speed: Optional[Number] = None,
acceleration: Optional[Number] = None,
torque: Optional[Number] = None,
) -> None:
...
) -> None: ...
@overload
def limits(self) -> Tuple[int, int, int]:
...
def limits(self) -> Tuple[int, int, int]: ...
def limits(self, *args):
"""
@@ -244,12 +241,10 @@ class Control:
kd: Optional[Number] = None,
integral_deadzone: Optional[Number] = None,
integral_rate: Optional[Number] = None,
) -> None:
...
) -> None: ...
@overload
def pid(self) -> Tuple[int, int, int, int, int]:
...
def pid(self) -> Tuple[int, int, int, int, int]: ...
def pid(self, *args):
"""pid(kp, ki, kd, integral_deadzone, integral_rate)
@@ -277,12 +272,10 @@ class Control:
@overload
def target_tolerances(
self, speed: Optional[Number] = None, position: Optional[Number] = None
) -> None:
...
) -> None: ...
@overload
def target_tolerances(self) -> Tuple[int, int]:
...
def target_tolerances(self) -> Tuple[int, int]: ...
def target_tolerances(self, *args):
"""target_tolerances(speed, position)
@@ -303,12 +296,10 @@ class Control:
@overload
def stall_tolerances(
self, speed: Optional[Number] = None, time: Optional[Number] = None
) -> None:
...
) -> None: ...
@overload
def stall_tolerances(self) -> Tuple[int, int]:
...
def stall_tolerances(self) -> Tuple[int, int]: ...
def stall_tolerances(self, speed, time):
"""stall_tolerances(speed, time)
@@ -350,12 +341,10 @@ class Model:
"""
@overload
def settings(self, values: tuple) -> None:
...
def settings(self, values: tuple) -> None: ...
@overload
def settings(self) -> tuple:
...
def settings(self) -> tuple: ...
def settings(self, speed, time):
"""settings(values)
@@ -620,12 +609,10 @@ class Speaker:
"""Plays beeps and sounds using a speaker."""
@overload
def volume(self, volume: Number) -> None:
...
def volume(self, volume: Number) -> None: ...
@overload
def volume(self) -> int:
...
def volume(self) -> int: ...
def volume(self, *args):
"""volume(volume)
@@ -913,11 +900,10 @@ class LightMatrix:
class Keypad:
"""Get status of buttons on a keypad layout."""
def __init__(self, active_buttons):
...
def __init__(self, active_buttons): ...
def pressed(self) -> Collection[Button]:
"""pressed() -> Collection[Button]
def pressed(self) -> Set[Button]:
"""pressed() -> Set[Button]
Checks which buttons are currently pressed.
@@ -1027,12 +1013,10 @@ class Accelerometer(SimpleAccelerometer):
"""Get measurements from an accelerometer."""
@overload
def acceleration(self, axis: Axis) -> float:
...
def acceleration(self, axis: Axis) -> float: ...
@overload
def acceleration(self) -> Matrix:
...
def acceleration(self) -> Matrix: ...
def acceleration(self, *args):
"""
@@ -1082,12 +1066,10 @@ class IMU(Accelerometer):
self,
angular_velocity_threshold: float = None,
acceleration_threshold: float = None,
) -> None:
...
) -> None: ...
@overload
def settings(self) -> Tuple[float, float]:
...
def settings(self) -> Tuple[float, float]: ...
def settings(self, *args):
"""
@@ -1152,12 +1134,10 @@ class IMU(Accelerometer):
"""
@overload
def angular_velocity(self, axis: Axis) -> float:
...
def angular_velocity(self, axis: Axis) -> float: ...
@overload
def angular_velocity(self) -> Matrix:
...
def angular_velocity(self) -> Matrix: ...
def angular_velocity(self, *args):
"""
@@ -1271,12 +1251,10 @@ class CommonColorSensor:
"""
@overload
def detectable_colors(self, colors: Collection[Color]) -> None:
...
def detectable_colors(self, colors: Collection[Color]) -> None: ...
@overload
def detectable_colors(self) -> Collection[Color]:
...
def detectable_colors(self) -> Collection[Color]: ...
def detectable_colors(self, *args):
"""
+62 -4
View File
@@ -298,12 +298,10 @@ class LWP3Device:
"""
@overload
def name(self, name: str) -> None:
...
def name(self, name: str) -> None: ...
@overload
def name(self) -> str:
...
def name(self) -> str: ...
def name(self, *args):
"""name(name)
@@ -338,6 +336,66 @@ class LWP3Device:
"""
class XboxController:
"""Microsoft® Xbox® controller."""
buttons = _common.Keypad([])
def __init__(self):
""""""
def joystick_left(self) -> Tuple[int, int]:
"""joystick_left() -> Tuple
Gets the left joystick position as percentages between -100%
and 100%. The center position is (0, 0).
Returns:
Tuple of X (horizontal) and Y (vertical) position.
"""
def joystick_right(self) -> Tuple[int, int]:
"""joystick_right() -> Tuple
Gets the right joystick position as percentages between -100%
and 100%. The center position is (0, 0).
Returns:
Tuple of X (horizontal) and Y (vertical) position.
"""
def triggers(self) -> Tuple[int, int]:
"""triggers() -> Tuple
Gets the left and right trigger positions as percentages between 0%
and 100%.
Returns:
Tuple of left and right trigger positions.
"""
def dpad(self) -> int:
"""dpad() -> int
Gets the direction-pad position. ``1`` is up, ``2`` is up-right,
``3`` is right, ``4`` is down-right, ``5`` is down, ``6`` is
down-left, ``7`` is left, ``8`` is up-left, and ``0`` is not pressed.
Returns:
Direction-pad position.
"""
def profile(self) -> int:
"""profile() -> int
Gets the current profile of the controller. Only available on the
Xbox Elite Controller Series 2.
Returns:
Profile number.
"""
# hide from jedi
if TYPE_CHECKING:
del MaybeAwaitable