From bc0eb35c3caa9dd536875e6ff8a969da9877c740 Mon Sep 17 00:00:00 2001 From: Dan Date: Thu, 31 Dec 2020 20:39:50 -0500 Subject: [PATCH 01/11] Moving winreg check to support other OSes --- pydualsense/hidguardian.py | 18 ++++++++++++++++++ pydualsense/pydualsense.py | 25 +++++-------------------- 2 files changed, 23 insertions(+), 20 deletions(-) create mode 100644 pydualsense/hidguardian.py diff --git a/pydualsense/hidguardian.py b/pydualsense/hidguardian.py new file mode 100644 index 0000000..2449b39 --- /dev/null +++ b/pydualsense/hidguardian.py @@ -0,0 +1,18 @@ +import winreg +import sys + +def check_hide(self) -> 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: + print(e) + + return False \ No newline at end of file diff --git a/pydualsense/pydualsense.py b/pydualsense/pydualsense.py index b7e1206..f9a50f1 100644 --- a/pydualsense/pydualsense.py +++ b/pydualsense/pydualsense.py @@ -4,7 +4,6 @@ from .enums import (LedOptions, PlayerID, PulseOptions, TriggerModes, Brightness) import threading import sys -import winreg class pydualsense: def __init__(self, verbose: bool = False) -> None:# @@ -43,23 +42,7 @@ class pydualsense: self.ds_thread = False self.report_thread.join() self.device.close() - - def _check_hide(self) -> 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: - print(e) - - return False - + def __find_device(self) -> hid.Device: """ @@ -74,8 +57,10 @@ class pydualsense: """ # TODO: detect connection mode, bluetooth has a bigger write buffer # TODO: implement multiple controllers working - if self._check_hide(): - raise Exception('HIDGuardian detected. Delete the controller from HIDGuardian and restart PC to connect to controller') + if sys.platform.startswith('win32'): + import pydualsense.hidguardian as hidguardian + if hidguardian.check_hide(): + raise Exception('HIDGuardian detected. Delete the controller from HIDGuardian and restart PC to connect to controller') detected_device: hid.Device = None devices = hid.enumerate(vid=0x054c) for device in devices: From 83a37750d13e86ba4ec449f3049be91d08ae71bb Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 1 Jan 2021 00:28:46 -0500 Subject: [PATCH 02/11] linting --- pydualsense/hidguardian.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydualsense/hidguardian.py b/pydualsense/hidguardian.py index 2449b39..b8810e4 100644 --- a/pydualsense/hidguardian.py +++ b/pydualsense/hidguardian.py @@ -15,4 +15,4 @@ def check_hide(self) -> bool: except OSError as e: print(e) - return False \ No newline at end of file + return False From 7b0270fa7de857e85e03d699a717fb093f0652de Mon Sep 17 00:00:00 2001 From: Nougator <37145431+nougator@users.noreply.github.com> Date: Fri, 1 Jan 2021 20:56:57 +0100 Subject: [PATCH 03/11] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f5ab9c6..f1c8b67 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ pip install pydualsense ```python -from pydualsense import pydualsense +from pydualsense import pydualsense, TriggerModes ds = pydualsense() # open controller ds.init() # initialize controller From ff01788c895006807591c3c04b09016105af5982 Mon Sep 17 00:00:00 2001 From: Florian K <37000563+flok@users.noreply.github.com> Date: Fri, 1 Jan 2021 23:39:54 +0100 Subject: [PATCH 04/11] Mypy enums import error fix --- pydualsense/pydualsense.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pydualsense/pydualsense.py b/pydualsense/pydualsense.py index 5e1c693..8907719 100644 --- a/pydualsense/pydualsense.py +++ b/pydualsense/pydualsense.py @@ -5,8 +5,7 @@ if sys.version_info >= (3,8): os.add_dll_directory(os.getcwd()) import hid # type: ignore -from .enums import (LedOptions, PlayerID, - PulseOptions, TriggerModes, Brightness) +from .enums import (LedOptions, PlayerID, PulseOptions, TriggerModes, Brightness) # type: ignore import threading import winreg class pydualsense: @@ -538,4 +537,4 @@ class DSTrigger: if not isinstance(mode, TriggerModes): raise TypeError('Trigger mode parameter needs to be of type `TriggerModes`') - self.mode = mode \ No newline at end of file + self.mode = mode From 1e0b23da41ae9e779421694bdec8067fd56d2506 Mon Sep 17 00:00:00 2001 From: Florian K <37000563+flok@users.noreply.github.com> Date: Fri, 1 Jan 2021 23:41:15 +0100 Subject: [PATCH 05/11] Update python-mypy.yml --- .github/workflows/python-mypy.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/python-mypy.yml b/.github/workflows/python-mypy.yml index c5f0f7f..3af1328 100644 --- a/.github/workflows/python-mypy.yml +++ b/.github/workflows/python-mypy.yml @@ -11,11 +11,11 @@ jobs: - name: Set up Python uses: actions/setup-python@v2 with: - python-version: '3.x' + python-version: '3.9' - name: Install Dependencies run: | python -m pip install --upgrade pip - pip install mypy + python -m pip install mypy - name: mypy run: | - mypy pydualsense/ \ No newline at end of file + mypy pydualsense/ From e766dca70f54b818269c5052b3cb77f3cdeb9cee Mon Sep 17 00:00:00 2001 From: Florian K <37000563+flok@users.noreply.github.com> Date: Fri, 1 Jan 2021 23:42:29 +0100 Subject: [PATCH 06/11] Deleting mypy action Mypy gives weird results on github. Lets only work with it locally --- .github/workflows/python-mypy.yml | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 .github/workflows/python-mypy.yml diff --git a/.github/workflows/python-mypy.yml b/.github/workflows/python-mypy.yml deleted file mode 100644 index 3af1328..0000000 --- a/.github/workflows/python-mypy.yml +++ /dev/null @@ -1,21 +0,0 @@ -name: Mypy - -on: [push] - -jobs: - build: - runs-on: ubuntu-latest - name: Mypy - steps: - - uses: actions/checkout@v2 - - name: Set up Python - uses: actions/setup-python@v2 - with: - python-version: '3.9' - - name: Install Dependencies - run: | - python -m pip install --upgrade pip - python -m pip install mypy - - name: mypy - run: | - mypy pydualsense/ From 71a49da5d2ec8889e2e5873a1bb1b4af42b7290f Mon Sep 17 00:00:00 2001 From: Dan Date: Sat, 2 Jan 2021 23:29:59 -0500 Subject: [PATCH 07/11] Removing accidently merged imports --- .gitignore | 3 +++ pydualsense/pydualsense.py | 2 -- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index f379579..8175c97 100644 --- a/.gitignore +++ b/.gitignore @@ -149,6 +149,9 @@ dmypy.json !.vscode/extensions.json *.code-workspace +### pycharm ### +.idea/* + # End of https://www.toptal.com/developers/gitignore/api/python,vscode pydualsense/interface.py diff --git a/pydualsense/pydualsense.py b/pydualsense/pydualsense.py index 8629fe3..5f2d574 100644 --- a/pydualsense/pydualsense.py +++ b/pydualsense/pydualsense.py @@ -7,8 +7,6 @@ if sys.version_info >= (3,8): import hid # type: ignore from .enums import (LedOptions, PlayerID, PulseOptions, TriggerModes, Brightness) # type: ignore import threading -import sys -import winreg class pydualsense: def __init__(self, verbose: bool = False) -> None:# From 04ce807bc0295e147bb5f26b180960b8ae54cd82 Mon Sep 17 00:00:00 2001 From: Dan Date: Sun, 3 Jan 2021 22:16:31 -0500 Subject: [PATCH 08/11] Added platform check for add_dll_directory --- pydualsense/pydualsense.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydualsense/pydualsense.py b/pydualsense/pydualsense.py index 5f2d574..dcab7bf 100644 --- a/pydualsense/pydualsense.py +++ b/pydualsense/pydualsense.py @@ -1,7 +1,7 @@ # needed for python > 3.8 import os, sys -if sys.version_info >= (3,8): +if sys.platform.startswith('win32') and sys.version_info >= (3,8): os.add_dll_directory(os.getcwd()) import hid # type: ignore From c1c10e4eacf37818e31b09f83c0e5aba7001fbad Mon Sep 17 00:00:00 2001 From: Dan Date: Wed, 6 Jan 2021 18:25:15 -0500 Subject: [PATCH 09/11] Remove self reference --- pydualsense/hidguardian.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydualsense/hidguardian.py b/pydualsense/hidguardian.py index b8810e4..f7114b8 100644 --- a/pydualsense/hidguardian.py +++ b/pydualsense/hidguardian.py @@ -1,7 +1,7 @@ import winreg import sys -def check_hide(self) -> bool: +def check_hide() -> bool: """check if hidguardian is used and controller is hidden """ if sys.platform.startswith('win32'): From 4c86d716339422c8eb9706bef7b4f8eff54e0f86 Mon Sep 17 00:00:00 2001 From: Florian Kaiser <37000563+flok@users.noreply.github.com> Date: Sun, 10 Jan 2021 14:06:53 +0100 Subject: [PATCH 10/11] Refactor hidapi --- pydualsense/pydualsense.py | 20 +++++++++----------- setup.py | 4 ++-- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/pydualsense/pydualsense.py b/pydualsense/pydualsense.py index dcab7bf..36d6ee7 100644 --- a/pydualsense/pydualsense.py +++ b/pydualsense/pydualsense.py @@ -4,7 +4,7 @@ import os, sys if sys.platform.startswith('win32') and sys.version_info >= (3,8): os.add_dll_directory(os.getcwd()) -import hid # type: ignore +import hidapi from .enums import (LedOptions, PlayerID, PulseOptions, TriggerModes, Brightness) # type: ignore import threading class pydualsense: @@ -22,7 +22,7 @@ class pydualsense: def init(self): """initialize module and device states """ - self.device: hid.Device = self.__find_device() + self.device: hidapi.Device = self.__find_device() self.light = DSLight() # control led light of ds self.audio = DSAudio() # ds audio setting self.triggerL = DSTrigger() # left trigger @@ -36,8 +36,6 @@ class pydualsense: self.report_thread = threading.Thread(target=self.sendReport) self.report_thread.start() - self.init = True - def close(self): """ Stops the report thread and closes the HID device @@ -45,9 +43,9 @@ class pydualsense: self.ds_thread = False self.report_thread.join() self.device.close() - - def __find_device(self) -> hid.Device: + + def __find_device(self) -> hidapi.Device: """ find HID device and open it @@ -64,17 +62,17 @@ class pydualsense: import pydualsense.hidguardian as hidguardian if hidguardian.check_hide(): raise Exception('HIDGuardian detected. Delete the controller from HIDGuardian and restart PC to connect to controller') - detected_device: hid.Device = None - devices = hid.enumerate(vid=0x054c) + detected_device: hidapi.Device = None + devices = hidapi.enumerate(vendor_id=0x054c) for device in devices: - if device['vendor_id'] == 0x054c and device['product_id'] == 0x0CE6: + if device.vendor_id == 0x054c and device.product_id == 0x0CE6: detected_device = device if detected_device == None: raise Exception('No device detected') - dual_sense = hid.Device(vid=detected_device['vendor_id'], pid=detected_device['product_id']) + dual_sense = hidapi.Device(vendor_id=detected_device.vendor_id, product_id=detected_device.product_id) return dual_sense def setLeftMotor(self, intensity: int): @@ -122,7 +120,7 @@ class pydualsense: # read data from the input report of the controller inReport = self.device.read(self.receive_buffer_size) - + print(inReport) # decrypt the packet and bind the inputs self.readInput(inReport) diff --git a/setup.py b/setup.py index c4c8b28..ea21d50 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ with open("README.md", "r") as fh: setup( name='pydualsense', - version='0.4.2', + version='0.5.0', description='use your DualSense (PS5) controller with python', long_description=long_description, long_description_content_type="text/markdown", @@ -14,5 +14,5 @@ setup( author='Florian K', license='MIT License', packages=setuptools.find_packages(), - install_requires=['hid>=1.0.4'] + install_requires=['hidapi-usb'] ) From 3a14ab3e7a802ca1ca2e35de79364e8646a01d8c Mon Sep 17 00:00:00 2001 From: Flo <37000563+flok@users.noreply.github.com> Date: Sun, 10 Jan 2021 14:53:56 +0100 Subject: [PATCH 11/11] Update setup.py --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index ea21d50..7214f17 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ with open("README.md", "r") as fh: setup( name='pydualsense', - version='0.5.0', + version='0.5.1', description='use your DualSense (PS5) controller with python', long_description=long_description, long_description_content_type="text/markdown", @@ -14,5 +14,5 @@ setup( author='Florian K', license='MIT License', packages=setuptools.find_packages(), - install_requires=['hidapi-usb'] + install_requires=['hidapi-usb', 'cffi'] )