Compare commits
53 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
7c79dc5fbf | ||
|
98271e866f | ||
|
caa2062cea | ||
|
9359e6eac4 | ||
|
c54f58bcee | ||
|
d19bfc6507 | ||
|
2a448890ff | ||
|
5404852ec4 | ||
|
12b5743895 | ||
|
ab3f786013 | ||
|
e8cb5de594 | ||
|
79bf833c9a | ||
|
e1907e7a6f | ||
|
60aa11b496 | ||
|
330d117340 | ||
|
9d8ab950de | ||
|
40f74472d7 | ||
|
b091660130 | ||
|
82d407bfe2 | ||
|
0f279f1ee8 | ||
|
624d17c919 | ||
|
24c628a182 | ||
|
36e8886754 | ||
|
28605e0023 | ||
|
f5529f1463 | ||
|
b3ff9fd375 | ||
|
98b13798cd | ||
|
b51c8b49f6 | ||
|
cdbe03ad56 | ||
|
3a14ab3e7a | ||
|
32f9042abb | ||
|
4c86d71633 | ||
|
9c79d961f1 | ||
|
c1c10e4eac | ||
|
04ce807bc0 | ||
|
71a49da5d2 | ||
|
f4e1d73dd3 | ||
|
e766dca70f | ||
|
1e0b23da41 | ||
|
ff01788c89 | ||
|
c07b975bc5 | ||
|
7b0270fa7d | ||
|
d76717c163 | ||
|
786657cc90 | ||
|
11e78fbece | ||
|
a3f697866b | ||
|
1530c79dd7 | ||
|
83a37750d1 | ||
|
bc0eb35c3c | ||
|
93b5e38e6e | ||
|
94cb09dbdd | ||
|
8fb31f86ba | ||
|
e04766d48d |
22
.github/workflows/docs_publish.yml
vendored
Normal file
22
.github/workflows/docs_publish.yml
vendored
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
name: publish_docs
|
||||||
|
on: [push, pull_request, workflow_dispatch]
|
||||||
|
jobs:
|
||||||
|
docs:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- uses: actions/setup-python@v2
|
||||||
|
- name: Install dependencies
|
||||||
|
run: |
|
||||||
|
pip install sphinx furo
|
||||||
|
- name: Sphinx build
|
||||||
|
run: |
|
||||||
|
sphinx-build docs/source docs/build
|
||||||
|
- name: Deploy
|
||||||
|
uses: peaceiris/actions-gh-pages@v3
|
||||||
|
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
|
||||||
|
with:
|
||||||
|
publish_branch: gh-pages
|
||||||
|
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
publish_dir: docs/build/
|
||||||
|
force_orphan: true
|
3
.gitignore
vendored
3
.gitignore
vendored
@@ -149,6 +149,9 @@ dmypy.json
|
|||||||
!.vscode/extensions.json
|
!.vscode/extensions.json
|
||||||
*.code-workspace
|
*.code-workspace
|
||||||
|
|
||||||
|
### pycharm ###
|
||||||
|
.idea/*
|
||||||
|
|
||||||
# End of https://www.toptal.com/developers/gitignore/api/python,vscode
|
# End of https://www.toptal.com/developers/gitignore/api/python,vscode
|
||||||
|
|
||||||
pydualsense/interface.py
|
pydualsense/interface.py
|
||||||
|
55
README.md
55
README.md
@@ -1,29 +1,56 @@
|
|||||||
# pydualsense
|
# 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 receive and update the controller.
|
control your dualsense through python. using the hid library this package implements the report features for controlling your PS5 controller.
|
||||||
|
|
||||||
# install
|
# Documentation
|
||||||
|
|
||||||
Just install the package from [pypi](https://pypi.org/project/pydualsense/)
|
You can find the documentation at [docs](https://flok.github.io/pydualsense/)
|
||||||
|
|
||||||
|
# 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/).
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pip install pydualsense
|
pip install --upgrade 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 --upgrade pydualsense
|
||||||
|
```
|
||||||
|
|
||||||
# usage
|
# usage
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
|
||||||
from pydualsense import pydualsense
|
from pydualsense import pydualsense, TriggerModes
|
||||||
|
|
||||||
|
def cross_pressed(state):
|
||||||
|
print(state)
|
||||||
|
|
||||||
ds = pydualsense() # open controller
|
ds = pydualsense() # open controller
|
||||||
ds.setColor(255,0,0) # set touchpad color to red
|
ds.init() # initialize controller
|
||||||
ds.setLeftTriggerMode(TriggerModes.Rigid)
|
|
||||||
ds.setLeftTriggerForce(1, 255)
|
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)
|
||||||
ds.close() # closing the controller
|
ds.close() # closing the controller
|
||||||
```
|
```
|
||||||
|
|
||||||
See ``examples`` folder for some more ideas
|
See [examples](https://github.com/flok/pydualsense/tree/master/examples) or [examples docs](https://flok.github.io/pydualsense/examples.html) folder for some more ideas
|
||||||
|
|
||||||
# Help wanted
|
# Help wanted
|
||||||
|
|
||||||
@@ -31,7 +58,7 @@ Help wanted from people that want to use this and have feature requests. Just op
|
|||||||
|
|
||||||
# dependecies
|
# dependecies
|
||||||
|
|
||||||
- hid >= 1.0.4
|
- hidapi-usb >= 0.3
|
||||||
|
|
||||||
# Credits
|
# Credits
|
||||||
|
|
||||||
@@ -44,5 +71,7 @@ Most stuff for this implementation were provided by and used from:
|
|||||||
|
|
||||||
# Coming soon
|
# Coming soon
|
||||||
|
|
||||||
- reading the states of the controller to enable a fully compatibility with python - partially done
|
- add bluetooth support
|
||||||
|
- add multiple controllers
|
||||||
|
- partially done
|
||||||
- add documentation using sphinx
|
- add documentation using sphinx
|
||||||
|
0
docs/.nojekyll
Normal file
0
docs/.nojekyll
Normal file
23
docs/Makefile
Normal file
23
docs/Makefile
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
# Minimal makefile for Sphinx documentation
|
||||||
|
#
|
||||||
|
|
||||||
|
# You can set these variables from the command line, and also
|
||||||
|
# from the environment for the first two.
|
||||||
|
SPHINXOPTS ?=
|
||||||
|
SPHINXBUILD ?= sphinx-build
|
||||||
|
SOURCEDIR = source
|
||||||
|
BUILDDIR = build
|
||||||
|
|
||||||
|
# Put it first so that "make" without argument is like "make help".
|
||||||
|
help:
|
||||||
|
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
||||||
|
|
||||||
|
.PHONY: help Makefile
|
||||||
|
|
||||||
|
# Catch-all target: route all unknown targets to Sphinx using the new
|
||||||
|
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
|
||||||
|
%: Makefile
|
||||||
|
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
||||||
|
|
||||||
|
generate_doc:
|
||||||
|
sphinx-apidoc ../pydualsense -f -o ./source --ext-autodoc --ext-coverage --ext-todo
|
10
docs/source/api.rst
Normal file
10
docs/source/api.rst
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
API
|
||||||
|
===
|
||||||
|
|
||||||
|
This is the front page for the API documentation of the **pydualsense** library.
|
||||||
|
|
||||||
|
|
||||||
|
.. toctree::
|
||||||
|
ds_enum
|
||||||
|
ds_main
|
||||||
|
ds_eventsystem
|
43
docs/source/conf.py
Normal file
43
docs/source/conf.py
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import sys
|
||||||
|
import os
|
||||||
|
# Configuration file for the Sphinx documentation builder.
|
||||||
|
#
|
||||||
|
# For the full list of built-in configuration values, see the documentation:
|
||||||
|
# https://www.sphinx-doc.org/en/master/usage/configuration.html
|
||||||
|
|
||||||
|
# -- Project information -----------------------------------------------------
|
||||||
|
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
|
||||||
|
|
||||||
|
project = 'pydualsense'
|
||||||
|
copyright = '2022, Florian (flok) K'
|
||||||
|
author = 'Florian (flok) K'
|
||||||
|
release = '0.6.1'
|
||||||
|
|
||||||
|
sys.path.insert(0, os.path.abspath('..'))
|
||||||
|
sys.path.insert(0, os.path.abspath('../../'))
|
||||||
|
|
||||||
|
# -- General configuration ---------------------------------------------------
|
||||||
|
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
|
||||||
|
|
||||||
|
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.napoleon', 'sphinx.ext.coverage', 'sphinx.ext.todo']
|
||||||
|
|
||||||
|
templates_path = ['templates']
|
||||||
|
exclude_patterns = []
|
||||||
|
|
||||||
|
# -- Options for HTML output -------------------------------------------------
|
||||||
|
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
|
||||||
|
|
||||||
|
html_theme = 'furo'
|
||||||
|
html_static_path = ['static']
|
||||||
|
|
||||||
|
autodoc_default_options = {
|
||||||
|
'members': True,
|
||||||
|
'member-order': 'bysource',
|
||||||
|
'special-members': '__init__',
|
||||||
|
'undoc-members': True,
|
||||||
|
'exclude-members': '__weakref__'
|
||||||
|
}
|
||||||
|
|
||||||
|
autoclass_content = 'both'
|
||||||
|
todo_include_todos = True
|
||||||
|
|
11
docs/source/ds_enum.rst
Normal file
11
docs/source/ds_enum.rst
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
pydualsense enums classes
|
||||||
|
=========================
|
||||||
|
|
||||||
|
The enum module provides the used `enums` by **pydualsense**. These `enums` are used to update the state of the controller as a parameter to call the used functions.
|
||||||
|
|
||||||
|
|
||||||
|
.. automodule:: pydualsense.enums
|
||||||
|
:noindex:
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
:show-inheritance:
|
10
docs/source/ds_eventsystem.rst
Normal file
10
docs/source/ds_eventsystem.rst
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
pydualsense event system classes
|
||||||
|
=========================
|
||||||
|
|
||||||
|
The `Event System` implements the event system used for the button callbacks
|
||||||
|
|
||||||
|
.. automodule:: pydualsense.event_system
|
||||||
|
:noindex:
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
:show-inheritance:
|
11
docs/source/ds_main.rst
Normal file
11
docs/source/ds_main.rst
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
pydualsense main class
|
||||||
|
======================
|
||||||
|
|
||||||
|
`pydualsense` is the main class of the library with the same name. It provides access to the states of the controller through manual reading of the :class:`DSState <pydualsense.pydualsense.DSState>`
|
||||||
|
|
||||||
|
|
||||||
|
.. automodule:: pydualsense.pydualsense
|
||||||
|
:noindex:
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
:show-inheritance:
|
82
docs/source/examples.rst
Normal file
82
docs/source/examples.rst
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
Examples
|
||||||
|
========
|
||||||
|
|
||||||
|
This pages displays some examples that on how the library can be used. All the examples can also be found inside the `examples` folder on the github repository.
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
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 down {state}')
|
||||||
|
|
||||||
|
|
||||||
|
def joystick(stateX, stateY):
|
||||||
|
print(f'joystick {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:
|
||||||
|
...
|
||||||
|
|
||||||
|
# close device
|
||||||
|
dualsense.close()
|
||||||
|
|
||||||
|
|
||||||
|
The above example demonstrates the newly added c# like event system that makes it possible to trigger an event for the inputs of the controller.
|
||||||
|
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
from pydualsense import *
|
||||||
|
|
||||||
|
# get dualsense instance
|
||||||
|
dualsense = pydualsense()
|
||||||
|
# initialize controller and connect
|
||||||
|
dualsense.init()
|
||||||
|
|
||||||
|
print('Trigger Effect demo started')
|
||||||
|
|
||||||
|
# set left and right rumble motors
|
||||||
|
dualsense.setLeftMotor(255)
|
||||||
|
dualsense.setRightMotor(100)
|
||||||
|
|
||||||
|
# set left l2 trigger to Rigid and set index 1 to force 255
|
||||||
|
dualsense.triggerL.setMode(TriggerModes.Rigid)
|
||||||
|
dualsense.triggerL.setForce(1, 255)
|
||||||
|
|
||||||
|
# set left r2 trigger to Rigid
|
||||||
|
dualsense.triggerR.setMode(TriggerModes.Pulse_A)
|
||||||
|
dualsense.triggerR.setForce(0, 200)
|
||||||
|
dualsense.triggerR.setForce(1, 255)
|
||||||
|
dualsense.triggerR.setForce(2, 175)
|
||||||
|
|
||||||
|
# loop until r1 is pressed to feel effect
|
||||||
|
while not dualsense.state.R1:
|
||||||
|
...
|
||||||
|
|
||||||
|
# terminate the thread for message and close the device
|
||||||
|
dualsense.close()
|
26
docs/source/index.rst
Normal file
26
docs/source/index.rst
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
Welcome to pydualsense's documentation!
|
||||||
|
=======================================
|
||||||
|
|
||||||
|
|
||||||
|
**pydualsense** is a Python library that helps you interact with your PlayStation 5 DualSense controller. It reads the current state of the controller and also allows to update the triggers and other options on the controller.
|
||||||
|
|
||||||
|
To get started check out the :doc:`usage` section for more information on how to install.
|
||||||
|
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
This project is under active development.
|
||||||
|
|
||||||
|
Contents
|
||||||
|
--------
|
||||||
|
|
||||||
|
.. toctree::
|
||||||
|
usage
|
||||||
|
api
|
||||||
|
examples
|
||||||
|
modules
|
||||||
|
|
||||||
|
|
||||||
|
TODOs
|
||||||
|
-----
|
||||||
|
|
||||||
|
.. todolist::
|
7
docs/source/modules.rst
Normal file
7
docs/source/modules.rst
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
pydualsense
|
||||||
|
===========
|
||||||
|
|
||||||
|
.. toctree::
|
||||||
|
:maxdepth: 4
|
||||||
|
|
||||||
|
pydualsense
|
45
docs/source/pydualsense.rst
Normal file
45
docs/source/pydualsense.rst
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
pydualsense package
|
||||||
|
===================
|
||||||
|
|
||||||
|
Submodules
|
||||||
|
----------
|
||||||
|
|
||||||
|
pydualsense.enums module
|
||||||
|
------------------------
|
||||||
|
|
||||||
|
.. automodule:: pydualsense.enums
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
||||||
|
pydualsense.event\_system module
|
||||||
|
--------------------------------
|
||||||
|
|
||||||
|
.. automodule:: pydualsense.event_system
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
||||||
|
pydualsense.hidguardian module
|
||||||
|
------------------------------
|
||||||
|
|
||||||
|
.. automodule:: pydualsense.hidguardian
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
||||||
|
pydualsense.pydualsense module
|
||||||
|
------------------------------
|
||||||
|
|
||||||
|
.. automodule:: pydualsense.pydualsense
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
||||||
|
Module contents
|
||||||
|
---------------
|
||||||
|
|
||||||
|
.. automodule:: pydualsense
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
:show-inheritance:
|
38
docs/source/usage.rst
Normal file
38
docs/source/usage.rst
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
Usage
|
||||||
|
=====
|
||||||
|
|
||||||
|
Installation
|
||||||
|
------------
|
||||||
|
|
||||||
|
To use **pydualsense**, first install it using pip:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
(.venv) $ pip install --upgrade pydualsense
|
||||||
|
|
||||||
|
This install the needed dependencies and the **pydualsense** library itself.
|
||||||
|
|
||||||
|
|
||||||
|
Windows
|
||||||
|
-------
|
||||||
|
|
||||||
|
If you are on Windows the hidapi need to downloaded from `here <https://github.com/libusb/hidapi/releases>`_.
|
||||||
|
The downloaded `.dll` file need to be placed in a path that is in your environments variable `path`.
|
||||||
|
|
||||||
|
|
||||||
|
Linux based
|
||||||
|
-----------
|
||||||
|
|
||||||
|
If you are on a linux based system (e.g debian) you need to first need to install the hidapi through your package manager.
|
||||||
|
|
||||||
|
On Ubuntu systems the package `libhidapi-dev` is required.
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
sudo apt install libhidapi-dev
|
||||||
|
|
||||||
|
|
||||||
|
Examples
|
||||||
|
--------
|
||||||
|
|
||||||
|
For code examles on using the library see :doc:`examples`
|
@@ -8,15 +8,16 @@ print('Trigger Effect demo started')
|
|||||||
|
|
||||||
dualsense.setLeftMotor(255)
|
dualsense.setLeftMotor(255)
|
||||||
dualsense.setRightMotor(100)
|
dualsense.setRightMotor(100)
|
||||||
dualsense.setLeftTriggerMode(TriggerModes.Rigid)
|
dualsense.triggerL.setMode(TriggerModes.Rigid)
|
||||||
dualsense.setLeftTriggerForce(1, 255)
|
dualsense.triggerL.setForce(1, 255)
|
||||||
|
|
||||||
dualsense.setRightTriggerMode(TriggerModes.Pulse_A)
|
dualsense.triggerR.setMode(TriggerModes.Pulse_A)
|
||||||
dualsense.setRightTriggerForce(0, 200)
|
dualsense.triggerR.setForce(0, 200)
|
||||||
dualsense.setRightTriggerForce(1, 255)
|
dualsense.triggerR.setForce(1, 255)
|
||||||
dualsense.setRightTriggerForce(2, 175)
|
dualsense.triggerR.setForce(2, 175)
|
||||||
|
|
||||||
import time; time.sleep(3)
|
|
||||||
|
|
||||||
|
# loop until r1 is pressed to feel effect
|
||||||
|
while not dualsense.state.R1:
|
||||||
|
...
|
||||||
# terminate the thread for message and close the device
|
# terminate the thread for message and close the device
|
||||||
dualsense.close()
|
dualsense.close()
|
@@ -4,11 +4,11 @@ from pydualsense import *
|
|||||||
dualsense = pydualsense()
|
dualsense = pydualsense()
|
||||||
dualsense.init()
|
dualsense.init()
|
||||||
# set color around touchpad to red
|
# set color around touchpad to red
|
||||||
dualsense.setColor(0,0,255)
|
dualsense.light.setColorI(255,0,0)
|
||||||
# enable microphone indicator
|
# mute microphone
|
||||||
dualsense.setMicrophoneLED(1)
|
dualsense.audio.setMicrophoneState(True)
|
||||||
# set all player indicators on
|
# set all player 1 indicator on
|
||||||
dualsense.setPlayerID(PlayerID.all)
|
dualsense.light.setPlayerID(PlayerID.player1)
|
||||||
# sleep a little to see the result on the controller
|
# sleep a little to see the result on the controller
|
||||||
# this is not needed in normal usage
|
# this is not needed in normal usage
|
||||||
import time; time.sleep(2)
|
import time; time.sleep(2)
|
||||||
|
@@ -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()
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,2 +1,3 @@
|
|||||||
from .enums import LedOptions,Brightness,PlayerID,PulseOptions,TriggerModes
|
from .enums import LedOptions, Brightness, PlayerID, PulseOptions, TriggerModes
|
||||||
from .pydualsense import pydualsense, DSAudio, DSLight, DSTrigger
|
from .event_system import Event
|
||||||
|
from .pydualsense import pydualsense, DSLight, DSState, DSTouchpad, DSTrigger, DSAudio
|
||||||
|
@@ -1,38 +1,46 @@
|
|||||||
from enum import IntFlag
|
from enum import IntFlag
|
||||||
|
|
||||||
|
|
||||||
|
class ConnectionType(IntFlag):
|
||||||
|
BT = 0x0
|
||||||
|
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 = 1,
|
PLAYER_1 = 4
|
||||||
player2 = 2,
|
PLAYER_2 = 10
|
||||||
player3 = 4,
|
PLAYER_3 = 21
|
||||||
player4 = 8,
|
PLAYER_4 = 27
|
||||||
player5 = 16,
|
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
|
||||||
|
|
||||||
|
60
pydualsense/event_system.py
Normal file
60
pydualsense/event_system.py
Normal 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)
|
20
pydualsense/hidguardian.py
Normal file
20
pydualsense/hidguardian.py
Normal file
@@ -0,0 +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, 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
|
@@ -1,219 +1,232 @@
|
|||||||
from os import device_encoding
|
import logging
|
||||||
import hid
|
import os
|
||||||
from .enums import (LedOptions, PlayerID,
|
|
||||||
PulseOptions, TriggerModes, Brightness)
|
|
||||||
import threading
|
|
||||||
import sys
|
import sys
|
||||||
import winreg
|
from sys import platform
|
||||||
|
|
||||||
|
if platform.startswith('Windows') and sys.version_info >= (3, 8):
|
||||||
|
os.add_dll_directory(os.getcwd())
|
||||||
|
|
||||||
|
import hidapi
|
||||||
|
from .enums import (LedOptions, PlayerID, PulseOptions, TriggerModes, Brightness, ConnectionType) # type: ignore
|
||||||
|
import threading
|
||||||
|
from .event_system import Event
|
||||||
|
from copy import deepcopy
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger()
|
||||||
|
FORMAT = '%(asctime)s %(message)s'
|
||||||
|
logging.basicConfig(format=FORMAT)
|
||||||
|
logger.setLevel(logging.INFO)
|
||||||
|
|
||||||
class pydualsense:
|
class pydualsense:
|
||||||
|
|
||||||
def __init__(self, verbose: bool = False) -> None:
|
def __init__(self, verbose: bool = False) -> None:
|
||||||
|
"""
|
||||||
|
initialise the library but dont connect to the controller. call :func:`init() <pydualsense.pydualsense.init>` to connect to the controller
|
||||||
|
|
||||||
|
Args:
|
||||||
|
verbose (bool, optional): display verbose out (debug prints of input and output). Defaults to False.
|
||||||
|
"""
|
||||||
# TODO: maybe add a init function to not automatically allocate controller when class is declared
|
# TODO: maybe add a init function to not automatically allocate controller when class is declared
|
||||||
self.verbose = verbose
|
self.verbose = verbose
|
||||||
self.receive_buffer_size = 64
|
|
||||||
self.send_report_size = 48
|
if self.verbose:
|
||||||
self.color = (0,0,255) # set color around touchpad to blue
|
logger.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
self.leftMotor = 0
|
self.leftMotor = 0
|
||||||
self.rightMotor = 0
|
self.rightMotor = 0
|
||||||
|
|
||||||
|
self.last_states = None
|
||||||
def init(self):
|
|
||||||
"""initialize module and device
|
self.register_available_events()
|
||||||
|
|
||||||
|
def register_available_events(self) -> None:
|
||||||
"""
|
"""
|
||||||
self.device: hid.Device = self.__find_device()
|
register all available events that can be used for the controller
|
||||||
|
"""
|
||||||
|
|
||||||
|
# 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) -> None:
|
||||||
|
"""
|
||||||
|
initialize module and device states. Starts the sendReport background thread at the end
|
||||||
|
"""
|
||||||
|
self.device: hidapi.Device = self.__find_device()
|
||||||
self.light = DSLight() # control led light of ds
|
self.light = DSLight() # control led light of ds
|
||||||
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'):
|
||||||
|
self.conType = self.determineConnectionType() # determine USB or BT connection
|
||||||
|
else:
|
||||||
|
# set for usb manually
|
||||||
|
self.input_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()
|
||||||
|
|
||||||
self.init = True
|
def determineConnectionType(self) -> ConnectionType:
|
||||||
|
"""
|
||||||
|
Determine the connection type of the controller. eg USB or BT.
|
||||||
|
|
||||||
|
Currently only USB is supported.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
ConnectionType: Detected connection type of the controller.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if self.device._device.input_report_length == 64:
|
||||||
|
self.input_report_length = 64
|
||||||
|
self.output_report_length = 64
|
||||||
|
return ConnectionType.USB
|
||||||
|
elif self.device._device.input_report_length == 78:
|
||||||
|
self.input_report_length = 78
|
||||||
|
self.output_report_length = 78
|
||||||
|
return ConnectionType.BT
|
||||||
|
|
||||||
|
def close(self) -> None:
|
||||||
|
"""
|
||||||
|
Stops the report thread and closes the HID device
|
||||||
|
"""
|
||||||
|
# TODO: reset trigger effect to default
|
||||||
|
|
||||||
def close(self):
|
|
||||||
self.ds_thread = False
|
self.ds_thread = False
|
||||||
self.report_thread.join()
|
self.report_thread.join()
|
||||||
self.device.close()
|
self.device.close()
|
||||||
|
|
||||||
def _check_hide(self):
|
def __find_device(self) -> hidapi.Device:
|
||||||
"""check if hidguardian is used and controller is hidden
|
|
||||||
"""
|
"""
|
||||||
if sys.platform.startswith('win32'):
|
find HID dualsense device and open it
|
||||||
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
|
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
Exception: HIDGuardian detected
|
||||||
|
Exception: No device detected
|
||||||
|
|
||||||
def __find_device(self):
|
Returns:
|
||||||
|
hid.Device: returns opened controller device
|
||||||
|
"""
|
||||||
# TODO: detect connection mode, bluetooth has a bigger write buffer
|
# TODO: detect connection mode, bluetooth has a bigger write buffer
|
||||||
# TODO: implement multiple controllers working
|
# TODO: implement multiple controllers working
|
||||||
if self._check_hide():
|
if sys.platform.startswith('win32'):
|
||||||
raise Exception('HIDGuardian detected. Delete the controller from HIDGuardian and restart PC to connect to controller')
|
import pydualsense.hidguardian as hidguardian
|
||||||
detected_device = None
|
if hidguardian.check_hide():
|
||||||
devices = hid.enumerate(vid=0x054c)
|
raise Exception('HIDGuardian detected. Delete the controller from HIDGuardian and restart PC to connect to controller')
|
||||||
|
detected_device: hidapi.Device = None
|
||||||
|
devices = hidapi.enumerate(vendor_id=0x054c)
|
||||||
for device in devices:
|
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
|
detected_device = device
|
||||||
|
|
||||||
|
if detected_device is None:
|
||||||
if detected_device == None:
|
|
||||||
raise Exception('No device detected')
|
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
|
return dual_sense
|
||||||
|
|
||||||
def setLeftMotor(self, intensity: int):
|
def setLeftMotor(self, intensity: int) -> None:
|
||||||
if intensity > 255:
|
"""
|
||||||
|
set left motor rumble
|
||||||
|
|
||||||
|
Args:
|
||||||
|
intensity (int): rumble intensity
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
TypeError: intensity false type
|
||||||
|
Exception: intensity out of bounds 0..255
|
||||||
|
"""
|
||||||
|
if not isinstance(intensity, int):
|
||||||
|
raise TypeError('left motor intensity needs to be an int')
|
||||||
|
|
||||||
|
if intensity > 255 or intensity < 0:
|
||||||
raise Exception('maximum intensity is 255')
|
raise Exception('maximum intensity is 255')
|
||||||
self.leftMotor = intensity
|
self.leftMotor = intensity
|
||||||
|
|
||||||
|
def setRightMotor(self, intensity: int) -> None:
|
||||||
|
"""
|
||||||
|
set right motor rumble
|
||||||
|
|
||||||
def setRightMotor(self, intensity: int):
|
Args:
|
||||||
if intensity > 255:
|
intensity (int): rumble intensity
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
TypeError: intensity false type
|
||||||
|
Exception: intensity out of bounds 0..255
|
||||||
|
"""
|
||||||
|
if not isinstance(intensity, int):
|
||||||
|
raise TypeError('right motor intensity needs to be an int')
|
||||||
|
|
||||||
|
if intensity > 255 or intensity < 0:
|
||||||
raise Exception('maximum intensity is 255')
|
raise Exception('maximum intensity is 255')
|
||||||
self.rightMotor = intensity
|
self.rightMotor = intensity
|
||||||
|
|
||||||
|
|
||||||
# right trigger
|
|
||||||
def setRightTriggerMode(self, mode: TriggerModes):
|
|
||||||
"""set the trigger mode for R2
|
|
||||||
|
|
||||||
:param mode: enum of Trigger mode
|
def sendReport(self) -> None:
|
||||||
:type mode: TriggerModes
|
|
||||||
"""
|
|
||||||
self.triggerR.mode = mode
|
|
||||||
|
|
||||||
|
|
||||||
def setRightTriggerForce(self, forceID: int, force: int):
|
|
||||||
"""set the right trigger force. trigger consist of 7 parameter
|
|
||||||
|
|
||||||
:param forceID: parameter id from 0 to 6
|
|
||||||
:type forceID: int
|
|
||||||
:param force: force from 0..ff (0..255) applied to the trigger
|
|
||||||
:type force: int
|
|
||||||
"""
|
|
||||||
if forceID > 6:
|
|
||||||
raise Exception('only 7 parameters available')
|
|
||||||
|
|
||||||
self.triggerR.setForce(id=forceID, force=force)
|
|
||||||
|
|
||||||
|
|
||||||
def setLeftTriggerMode(self, mode: TriggerModes):
|
|
||||||
"""set the trigger mode for L2
|
|
||||||
|
|
||||||
:param mode: enum of Trigger mode
|
|
||||||
:type mode: TriggerModes
|
|
||||||
"""
|
|
||||||
self.triggerL.mode = mode
|
|
||||||
|
|
||||||
|
|
||||||
def setLeftTriggerForce(self, forceID: int, force: int):
|
|
||||||
"""set the left trigger force. trigger consist of 7 parameter
|
|
||||||
|
|
||||||
:param forceID: parameter id from 0 to 6
|
|
||||||
:type forceID: int
|
|
||||||
:param force: force from 0..ff (0..255) applied to the trigger
|
|
||||||
:type force: int
|
|
||||||
"""
|
|
||||||
|
|
||||||
if forceID > 6:
|
|
||||||
raise Exception('only 7 parameters available')
|
|
||||||
self.triggerL.setForce(id=forceID, force=force)
|
|
||||||
|
|
||||||
|
|
||||||
# TODO: audio
|
|
||||||
# audio stuff
|
|
||||||
def setMicrophoneLED(self, 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
|
"""background thread handling the reading of the device and updating its states
|
||||||
"""
|
"""
|
||||||
while self.ds_thread:
|
while self.ds_thread:
|
||||||
|
|
||||||
# read data from the input report of the controller
|
# read data from the input report of the controller
|
||||||
inReport = self.device.read(self.receive_buffer_size)
|
inReport = self.device.read(self.input_report_length)
|
||||||
|
if self.verbose:
|
||||||
|
logger.debug(inReport)
|
||||||
# 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()
|
||||||
|
|
||||||
# write the report to the device
|
# write the report to the device
|
||||||
self.writeReport(outReport)
|
self.writeReport(outReport)
|
||||||
|
|
||||||
def readInput(self, inReport):
|
def readInput(self, inReport) -> None:
|
||||||
"""read the reported data from the controller
|
"""
|
||||||
|
read the input from the controller and assign the states
|
||||||
|
|
||||||
:param inReport: report of the controller
|
Args:
|
||||||
:type inReport: bytes
|
inReport (bytearray): read bytearray containing the state of the whole controller
|
||||||
"""
|
"""
|
||||||
states = list(inReport) # convert bytes to list
|
states = list(inReport) # convert bytes to list
|
||||||
# states 0 is always 1
|
# states 0 is always 1
|
||||||
@@ -232,7 +245,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)
|
||||||
@@ -250,7 +262,7 @@ class pydualsense:
|
|||||||
misc2 = states[10]
|
misc2 = states[10]
|
||||||
self.state.ps = (misc2 & (1 << 0)) != 0
|
self.state.ps = (misc2 & (1 << 0)) != 0
|
||||||
self.state.touchBtn = (misc2 & 0x02) != 0
|
self.state.touchBtn = (misc2 & 0x02) != 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
|
||||||
@@ -264,37 +276,124 @@ 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
|
||||||
|
|
||||||
|
# send all events if neede
|
||||||
|
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.l2_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 temp 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: control mouse with touchpad for fun as DS4Windows
|
# TODO: control mouse with touchpad for fun as DS4Windows
|
||||||
|
|
||||||
|
def writeReport(self, outReport) -> None:
|
||||||
|
"""
|
||||||
|
write the report to the device
|
||||||
|
|
||||||
def writeReport(self, outReport):
|
Args:
|
||||||
"""Write the given report to the device
|
outReport (list): report to be written to device
|
||||||
|
|
||||||
:param outReport: report with data for the controller
|
|
||||||
:type outReport: list
|
|
||||||
"""
|
"""
|
||||||
self.device.write(bytes(outReport))
|
self.device.write(bytes(outReport))
|
||||||
|
|
||||||
|
def prepareReport(self) -> None:
|
||||||
def prepareReport(self):
|
|
||||||
"""prepare the report for the controller with all the settings set since the previous update
|
|
||||||
|
|
||||||
:return: report for the controller with all infos
|
|
||||||
:rtype: list
|
|
||||||
"""
|
"""
|
||||||
outReport = [0] * 48 # create empty list with range of output report
|
prepare the output to be send to the controller
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
list: report to send to controller
|
||||||
|
"""
|
||||||
|
|
||||||
|
outReport = [0] * self.output_report_length # create empty list with range of output report
|
||||||
# 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)
|
||||||
@@ -316,14 +415,16 @@ class pydualsense:
|
|||||||
# 0x80 ???
|
# 0x80 ???
|
||||||
outReport[2] = 0x1 | 0x2 | 0x4 | 0x10 | 0x40 # [2]
|
outReport[2] = 0x1 | 0x2 | 0x4 | 0x10 | 0x40 # [2]
|
||||||
|
|
||||||
outReport[3] = self.leftMotor # left low freq motor 0-255 # [3]
|
outReport[3] = self.rightMotor # right low freq motor 0-255 # [3]
|
||||||
outReport[4] = self.rightMotor # right low freq motor 0-255 # [4]
|
outReport[4] = self.leftMotor # left low freq motor 0-255 # [4]
|
||||||
|
|
||||||
# outReport[5] - outReport[8] audio related
|
# outReport[5] - outReport[8] audio related
|
||||||
|
|
||||||
# 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 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
|
||||||
outReport[12] = self.triggerR.forces[0]
|
outReport[12] = self.triggerR.forces[0]
|
||||||
@@ -347,33 +448,54 @@ class pydualsense:
|
|||||||
outReport[42] = self.light.pulseOptions.value
|
outReport[42] = self.light.pulseOptions.value
|
||||||
outReport[43] = self.light.brightness.value
|
outReport[43] = self.light.brightness.value
|
||||||
outReport[44] = self.light.playerNumber.value
|
outReport[44] = self.light.playerNumber.value
|
||||||
outReport[45] = self.color[0]
|
outReport[45] = self.light.TouchpadColor[0]
|
||||||
outReport[46] = self.color[1]
|
outReport[46] = self.light.TouchpadColor[1]
|
||||||
outReport[47] = self.color[2]
|
outReport[47] = self.light.TouchpadColor[2]
|
||||||
|
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print(outReport)
|
logger.debug(outReport)
|
||||||
|
|
||||||
return outReport
|
return outReport
|
||||||
|
|
||||||
|
|
||||||
class DSTouchpad:
|
class DSTouchpad:
|
||||||
|
"""
|
||||||
|
Dualsense Touchpad class. Contains X and Y position of touch and if the touch isActive
|
||||||
|
"""
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
|
"""
|
||||||
|
Class represents the Touchpad of the controller
|
||||||
|
"""
|
||||||
self.isActive = False
|
self.isActive = False
|
||||||
self.ID = 0
|
self.ID = 0
|
||||||
self.X = 0
|
self.X = 0
|
||||||
self.Y = 0
|
self.Y = 0
|
||||||
|
|
||||||
|
|
||||||
class DSState:
|
class DSState:
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.packerC = 0
|
"""
|
||||||
|
All dualsense states (inputs) that can be read. Second method to check if a input is pressed.
|
||||||
|
"""
|
||||||
self.square, self.triangle, self.circle, self.cross = False, False, False, False
|
self.square, self.triangle, self.circle, self.cross = False, False, False, False
|
||||||
self.DpadUp, self.DpadDown, self.DpadLeft, self.DpadRight = False, False, False, False
|
self.DpadUp, self.DpadDown, self.DpadLeft, self.DpadRight = 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.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.micBtn = False
|
||||||
|
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: int):
|
||||||
|
"""
|
||||||
|
Sets the dpad state variables according to the integers that was read from the controller
|
||||||
|
|
||||||
|
Args:
|
||||||
|
dpad_state (int): integer number representing the dpad state
|
||||||
|
"""
|
||||||
if dpad_state == 0:
|
if dpad_state == 0:
|
||||||
self.DpadUp = True
|
self.DpadUp = True
|
||||||
self.DpadDown = False
|
self.DpadDown = False
|
||||||
@@ -422,64 +544,219 @@ class DSState:
|
|||||||
|
|
||||||
|
|
||||||
class DSLight:
|
class DSLight:
|
||||||
"""DualSense Light class
|
"""
|
||||||
|
Represents all features of lights on the controller
|
||||||
make it simple, no get or set functions. quick and dirty
|
|
||||||
"""
|
"""
|
||||||
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)
|
||||||
|
|
||||||
|
def setLEDOption(self, option: LedOptions):
|
||||||
|
"""
|
||||||
|
Sets the LED Option
|
||||||
|
|
||||||
|
Args:
|
||||||
|
option (LedOptions): Led option
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
TypeError: LedOption is false type
|
||||||
|
"""
|
||||||
|
if not isinstance(option, LedOptions):
|
||||||
|
raise TypeError('Need LEDOption type')
|
||||||
|
self.ledOption = option
|
||||||
|
|
||||||
|
def setPulseOption(self, option: PulseOptions):
|
||||||
|
"""
|
||||||
|
Sets the Pulse Option of the LEDs
|
||||||
|
|
||||||
|
Args:
|
||||||
|
option (PulseOptions): pulse option of the LEDs
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
TypeError: Pulse option is false type
|
||||||
|
"""
|
||||||
|
if not isinstance(option, PulseOptions):
|
||||||
|
raise TypeError('Need PulseOption type')
|
||||||
|
self.pulseOptions = option
|
||||||
|
|
||||||
def setBrightness(self, brightness: Brightness):
|
def setBrightness(self, brightness: Brightness):
|
||||||
self._brightness = brightness
|
"""
|
||||||
|
Defines the brightness of the Player LEDs
|
||||||
|
|
||||||
|
Args:
|
||||||
|
brightness (Brightness): brightness of LEDS
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
TypeError: brightness false type
|
||||||
|
"""
|
||||||
|
if not isinstance(brightness, Brightness):
|
||||||
|
raise TypeError('Need Brightness type')
|
||||||
|
self.brightness = brightness
|
||||||
|
|
||||||
|
def setPlayerID(self, player: PlayerID):
|
||||||
|
"""
|
||||||
|
Sets the PlayerID of the controller with the choosen LEDs.
|
||||||
|
The controller has 4 Player states
|
||||||
|
|
||||||
|
Args:
|
||||||
|
player (PlayerID): chosen PlayerID for the Controller
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
TypeError: [description]
|
||||||
|
"""
|
||||||
|
if not isinstance(player, PlayerID):
|
||||||
|
raise TypeError('Need PlayerID type')
|
||||||
|
self.playerNumber = player
|
||||||
|
|
||||||
|
def setColorI(self, r: int, g: int, b: int) -> None:
|
||||||
|
"""
|
||||||
|
Sets the Color around the Touchpad of the controller
|
||||||
|
|
||||||
|
Args:
|
||||||
|
r (int): red channel
|
||||||
|
g (int): green channel
|
||||||
|
b (int): blue channel
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
TypeError: color channels have wrong type
|
||||||
|
Exception: color channels are out of bounds
|
||||||
|
"""
|
||||||
|
if not isinstance(r, int) or not isinstance(g, int) or not isinstance(b, int):
|
||||||
|
raise TypeError('Color parameter need to be int')
|
||||||
|
# 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):
|
||||||
|
raise Exception('colors have values from 0 to 255 only')
|
||||||
|
self.TouchpadColor = (r, g, b)
|
||||||
|
|
||||||
|
def setColorT(self, color: tuple) -> None:
|
||||||
|
"""
|
||||||
|
Sets the Color around the Touchpad as a tuple
|
||||||
|
|
||||||
|
Args:
|
||||||
|
color (tuple): color as tuple
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
TypeError: color has wrong type
|
||||||
|
Exception: color channels are out of bounds
|
||||||
|
"""
|
||||||
|
if not isinstance(color, tuple):
|
||||||
|
raise TypeError('Color type is tuple')
|
||||||
|
# unpack for out of bounds check
|
||||||
|
r, g, b = map(int, color)
|
||||||
|
# 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):
|
||||||
|
raise Exception('colors have values from 0 to 255 only')
|
||||||
|
self.TouchpadColor = (r, g, b)
|
||||||
|
|
||||||
|
|
||||||
class DSAudio:
|
class DSAudio:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
|
"""
|
||||||
|
initialize the limited Audio features of the controller
|
||||||
|
"""
|
||||||
self.microphone_mute = 0
|
self.microphone_mute = 0
|
||||||
self.microphone_led = 0
|
self.microphone_led = 0
|
||||||
|
|
||||||
|
def setMicrophoneLED(self, value):
|
||||||
|
"""
|
||||||
|
Activates or disables the microphone led.
|
||||||
|
This doesnt change the mute/unmutes the microphone itself.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
value (bool): On or off microphone LED
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
Exception: false state for the led
|
||||||
|
"""
|
||||||
|
if not isinstance(value, bool):
|
||||||
|
raise TypeError('MicrophoneLED can only be a bool')
|
||||||
|
self.microphone_led = value
|
||||||
|
|
||||||
|
def setMicrophoneState(self, state: bool):
|
||||||
|
"""
|
||||||
|
Set the microphone state and also sets the microphone led accordingle
|
||||||
|
|
||||||
|
Args:
|
||||||
|
state (bool): desired state of the microphone
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
TypeError: state was not a bool
|
||||||
|
"""
|
||||||
|
|
||||||
|
if not isinstance(state, bool):
|
||||||
|
raise TypeError('state needs to be bool')
|
||||||
|
|
||||||
|
self.setMicrophoneLED(state) # set led accordingly
|
||||||
|
self.microphone_mute = state
|
||||||
|
|
||||||
|
|
||||||
class DSTrigger:
|
class DSTrigger:
|
||||||
|
"""
|
||||||
|
Dualsense trigger class. Allowes for multiple :class:`TriggerModes <pydualsense.enums.TriggerModes>` and multiple forces
|
||||||
|
|
||||||
|
# TODO: make this interface more userfriendly so a developer knows what he is doing
|
||||||
|
"""
|
||||||
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)]
|
||||||
|
|
||||||
def setForce(self, id:int = 0, force:int = 0):
|
def setForce(self, forceID: int = 0, force: int = 0):
|
||||||
"""set the force of the trigger
|
|
||||||
|
|
||||||
:param id: id of the trigger parameters. 6 possible, defaults to 0
|
|
||||||
:type id: int, optional
|
|
||||||
:param force: force 0 to 255, defaults to 0
|
|
||||||
:type force: int, optional
|
|
||||||
:raises Exception: false trigger parameter accessed. only available trigger parameters from 0 to 6
|
|
||||||
"""
|
"""
|
||||||
if id > 6 or id < 0:
|
Sets the forces of the choosen force parameter
|
||||||
raise Exception('only trigger parameters 0 to 6 available')
|
|
||||||
self.forces[id] = force
|
Args:
|
||||||
|
forceID (int, optional): force parameter. Defaults to 0.
|
||||||
|
force (int, optional): applied force to the parameter. Defaults to 0.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
TypeError: wrong type of forceID or force
|
||||||
|
Exception: choosen a false force parameter
|
||||||
|
"""
|
||||||
|
if not isinstance(forceID, int) or not isinstance(force, int):
|
||||||
|
raise TypeError('forceID and force needs to be type int')
|
||||||
|
|
||||||
|
if forceID > 6 or forceID < 0:
|
||||||
|
raise Exception('only 7 parameters available')
|
||||||
|
|
||||||
|
self.forces[forceID] = force
|
||||||
|
|
||||||
def setMode(self, mode: TriggerModes):
|
def setMode(self, mode: TriggerModes):
|
||||||
"""set mode on the trigger
|
|
||||||
|
|
||||||
:param mode: mode for trigger
|
|
||||||
:type mode: TriggerModes
|
|
||||||
"""
|
"""
|
||||||
|
Set the Mode for the Trigger
|
||||||
|
|
||||||
|
Args:
|
||||||
|
mode (TriggerModes): Trigger mode
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
TypeError: false Trigger mode type
|
||||||
|
"""
|
||||||
|
if not isinstance(mode, TriggerModes):
|
||||||
|
raise TypeError('Trigger mode parameter needs to be of type `TriggerModes`')
|
||||||
|
|
||||||
self.mode = mode
|
self.mode = mode
|
||||||
|
|
||||||
def getTriggerPacket(self):
|
|
||||||
"""returns array of the trigger modes and its parameters
|
|
||||||
|
|
||||||
:return: packet of the trigger settings
|
class DSGyro:
|
||||||
:rtype: list
|
"""
|
||||||
"""
|
Class representing the Gyro2 of the controller
|
||||||
# create packet
|
"""
|
||||||
packet = [self.mode.value]
|
def __init__(self) -> None:
|
||||||
packet += [self.forces[i] for i in range(6)]
|
self.Pitch = 0
|
||||||
packet += [0,0] # unknown what these do ?
|
self.Yaw = 0
|
||||||
packet.append(self.forces[-1]) # last force has a offset of 2 from the other forces. this is the frequency of the actuation
|
self.Roll = 0
|
||||||
return packet
|
|
||||||
|
|
||||||
|
class DSAccelerometer:
|
||||||
|
"""
|
||||||
|
Class representing the Accelerometer of the controller
|
||||||
|
"""
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self.X = 0
|
||||||
|
self.Y = 0
|
||||||
|
self.Z = 0
|
||||||
|
3
requirements.txt
Normal file
3
requirements.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
hid-usb
|
||||||
|
sphinx
|
||||||
|
furo
|
6
setup.py
6
setup.py
@@ -6,13 +6,13 @@ with open("README.md", "r") as fh:
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='pydualsense',
|
name='pydualsense',
|
||||||
version='0.3.0',
|
version='0.6.3',
|
||||||
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",
|
||||||
url='https://github.com/flok/pydualsense',
|
url='https://github.com/flok/pydualsense',
|
||||||
author='Florian K',
|
author='Florian (flok) K',
|
||||||
license='MIT License',
|
license='MIT License',
|
||||||
packages=setuptools.find_packages(),
|
packages=setuptools.find_packages(),
|
||||||
install_requires=['hid>=1.0.4']
|
install_requires=['hidapi-usb>=0.3', 'cffi']
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user