Skip to main content

A Python package to connect to xbox and interface with its buttons and joysticks

Project description

xbox.py

Python class to support reading xbox 360 wired and wireless controller input under Linux. Makes it easy to get real-time input from controller buttons, analog sticks and triggers. Built and tested on RaspberryPi running Raspbian.

Requires that xboxdrv be installed first:

sudo apt-get install xboxdrv

To test the driver, issue the following command and see if the controller inputs are recognized

sudo xboxdrv --detach-kernel-driver

See http://pingus.seul.org/~grumbel/xboxdrv/ for details on xboxdrv

Download the python module and sample code with the following:

wget https://raw.githubusercontent.com/FRC4564/Xbox/master/xbox.py
wget https://raw.githubusercontent.com/FRC4564/Xbox/master/sample.py

You can run the sample code to see how the Joystick class works.

sudo python sample.py

Example class usage:

import xbox
joy = xbox.Joystick()         #Initialize joystick

if joy.A():                   #Test state of the A button (1=pressed, 0=not pressed)
    print 'A button pressed'
x_axis   = joy.leftX()        #X-axis of the left stick (values -1.0 to 1.0)
(x,y)    = joy.leftStick()    #Returns tuple containing left X and Y axes (values -1.0 to 1.0)
trigger  = joy.rightTrigger() #Right trigger position (values 0 to 1.0)

joy.close()                   #Cleanup before exit

Note: Run your code with sudo privileges to allow xboxdrv the necessary control over USB devices. If you want, you can provide your user account with the proper access, so you needn't use sudo.

First, add your user to the root group. Here's how to do this for the user ‘pi’

sudo usermod -a -G root pi

Create a permissions file using the nano text editor.

sudo nano /etc/udev/rules.d/55-permissions-uinput.rules

Enter the following rule and save your entry.

KERNEL=="uinput", MODE="0660", GROUP="root"

Troubleshooting

I find that xboxdrv occasionally has trouble connecting to the controller. You may see a USB device error or something similar. Issuing the following command will detach and reconnect the controller.

sudo xboxdrv --detach-kernel-driver

You should now be able to move the joysticks and press buttons to see the controller state display for all inputs. Just press Ctrl-C to exit and then relaunch your python code that uses xbox.py.

If your wireless controller still won't connect, press the sync button on the controller and the receiver (both devices need to be powered).

Usage

A good application for an XBox controller is for robot control. Check out Basic PiBot for more details.

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

xbox-remote-1.0.5.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

xbox_remote-1.0.5-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file xbox-remote-1.0.5.tar.gz.

File metadata

  • Download URL: xbox-remote-1.0.5.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.5

File hashes

Hashes for xbox-remote-1.0.5.tar.gz
Algorithm Hash digest
SHA256 d0e51d94d83341b9c8d18c9fb4df530cba3afbe163c0821a324dc63935f1232d
MD5 93dc64e1b0a218a057d17173376e1010
BLAKE2b-256 aa6d2aa5bd3a9c55338088d3a19268a7f8513e66033051b21d0f8a02bb109dfa

See more details on using hashes here.

File details

Details for the file xbox_remote-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: xbox_remote-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.5

File hashes

Hashes for xbox_remote-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 723bd41490bd73c382edd99d6e6ea45e025999d11fb638edbbba0f5e02aa9271
MD5 dabfc30b76cd541ee1f9741cc1b49d8a
BLAKE2b-256 20bfad64b5e3aae46ba5a7c3532075eb1eb1ac1e5cd4014994a066860a006b17

See more details on using hashes here.

Supported by

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