|
|
@@ -1,14 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
# needed for python > 3.8
|
|
|
|
# needed for python > 3.8
|
|
|
|
import os, sys
|
|
|
|
import os
|
|
|
|
|
|
|
|
import sys
|
|
|
|
from sys import platform
|
|
|
|
from sys import platform
|
|
|
|
|
|
|
|
|
|
|
|
if platform.startswith('Windows') and sys.version_info >= (3,8):
|
|
|
|
if platform.startswith('Windows') and sys.version_info >= (3, 8):
|
|
|
|
os.add_dll_directory(os.getcwd())
|
|
|
|
os.add_dll_directory(os.getcwd())
|
|
|
|
|
|
|
|
|
|
|
|
import hidapi
|
|
|
|
import hidapi
|
|
|
|
from .enums import (LedOptions, PlayerID, PulseOptions, TriggerModes, Brightness, ConnectionType) # type: ignore
|
|
|
|
from .enums import (LedOptions, PlayerID, PulseOptions, TriggerModes, Brightness, ConnectionType) # type: ignore
|
|
|
|
import threading
|
|
|
|
import threading
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class pydualsense:
|
|
|
|
class pydualsense:
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, verbose: bool = False) -> None:#
|
|
|
|
def __init__(self, verbose: bool = False) -> None:#
|
|
|
@@ -18,7 +21,6 @@ class pydualsense:
|
|
|
|
self.leftMotor = 0
|
|
|
|
self.leftMotor = 0
|
|
|
|
self.rightMotor = 0
|
|
|
|
self.rightMotor = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def init(self):
|
|
|
|
def init(self):
|
|
|
|
"""initialize module and device states
|
|
|
|
"""initialize module and device states
|
|
|
|
"""
|
|
|
|
"""
|
|
|
@@ -37,7 +39,6 @@ class pydualsense:
|
|
|
|
self.input_report_length = 64
|
|
|
|
self.input_report_length = 64
|
|
|
|
self.output_report_length = 64
|
|
|
|
self.output_report_length = 64
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# thread for receiving and sending
|
|
|
|
# thread for receiving and sending
|
|
|
|
self.ds_thread = True
|
|
|
|
self.ds_thread = True
|
|
|
|
self.report_thread = threading.Thread(target=self.sendReport)
|
|
|
|
self.report_thread = threading.Thread(target=self.sendReport)
|
|
|
@@ -49,12 +50,11 @@ class pydualsense:
|
|
|
|
self.input_report_length = 64
|
|
|
|
self.input_report_length = 64
|
|
|
|
self.output_report_length = 64
|
|
|
|
self.output_report_length = 64
|
|
|
|
return ConnectionType.USB
|
|
|
|
return ConnectionType.USB
|
|
|
|
elif self.device._device.input_report_length == 78:
|
|
|
|
elif self.device._device.input_report_length == 78:
|
|
|
|
self.input_report_length = 78
|
|
|
|
self.input_report_length = 78
|
|
|
|
self.output_report_length = 78
|
|
|
|
self.output_report_length = 78
|
|
|
|
return ConnectionType.BT
|
|
|
|
return ConnectionType.BT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
def close(self):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Stops the report thread and closes the HID device
|
|
|
|
Stops the report thread and closes the HID device
|
|
|
@@ -63,7 +63,6 @@ class pydualsense:
|
|
|
|
self.report_thread.join()
|
|
|
|
self.report_thread.join()
|
|
|
|
self.device.close()
|
|
|
|
self.device.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __find_device(self) -> hidapi.Device:
|
|
|
|
def __find_device(self) -> hidapi.Device:
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
find HID device and open it
|
|
|
|
find HID device and open it
|
|
|
@@ -87,8 +86,7 @@ class pydualsense:
|
|
|
|
if device.vendor_id == 0x054c and device.product_id == 0x0CE6:
|
|
|
|
if device.vendor_id == 0x054c and device.product_id == 0x0CE6:
|
|
|
|
detected_device = device
|
|
|
|
detected_device = device
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if detected_device is None:
|
|
|
|
if detected_device == None:
|
|
|
|
|
|
|
|
raise Exception('No device detected')
|
|
|
|
raise Exception('No device detected')
|
|
|
|
|
|
|
|
|
|
|
|
dual_sense = hidapi.Device(vendor_id=detected_device.vendor_id, product_id=detected_device.product_id)
|
|
|
|
dual_sense = hidapi.Device(vendor_id=detected_device.vendor_id, product_id=detected_device.product_id)
|
|
|
@@ -112,7 +110,6 @@ class pydualsense:
|
|
|
|
raise Exception('maximum intensity is 255')
|
|
|
|
raise Exception('maximum intensity is 255')
|
|
|
|
self.leftMotor = intensity
|
|
|
|
self.leftMotor = intensity
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def setRightMotor(self, intensity: int):
|
|
|
|
def setRightMotor(self, intensity: int):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
set right motor rumble
|
|
|
|
set right motor rumble
|
|
|
@@ -131,7 +128,6 @@ class pydualsense:
|
|
|
|
raise Exception('maximum intensity is 255')
|
|
|
|
raise Exception('maximum intensity is 255')
|
|
|
|
self.rightMotor = intensity
|
|
|
|
self.rightMotor = intensity
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sendReport(self):
|
|
|
|
def sendReport(self):
|
|
|
|
"""background thread handling the reading of the device and updating its states
|
|
|
|
"""background thread handling the reading of the device and updating its states
|
|
|
|
"""
|
|
|
|
"""
|
|
|
@@ -143,7 +139,6 @@ class pydualsense:
|
|
|
|
# decrypt the packet and bind the inputs
|
|
|
|
# decrypt the packet and bind the inputs
|
|
|
|
self.readInput(inReport)
|
|
|
|
self.readInput(inReport)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# prepare new report for device
|
|
|
|
# prepare new report for device
|
|
|
|
outReport = self.prepareReport()
|
|
|
|
outReport = self.prepareReport()
|
|
|
|
|
|
|
|
|
|
|
@@ -174,7 +169,6 @@ class pydualsense:
|
|
|
|
self.state.cross = (buttonState & (1 << 5)) != 0
|
|
|
|
self.state.cross = (buttonState & (1 << 5)) != 0
|
|
|
|
self.state.square = (buttonState & (1 << 4)) != 0
|
|
|
|
self.state.square = (buttonState & (1 << 4)) != 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# dpad
|
|
|
|
# dpad
|
|
|
|
dpad_state = buttonState & 0x0F
|
|
|
|
dpad_state = buttonState & 0x0F
|
|
|
|
self.state.setDPadState(dpad_state)
|
|
|
|
self.state.setDPadState(dpad_state)
|
|
|
@@ -194,7 +188,6 @@ class pydualsense:
|
|
|
|
self.state.touchBtn = (misc2 & 0x02) != 0
|
|
|
|
self.state.touchBtn = (misc2 & 0x02) != 0
|
|
|
|
self.state.micBtn = (misc2 & 0x04) != 0
|
|
|
|
self.state.micBtn = (misc2 & 0x04) != 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# trackpad touch
|
|
|
|
# trackpad touch
|
|
|
|
self.state.trackPadTouch0.ID = inReport[33] & 0x7F
|
|
|
|
self.state.trackPadTouch0.ID = inReport[33] & 0x7F
|
|
|
|
self.state.trackPadTouch0.isActive = (inReport[33] & 0x80) == 0
|
|
|
|
self.state.trackPadTouch0.isActive = (inReport[33] & 0x80) == 0
|
|
|
@@ -207,17 +200,16 @@ class pydualsense:
|
|
|
|
self.state.trackPadTouch1.X = ((inReport[39] & 0x0f) << 8) | (inReport[38])
|
|
|
|
self.state.trackPadTouch1.X = ((inReport[39] & 0x0f) << 8) | (inReport[38])
|
|
|
|
self.state.trackPadTouch1.Y = ((inReport[40]) << 4) | ((inReport[39] & 0xf0) >> 4)
|
|
|
|
self.state.trackPadTouch1.Y = ((inReport[40]) << 4) | ((inReport[39] & 0xf0) >> 4)
|
|
|
|
|
|
|
|
|
|
|
|
# print(f'1Active = {self.state.trackPadTouch0.isActive}')
|
|
|
|
# print(f'1Active = {self.state.trackPadTouch0.isActive}')
|
|
|
|
# print(f'X1: {self.state.trackPadTouch0.X} Y2: {self.state.trackPadTouch0.Y}')
|
|
|
|
# print(f'X1: {self.state.trackPadTouch0.X} Y2: {self.state.trackPadTouch0.Y}')
|
|
|
|
|
|
|
|
|
|
|
|
# print(f'2Active = {self.state.trackPadTouch1.isActive}')
|
|
|
|
# print(f'2Active = {self.state.trackPadTouch1.isActive}')
|
|
|
|
# print(f'X2: {self.state.trackPadTouch1.X} Y2: {self.state.trackPadTouch1.Y}')
|
|
|
|
# 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}')
|
|
|
|
# print(f'DPAD {self.state.DpadLeft} {self.state.DpadUp} {self.state.DpadRight} {self.state.DpadDown}')
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: implement gyrometer and accelerometer
|
|
|
|
# TODO: implement gyrometer and accelerometer
|
|
|
|
# TODO: control mouse with touchpad for fun as DS4Windows
|
|
|
|
# TODO: control mouse with touchpad for fun as DS4Windows
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def writeReport(self, outReport):
|
|
|
|
def writeReport(self, outReport):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
write the report to the device
|
|
|
|
write the report to the device
|
|
|
@@ -227,7 +219,6 @@ class pydualsense:
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
self.device.write(bytes(outReport))
|
|
|
|
self.device.write(bytes(outReport))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def prepareReport(self):
|
|
|
|
def prepareReport(self):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
prepare the output to be send to the controller
|
|
|
|
prepare the output to be send to the controller
|
|
|
@@ -240,7 +231,6 @@ class pydualsense:
|
|
|
|
# packet type
|
|
|
|
# packet type
|
|
|
|
outReport[0] = 0x2
|
|
|
|
outReport[0] = 0x2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# flags determing what changes this packet will perform
|
|
|
|
# flags determing what changes this packet will perform
|
|
|
|
# 0x01 set the main motors (also requires flag 0x02); setting this by itself will allow rumble to gracefully terminate and then re-enable audio haptics, whereas not setting it will kill the rumble instantly and re-enable audio haptics.
|
|
|
|
# 0x01 set the main motors (also requires flag 0x02); setting this by itself will allow rumble to gracefully terminate and then re-enable audio haptics, whereas not setting it will kill the rumble instantly and re-enable audio haptics.
|
|
|
|
# 0x02 set the main motors (also requires flag 0x01; without bit 0x01 motors are allowed to time out without re-enabling audio haptics)
|
|
|
|
# 0x02 set the main motors (also requires flag 0x01; without bit 0x01 motors are allowed to time out without re-enabling audio haptics)
|
|
|
@@ -270,7 +260,7 @@ class pydualsense:
|
|
|
|
# set Micrphone LED, setting doesnt effect microphone settings
|
|
|
|
# set Micrphone LED, setting doesnt effect microphone settings
|
|
|
|
outReport[9] = self.audio.microphone_led # [9]
|
|
|
|
outReport[9] = self.audio.microphone_led # [9]
|
|
|
|
|
|
|
|
|
|
|
|
outReport[10] = 0x10 if self.audio.microphone_mute == True else 0x00
|
|
|
|
outReport[10] = 0x10 if self.audio.microphone_mute is True else 0x00
|
|
|
|
|
|
|
|
|
|
|
|
# add right trigger mode + parameters to packet
|
|
|
|
# add right trigger mode + parameters to packet
|
|
|
|
outReport[11] = self.triggerR.mode.value
|
|
|
|
outReport[11] = self.triggerR.mode.value
|
|
|
@@ -302,6 +292,7 @@ class pydualsense:
|
|
|
|
print(outReport)
|
|
|
|
print(outReport)
|
|
|
|
return outReport
|
|
|
|
return outReport
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DSTouchpad:
|
|
|
|
class DSTouchpad:
|
|
|
|
def __init__(self) -> None:
|
|
|
|
def __init__(self) -> None:
|
|
|
|
"""
|
|
|
|
"""
|
|
|
@@ -312,6 +303,7 @@ class DSTouchpad:
|
|
|
|
self.X = 0
|
|
|
|
self.X = 0
|
|
|
|
self.Y = 0
|
|
|
|
self.Y = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DSState:
|
|
|
|
class DSState:
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
def __init__(self) -> None:
|
|
|
@@ -321,7 +313,7 @@ class DSState:
|
|
|
|
self.L1, self.L2, self.L3, self.R1, self.R2, self.R3, self.R2Btn, self.L2Btn = False, False, False, False, False, False, False, False
|
|
|
|
self.L1, self.L2, self.L3, self.R1, self.R2, self.R3, self.R2Btn, self.L2Btn = False, False, False, False, False, False, False, False
|
|
|
|
self.share, self.options, self.ps, self.touch1, self.touch2, self.touchBtn, self.touchRight, self.touchLeft = False, False, False, False, False, False, False, False
|
|
|
|
self.share, self.options, self.ps, self.touch1, self.touch2, self.touchBtn, self.touchRight, self.touchLeft = False, False, False, False, False, False, False, False
|
|
|
|
self.touchFinger1, self.touchFinger2 = False, False
|
|
|
|
self.touchFinger1, self.touchFinger2 = False, False
|
|
|
|
self.RX, self.RY, self.LX, self.LY = 128,128,128,128
|
|
|
|
self.RX, self.RY, self.LX, self.LY = 128, 128, 128, 128
|
|
|
|
self.trackPadTouch0, self.trackPadTouch1 = DSTouchpad(), DSTouchpad()
|
|
|
|
self.trackPadTouch0, self.trackPadTouch1 = DSTouchpad(), DSTouchpad()
|
|
|
|
|
|
|
|
|
|
|
|
def setDPadState(self, dpad_state):
|
|
|
|
def setDPadState(self, dpad_state):
|
|
|
@@ -379,9 +371,9 @@ class DSLight:
|
|
|
|
def __init__(self) -> None:
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.brightness: Brightness = Brightness.low # sets
|
|
|
|
self.brightness: Brightness = Brightness.low # sets
|
|
|
|
self.playerNumber: PlayerID = PlayerID.player1
|
|
|
|
self.playerNumber: PlayerID = PlayerID.player1
|
|
|
|
self.ledOption : LedOptions = LedOptions.Both
|
|
|
|
self.ledOption: LedOptions = LedOptions.Both
|
|
|
|
self.pulseOptions : PulseOptions = PulseOptions.Off
|
|
|
|
self.pulseOptions: PulseOptions = PulseOptions.Off
|
|
|
|
self.TouchpadColor = (0,0,255)
|
|
|
|
self.TouchpadColor = (0, 0, 255)
|
|
|
|
|
|
|
|
|
|
|
|
def setLEDOption(self, option: LedOptions):
|
|
|
|
def setLEDOption(self, option: LedOptions):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
@@ -425,7 +417,7 @@ class DSLight:
|
|
|
|
raise TypeError('Need Brightness type')
|
|
|
|
raise TypeError('Need Brightness type')
|
|
|
|
self.brightness = brightness
|
|
|
|
self.brightness = brightness
|
|
|
|
|
|
|
|
|
|
|
|
def setPlayerID(self, player : PlayerID):
|
|
|
|
def setPlayerID(self, player: PlayerID):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Sets the PlayerID of the controller with the choosen LEDs.
|
|
|
|
Sets the PlayerID of the controller with the choosen LEDs.
|
|
|
|
The controller has 4 Player states
|
|
|
|
The controller has 4 Player states
|
|
|
@@ -440,7 +432,7 @@ class DSLight:
|
|
|
|
raise TypeError('Need PlayerID type')
|
|
|
|
raise TypeError('Need PlayerID type')
|
|
|
|
self.playerNumber = player
|
|
|
|
self.playerNumber = player
|
|
|
|
|
|
|
|
|
|
|
|
def setColorI(self, r: int , g: int, b: int) -> None:
|
|
|
|
def setColorI(self, r: int, g: int, b: int) -> None:
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Sets the Color around the Touchpad of the controller
|
|
|
|
Sets the Color around the Touchpad of the controller
|
|
|
|
|
|
|
|
|
|
|
@@ -458,8 +450,7 @@ class DSLight:
|
|
|
|
# check if color is out of bounds
|
|
|
|
# check if color is out of bounds
|
|
|
|
if (r > 255 or g > 255 or b > 255) or (r < 0 or g < 0 or b < 0):
|
|
|
|
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')
|
|
|
|
raise Exception('colors have values from 0 to 255 only')
|
|
|
|
self.TouchpadColor = (r,g,b)
|
|
|
|
self.TouchpadColor = (r, g, b)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def setColorT(self, color: tuple) -> None:
|
|
|
|
def setColorT(self, color: tuple) -> None:
|
|
|
|
"""
|
|
|
|
"""
|
|
|
@@ -475,11 +466,11 @@ class DSLight:
|
|
|
|
if not isinstance(color, tuple):
|
|
|
|
if not isinstance(color, tuple):
|
|
|
|
raise TypeError('Color type is tuple')
|
|
|
|
raise TypeError('Color type is tuple')
|
|
|
|
# unpack for out of bounds check
|
|
|
|
# unpack for out of bounds check
|
|
|
|
r,g,b = map(int, color)
|
|
|
|
r, g, b = map(int, color)
|
|
|
|
# check if color is out of bounds
|
|
|
|
# check if color is out of bounds
|
|
|
|
if (r > 255 or g > 255 or b > 255) or (r < 0 or g < 0 or b < 0):
|
|
|
|
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')
|
|
|
|
raise Exception('colors have values from 0 to 255 only')
|
|
|
|
self.TouchpadColor = (r,g,b)
|
|
|
|
self.TouchpadColor = (r, g, b)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DSAudio:
|
|
|
|
class DSAudio:
|
|
|
@@ -499,7 +490,7 @@ class DSAudio:
|
|
|
|
Exception: false state for the led
|
|
|
|
Exception: false state for the led
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
if not isinstance(value, bool):
|
|
|
|
if not isinstance(value, bool):
|
|
|
|
raise TypeError('MicrophoneLED can only be a bool')
|
|
|
|
raise TypeError('MicrophoneLED can only be a bool')
|
|
|
|
self.microphone_led = value
|
|
|
|
self.microphone_led = value
|
|
|
|
|
|
|
|
|
|
|
|
def setMicrophoneMute(self, state):
|
|
|
|
def setMicrophoneMute(self, state):
|
|
|
@@ -514,7 +505,7 @@ class DSAudio:
|
|
|
|
class DSTrigger:
|
|
|
|
class DSTrigger:
|
|
|
|
def __init__(self) -> None:
|
|
|
|
def __init__(self) -> None:
|
|
|
|
# trigger modes
|
|
|
|
# trigger modes
|
|
|
|
self.mode : TriggerModes = TriggerModes.Off
|
|
|
|
self.mode: TriggerModes = TriggerModes.Off
|
|
|
|
|
|
|
|
|
|
|
|
# force parameters for the triggers
|
|
|
|
# force parameters for the triggers
|
|
|
|
self.forces = [0 for i in range(7)]
|
|
|
|
self.forces = [0 for i in range(7)]
|
|
|
|