Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to "rumble" a Xbox 360 controller with Python?

Is it possible to "rumble" my wireless Xbox 360 controller for PC with Python? I've only found solution for reading input but I cant find information about vibration/rumble.

EDIT:

Following the code provided by @AdamRosenfield I get the following error.

Traceback (most recent call last):
  File "C:\Users\Usuario\Desktop\rumble.py", line 8, in <module>
    xinput = ctypes.windll.Xinput  # Load Xinput.dll
  File "C:\Python27\lib\ctypes\__init__.py", line 435, in __getattr__
    dll = self._dlltype(name)
  File "C:\Python27\lib\ctypes\__init__.py", line 365, in __init__
    self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126] The specified module could not be found. 

Plese note that the last error was translated from Spanish.

like image 931
Belohlavek Avatar asked Nov 03 '13 03:11

Belohlavek


People also ask

How do I make my Xbox 360 controller vibrate?

Press the Xbox button  to open the guide, and then select Profile & system > Settings. Select Accessibility > Controller, and then select Vibration settings. If you have multiple controllers, choose the controller that you want to change and select Configure.

How do I make my Xbox 360 controller vibrate PC?

Go right on the pad until you can choose “Settings“. Toggle down and select”Preferences“. Toggle down and select “Vibration“. Clear the check from “Enable Vibration“.

Does Xbox controller have rumble?

Thanks for checking! Yes, this Xbox controller supports rumble feedback.


1 Answers

It's possible, but it's not easy. In C, you'd use the XInputSetState() function to control the rumble. To access that from Python, you'd have to either compile a Python extension written in C or use the ctypes library.

Something like this should work, though bear in mind I haven't tested this:

import ctypes

# Define necessary structures
class XINPUT_VIBRATION(ctypes.Structure):
    _fields_ = [("wLeftMotorSpeed", ctypes.c_ushort),
                ("wRightMotorSpeed", ctypes.c_ushort)]

xinput = ctypes.windll.xinput1_1  # Load Xinput.dll

# Set up function argument types and return type
XInputSetState = xinput.XInputSetState
XInputSetState.argtypes = [ctypes.c_uint, ctypes.POINTER(XINPUT_VIBRATION)]
XInputSetState.restype = ctypes.c_uint

# Now we're ready to call it.  Set left motor to 100%, right motor to 50%
# for controller 0
vibration = XINPUT_VIBRATION(65535, 32768)
XInputSetState(0, ctypes.byref(vibration))

# You can also create a helper function like this:
def set_vibration(controller, left_motor, right_motor):
    vibration = XINPUT_VIBRATION(int(left_motor * 65535), int(right_motor * 65535))
    XInputSetState(controller, ctypes.byref(vibration))

# ... and use it like so
set_vibration(0, 1.0, 0.5)
like image 72
Adam Rosenfield Avatar answered Oct 06 '22 01:10

Adam Rosenfield