6 Commits
v0.5.5 ... v0.6

Author SHA1 Message Date
Flo
9d8ab950de Merge pull request #32 from flok/event_system
Event System and Gyro / Accelerometer support
2022-08-14 15:55:03 +02:00
Florian Kaiser
40f74472d7 Update README.md and version to 0.6 2022-08-14 15:51:39 +02:00
Florian Kaiser
b091660130 Add event system for states, added gyro and accelerometer 2022-08-14 15:48:58 +02:00
Florian Kaiser
82d407bfe2 Linting with pep8 2022-08-14 14:08:57 +02:00
Flo
0f279f1ee8 Update hidapi-usb version for linux support 2021-08-05 23:02:39 +02:00
Flo
624d17c919 Update README.md with Linux instructions 2021-08-05 21:55:24 +02:00
8 changed files with 351 additions and 87 deletions

View File

@@ -1,21 +1,45 @@
# pydualsense # pydualsense
control your dualsense through python. using the hid library this package implements the report features for controlling your new PS5 controller. control your dualsense through python. using the hid library this package implements the report features for controlling your new PS5 controller.
# install # Installation
## Windows
Download [hidapi](https://github.com/libusb/hidapi/releases) and place the x64 .dll file into your Workspace. After that install the package from [pypi](https://pypi.org/project/pydualsense/). Download [hidapi](https://github.com/libusb/hidapi/releases) and place the x64 .dll file into your Workspace. After that install the package from [pypi](https://pypi.org/project/pydualsense/).
```bash ```bash
pip install pydualsense pip install pydualsense
``` ```
## Linux
On Linux based system you first need to install the hidapi through your package manager of your system.
On an Ubuntu system the package ```libhidapi-dev``` is required.
```bash
sudo apt install libhidapi-dev
```
After that install the package from [pypi](https://pypi.org/project/pydualsense/).
```bash
pip install pydualsense
```
# usage # usage
```python ```python
from pydualsense import pydualsense, TriggerModes from pydualsense import pydualsense, TriggerModes
def cross_pressed(state):
print(state)
ds = pydualsense() # open controller ds = pydualsense() # open controller
ds.init() # initialize controller ds.init() # initialize controller
ds.cross_pressed += cross_pressed
ds.light.setColorI(255,0,0) # set touchpad color to red ds.light.setColorI(255,0,0) # set touchpad color to red
ds.triggerL.setMode(TriggerModes.Rigid) ds.triggerL.setMode(TriggerModes.Rigid)
ds.triggerL.setForce(1, 255) ds.triggerL.setForce(1, 255)
@@ -30,7 +54,7 @@ Help wanted from people that want to use this and have feature requests. Just op
# dependecies # dependecies
- hidapi-usb >= 0.2.6 - hidapi-usb >= 0.3
# Credits # Credits

View File

@@ -1,15 +1,41 @@
from pydualsense import * from pydualsense import *
def cross_down(state):
print(f'cross {state}')
def circle_down(state):
print(f'circle {state}')
def dpad_down(state):
print(f'dpad {state}')
def joystick(stateX, stateY):
print(f'lj {stateX} {stateY}')
def gyro_changed(pitch, yaw, roll):
print(f'{pitch}, {yaw}, {roll}')
# create dualsense # create dualsense
dualsense = pydualsense() dualsense = pydualsense()
# find device and initialize # find device and initialize
dualsense.init() dualsense.init()
# add events handler functions
dualsense.cross_pressed += cross_down
dualsense.circle_pressed += circle_down
dualsense.dpad_down += dpad_down
dualsense.left_joystick_changed += joystick
dualsense.gyro_changed += gyro_changed
# read controller state until R1 is pressed # read controller state until R1 is pressed
while not dualsense.state.R1: 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 # close device
dualsense.close() dualsense.close()

View File

@@ -1,41 +1,46 @@
from enum import IntFlag from enum import IntFlag
class ConnectionType(IntFlag): class ConnectionType(IntFlag):
BT = 0x0, BT = 0x0
USB = 0x1 USB = 0x1
class LedOptions(IntFlag): class LedOptions(IntFlag):
Off=0x0, Off = 0x0
PlayerLedBrightness=0x1, PlayerLedBrightness = 0x1
UninterrumpableLed=0x2, UninterrumpableLed = 0x2
Both = 0x01 | 0x02 Both = 0x01 | 0x02
class PulseOptions(IntFlag): class PulseOptions(IntFlag):
Off=0x0, Off = 0x0
FadeBlue=0x1, FadeBlue = 0x1
FadeOut = 0x2 FadeOut = 0x2
class Brightness(IntFlag): class Brightness(IntFlag):
high = 0x0, high = 0x0
medium = 0x1, medium = 0x1
low = 0x2 low = 0x2
class PlayerID(IntFlag): class PlayerID(IntFlag):
player1 = 4, PLAYER_1 = 4
player2 = 10, PLAYER_2 = 10
player3 = 21, PLAYER_3 = 21
player4 = 27, PLAYER_4 = 27
all = 31 ALL = 31
class TriggerModes(IntFlag): class TriggerModes(IntFlag):
Off = 0x0, # no resistance Off = 0x0 # no resistance
Rigid = 0x1, # continous resistance Rigid = 0x1 # continous resistance
Pulse = 0x2, # section resistance Pulse = 0x2 # section resistance
Rigid_A = 0x1 | 0x20, Rigid_A = 0x1 | 0x20
Rigid_B = 0x1 | 0x04, Rigid_B = 0x1 | 0x04
Rigid_AB = 0x1 | 0x20 | 0x04, Rigid_AB = 0x1 | 0x20 | 0x04
Pulse_A = 0x2 | 0x20, Pulse_A = 0x2 | 0x20
Pulse_B = 0x2 | 0x04, Pulse_B = 0x2 | 0x04
Pulse_AB = 0x2 | 0x20 | 0x04, Pulse_AB = 0x2 | 0x20 | 0x04
Calibration = 0xFC Calibration = 0xFC

View File

@@ -0,0 +1,60 @@
from collections import defaultdict
class Event(object):
"""
Base class for the event driven system
"""
def __init__(self) -> None:
"""
initialise event system
"""
self._event_handler = []
def subscribe(self, fn):
"""
add a event subscription
Args:
fn (function): _description_
"""
self._event_handler.append(fn)
return self
def unsubscribe(self, fn):
"""
delete event subscription fn
Args:
fn (function): _description_
"""
self._event_handler.remove(fn)
return self
def __iadd__(self, fn):
"""
add event subscription fn
Args:
fn (function): _description_
"""
self._event_handler.append(fn)
return self
def __isub__(self, fn):
"""
delete event subscription fn
Args:
fn (function): _description_
"""
self._event_handler.remove(fn)
return self
def __call__(self, *args, **keywargs):
"""
calls all event subscription functions
"""
for eventhandler in self._event_handler:
eventhandler(*args, **keywargs)

View File

@@ -1,13 +1,15 @@
import winreg import winreg
import sys import sys
def check_hide() -> bool: def check_hide() -> bool:
"""check if hidguardian is used and controller is hidden """
check if hidguardian is used and controller is hidden
""" """
if sys.platform.startswith('win32'): if sys.platform.startswith('win32'):
try: try:
access_reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) access_reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
access_key = winreg.OpenKey(access_reg, 'SYSTEM\CurrentControlSet\Services\HidGuardian\Parameters', 0, winreg.KEY_READ) access_key = winreg.OpenKey(access_reg, r'SYSTEM\CurrentControlSet\Services\HidGuardian\Parameters', 0, winreg.KEY_READ)
affected_devices = winreg.QueryValueEx(access_key, 'AffectedDevices')[0] affected_devices = winreg.QueryValueEx(access_key, 'AffectedDevices')[0]
if "054C" in affected_devices and "0CE6" in affected_devices: if "054C" in affected_devices and "0CE6" in affected_devices:
return True return True

View File

@@ -1,6 +1,5 @@
import os
# needed for python > 3.8 import sys
import os, 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):
@@ -9,6 +8,10 @@ if platform.startswith('Windows') and sys.version_info >= (3,8):
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
from event_system import Event
from copy import deepcopy
class pydualsense: class pydualsense:
def __init__(self, verbose: bool = False) -> None:# def __init__(self, verbose: bool = False) -> None:#
@@ -18,6 +21,54 @@ class pydualsense:
self.leftMotor = 0 self.leftMotor = 0
self.rightMotor = 0 self.rightMotor = 0
self.last_states = None
self.register_available_events()
def register_available_events(self):
# button events
self.triangle_pressed = Event()
self.circle_pressed = Event()
self.cross_pressed = Event()
self.square_pressed = Event()
# dpad events
# TODO: add a event that sends the pressed key if any key is pressed
# self.dpad_changed = Event()
self.dpad_up = Event()
self.dpad_down = Event()
self.dpad_left = Event()
self.dpad_right = Event()
# joystick
self.left_joystick_changed = Event()
self.right_joystick_changed = Event()
# trigger back buttons
self.r1_changed = Event()
self.r2_changed = Event()
self.r3_changed = Event()
self.l1_changed = Event()
self.l2_changed = Event()
self.l3_changed = Event()
# misc
self.ps_pressed = Event()
self.touch_pressed = Event()
self.microphone_pressed = Event()
self.share_pressed = Event()
self.option_pressed = Event()
# trackpad touch
# handles 1 or 2 fingers
#self.trackpad_frame_reported = Event()
# gyrometer events
self.gyro_changed = Event()
self.accelerometer_changed = Event()
def init(self): def init(self):
"""initialize module and device states """initialize module and device states
@@ -27,7 +78,6 @@ class pydualsense:
self.audio = DSAudio() # ds audio setting self.audio = DSAudio() # ds audio setting
self.triggerL = DSTrigger() # left trigger self.triggerL = DSTrigger() # left trigger
self.triggerR = DSTrigger() # right trigger self.triggerR = DSTrigger() # right trigger
self.state = DSState() # controller states self.state = DSState() # controller states
if platform.startswith('Windows'): if platform.startswith('Windows'):
@@ -37,8 +87,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
self.ds_thread = True self.ds_thread = True
self.report_thread = threading.Thread(target=self.sendReport) self.report_thread = threading.Thread(target=self.sendReport)
self.report_thread.start() self.report_thread.start()
@@ -54,7 +102,6 @@ class pydualsense:
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 +110,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 +133,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 +157,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 +175,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 +186,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 +216,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 +235,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 +247,102 @@ 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}') # accelerometer
# print(f'X1: {self.state.trackPadTouch0.X} Y2: {self.state.trackPadTouch0.Y}') self.state.accelerometer.X = int.from_bytes(([inReport[16], inReport[17]]), byteorder='little', signed=True)
self.state.accelerometer.Y = int.from_bytes(([inReport[18], inReport[19]]), byteorder='little', signed=True)
self.state.accelerometer.Z = int.from_bytes(([inReport[20], inReport[21]]), byteorder='little', signed=True)
# print(f'2Active = {self.state.trackPadTouch1.isActive}') # gyrometer
# print(f'X2: {self.state.trackPadTouch1.X} Y2: {self.state.trackPadTouch1.Y}') self.state.gyro.Pitch = int.from_bytes(([inReport[22], inReport[23]]), byteorder='little', signed=True)
# print(f'DPAD {self.state.DpadLeft} {self.state.DpadUp} {self.state.DpadRight} {self.state.DpadDown}') self.state.gyro.Yaw = int.from_bytes(([inReport[24], inReport[25]]), byteorder='little', signed=True)
self.state.gyro.Roll = int.from_bytes(([inReport[26], inReport[27]]), byteorder='little', signed=True)
# first call we dont have a "last state" so we create if with the first occurence
if self.last_states is None:
self.last_states = deepcopy(self.state)
return
if self.state.circle != self.last_states.circle:
self.circle_pressed(self.state.circle)
if self.state.cross != self.last_states.cross:
self.cross_pressed(self.state.cross)
if self.state.triangle != self.last_states.triangle:
self.triangle_pressed(self.state.triangle)
if self.state.square != self.last_states.square:
self.square_pressed(self.state.square)
if self.state.DpadDown != self.last_states.DpadDown:
self.dpad_down(self.state.DpadDown)
if self.state.DpadLeft != self.last_states.DpadLeft:
self.dpad_left(self.state.DpadLeft)
if self.state.DpadRight != self.last_states.DpadRight:
self.dpad_right(self.state.DpadRight)
if self.state.DpadUp != self.last_states.DpadUp:
self.dpad_up(self.state.DpadUp)
if self.state.LX != self.last_states.LX or self.state.LY != self.last_states.LY:
self.left_joystick_changed(self.state.LX, self.state.LY)
if self.state.RX != self.last_states.RX or self.state.RY != self.last_states.RY:
self.right_joystick_changed(self.state.RX, self.state.RY)
if self.state.R1 != self.last_states.R1:
self.r1_changed(self.state.R1)
if self.state.R2 != self.last_states.R2:
self.r2_changed(self.state.R2)
if self.state.L1 != self.last_states.L1:
self.l1_changed(self.state.L1)
if self.state.L2 != self.last_states.L2:
self.l1_changed(self.state.L2)
if self.state.R3 != self.last_states.R3:
self.r3_changed(self.state.R3)
if self.state.L3 != self.last_states.L3:
self.l3_changed(self.state.L3)
if self.state.ps != self.last_states.ps:
self.ps_pressed(self.state.ps)
if self.state.touchBtn != self.last_states.touchBtn:
self.touch_pressed(self.state.touchBtn)
if self.state.micBtn != self.last_states.micBtn:
self.microphone_pressed(self.state.micBtn)
if self.state.share != self.last_states.share:
self.share_pressed(self.state.share)
if self.state.options != self.last_states.options:
self.option_pressed(self.state.options)
if self.state.accelerometer.X != self.last_states.accelerometer.X or \
self.state.accelerometer.Y != self.last_states.accelerometer.Y or \
self.state.accelerometer.Z != self.last_states.accelerometer.Z:
self.accelerometer_changed(self.state.accelerometer.X, self.state.accelerometer.Y, self.state.accelerometer.Z)
if self.state.gyro.Pitch != self.last_states.gyro.Pitch or \
self.state.gyro.Yaw != self.last_states.gyro.Yaw or \
self.state.gyro.Roll != self.last_states.gyro.Roll:
self.gyro_changed(self.state.gyro.Pitch, self.state.gyro.Yaw, self.state.gyro.Roll)
# copy current state into ltemp object to check next cycle if a change occuret
# and event trigger is needed
self.last_states = deepcopy(self.state) # copy current state into object to check next time
# 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 +352,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 +364,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 +393,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 +425,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 +436,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,8 +446,11 @@ 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.micBtn = 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()
self.gyro = DSGyro()
self.accelerometer = DSAccelerometer()
def setDPadState(self, dpad_state): def setDPadState(self, dpad_state):
if dpad_state == 0: if dpad_state == 0:
@@ -378,7 +506,7 @@ 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.PLAYER_1
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)
@@ -460,7 +588,6 @@ class DSLight:
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:
""" """
Sets the Color around the Touchpad as a tuple Sets the Color around the Touchpad as a tuple
@@ -553,3 +680,23 @@ class DSTrigger:
raise TypeError('Trigger mode parameter needs to be of type `TriggerModes`') raise TypeError('Trigger mode parameter needs to be of type `TriggerModes`')
self.mode = mode self.mode = mode
class DSGyro:
def __init__(self) -> None:
"""
Class represents the Gyro of the controller
"""
self.Pitch = 0
self.Yaw = 0
self.Roll = 0
class DSAccelerometer:
def __init__(self) -> None:
"""
Class represents the Accelerometer of the controller
"""
self.X = 0
self.Y = 0
self.Z = 0

View File

@@ -6,7 +6,7 @@ with open("README.md", "r") as fh:
setup( setup(
name='pydualsense', name='pydualsense',
version='0.5.5', version='0.6',
description='use your DualSense (PS5) controller with python', description='use your DualSense (PS5) controller with python',
long_description=long_description, long_description=long_description,
long_description_content_type="text/markdown", long_description_content_type="text/markdown",