Using Python to connect to a PS4 and a map of all identified events.
This code was written to connect a LEGO EV3 Brick to a PS4 controller. The EV3 is running ev3dev and running a version of Python called Pybricks. However, the code reacting to the PS4 controller events and the mapping of different buttons should apply to any Python/PS4 project.
This code assumes that the PS4 controller has already been paired.
When the PS4 is paried with the device it creates three event files. These files are updated when the PS4 controller experiecnes an event (such as a button press).
Using a terminal check out the contents of the /dev/input folder before and after you pair the Bluetooth device. You should notice three new event files. On my device these files where:
- /dev/input/event2 (touchpad events)
- /dev/input/event3 (controller movement, like tilting, etc...)
- /dev/input/event4 (buttons, sticks, etc...)
Each event provides five values, but we only need the event ID, code, and value. Here is a list of all events I could find:
For me button and stick events were found in /dev/input/event4. If you're working on a PS4 project, these are probably the events you're looking for.
| Event | ID | Code | Possible Values | Description |
|---|---|---|---|---|
| X Button | 1 | 304 | ||
| Circle Button | 1 | 305 | ||
| Triangle Burron | 1 | 307 | ||
| Square Button | 1 | 308 | 0 or 1 | 0 Released/1 Pressed |
| Share Button | 1 | 314 | 0 or 1 | 0 Released/1 Pressed |
| Options Button | 1 | 315 | 0 or 1 | 0 Released/1 Pressed |
| PS Button | 1 | 316 | 0 or 1 | 0 Released/1 Pressed |
| Left Stick Push | 1 | 317 | 0 or 1 | 0 Released/1 Pressed |
| Right Stick Push | 1 | 318 | 0 or 1 | 0 Released/1 Pressed |
| L1 | 1 | 310 | 0 or 1 | 0 Released/1 Pressed |
| R1 | 1 | 311 | 0 or 1 | 0 Released/1 Pressed |
| L2 | 1 | 312 | 0 or 1 | 0 Released/1 Pressed |
| R2 | 1 | 313 | 0 or 1 | 0 Released/1 Pressed |