Coder Social home page Coder Social logo

pydualsense's Introduction

pydualsense

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

Installation

Windows

Download hidapi and place the x64 .dll file into your Workspace. After that install the package from pypi.

pip install --upgrade pydualsense

Linux

On Linux based system you first need to add a udev rule to let the user access the PS5 controller without requiring root privileges.

sudo cp 70-ps5-controller.rules /etc/udev/rules.d
sudo udevadm control --reload-rules
sudo udevadm trigger

Then install the hidapi through your package manager of your system.

On an Ubuntu system the package libhidapi-dev is required.

sudo apt install libhidapi-dev

After that install the package from pypi.

pip install --upgrade pydualsense

usage

from pydualsense import pydualsense, TriggerModes

def cross_pressed(state):
    print(state)

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

ds.cross_pressed += cross_pressed
ds.light.setColorI(255,0,0) # set touchpad color to red
ds.triggerL.setMode(TriggerModes.Rigid)
ds.triggerL.setForce(1, 255)
ds.close() # closing the controller

See examples or examples docs folder for some more ideas

Help wanted

Help wanted from people that want to use this and have feature requests. Just open a issue with the correct label.

dependecies

  • hidapi-usb >= 0.3

Credits

Most stuff for this implementation were provided by and used from:

Coming soon

  • add multiple controllers
  • add documentation using sphinx

pydualsense's People

Contributors

crimsonzen avatar dsb298 avatar flok avatar kappaj avatar kit-nya avatar thecomputerdan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

pydualsense's Issues

Multiple Controller support

I want to support multiple controller for using this in a local player environment.

In my mind this shouldn't be that hard.

Steps:

  • on the iterate just open all found devices and save them in a list
  • rewrite the library to select the device and not self.

If someone himself wants to implement this just create a Pull Request and i will merge it :)

OSError: Could not open connection to device.

Ubuntu 22.04

Traceback (most recent call last):
  File "/home/matt/projects/particle-filter/src/dualsense/test_controller.py", line 7, in <module>
    ds.init() # initialize controller
  File "/home/matt/.local/lib/python3.10/site-packages/pydualsense/pydualsense.py", line 95, in init
    self.device: hidapi.Device = self.__find_device()
  File "/home/matt/.local/lib/python3.10/site-packages/pydualsense/pydualsense.py", line 168, in __find_device
    dual_sense = hidapi.Device(vendor_id=detected_device.vendor_id, product_id=detected_device.product_id)
  File "/home/matt/.local/lib/python3.10/site-packages/hidapi.py", line 256, in __init__
    raise IOError("Could not open connection to device.")
OSError: Could not open connection to device.

Mute button

In fortnite mute button act as a master mute on the controller. I even went further and explore it on sound setting with and without headphones and it doesn't show incoming input when activated.

L2 trigger value comparison mistyped

Small mistyping in line 335 from pydualsense.py:

if self.state.L2 != self.last_states.L2:
    self.l1_changed(self.state.L2)

Should be:

if self.state.L2 != self.last_states.L2:
    self.l2_changed(self.state.L2)

Is it possible to send input state on writeReport ?

Hi !

Is not an issue but I don't where I can ask my question :)

  1. Is it possible to get the current state of the controller and to send it to controller ?
    It's not very clear, but I read my device with that

inReport = dualsense.device.read(dualsense.receive_buffer_size)

And if I immediately send these data to my controller with that

dualsense.device.write(inReport)

It's not working properly.

  1. Is it possible to send an input state ?
    For exemple : What I need to write on my device if I want to simulate a click on my Square button ?
    I think it's linked with my first question.

Thank you for your work!

setLeftMotor and setRightMotor backwards?

I ran the effects.py demo and changes the values of the above functions. setting both to 255 = max rumble, 0 = no rumble. this is expected. However, setting setLeftMotor to 0 and setRightMotor to 255 seems to rumble on the left instead of right, and vice versa.

Failure on macOS, M1

I'm trying on my M1 Mac, and got error out of index. I guess some hid data were missing

