A package to suit your non-blocking PS4 interface
Project description
About
A package to suit your non-blocking PS4 interface
PS4 Views
LED View of the PS4
Top View of the PS4
Installation
Depend on your prefrence you can install this package using either one of these commands
python -m pip install AsynchronousPS4Controller
python3 -m pip install AsynchronousPS4Controller
pip install AsynchronousPS4Controller
pip3 install AsynchronousPS4Controller
Usage
-
Connect your controller to Bluetooth. For the first time connecting, Hold the share button, then hold the PlayStation Button. The lights on the controller will blink rapidly. Open your bluetooth settings to add the device.
-
Check which interfaces you have available at
/dev/input
vials /dev/input/ | grep js
command. The terminal will list all input paths that starts withjs
. When you connect a new device, there should be a entry and it usually starts atjs0
. The more controllers/joysticks are connected the morejsN
interfaces you will see. So if you have only one controller/joystick connected, you should only see 1 interface:/dev/input/js0
, take a note of it as we will need it in next step. -
Copy the code below into a Python file, lets say
main.py
import asyncio from AsynchronousPS4Controller import Controller controller = Controller(path="/dev/input/js0") # you can start listening before controller is paired, as long as you pair it within the timeout window asyncio.run(controller.listen(timeout=30))
-
Now run that file like so
python main.py
and use your controller. You will see output on your screen based on the input to your controller. You can bind your own logic to each one of those events. Lets say you want print "Hello world" on X press and "Goodbye world" on X release then the code would look like this:import asyncio from AsynchronousPS4Controller import Controller class MyController(Controller): def __init__(self, **kwargs): Controller.__init__(self, **kwargs) async def on_x_press(self): print("Hello world") async def on_x_release(self): print("Goodbye world") controller = MyController(path="/dev/input/js0") # you can start listening before controller is paired, as long as you pair it within the timeout window asyncio.run(controller.listen(timeout=30)) # you can start listening before controller is paired, as long as you pair it within the timeout window asyncio.run(controller.listen(timeout=30))
Here is a list of all the events you can override in a similar manner:
on_x_press on_x_release on_triangle_press on_triangle_release on_circle_press on_circle_release on_square_press on_square_release on_L1_press on_L1_release on_L2_press on_L2_release on_R1_press on_R1_release on_R2_press on_R2_release on_up_arrow_press on_up_down_arrow_release on_down_arrow_press on_left_arrow_press on_left_right_arrow_release on_right_arrow_press on_L3_up on_L3_down on_L3_left on_L3_right on_L3_x_at_rest # Left joystick goes back to x-axis after moving on_L3_y_at_rest # Left joystick goes back to y-axis after moving on_L3_press # Left joystick is clicked. on_L3_release # Left joystick is released after the click. on_R3_up on_R3_down on_R3_left on_R3_right on_R3_x_at_rest # Right joystick goes back to x-axis after moving on_R3_y_at_rest # Right joystick goes back to y-axis after moving on_R3_press # Right joystick button is clicked. on_R3_release # Right joystick button is released after the click. on_options_press on_options_release on_share_press on_share_release on_playstation_button_press on_playstation_button_release
Registering Callbacks
It is possible to bound callbacks to the listen
function.
Three call back functions can be registered: on_connect
, on_disconnect
and on_timeout
Here is an example how to do it.
import asyncio
from AsynchronousPS4Controller import Controller
def connect():
# any code you want to run during initial connection with the controller
pass
def disconnect():
# any code you want to run during loss of connection with the controller or keyboard interrupt
pass
def timeout():
# any code you want to run during timeout of controller before exit with code 1
pass
class MyController(Controller):
def __init__(self, **kwargs):
Controller.__init__(self, **kwargs)
controller = MyController(interface="/dev/input/js0")
asyncio.run(controller.listen(timeout=30,connect_callback=connect, disconnect_callback=disconnect,timeout_callback=timeout))
Known limitations at this time
- Mouse pad events and clicks are not detected (This is due to it works through
/dev/input/mouseN
or/dev/input/mice
and uses a different way to unpack) - Sensor information (accelerometers and/or gyro) is not detected (This is due to it works through
/dev/input/eventN
and uses a different way to unpack)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file AsynchronousPS4Controller-0.1.0.tar.gz
.
File metadata
- Download URL: AsynchronousPS4Controller-0.1.0.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0c0475f623ac39ef9e21453dab9a3333d57579047dae5aaf7f02cc92ad43001 |
|
MD5 | 31215d93ac4922aa7a0b419001aad281 |
|
BLAKE2b-256 | e051757acb8924bbf8344b29b2bab7b57eb13d428e02e806b029bbd523e890f7 |
File details
Details for the file AsynchronousPS4Controller-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: AsynchronousPS4Controller-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b8ca86291ffb7d11b14bb452d7238a1c79a40ac562aac27927b4812a59dffb5f |
|
MD5 | 7d7849f01a4bcd99510de0caac5f4f67 |
|
BLAKE2b-256 | 74aeaf472fadba7a794ac59958e6c5eb767c96d273b01b7abf96975b6caee129 |