A package to suit your non-blocking PS4 interface
Depend on your preference 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
- 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))
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))
- 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)