pydualsense main class

The pydualsense class is the core interface for the DualSense controller. It provides comprehensive control and monitoring of all controller features.

Features

  • Button state monitoring (triangle, circle, cross, square)

  • D-pad control

  • Trigger control with analog values

  • Touchpad input handling

  • Motion sensors (gyroscope and accelerometer)

  • LED control and customization

  • Audio control

  • Battery monitoring

  • Event-based input handling

Initialization

from pydualsense import pydualsense

# Create controller instance
ds = pydualsense()

# Initialize connection
ds.init()

Core Properties

  • state - Current state of all controller inputs

  • light - LED control interface

  • audio - Audio control interface

  • triggerL - Left trigger control

  • triggerR - Right trigger control

  • battery - Battery status information

State Properties

  • state.square - Square button state

  • state.triangle - Triangle button state

  • state.circle - Circle button state

  • state.cross - Cross button state

  • state.L1, state.R1 - L1/R1 button states

  • state.L2, state.R2 - L2/R2 button states

  • state.L3, state.R3 - L3/R3 button states

  • state.DpadUp, state.DpadDown - D-pad states

  • state.DpadLeft, state.DpadRight - D-pad states

  • state.LX, state.LY - Left joystick position

  • state.RX, state.RY - Right joystick position

  • state.L1_value, state.R1_value - Trigger analog values (0-255)

  • state.L2_value, state.R2_value - Trigger analog values (0-255)

  • state.trackPadTouch0, state.trackPadTouch1 - Touchpad touch data

  • state.gyro - Gyroscope data (Pitch, Yaw, Roll)

  • state.accelerometer - Accelerometer data (X, Y, Z)

Event System

The controller provides an event-based system for monitoring input changes:

Button Events

  • triangle_pressed - Triangle button state changes

  • circle_pressed - Circle button state changes

  • cross_pressed - Cross button state changes

  • square_pressed - Square button state changes

D-pad Events

  • dpad_up - D-pad up state changes

  • dpad_down - D-pad down state changes

  • dpad_left - D-pad left state changes

  • dpad_right - D-pad right state changes

Trigger Events

  • l1_changed - L1 button state changes

  • l2_changed - L2 button state changes

  • l3_changed - L3 button state changes

  • r1_changed - R1 button state changes

  • r2_changed - R2 button state changes

  • r3_changed - R3 button state changes

  • l2_value_changed - Left trigger analog value changes

  • r2_value_changed - Right trigger analog value changes

Motion Events

  • gyro_changed - Gyroscope data changes

  • accelerometer_changed - Accelerometer data changes

Joystick Events

  • left_joystick_changed - Left joystick position changes

  • right_joystick_changed - Right joystick position changes

Other Events

  • ps_pressed - PS button state changes

  • touch_pressed - Touchpad button state changes

  • microphone_pressed - Microphone button state changes

  • share_pressed - Share button state changes

  • option_pressed - Options button state changes

Examples

Basic Usage

from pydualsense import pydualsense

# Initialize controller
ds = pydualsense()
ds.init()

# Read button states
if ds.state.circle:
    print("Circle button is pressed")

# Read trigger analog values
print(f"L2 value: {ds.state.L2_value}")
print(f"R2 value: {ds.state.R2_value}")

# Read motion data
print(f"Gyro: P={ds.state.gyro.Pitch}, Y={ds.state.gyro.Yaw}, R={ds.state.gyro.Roll}")
print(f"Accel: X={ds.state.accelerometer.X}, Y={ds.state.accelerometer.Y}, Z={ds.state.accelerometer.Z}")

Event Handling

# Monitor button presses
def on_circle_pressed(state):
    print(f"Circle button {'pressed' if state else 'released'}")

ds.circle_pressed += on_circle_pressed

# Monitor trigger analog values
def on_l2_value_changed(value):
    print(f"L2 trigger value: {value}")

ds.l2_value_changed += on_l2_value_changed

# Monitor motion data
def on_gyro_changed(pitch, yaw, roll):
    print(f"Gyro: P={pitch}, Y={yaw}, R={roll}")

ds.gyro_changed += on_gyro_changed

Error Handling

The class handles various error conditions:

  • Connection errors when controller is not found

  • Invalid parameter values for motor intensity, colors, etc.

  • Type errors for incorrect parameter types

API Reference