10 Commits
v0.6 ... v0.6.2

Author SHA1 Message Date
Flo
d19bfc6507 release of 0.6.2
Update to 0.6.2 for typo and new release
2022-08-17 15:57:28 +02:00
Flo
2a448890ff Fix small typo on l2 event
Add small typo on the l2 event #35 - thanks to @thiagonc2 for reporting
2022-08-17 15:56:24 +02:00
Flo
5404852ec4 Merge pull request #34 from flok/dev
Update readme with docs link
2022-08-15 13:54:44 +02:00
Florian Kaiser
12b5743895 Update readme with docs link 2022-08-15 13:53:13 +02:00
Florian Kaiser
ab3f786013 added more documentation 2022-08-14 22:51:18 +02:00
Florian Kaiser
e8cb5de594 Update doc-strings and examples 2022-08-14 22:51:02 +02:00
Florian Kaiser
79bf833c9a Update requirements and author name in setup.py 2022-08-14 22:06:34 +02:00
Florian Kaiser
e1907e7a6f Add github action to publish docs 2022-08-14 20:29:41 +02:00
Florian Kaiser
60aa11b496 Add Docs 2022-08-14 20:27:55 +02:00
Florian Kaiser
330d117340 Fix wrong import v0.6.1 2022-08-14 16:20:15 +02:00
19 changed files with 425 additions and 41 deletions

22
.github/workflows/docs_publish.yml vendored Normal file
View 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

View File

