Skip to content

Commit 9af9d29

Browse files
committed
pybricks.iodevices: Document PlayStation Controller.
1 parent e610a0a commit 9af9d29

5 files changed

Lines changed: 202 additions & 5 deletions

File tree

‎doc/common/extensions/requirements-static.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,14 @@
5656
"hub-network",
5757
}
5858
| FEATURES_MEDIUM,
59-
"ev3brick": {"ev3devices", "nxtdevices", "image", "hub-network"} | FEATURES_MEDIUM,
59+
"ev3brick": {
60+
"ev3devices",
61+
"nxtdevices",
62+
"image",
63+
"hub-network",
64+
"playstation-controller",
65+
}
66+
| FEATURES_MEDIUM,
6067
}
6168

6269

‎doc/main/iodevices/index.rst‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@ Wireless devices
1313
:hidden:
1414

1515
xboxcontroller
16+
playstationcontroller
1617

17-
.. pybricks-requirements:: pybricks-iodevices
18+
.. pybricks-requirements:: xbox-controller
1819

1920
.. pybricks-classlink:: XboxController
2021

2122
.. figure:: ../../main/diagrams_source/xboxcontroller.png
2223
:width: 40 %
2324
:target: xboxcontroller.html
2425

26+
.. pybricks-requirements:: playstation-controller
27+
28+
.. pybricks-classlink:: PlayStationController
29+
2530
LEGO protocol devices
2631
---------------------
2732

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
.. pybricks-requirements:: playstation-controller
2+
3+
PlayStation Controller
4+
^^^^^^^^^^^^^^^^^^^^^^^^^
5+
6+
.. blockimg:: pybricks_variables_set_xbox_controller
7+
8+
.. autoclass:: pybricks.iodevices.PlayStationController
9+
:no-members:
10+
11+
.. automethod:: pybricks.iodevices::PlayStationController.name
12+
13+
.. blockimg:: pybricks_blockButtonIsPressed_XboxController
14+
15+
.. automethod:: pybricks.iodevices::PlayStationController.buttons.pressed
16+
17+
Buttons include:
18+
19+
* ``Button.A`` (cross), ``Button.B`` (circle), ``Button.X`` (square),
20+
and ``Button.Y`` (triangle).
21+
* ``Button.UP``, ``Button.DOWN``, ``Button.LEFT``, ``Button.RIGHT``
22+
(direction pad). At most two of these can be pressed at the same time.
23+
* ``Button.LB`` and ``Button.RB`` (L1 and R1).
24+
* ``Button.LJ`` and ``Button.RJ`` (L3 and R3, pressing the joysticks).
25+
* ``Button.VIEW`` (share or create), ``Button.MENU`` (options),
26+
``Button.GUIDE`` (the PlayStation logo), and ``Button.UPLOAD``
27+
(pressing the touchpad).
28+
29+
.. note:: The buttons are currently reported using the names of the
30+
equivalent buttons on the Xbox controller, as listed above. This will
31+
change in a future version.
32+
33+
.. blockimg:: pybricks_blockJoystickValue_xbox_lj_x
34+
35+
.. blockimg:: pybricks_blockJoystickValue_xbox_lj_y
36+
37+
.. automethod:: pybricks.iodevices::PlayStationController.joystick_left
38+
39+
.. blockimg:: pybricks_blockJoystickValue_xbox_rj_x
40+
41+
.. blockimg:: pybricks_blockJoystickValue_xbox_rj_y
42+
43+
.. automethod:: pybricks.iodevices::PlayStationController.joystick_right
44+
45+
.. blockimg:: pybricks_blockJoystickValue_xbox_lt
46+
47+
.. blockimg:: pybricks_blockJoystickValue_xbox_rt
48+
49+
.. automethod:: pybricks.iodevices::PlayStationController.triggers
50+
51+
.. blockimg:: pybricks_blockJoystickValue_xbox_dpad
52+
53+
.. automethod:: pybricks.iodevices::PlayStationController.dpad
54+
55+
.. _playstation-controller-pairing:
56+
57+
PlayStation Controller Pairing Instructions
58+
===========================================
59+
60+
The first time you use a controller with a hub, you will need to pair them
61+
using the hub menu:
62+
63+
1. On the hub, go to the settings tab and choose ``Add new gamepad``.
64+
2. Put the controller in pairing mode: press and hold the share (or create)
65+
button and the PlayStation button at the same time until the light starts
66+
flashing rapidly.
67+
3. Select the controller on the hub when it appears in the list.
68+
69+
When the connection is successful, the light on the controller stops
70+
flashing. You can now start your program. Only one controller can be connected at a time.
71+
72+
Repeat Connections
73+
------------------
74+
75+
If you keep using the same controller with the same hub, you can simply
76+
press the PlayStation button on the controller the next time and it will
77+
connect to the hub automatically.
78+
79+
If you connect the controller to your console or to another device in the
80+
meantime, you will need to pair it with the hub again as described above.

‎jedi/tests/test_complete_import.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def test_from_pybricks_iodevices_import():
108108
"I2CDevice",
109109
"LUMPDevice",
110110
"LWP3Device",
111+
"PlayStationController",
111112
"PUPDevice",
112113
"UARTDevice",
113114
"XboxController",

