-
Notifications
You must be signed in to change notification settings - Fork 11
Using the Dualshock 4 Controller with the PI3
Here is a list of software that is needed before continuing on. Python must be installed before installing ds4drv or pygame because they are both Python libraries.
- Python 2.7 or 3.3+ - Since the PI3 runs on Debian, the development package of Python 2.7 or 3.3+ is needed.
- ds4drv - sudo pip install ds4drv
- Pygame - sudo apt-get install python-pygame
DO NOT JUST PAIR THE CONTROLLER VIA BLUETOOTH. It is very tempting to just pair the DS4 controller to Bluetooth as soon as the PI3 boots up, but this is a mistake. The controller has to be paired through ds4drv.
Now that the warning is out of the way, let's get started.
Step 1: Run ds4drv
$ ds4drv
It should of printed "Scanning for devices" in the terminal.
Step 2: Hold the PS button and the share button on the DS4 controller until LED display starts flashing.
This puts the controller in a broadcast mode. The ds4drv should now see the controller and automatically pair with it.
An easy way to interfaces with the controller is to use the joystick example provided on the Pygame website. Just copy and paste the code provided on the webpage into a python editor. With ds4drv still running, run the Pygame joystick demo. A window like this should pop-up.
Now you can see the button presses and joystick values when using inputs on the DS4 controller.
Something very useful to note about the example is how it handles buttons presses. Here it is in the code:
if event.type == pygame.JOYBUTTONDOWN:
print("Joystick button pressed.")
if event.type == pygame.JOYBUTTONUP:
print("Joystick button released.")
It is setup as an event. So, whenever a button is pressed the JOYBUTTONDOWN event is called, the example prints out "Joystick button pressed.". In order to see which button is pressed, call get_button(button) once the JOYBUTTONDOWN event has occurred. The function get_button takes in a button ID, and returns a boolean that determines whether or not the button has been pressed. So, if all buttons are checked once the JOYBUTTONDOWN event occurs, then the one that returns true is the button that was pressed. Getting the value of the axis of a joystick works the same way except the JOYAXISMOTION event occurs when one of the joysticks has moved.