@@ -1,5 +1,9 @@
# 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 PS5 controller.
# Documentation
You can find the documentation at [docs](https://flok.github.io/pydualsense/)
# Installation
@@ -8,7 +12,7 @@ control your dualsense through python. using the hid library this package implem
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
pip install pydualsense
pip install --upgrade pydualsense
```
## Linux
@@ -24,7 +28,7 @@ sudo apt install libhidapi-dev
After that install the package from [pypi](https://pypi.org/project/pydualsense/).
```bash
pip install pydualsense
pip install --upgrade pydualsense
```
# usage
@@ -46,7 +50,7 @@ ds.triggerL.setForce(1, 255)
ds.close() # closing the controller
```
See [examples](https://github.com/flok/pydualsense/tree/master/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
@@ -69,5 +73,5 @@ Most stuff for this implementation were provided by and used from:
- add bluetooth support
- add multiple controllers
- reading the states of the controller to enable a fully compatibility with python - partially done
- partially done
- add documentation using sphinx

0
docs/.nojekyll Normal file
View File

23
docs/Makefile Normal file
View 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

9
docs/source/api.rst Normal file
View File

@@ -0,0 +1,9 @@
API
===
This is the front page for the API documentation of the **pydualsense** library.
.. toctree::
ds_enum
ds_main

43
docs/source/conf.py Normal file
View 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
View 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:

11
docs/source/ds_main.rst Normal file
View 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
View 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()

25
docs/source/index.rst Normal file
View File

@@ -0,0 +1,25 @@
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
TODOs
-----
.. todolist::

7
docs/source/modules.rst Normal file
View File

@@ -0,0 +1,7 @@
pydualsense
===========
.. toctree::
:maxdepth: 4
pydualsense

View 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
View 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`

View File

@@ -16,7 +16,8 @@ dualsense.triggerR.setForce(0, 200)
dualsense.triggerR.setForce(1, 255)
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
dualsense.close()

View File

@@ -6,7 +6,7 @@ dualsense.init()
# set color around touchpad to red
dualsense.light.setColorI(255,0,0)
# mute microphone
dualsense.audio.setMicrophoneMute(True)
dualsense.audio.setMicrophoneState(True)
# set all player 1 indicator on
dualsense.light.setPlayerID(PlayerID.player1)
# sleep a little to see the result on the controller

View File

@@ -1,2 +1,3 @@
from .enums import LedOptions, Brightness, PlayerID, PulseOptions, TriggerModes
from .event_system import Event
from .pydualsense import pydualsense, DSLight, DSState, DSTouchpad, DSTrigger, DSAudio

View File

@@ -1,3 +1,4 @@
import logging
import os
import sys
from sys import platform
@@ -8,16 +9,30 @@ if platform.startswith('Windows') and sys.version_info >= (3, 8):
import hidapi
from .enums import (LedOptions, PlayerID, PulseOptions, TriggerModes, Brightness, ConnectionType) # type: ignore
import threading
from event_system import Event
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:
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
self.verbose = verbose
if self.verbose:
logger.setLevel(logging.DEBUG)
self.leftMotor = 0
self.rightMotor = 0
@@ -25,7 +40,10 @@ class pydualsense:
self.register_available_events()
def register_available_events(self):
def register_available_events(self) -> None:
"""
register all available events that can be used for the controller
"""
# button events
self.triangle_pressed = Event()
@@ -70,8 +88,9 @@ class pydualsense:
self.accelerometer_changed = Event()
def init(self):
"""initialize module and device states
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
@@ -92,6 +111,14 @@ class pydualsense:
self.report_thread.start()
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
@@ -102,17 +129,19 @@ class pydualsense:
self.output_report_length = 78
return ConnectionType.BT
def close(self):
def close(self) -> None:
"""
Stops the report thread and closes the HID device
"""
# TODO: reset trigger effect to default
self.ds_thread = False
self.report_thread.join()
self.device.close()
def __find_device(self) -> hidapi.Device:
"""
find HID device and open it
find HID dualsense device and open it
Raises:
Exception: HIDGuardian detected
@@ -139,7 +168,7 @@ class pydualsense:
dual_sense = hidapi.Device(vendor_id=detected_device.vendor_id, product_id=detected_device.product_id)
return dual_sense
def setLeftMotor(self, intensity: int):
def setLeftMotor(self, intensity: int) -> None:
"""
set left motor rumble
@@ -157,7 +186,7 @@ class pydualsense:
raise Exception('maximum intensity is 255')
self.leftMotor = intensity
def setRightMotor(self, intensity: int):
def setRightMotor(self, intensity: int) -> None:
"""
set right motor rumble
@@ -175,14 +204,14 @@ class pydualsense:
raise Exception('maximum intensity is 255')
self.rightMotor = intensity
def sendReport(self):
def sendReport(self) -> None:
"""background thread handling the reading of the device and updating its states
"""
while self.ds_thread:
# read data from the input report of the controller
inReport = self.device.read(self.input_report_length)
if self.verbose:
print(inReport)
logger.debug(inReport)
# decrypt the packet and bind the inputs
self.readInput(inReport)
@@ -192,7 +221,7 @@ class pydualsense:
# write the report to the device
self.writeReport(outReport)
def readInput(self, inReport):
def readInput(self, inReport) -> None:
"""
read the input from the controller and assign the states
@@ -262,6 +291,7 @@ class pydualsense:
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)
@@ -302,7 +332,7 @@ class pydualsense:
self.l1_changed(self.state.L1)
if self.state.L2 != self.last_states.L2:
self.l1_changed(self.state.L2)
self.l2_changed(self.state.L2)
if self.state.R3 != self.last_states.R3:
self.r3_changed(self.state.R3)
@@ -335,15 +365,15 @@ class pydualsense:
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
"""
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
def writeReport(self, outReport):
def writeReport(self, outReport) -> None:
"""
write the report to the device
@@ -352,7 +382,7 @@ class pydualsense:
"""
self.device.write(bytes(outReport))
def prepareReport(self):
def prepareReport(self) -> None:
"""
prepare the output to be send to the controller
@@ -421,12 +451,17 @@ class pydualsense:
outReport[45] = self.light.TouchpadColor[0]
outReport[46] = self.light.TouchpadColor[1]
outReport[47] = self.light.TouchpadColor[2]
if self.verbose:
print(outReport)
logger.debug(outReport)
return outReport
class DSTouchpad:
"""
Dualsense Touchpad class. Contains X and Y position of touch and if the touch isActive
"""
def __init__(self) -> None:
"""
Class represents the Touchpad of the controller
@@ -440,7 +475,9 @@ class DSTouchpad:
class DSState:
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.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
@@ -452,7 +489,13 @@ class DSState:
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:
self.DpadUp = True
self.DpadDown = False
@@ -611,6 +654,9 @@ class DSLight:
class DSAudio:
def __init__(self) -> None:
"""
initialize the limited Audio features of the controller
"""
self.microphone_mute = 0
self.microphone_led = 0
@@ -629,7 +675,16 @@ class DSAudio:
raise TypeError('MicrophoneLED can only be a bool')
self.microphone_led = value
def setMicrophoneMute(self, state):
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')
@@ -639,6 +694,11 @@ class DSAudio:
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:
# trigger modes
self.mode: TriggerModes = TriggerModes.Off
@@ -683,20 +743,20 @@ class DSTrigger:
class DSGyro:
"""
Class representing the Gyro2 of the controller
"""
def __init__(self) -> None:
"""
Class represents the Gyro of the controller
"""
self.Pitch = 0
self.Yaw = 0
self.Roll = 0
class DSAccelerometer:
"""
Class representing the Accelerometer of the controller
"""
def __init__(self) -> None:
"""
Class represents the Accelerometer of the controller
"""
self.X = 0
self.Y = 0
self.Z = 0
self.Z = 0

View File

@@ -1 +1,3 @@
hid==1.0.4
hid-usb
sphinx
furo

View File

@@ -6,12 +6,12 @@ with open("README.md", "r") as fh:
setup(
name='pydualsense',
version='0.6',
version='0.6.2',
description='use your DualSense (PS5) controller with python',
long_description=long_description,
long_description_content_type="text/markdown",
url='https://github.com/flok/pydualsense',
author='Florian K',
author='Florian (flok) K',
license='MIT License',
packages=setuptools.find_packages(),
install_requires=['hidapi-usb>=0.3', 'cffi']