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 inputslight
- LED control interfaceaudio
- Audio control interfacetriggerL
- Left trigger controltriggerR
- Right trigger controlbattery
- Battery status information
State Properties¶
state.square
- Square button statestate.triangle
- Triangle button statestate.circle
- Circle button statestate.cross
- Cross button statestate.L1
,state.R1
- L1/R1 button statesstate.L2
,state.R2
- L2/R2 button statesstate.L3
,state.R3
- L3/R3 button statesstate.DpadUp
,state.DpadDown
- D-pad statesstate.DpadLeft
,state.DpadRight
- D-pad statesstate.LX
,state.LY
- Left joystick positionstate.RX
,state.RY
- Right joystick positionstate.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 datastate.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:
D-pad Events¶
dpad_up
- D-pad up state changesdpad_down
- D-pad down state changesdpad_left
- D-pad left state changesdpad_right
- D-pad right state changes
Trigger Events¶
l1_changed
- L1 button state changesl2_changed
- L2 button state changesl3_changed
- L3 button state changesr1_changed
- R1 button state changesr2_changed
- R2 button state changesr3_changed
- R3 button state changesl2_value_changed
- Left trigger analog value changesr2_value_changed
- Right trigger analog value changes
Motion Events¶
gyro_changed
- Gyroscope data changesaccelerometer_changed
- Accelerometer data changes
Joystick Events¶
left_joystick_changed
- Left joystick position changesright_joystick_changed
- Right joystick position changes
Other Events¶
ps_pressed
- PS button state changestouch_pressed
- Touchpad button state changesmicrophone_pressed
- Microphone button state changesshare_pressed
- Share button state changesoption_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