Skip to main content

SDL2 joystick driver for python

Project description

clamps

GitHub PyPI - Python Version PyPI PyPI - Downloads

Note: I only have playstations, so I don't know if this will work with other joysticks.

This is actually something I did long ago, but finally decided to make it a module available on PyPi.

Setup

This requires sdl2 inorder to work:

brew install sdl2       # macos
apt install libsdl2-dev # linux

This package will install the python bindings PySDL2 from pypi.

Tested:

  • Works with joystick connected via USB cable
  • Works with joystick connected via bluetooth
  • Works with PS4 controller

Example

#!/usr/bin/env python3
import time
from clamps import PS4Joystick

def main():
    import time

    js = PS4Joystick()

    while js.valid:
        try:
            ps4 = js.get()
            print(ps4,"\n----------------------\n")
            time.sleep(0.1)
            if ps4.buttons.pad is True:
                break
        except KeyboardInterrupt:
            print('js exiting ...')
            break


if __name__ == "__main__":
    main()

Output of get() is a namedtuple with the following fields:

JS(info=JSInfo(num_buttons=16, num_axes=6, num_hats=0), leftstick=Axis(x=-0.003936767578125, y=0.011749267578125), rightstick=Axis(x=-0.01177978515625, y=-0.050994873046875), triggers=Axis(x=0.0, y=0.0), buttons=PS4Buttons(x=False, circle=False, square=False, triangle=False, share=False, ps=False, options=False, L3=False, R3=False, L1=False, R1=False, dp_up=False, dp_down=False, dp_left=False, dp_right=False, pad=True))

How to pair a DUALSHOCK 4 wireless controller with a supported device

To pair your wireless controller with a supported device using Bluetooth for the first time, turn on pairing mode.

  1. Make sure the player indicator on the controller is off. If the player indicator is on, press and hold the PS button until it turns off. If a USB is connected to the controller, disconnect it.
  2. While pressing and holding the SHARE button, press and hold the PS Button until the light bar flashes.
  3. Enable Bluetooth on your device, and then select the controller from the list of Bluetooth devices. When pairing is complete, the light bar blinks, and then the player indicator lights up.

MIT License

Copyright (c) 2014 Kevin Walchko

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

clamps-0.0.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

clamps-0.0.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file clamps-0.0.1.tar.gz.

File metadata

  • Download URL: clamps-0.0.1.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.6 Darwin/21.6.0

File hashes

Hashes for clamps-0.0.1.tar.gz
Algorithm Hash digest
SHA256 992bd0755b473415438ceb505636705868ff6140f12ad003c8da5c4bd6143000
MD5 f30a7687579b99ba6401db6e3447a699
BLAKE2b-256 b9b4e2c9e166ee8f6604b7ffdd7227ea334ca55e7ea642ce88ca2043249d70b8

See more details on using hashes here.

File details

Details for the file clamps-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: clamps-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.6 Darwin/21.6.0

File hashes

Hashes for clamps-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1918dbdc15ee24e2dbc8bd8062b4364ba5d9008e680e17a53cbcd5f66a8a5324
MD5 ac9161d0b58ce5074dfd49dba7f3d0d7
BLAKE2b-256 3608e237808d12473b31f58260859fd3d95396b190cdafbb6cf4ce1117632cc0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page