Python 3.10.8 (main, Oct 13 2022, 09:48:40) [Clang 14.0.0 (clang-1400.0.29.102)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pydualsense
>>> obj = pydualsense.pydualsense()
>>> obj.init()
>>> Exception in thread Thread-1 (sendReport):
Traceback (most recent call last):
  File "/opt/homebrew/Cellar/[email protected]/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/opt/homebrew/Cellar/[email protected]/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "/opt/homebrew/lib/python3.10/site-packages/pydualsense/pydualsense.py", line 216, in sendReport
    self.readInput(inReport)
  File "/opt/homebrew/lib/python3.10/site-packages/pydualsense/pydualsense.py", line 262, in readInput
    misc2 = states[10]
IndexError: list index out of range

>>> obj.device.get_product_string()
'DualSense Wireless Controller'
>>> data = obj.device.read(64)
>>> data
b'\x01\x80~\x7f~\x08\x00\x08\x00\x00'

Add HidHide detection

Add a detection to see if hidhide for the controller is activated and notify the user that the controller is hidden.

Bluetooth support

Currently this library only supports usb-c cable support. I want to add the bluetooth support in the next weeks.

Is it possible to press buttons?

I have tried to find a way to make the code press the Cross button but without success. Is it possible? Or I just can change lights/motors?

I tried changing the state and after looking at the code, couldn't find a way to change it.

Thanks!

Raspberry Pi library issue

Hi,

On the Raspberry Pi 4, I get the raised exception "OSError: Could not find any hidapi library"

I've read through the commit notes, and I see that Linux support's been expanded, but is there anything specific I need to do to make this work?

I've downloaded the hidapi v0.10.1 zip and extracted the x64 version of the hidapi.dll file to my python project folder. I then installed this lib with "pip install pydualsense"

But the pydualsense library can't find the DLL.

I've also tried dropping "hidapi.dll" into /home/pi/.local/lib/python3.7/site-packages/ since that's where hidapi.py is. No luck.

Thanks,
Ryan

the sixaxis data is unreadable

I don't know if the data is correct. The data jumps very violently. The value sum of squares of the accelerometer in the static state has a large jump. However, it should not undergo a large jump under static conditions. At the same time, there is no problem with my gamepad, because the data it reads in ds4windows behaves normally

better trigger parameter functions

currently the trigger parameters are just a list with 0 description of what they actually do. We should make functions for each parameters to tell exactly what they are for.

Bluetooth connection

I am trying to connect my controller via Bluetooth. I am using Ubuntu 18.04

I noticed that the report size is only 10, therefore the code is throwing an error when running

Report length 10
Device connected via bluetooth
[INFO] [1683724328.458037]: Connected to controller.
Exception in thread Thread-15:
Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/home/jfine/.local/lib/python3.6/site-packages/pydualsense/pydualsense.py", line 233, in sendReport
    self.readInput(inReport)
  File "/home/jfine/.local/lib/python3.6/site-packages/pydualsense/pydualsense.py", line 287, in readInput
    misc2 = states[10]
IndexError: list index out of range

hidapi fails to install

Hi, trying to install pydualsense on my Nvidia Shield Pro 2019. I have termux installed. I get these errors when I try pip install hidapi:

hidapi/libusb/hid.c:47:10: fatal error: 'libusb.h' file not found
hidapi/linux/hid.c:44:10: fatal error: 'libudev.h' file not found

Any help is highly appreciated!

Import error on Python 3.9.5

Arch Linux

Python 3.9.5 (default, May 24 2021, 12:50:35) 
[GCC 11.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pydualsense import pydualsense, TriggerModes
Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/cffi/cparser.py", line 336, in _parse
    ast = _get_parser().parse(fullcsource)
  File "/usr/lib/python3.9/site-packages/pycparser/c_parser.py", line 149, in parse
    return self.cparser.parse(
  File "/usr/lib/python3.9/site-packages/pycparser/ply/yacc.py", line 331, in parse
    return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
  File "/usr/lib/python3.9/site-packages/pycparser/ply/yacc.py", line 1199, in parseopt_notrack
    tok = call_errorfunc(self.errorfunc, errtoken, self)
  File "/usr/lib/python3.9/site-packages/pycparser/ply/yacc.py", line 193, in call_errorfunc
    r = errorfunc(token)
  File "/usr/lib/python3.9/site-packages/pycparser/c_parser.py", line 1858, in p_error
    self._parse_error(
  File "/usr/lib/python3.9/site-packages/pycparser/plyparser.py", line 67, in _parse_error
    raise ParseError("%s: %s" % (coord, msg))
pycparser.plyparser.ParseError: <cdef source string>:4:5: before: ULONG_PTR

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.9/site-packages/pydualsense/__init__.py", line 2, in <module>
    from .pydualsense import pydualsense, DSLight, DSState, DSTouchpad, DSTrigger, DSAudio
  File "/usr/lib/python3.9/site-packages/pydualsense/pydualsense.py", line 7, in <module>
    import hidapi
  File "/usr/lib/python3.9/site-packages/hidapi.py", line 4, in <module>
    ffi.cdef("""
  File "/usr/lib/python3.9/site-packages/cffi/api.py", line 112, in cdef
    self._cdef(csource, override=override, packed=packed, pack=pack)
  File "/usr/lib/python3.9/site-packages/cffi/api.py", line 126, in _cdef
    self._parser.parse(csource, override=override, **options)
  File "/usr/lib/python3.9/site-packages/cffi/cparser.py", line 389, in parse
    self._internal_parse(csource)
  File "/usr/lib/python3.9/site-packages/cffi/cparser.py", line 394, in _internal_parse
    ast, macros, csource = self._parse(csource)
  File "/usr/lib/python3.9/site-packages/cffi/cparser.py", line 338, in _parse
    self.convert_pycparser_error(e, csource)
  File "/usr/lib/python3.9/site-packages/cffi/cparser.py", line 367, in convert_pycparser_error
    raise CDefError(msg)
cffi.CDefError: cannot parse "ULONG_PTR Internal;"
<cdef source string>:4:5: before: ULONG_PTR

Examples for Button/Joystick Control

Good Evening,

Just learning a bit of python I was wondering if you had any examples of controlling user input?

I tried reading the source code a bit and its just hanging if I set the states, im probably doing this wrong.

from pydualsense import *
# create dualsense
dualsense = pydualsense()

# find device and initialize
dualsense.init()

dualsense.state.ps = "True"
dualsense.sendReport()
time.sleep(2.5)
dualsense.state.ps = "False"
dualsense.sendReport()

# close device
dualsense.close()

help wanted

check this out

script attached, you willl need a few libraries to run it, mainly tkinter

Arm/Raspberry Pi support

Hi,

Sorry I'm not very processor/instruction literate and I'm impressed with what you've done. Any idea if I can get this to work on a raspberry pi 4b? Pip had no issues installing the module but just loading the module in an otherwise empty python script gives an error:

cffi.CDefError: cannot parse "ULONG_PTR Internal;
:4:5: before: ULONG_PTR

This exception occurred after an earlier exception:

pycparser.plyparser.ParseError: :4:5: before: ULONG_PTR

Sorry if this isn't relevant, but any pointers in the right direction would be greatly appreciated!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.