The Xbox360Controller
classes constructor takes a few parameters:
index
: the index of the controller to use. If you have connected two controllers connected, the first one would be0
, the second on1
and so on. Defaults to0
(obviously).axis_threshold
: minimum value of an axis for its event to be triggered. This is useful to not get a flood of useless events with ridiculously small values even if the axis is at(0, 0)
. Applies to all axes and triggers. Defaults to0.2
.raw_mode
: don't use a hardcoded set of axes, buttons, triggers and the hat expected for a regular Xbox360 controller but thexpad
detection features. This allows support for basically every joystick or game controller supported byxpad
, but is badly documented and currently very limited. I will probably improve the situation soon, though.
controller
is an instance of Xbox360Controller
with raw_mode=False
.
Xbox360Controller.get_available()
: return a list ofXbox360Controller
instances containing each available controllercontroller.driver_version
: return the driver versions returned byioctl
withJSIOCGVERSION
on the device file andEVIOCGVERSION
on the event file. TBH, I'm not even sure what these mean 😕controller.num_axes
: return the total number of axes, triggers and hatscontroller.num_buttons
: return the total number of buttonscontroller.name
: return the controller's name as reported byxpad
controller.has_rumble
: return whether the controller supports rumbling or notcontroller.has_led
: return whether the program will be able to set the led or not, this might also returnFalse
because of permission issues. See the LED section below.controller.info()
: print some debug info, collected from the attributes stated abovecontroller.set_rumble(left, right, duration=1000)
: set the left and right rumbling strength for a given duration to the given percentage (0.0
-1.0
)controller.set_led(status)
: set the LED circle's status, available are listed abovecontroller.close()
: close all open file objects, recommended for cleanup if not using thewith
statement.
button
is an instance of Button
and one of controller.button_a
, controller.button_b
, controller.button_x
, controller.button_y
, controller.button_trigger_l
, controller.button_trigger_r
, controller.button_thumb_l
, controller.button_thumb_r
, controller.button_select
, controller.button_start
, controller.button_mode
.
button.when_pressed
: holds callable object to be called when the button is pressedbutton.when_released
: holds callable object to be called when the button is releasedbutton.is_pressed
: holds boolean whether the button is currently pressed or not
axis
is an instance of Axis
and one of controller.axis_l
,
controller.axis_r
, controller.hat
axis.when_moved
: holds callable object to be called when the axis is movedaxis.x
: holds the X value of the axisaxis.y
: holds the Y value of the axis
The axis values will be one of 1
, 0
or -1
; from top to bottom or right to
left.
axis
is an instance of RawAxis
and one of controller.trigger_l
,
controller.trigger_r
axis.when_moved
: holds callable object to be called when the axis is movedaxis.value
: holds the value of the axis
Advised to being used for internal stuff only, until properly documented:
controller.get_event()
: return the most recent controller event as aControllerEvent
controller.process_event(event)
: process aControllerEvent
and update the controller's input device instances
from xbox360controller import Xbox360Controller
with Xbox360Controller() as controller:
controller.set_rumble(0.5, 0.5, 1000)
time.sleep(0.5)
This will enable rumble on both sides of the controller with each 50% strength for one second (1000ms). The method call is non blocking, so in this example we need to wait 0.5 more seconds until the script can exit, otherwise the rumbling would not be noticed. In a more advanced use case with a loop you will not need the sleep.
import time
from xbox360controller import Xbox360Controller
with Xbox360Controller() as controller:
controller.set_led(Xbox360Controller.LED_ROTATE)
time.sleep(1)
controller.set_led(Xbox360Controller.LED_OFF)
This will let the LED circle rotate for one second and then turn it off.
Available modes are:
LED_OFF
LED_BLINK
LED_TOP_LEFT_BLINK_ON
LED_TOP_RIGHT_BLINK_ON
LED_BOTTOM_LEFT_BLINK_ON
LED_BOTTOM_RIGHT_BLINK_ON
LED_TOP_LEFT_ON
LED_TOP_RIGHT_ON
LED_BOTTOM_LEFT_ON
LED_BOTTOM_RIGHT_ON
LED_ROTATE
LED_BLINK_PREV
LED_BLINK_SLOW_PREV
LED_ROTATE_TWO
LED_BLINK_SLOW
LED_BLINK_ONCE_PREV
from xbox360controller import Xbox360Controller
with Xbox360Controller() as controller:
controller.info()