Skip to content

Commit

Permalink
Add support to PlayStation VR Aim Controller
Browse files Browse the repository at this point in the history
Signed-off-by: Douglas Schilling Landgraf <[email protected]>
  • Loading branch information
dougsland committed Sep 18, 2024
1 parent 7a8ffb9 commit 159ff0e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 18 deletions.
44 changes: 40 additions & 4 deletions docs/joysticks/tools/README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
# PS4 and PS5 Joystick Controller Logger
# PS4, PS5, and PS VR Aim Joystick Controller Logger

## Overview

This tool detects connected PS4 and PS5 controllers, tracks their inputs, and manages the special features of the DualSense controller (PS5), such as motor vibration. It is built using Pygame for general input handling and pydualsense for controlling the advanced features of the PS5 DualSense controller.
This tool detects connected PS4, PS5, and PS VR Aim controllers, tracks their inputs, and manages the special features of the DualSense controller (PS5), such as motor vibration. It is built using Pygame for general input handling and pydualsense for controlling the advanced features of the PS5 DualSense controller.

The tool also checks if the `vss-dbus` service is running, which could cause conflicts with the joystick device, and exits if the service is active.

## Features

- **PS4/PS5 Controller Input Logging**: Logs the button, axis, and D-pad (hat) inputs of a connected PS4 or PS5 controller.
- **Motor Vibration Control**: Uses Button 10 (Options button) to toggle motor vibration on and off. The vibration will continue until Button 10 is pressed again.
- **PS4/PS5/PS VR Aim Controller Input Logging**: Logs the button, axis, and D-pad (hat) inputs of a connected PS4, PS5, or PS VR Aim controller.
- **Motor Vibration Control**: Uses Button 10 (Options button) to toggle motor vibration on and off for PS4 and PS5 controllers. The vibration will continue until Button 10 is pressed again.
- **DualSense Controller Features**: Leverages the pydualsense library to manage advanced features of the PS5 DualSense controller, including motor control.
- **PS VR Aim Controller Support**: Logs inputs from the PS VR Aim Controller. Vibration is currently not supported for this device.
- **vss-dbus Conflict Detection**: Ensures the tool does not run if the `vss-dbus` service is active to avoid device conflicts.
- **Graceful Exit**: The tool handles `Ctrl + C` signals to exit gracefully, stopping any active vibration and releasing resources.

## Prerequisites

- **Pygame**: For general joystick handling.
- **pydualsense**: For advanced DualSense controller features (PS5).

## Supported Controllers

- PS4 (DualShock 4)
- PS5 (DualSense)
- PS VR Aim Controller (input logging, no vibration)
```
## Prerequisites
- **Pygame**: For general joystick handling.
- **pydualsense**: For advanced DualSense controller features.
Expand Down Expand Up @@ -74,4 +87,27 @@ Bus 003 Device 009: ID 054c:09cc Sony Corp. DualShock 4 [CUH-ZCT2x]
```

The controller is recognized successfully and registered with motion sensors and a touchpad, using the playstation driver to manage inputs.

## Sony PS VR Aim Controller Connection Information

When a Sony PS VR Aim Controller with ID `054c:0bb2` is connected, it is recognized by the system. Below is an example of the connection process logged by the system:

### `lsusb` Output:

```
Bus 003 Device 021: ID 054c:0bb2 Sony Corp. PS VR Aim Controller
```

### `dmesg` Output:

```
[100945.809572] usb 3-1: new full-speed USB device number 23 using xhci_hcd
[100945.936846] usb 3-1: New USB device found, idVendor=054c, idProduct=0bb2, bcdDevice= 1.00
[100945.936850] usb 3-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[100945.936851] usb 3-1: Product: PS VR Aim Controller
[100945.936852] usb 3-1: Manufacturer: Sony Interactive Entertainment
[100945.939224] input: Sony Interactive Entertainment PS VR Aim Controller as /devices/pci0000:00/0000:00:14.0/usb3/3-1/3-1:1.0/0003:054C:0BB2.0014/input/input70
[100945.939475] hid-generic 0003:054C:0BB2.0014: input,hidraw0: USB HID v1.11 Gamepad [Sony Interactive Entertainment PS VR Aim Controller] on usb-0000:00:14.0-1/input0
```

The controller is recognized successfully by the system as a USB HID gamepad, and the input device is registered with the hid-generic driver.
41 changes: 27 additions & 14 deletions docs/joysticks/tools/ps-controllers-pygame
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@

