Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
c39f3f2ea5 | ||
|
ea319db5a3 | ||
|
d62e8d133e | ||
|
fe435f6e36 | ||
|
cc767d5fcd | ||
|
1ab69d6c96 | ||
|
b004d2bc7b | ||
|
3f16538555 | ||
|
f1be774e68 | ||
|
9560d8e637 | ||
|
ecb42d9c0a | ||
|
604c5f2800 | ||
|
a54fb55b91 | ||
|
0bf55f756b | ||
|
2a5afd7cb0 | ||
|
0a6fee2f85 | ||
|
2f5579cc49 |
25
README.md
25
README.md
@@ -1,15 +1,17 @@
|
||||
# pydualsense
|
||||
control your dualsense through python. using the hid library this module implements the sending report for controlling you new PS5 controller. It creates a background thread to constantly update the controller.
|
||||
control your dualsense through python. using the hid library this module implements the sending report for controlling you new PS5 controller. It creates a background thread to constantly receive and update the controller.
|
||||
|
||||
# install
|
||||
|
||||
Just install the package from pypi
|
||||
Just install the package from [pypi](https://pypi.org/project/pydualsense/)
|
||||
|
||||
```bash
|
||||
pip install pydualsense
|
||||
```
|
||||
# usage
|
||||
|
||||
|
||||
|
||||
```python
|
||||
|
||||
from pydualsense import pydualsense
|
||||
@@ -20,10 +22,27 @@ ds.setLeftTriggerMode(TriggerModes.Rigid)
|
||||
ds.setLeftTriggerForce(1, 255)
|
||||
ds.close() # closing the controller
|
||||
```
|
||||
|
||||
See ``examples`` folder for some more ideas
|
||||
|
||||
# Help wanted
|
||||
|
||||
Help wanted from people that want to use this and have feature requests. Just open a issue with the correct label.
|
||||
|
||||
# dependecies
|
||||
|
||||
- hid >= 1.0.4
|
||||
|
||||
# Credits
|
||||
|
||||
|
||||
Most stuff for this implementation were provided by and used from:
|
||||
|
||||
|
||||
- [https://www.reddit.com/r/gamedev/comments/jumvi5/dualsense_haptics_leds_and_more_hid_output_report/](https://www.reddit.com/r/gamedev/comments/jumvi5/dualsense_haptics_leds_and_more_hid_output_report/)
|
||||
- [https://github.com/Ryochan7/DS4Windows](https://github.com/Ryochan7/DS4Windows)
|
||||
|
||||
# Coming soon
|
||||
|
||||
- reading the states of the controller to enable a fully compatibility with python - partially done
|
||||
- add documentation using sphinx
|
||||
- add documentation using sphinx
|
||||
|
15
examples/README.md
Normal file
15
examples/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# Examples
|
||||
|
||||
This folder contains some examples on applications for the library and its usage
|
||||
|
||||
## leds.py
|
||||
|
||||
The leds.py shows you how you can interact and change the lights of the controller
|
||||
|
||||
## effects.py
|
||||
|
||||
The effects.py show some effects of the controller
|
||||
|
||||
## read_controller.py
|
||||
|
||||
The read_controller.py display how you can access the button state of the controller
|
22
examples/effects.py
Normal file
22
examples/effects.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from pydualsense import *
|
||||
|
||||
# get dualsense instance
|
||||
dualsense = pydualsense()
|
||||
dualsense.init()
|
||||
|
||||
print('Trigger Effect demo started')
|
||||
|
||||
dualsense.setLeftMotor(255)
|
||||
dualsense.setRightMotor(100)
|
||||
dualsense.setLeftTriggerMode(TriggerModes.Rigid)
|
||||
dualsense.setLeftTriggerForce(1, 255)
|
||||
|
||||
dualsense.setRightTriggerMode(TriggerModes.Pulse_A)
|
||||
dualsense.setRightTriggerForce(0, 200)
|
||||
dualsense.setRightTriggerForce(1, 255)
|
||||
dualsense.setRightTriggerForce(2, 175)
|
||||
|
||||
import time; time.sleep(3)
|
||||
|
||||
# terminate the thread for message and close the device
|
||||
dualsense.close()
|
16
examples/leds.py
Normal file
16
examples/leds.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from pydualsense import *
|
||||
|
||||
# get dualsense instance
|
||||
dualsense = pydualsense()
|
||||
dualsense.init()
|
||||
# set color around touchpad to red
|
||||
dualsense.setColor(0,0,255)
|
||||
# enable microphone indicator
|
||||
dualsense.setMicrophoneLED(1)
|
||||
# set all player indicators on
|
||||
dualsense.setPlayerID(PlayerID.all)
|
||||
# sleep a little to see the result on the controller
|
||||
# this is not needed in normal usage
|
||||
import time; time.sleep(2)
|
||||
# terminate the thread for message and close the device
|
||||
dualsense.close()
|
15
examples/read_controller.py
Normal file
15
examples/read_controller.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from pydualsense import *
|
||||
|
||||
# create dualsense
|
||||
dualsense = pydualsense()
|
||||
# find device and initialize
|
||||
dualsense.init()
|
||||
|
||||
# read controller state until R1 is pressed
|
||||
while not dualsense.state.R1:
|
||||
print(f"Circle : {dualsense.state.circle} Cross : {dualsense.state.cross} L Stick X : {dualsense.state.LX} L Stick Y : {dualsense.state.LY}")
|
||||
|
||||
# close device
|
||||
dualsense.close()
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
from enum import IntFlag
|
||||
|
||||
class LedOptions(IntFlag):
|
||||
class LedOptions(IntFlag):
|
||||
Off=0x0,
|
||||
PlayerLedBrightness=0x1,
|
||||
UninterrumpableLed=0x2,
|
||||
@@ -25,12 +25,12 @@ class PlayerID(IntFlag):
|
||||
all = 31
|
||||
|
||||
class TriggerModes(IntFlag):
|
||||
Off =0x0, # no resistance
|
||||
Rigid =0x1, # continous resistance
|
||||
Pulse =0x2, # section resistance
|
||||
Rigid_A=0x1 | 0x20,
|
||||
Rigid_B=0x1 | 0x04,
|
||||
Rigid_AB=0x1 | 0x20 | 0x04,
|
||||
Off = 0x0, # no resistance
|
||||
Rigid = 0x1, # continous resistance
|
||||
Pulse = 0x2, # section resistance
|
||||
Rigid_A = 0x1 | 0x20,
|
||||
Rigid_B = 0x1 | 0x04,
|
||||
Rigid_AB = 0x1 | 0x20 | 0x04,
|
||||
Pulse_A = 0x2 | 0x20,
|
||||
Pulse_B = 0x2 | 0x04,
|
||||
Pulse_AB = 0x2 | 0x20 | 0x04,
|
||||
|
@@ -1,34 +0,0 @@
|
||||
from PyQt5 import QtCore, QtGui, QtWidgets
|
||||
import sys
|
||||
from interface import Ui_MainWindow
|
||||
from pydualsense import pydualsense
|
||||
|
||||
def colorR(value):
|
||||
global colorR
|
||||
colorR = value
|
||||
|
||||
def colorG(value):
|
||||
global colorG
|
||||
colorG = value
|
||||
|
||||
def colorB(value):
|
||||
global colorB
|
||||
colorB = value
|
||||
|
||||
def send():
|
||||
ds.setColor(colorR, colorG, colorB)
|
||||
ds.sendReport()
|
||||
if __name__ == "__main__":
|
||||
global ds
|
||||
app = QtWidgets.QApplication(sys.argv)
|
||||
MainWindow = QtWidgets.QMainWindow()
|
||||
ui = Ui_MainWindow()
|
||||
ui.setupUi(MainWindow)
|
||||
ds = pydualsense()
|
||||
# connect interface to
|
||||
ui.slider_r.valueChanged.connect(colorR)
|
||||
ui.slider_g.valueChanged.connect(colorG)
|
||||
ui.slider_b.valueChanged.connect(colorB)
|
||||
ui.pushButton.clicked.connect(send)
|
||||
MainWindow.show()
|
||||
sys.exit(app.exec_())
|
@@ -1,58 +1,95 @@
|
||||
from os import device_encoding
|
||||
import hid
|
||||
from .enums import (LedOptions, PlayerID,
|
||||
PulseOptions, TriggerModes, Brightness)
|
||||
import threading
|
||||
|
||||
import sys
|
||||
import winreg
|
||||
class pydualsense:
|
||||
|
||||
def __init__(self) -> None:
|
||||
def __init__(self, verbose: bool = False) -> None:
|
||||
# TODO: maybe add a init function to not automatically allocate controller when class is declared
|
||||
self.device: hid.Device = self.__find_device()
|
||||
self.light = DSLight() # control led light of ds
|
||||
self.audio = DSAudio()
|
||||
self.triggerL = DSTrigger()
|
||||
self.triggerR = DSTrigger()
|
||||
|
||||
self.color = (0,0,255) # set color around touchpad to blue
|
||||
|
||||
self.verbose = verbose
|
||||
self.receive_buffer_size = 64
|
||||
self.send_report_size = 48
|
||||
# controller states
|
||||
self.state = DSState()
|
||||
self.color = (0,0,255) # set color around touchpad to blue
|
||||
|
||||
self.leftMotor = 0
|
||||
self.rightMotor = 0
|
||||
|
||||
|
||||
def init(self):
|
||||
"""initialize module and device
|
||||
"""
|
||||
self.device: hid.Device = self.__find_device()
|
||||
self.light = DSLight() # control led light of ds
|
||||
self.audio = DSAudio() # ds audio setting
|
||||
self.triggerL = DSTrigger() # left trigger
|
||||
self.triggerR = DSTrigger() # right trigger
|
||||
|
||||
self.state = DSState() # controller states
|
||||
|
||||
|
||||
# thread for receiving and sending
|
||||
self.ds_thread = True
|
||||
self.report_thread = threading.Thread(target=self.sendReport)
|
||||
self.report_thread.start()
|
||||
|
||||
self.init = True
|
||||
|
||||
def close(self):
|
||||
self.ds_thread = False
|
||||
self.report_thread.join()
|
||||
self.device.close()
|
||||
|
||||
def _check_hide(self):
|
||||
"""check if hidguardian is used and controller is hidden
|
||||
"""
|
||||
if sys.platform.startswith('win32'):
|
||||
try:
|
||||
access_reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
|
||||
access_key = winreg.OpenKey(access_reg, 'SYSTEM\CurrentControlSet\Services\HidGuardian\Parameters', 0, winreg.KEY_READ)
|
||||
affected_devices = winreg.QueryValueEx(access_key, 'AffectedDevices')[0]
|
||||
if "054C" in affected_devices and "0CE6" in affected_devices:
|
||||
return True
|
||||
return False
|
||||
except OSError as e:
|
||||
print(e)
|
||||
else:
|
||||
# TODO: find something for other platforms. Maybe not even needed on linux
|
||||
return False
|
||||
|
||||
|
||||
def __find_device(self):
|
||||
devices = hid.enumerate(vid=0x054c)
|
||||
found_devices = []
|
||||
for device in devices:
|
||||
if device['vendor_id'] == 0x054c and device['product_id'] == 0x0CE6:
|
||||
found_devices.append(device)
|
||||
|
||||
# TODO: detect connection mode, bluetooth has a bigger write buffer
|
||||
# TODO: implement multiple controllers working
|
||||
if len(found_devices) != 1:
|
||||
raise Exception('no dualsense controller detected')
|
||||
if self._check_hide():
|
||||
raise Exception('HIDGuardian detected. Delete the controller from HIDGuardian and restart PC to connect to controller')
|
||||
detected_device = None
|
||||
devices = hid.enumerate(vid=0x054c)
|
||||
for device in devices:
|
||||
if device['vendor_id'] == 0x054c and device['product_id'] == 0x0CE6:
|
||||
detected_device = device
|
||||
|
||||
|
||||
dual_sense = hid.Device(vid=found_devices[0]['vendor_id'], pid=found_devices[0]['product_id'])
|
||||
|
||||
if detected_device == None:
|
||||
raise Exception('No device detected')
|
||||
|
||||
dual_sense = hid.Device(vid=detected_device['vendor_id'], pid=detected_device['product_id'])
|
||||
return dual_sense
|
||||
|
||||
|
||||
# color stuff
|
||||
def setColor(self, r: int, g:int, b:int):
|
||||
if r > 255 or g > 255 or b > 255:
|
||||
raise Exception('colors have values from 0 to 255 only')
|
||||
self.color = (r,g,b)
|
||||
def setLeftMotor(self, intensity: int):
|
||||
if intensity > 255:
|
||||
raise Exception('maximum intensity is 255')
|
||||
self.leftMotor = intensity
|
||||
|
||||
|
||||
def setRightMotor(self, intensity: int):
|
||||
if intensity > 255:
|
||||
raise Exception('maximum intensity is 255')
|
||||
self.rightMotor = intensity
|
||||
|
||||
|
||||
# right trigger
|
||||
def setRightTriggerMode(self, mode: TriggerModes):
|
||||
"""set the trigger mode for R2
|
||||
@@ -62,6 +99,7 @@ class pydualsense:
|
||||
"""
|
||||
self.triggerR.mode = mode
|
||||
|
||||
|
||||
def setRightTriggerForce(self, forceID: int, force: int):
|
||||
"""set the right trigger force. trigger consist of 7 parameter
|
||||
|
||||
@@ -76,7 +114,6 @@ class pydualsense:
|
||||
self.triggerR.setForce(id=forceID, force=force)
|
||||
|
||||
|
||||
# left trigger
|
||||
def setLeftTriggerMode(self, mode: TriggerModes):
|
||||
"""set the trigger mode for L2
|
||||
|
||||
@@ -85,6 +122,7 @@ class pydualsense:
|
||||
"""
|
||||
self.triggerL.mode = mode
|
||||
|
||||
|
||||
def setLeftTriggerForce(self, forceID: int, force: int):
|
||||
"""set the left trigger force. trigger consist of 7 parameter
|
||||
|
||||
@@ -102,8 +140,56 @@ class pydualsense:
|
||||
# TODO: audio
|
||||
# audio stuff
|
||||
def setMicrophoneLED(self, value):
|
||||
self.audio.microphoneLED = value
|
||||
self.audio.microphone_led = value
|
||||
|
||||
# color stuff
|
||||
def setColor(self, r: int, g:int, b:int):
|
||||
"""sets the led colour around the touchpad
|
||||
|
||||
:param r: red channel, 0..255
|
||||
:type r: int
|
||||
:param g: green channel, 0..255
|
||||
:type g: int
|
||||
:param b: blue channel, 0..255
|
||||
:type b: int
|
||||
:raises Exception: wron color values
|
||||
"""
|
||||
if (r > 255 or g > 255 or b > 255) or (r < 0 or g < 0 or b < 0):
|
||||
raise Exception('colors have values from 0 to 255 only')
|
||||
self.color = (r,g,b)
|
||||
|
||||
def setLEDOption(self, option: LedOptions):
|
||||
"""set led option
|
||||
|
||||
:param option: led option
|
||||
:type option: LedOptions
|
||||
"""
|
||||
self.light.ledOption = option
|
||||
|
||||
def setPulseOption(self, option: PulseOptions):
|
||||
"""set the pulse option for the leds
|
||||
|
||||
:param option: [description]
|
||||
:type option: PulseOptions
|
||||
"""
|
||||
self.light.pulseOptions = option
|
||||
|
||||
def setBrightness(self, brightness: Brightness):
|
||||
"""set the brightness of the player leds
|
||||
|
||||
:param brightness: brightness for the leds
|
||||
:type brightness: Brightness
|
||||
"""
|
||||
self.light.brightness = brightness
|
||||
|
||||
def setPlayerID(self, player : PlayerID):
|
||||
"""set the player ID. The controller has 5 white LED which signals
|
||||
which player the controller is
|
||||
|
||||
:param player: the player id from 1 to 5
|
||||
:type player: PlayerID
|
||||
"""
|
||||
self.light.playerNumber = player
|
||||
|
||||
def sendReport(self):
|
||||
"""background thread handling the reading of the device and updating its states
|
||||
@@ -112,7 +198,7 @@ class pydualsense:
|
||||
|
||||
# read data from the input report of the controller
|
||||
inReport = self.device.read(self.receive_buffer_size)
|
||||
|
||||
|
||||
# decrypt the packet and bind the inputs
|
||||
self.readInput(inReport)
|
||||
|
||||
@@ -131,10 +217,10 @@ class pydualsense:
|
||||
"""
|
||||
states = list(inReport) # convert bytes to list
|
||||
# states 0 is always 1
|
||||
self.state.LX = states[1]
|
||||
self.state.LY = states[2]
|
||||
self.state.RX = states[3]
|
||||
self.state.RY = states[4]
|
||||
self.state.LX = states[1] - 127
|
||||
self.state.LY = states[2] - 127
|
||||
self.state.RX = states[3] - 127
|
||||
self.state.RY = states[4] - 127
|
||||
self.state.L2 = states[5]
|
||||
self.state.R2 = states[6]
|
||||
|
||||
@@ -184,10 +270,9 @@ class pydualsense:
|
||||
# print(f'2Active = {self.state.trackPadTouch1.isActive}')
|
||||
# print(f'X2: {self.state.trackPadTouch1.X} Y2: {self.state.trackPadTouch1.Y}')
|
||||
# print(f'DPAD {self.state.DpadLeft} {self.state.DpadUp} {self.state.DpadRight} {self.state.DpadDown}')
|
||||
|
||||
|
||||
# TODO: implement gyrometer and accelerometer
|
||||
# TODO: control mouse with touchpad for fun as DS4Windows
|
||||
|
||||
|
||||
|
||||
def writeReport(self, outReport):
|
||||
@@ -206,7 +291,7 @@ class pydualsense:
|
||||
:rtype: list
|
||||
"""
|
||||
outReport = [0] * 48 # create empty list with range of output report
|
||||
# packet type
|
||||
# packet type
|
||||
outReport[0] = 0x2
|
||||
|
||||
|
||||
@@ -216,7 +301,7 @@ class pydualsense:
|
||||
# 0x04 set the right trigger motor
|
||||
# 0x08 set the left trigger motor
|
||||
# 0x10 modification of audio volume
|
||||
# 0x20 toggling of internal speaker while headset is connected
|
||||
# 0x20 toggling of internal speaker while headset is connected
|
||||
# 0x40 modification of microphone volume
|
||||
outReport[1] = 0xff # [1]
|
||||
|
||||
@@ -227,12 +312,12 @@ class pydualsense:
|
||||
# 0x08 will actively turn all LEDs off? Convenience flag? (if so, third parties might not support it properly)
|
||||
# 0x10 toggling white player indicator LEDs below touchpad
|
||||
# 0x20 ???
|
||||
# 0x40 adjustment of overall motor/effect power (index 37 - read note on triggers)
|
||||
# 0x40 adjustment of overall motor/effect power (index 37 - read note on triggers)
|
||||
# 0x80 ???
|
||||
outReport[2] = 0x1 | 0x2 | 0x4 | 0x10 | 0x40 # [2]
|
||||
|
||||
outReport[3]= 0 # left low freq motor 0-255 # [3]
|
||||
outReport[4] = 0 # right low freq motor 0-255 # [4]
|
||||
|
||||
outReport[3] = self.leftMotor # left low freq motor 0-255 # [3]
|
||||
outReport[4] = self.rightMotor # right low freq motor 0-255 # [4]
|
||||
|
||||
# outReport[5] - outReport[8] audio related
|
||||
|
||||
@@ -265,7 +350,8 @@ class pydualsense:
|
||||
outReport[45] = self.color[0]
|
||||
outReport[46] = self.color[1]
|
||||
outReport[47] = self.color[2]
|
||||
|
||||
if self.verbose:
|
||||
print(outReport)
|
||||
return outReport
|
||||
|
||||
class DSTouchpad:
|
||||
@@ -341,7 +427,7 @@ class DSLight:
|
||||
make it simple, no get or set functions. quick and dirty
|
||||
"""
|
||||
def __init__(self) -> None:
|
||||
self.brightness: Brightness = Brightness.low # sets
|
||||
self.brightness: Brightness = Brightness.low # sets
|
||||
self.playerNumber: PlayerID = PlayerID.player1
|
||||
self.ledOption : LedOptions = LedOptions.Both
|
||||
self.pulseOptions : PulseOptions = PulseOptions.Off
|
||||
@@ -349,11 +435,7 @@ class DSLight:
|
||||
def setBrightness(self, brightness: Brightness):
|
||||
self._brightness = brightness
|
||||
|
||||
def setPlayerNumer(self, player):
|
||||
if player > 5:
|
||||
raise Exception('only 5 players supported. choose 1-5')
|
||||
|
||||
|
||||
|
||||
class DSAudio:
|
||||
def __init__(self) -> None:
|
||||
|
Reference in New Issue
Block a user