Skip to content

Commit

Permalink
Merge pull request #32 from flok/event_system
Browse files Browse the repository at this point in the history
Event System and Gyro / Accelerometer support
  • Loading branch information
flok authored Aug 14, 2022
2 parents 0f279f1 + 40f7447 commit 9d8ab95
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 84 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ pip install pydualsense

from pydualsense import pydualsense, TriggerModes

def cross_pressed(state):
print(state)

ds = pydualsense() # open controller
ds.init() # initialize controller

ds.cross_pressed += cross_pressed
ds.light.setColorI(255,0,0) # set touchpad color to red
ds.triggerL.setMode(TriggerModes.Rigid)
ds.triggerL.setForce(1, 255)
Expand Down
32 changes: 29 additions & 3 deletions examples/read_controller.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,41 @@
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
dualsense = pydualsense()
# find device and initialize
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
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()


4 changes: 2 additions & 2 deletions pydualsense/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .enums import LedOptions,Brightness,PlayerID,PulseOptions,TriggerModes
from .pydualsense import pydualsense, DSLight, DSState, DSTouchpad, DSTrigger, DSAudio
from .enums import LedOptions, Brightness, PlayerID, PulseOptions, TriggerModes
from .pydualsense import pydualsense, DSLight, DSState, DSTouchpad, DSTrigger, DSAudio
57 changes: 31 additions & 26 deletions pydualsense/enums.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,46 @@
from enum import IntFlag


class ConnectionType(IntFlag):
BT = 0x0,
BT = 0x0
USB = 0x1


class LedOptions(IntFlag):
Off=0x0,
PlayerLedBrightness=0x1,
UninterrumpableLed=0x2,
Both=0x01 | 0x02
Off = 0x0
PlayerLedBrightness = 0x1
UninterrumpableLed = 0x2
Both = 0x01 | 0x02


class PulseOptions(IntFlag):
Off=0x0,
FadeBlue=0x1,
FadeOut=0x2
Off = 0x0
FadeBlue = 0x1
FadeOut = 0x2


class Brightness(IntFlag):
high = 0x0,
medium = 0x1,
high = 0x0
medium = 0x1
low = 0x2


class PlayerID(IntFlag):
player1 = 4,
player2 = 10,
player3 = 21,
player4 = 27,
all = 31
PLAYER_1 = 4
PLAYER_2 = 10
PLAYER_3 = 21
PLAYER_4 = 27
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,
Pulse_A = 0x2 | 0x20,
Pulse_B = 0x2 | 0x04,
Pulse_AB = 0x2 | 0x20 | 0x04,
Calibration= 0xFC

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
Calibration = 0xFC
60 changes: 60 additions & 0 deletions pydualsense/event_system.py
Original file line number Diff line number Diff line change
@@ -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)
28 changes: 15 additions & 13 deletions pydualsense/hidguardian.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import winreg
import sys


def check_hide() -> bool:
"""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:
pass
"""
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, r'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:
pass

return False
return False
Loading

0 comments on commit 9d8ab95

Please sign in to comment.