"""
Usage:
This script detects a connected PS4 or PS5 controller, tracks inputs, and controls
DualSense or DualShock4 features such as motor vibration. It ensures that the vss-dbus
service is not running to avoid conflicts with the joystick device.
This script detects a connected PS4, PS5, or PS VR Aim controller, tracks inputs, and controls
features such as motor vibration for DualSense (PS5) and DualShock 4 (PS4) controllers. It ensures
that the vss-dbus service is not running to avoid conflicts with the joystick device.
Ensure the vss-dbus service is stopped before running this tool:
sudo systemctl stop vss-dbus
To run this script with proper permissions:
sudo ./joystick_controller.py
Supported controllers:
- PS4 (DualShock 4)
- PS5 (DualSense)
- PS VR Aim Controller (input tracking only; vibration not supported)
"""

import os
Expand Down Expand Up @@ -60,12 +65,12 @@ class JoystickController:
sys.exit(1)

def initialize_controller(self):
"""Initializes Pygame and detects PS4 or PS5 controller."""
"""Initializes Pygame and detects PS4, PS5, or PS VR Aim controller."""
pygame.init()
pygame.joystick.init()

if pygame.joystick.get_count() == 0:
print("No controller detected. Please connect your PS4 or PS5 controller.")
print("No controller detected. Please connect your PS4, PS5, or PS VR Aim controller.")
sys.exit()

# Initialize joystick
Expand All @@ -80,6 +85,9 @@ class JoystickController:
elif "Wireless Controller" in controller_name or "DualShock" in controller_name:
self.controller_type = "PS4"
self.initialize_ps4_controller()
elif "PS VR Aim Controller" in controller_name:
self.controller_type = "PSVR"
self.initialize_psvr_controller()
else:
print(f"Unsupported controller: {controller_name}")
sys.exit(1)
Expand All @@ -99,19 +107,26 @@ class JoystickController:
"""Initializes the PS4 DualShock 4 controller."""
print("Initializing PS4 DualShock 4 controller features.")

def initialize_psvr_controller(self):
"""Initializes the PS VR Aim controller."""
print("Initializing PS VR Aim Controller features.")
# Custom handling for PS VR Aim Controller can be added here

def handle_vibration_with_button_10(self):
"""Handles motor vibration with Button 10 (Options button) for both controllers."""
"""Handles motor vibration with Button 10 (Options button) for PS4, PS5, or PS VR Aim controllers."""
button_10_state = self.joystick.get_button(10) # Button 10 (Options button)

if button_10_state != self.last_button_10_state:
if button_10_state:
if not self.is_motor_vibrating:
print("Starting motor vibration with Button 10")
if self.controller_type == "PS5":
self.ds.setLeftMotor(255) # Max power to left motor
self.ds.setRightMotor(255) # Max power to right motor
self.ds.setLeftMotor(255) # Max power to left motor
self.ds.setRightMotor(255) # Max power to right motor
elif self.controller_type == "PS4":
self.joystick.rumble(1.0, 1.0, 0) # Start PS4 rumble with infinite duration
elif self.controller_type == "PSVR":
print("Vibration support for PS VR Aim is not implemented.")
self.is_motor_vibrating = True
else:
print("Stopping motor vibration with Button 10")
Expand All @@ -124,12 +139,11 @@ class JoystickController:
self.last_button_10_state = button_10_state

# Continuously apply vibration while it is enabled
if self.is_motor_vibrating:
if self.controller_type == "PS4":
self.joystick.rumble(1.0, 1.0, 0) # Refresh PS4 rumble for continuous effect
if self.is_motor_vibrating and self.controller_type == "PS4":
self.joystick.rumble(1.0, 1.0, 0) # Refresh PS4 rumble for continuous effect

def log_inputs(self):
"""Logs button and analog inputs for both PS4 and PS5 controllers."""
"""Logs button and analog inputs for PS4, PS5, or PS VR Aim controllers."""
print(f"Controller has {self.joystick.get_numbuttons()} buttons, {self.joystick.get_numaxes()} axes, {self.joystick.get_numhats()} hats (D-pad)")

running = True
Expand Down Expand Up @@ -182,7 +196,6 @@ if __name__ == "__main__":
controller = JoystickController()
controller.check_sudo() # Ensure script is run with sudo
controller.check_vss_dbus_service() # Ensure vss-dbus is not running
controller.initialize_controller() # Initialize joystick (PS4/PS5)
controller.initialize_controller() # Initialize joystick (PS4/PS5/PS VR Aim)
controller.log_inputs() # Start logging inputs
controller.quit() # Clean up when done

0 comments on commit 159ff0e

Please sign in to comment.