4

I have managed to find a script that starts the xbox 360 controller to rumble(vibrate) however I can't get it to turn Off. Is there a way to set it so after 5 seconds the rumble stops?

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, 0.5, 0.5,)

Thanks

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
RKEONECS
  • 47
  • 1
  • 4

1 Answers1

3

It looks like you've got everything you need right in front of you with this script. The set_vibration helper function takes 3 input arguments:

  • controller id ( 0 in your circumstance )
  • left motor vibration scaled from 0 (off) to 1.0 (full on) - you've got it set to 50% by using 0.5
  • right motor vibration (also 0 - 1.0)

so to set it to vibrate at 50% power for only 5 seconds, try something like:

import time
set_vibration(0, 0.5, 0.5)
time.sleep(5)
set_vibration(0, 0, 0)

granted this is all just by inspection of your script, and not tested

IanTheEngineer
  • 196
  • 1
  • 4
  • Hi thanks that worked now looking at it I thought it would be something more complicated. I am now looking to assign the rumble (this script) to an event trigger so when left joystick is pressed on the xbox 360 controller the pad plays this script and rumbles. – RKEONECS Dec 10 '13 at 16:47