2013-11-03 12 views
5

Có thể "xáo trộn" bộ điều khiển Xbox 360 không dây của tôi cho PC bằng Python không? Tôi đã chỉ tìm thấy giải pháp để đọc đầu vào nhưng tôi không thể tìm thấy thông tin về rung/rumble.Có thể "xáo trộn" bộ điều khiển Xbox 360 bằng Python không?

EDIT:

Sau khi mã được cung cấp bởi @AdamRosenfield tôi nhận được lỗi sau.

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. 

Xin lưu ý rằng lỗi cuối cùng được dịch từ tiếng Tây Ban Nha.

Trả lời

4

Có thể, nhưng không dễ. Trong C, bạn sẽ sử dụng các XInputSetState() function để kiểm soát các rumble. Để truy cập nó từ Python, bạn phải biên dịch một phần mở rộng Python được viết bằng C hoặc sử dụng ctypes library.

Something như thế này nên làm việc, mặc dù gấu trong tâm trí tôi đã không kiểm tra này:

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) 
+0

Cám ơn trả lời, tôi nhận lỗi với "ctypes.struct": Nó không phải là một thuộc tính hợp lệ. Ngoài ra tôi không biết "ctypes.struct" xuất phát từ đâu! – Belohlavek

+0

@Belohlavek: Rất tiếc, phải là 'Cấu trúc', không phải' cấu trúc'. –

+0

Dự án cuối tuần tuyệt vời! +1 –

Các vấn đề liên quan