‎src/pybricks/iodevices.py‎

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ def state(self) -> tuple:
574574
"""
575575

576576
def joystick_left(self) -> tuple[int, int]:
577-
"""joystick_left() -> tuple
577+
"""joystick_left() -> tuple[int, int]: %
578578
579579
Gets the left joystick position as percentages between -100%
580580
and 100%. The center position is (0, 0). A square deadzone is applied:
@@ -588,7 +588,7 @@ def joystick_left(self) -> tuple[int, int]:
588588
"""
589589

590590
def joystick_right(self) -> tuple[int, int]:
591-
"""joystick_right() -> tuple
591+
"""joystick_right() -> tuple[int, int]: %
592592
593593
Gets the right joystick position as percentages between -100%
594594
and 100%. The center position is (0, 0). A square deadzone is applied:
@@ -602,7 +602,7 @@ def joystick_right(self) -> tuple[int, int]:
602602
"""
603603

604604
def triggers(self) -> tuple[int, int]:
605-
"""triggers() -> tuple
605+
"""triggers() -> tuple[int, int]: %
606606
607607
Gets the left and right trigger positions as percentages between 0%
608608
and 100%.
@@ -683,6 +683,110 @@ def rumble(
683683
"""
684684

685685

686+
class PlayStationController:
687+
"""Use a Sony® PlayStation® controller as a sensor in your projects to
688+
control them remotely.
689+
690+
This has been tested on the DUALSHOCK®4 wireless controller for
691+
PlayStation®4 and the DualSense™ wireless controller for PlayStation®5, but
692+
similar gamepads may also work.
693+
694+
Pair the controller in the hub menu first. When your program
695+
runs, it uses the controller that is currently connected.
696+
697+
For tips on connectivity and pairing, see
698+
:ref:`below <playstation-controller-pairing>`.
699+
700+
.. versionadded:: 4.1
701+
"""
702+
703+
buttons = _common.Keypad([])
704+
705+
def __init__(self, joystick_deadzone: int = 10):
706+
"""__init__(joystick_deadzone=10)
707+
708+
Arguments:
709+
joystick_deadzone (Number, %): Joystick deadzone (0 to 100). Values
710+
below this threshold in both axes will be reported as 0 to
711+
prevent stick drift.
712+
713+
Raises:
714+
OSError: If the controller is not connected.
715+
"""
716+
717+
def name(self) -> str | None:
718+
"""name() -> str | None
719+
720+
Gets the Bluetooth name of the connected controller.
721+
722+
Returns:
723+
Bluetooth name of the controller, or ``None`` if it is not known.
724+
725+
Raises:
726+
OSError: If the controller is not connected.
727+
"""
728+
729+
def joystick_left(self) -> tuple[int, int]:
730+
"""joystick_left() -> tuple[int, int]: %
731+
732+
Gets the left joystick position as percentages between -100%
733+
and 100%. The center position is (0, 0). A square deadzone is applied:
734+
if both axes are within the deadzone, both are reported as 0.
735+
736+
Returns:
737+
Tuple of X (horizontal) and Y (vertical) position.
738+
739+
Raises:
740+
OSError: If the controller is not connected.
741+
"""
742+
743+
def joystick_right(self) -> tuple[int, int]:
744+
"""joystick_right() -> tuple[int, int]: %
745+
746+
Gets the right joystick position as percentages between -100%
747+
and 100%. The center position is (0, 0). A square deadzone is applied:
748+
if both axes are within the deadzone, both are reported as 0.
749+
750+
Returns:
751+
Tuple of X (horizontal) and Y (vertical) position.
752+
753+
Raises:
754+
OSError: If the controller is not connected.
755+
"""
756+
757+
def triggers(self) -> tuple[int, int]:
758+
"""triggers() -> tuple[int, int]: %
759+
760+
Gets the left (L2) and right (R2) trigger positions as percentages
761+
between 0% and 100%.
762+
763+
Returns:
764+
Tuple of left and right trigger positions.
765+
766+
Raises:
767+
OSError: If the controller is not connected.
768+
"""
769+
770+
def dpad(self) -> int:
771+
"""dpad() -> int
772+
773+
Gets the direction-pad value. ``1`` is up, ``2`` is up-right, ``3``
774+
is right, ``4`` is down-right, ``5`` is down, ``6`` is down-left,
775+
``7`` is left, ``8`` is up-left, and ``0`` is not pressed.
776+
777+
This is essentially the same as reading the state of the
778+
``Button.UP``, ``Button.RIGHT``, ``Button.DOWN``, and ``Button.LEFT``
779+
buttons, but this method conveniently returns a number that indicates
780+
a direction.
781+
782+
Returns:
783+
Direction-pad position, indicating a direction.
784+
785+
Raises:
786+
OSError: If the controller is not connected.
787+
"""
788+
789+
686790
# Hide type-only names from jedi completions in the module namespace.
687791
if TYPE_CHECKING:
688792
del MaybeAwaitable

0 commit comments

Comments
 (0)