我正在尝试在 PC/Linux 上使用 PS2 Guitar Hero II 控制器。在尝试了多种 Playstation 转 USB 适配器后,我终于找到了一款不会导致任何输入延迟的适配器。
然而,我在检测 Linux 下的所有按钮时遇到了问题,如linux-input 邮件列表线程。
视窗
- 检测到所有按钮,但方向键(向上/向下扫弦)被错误地映射到与“Whammy bar”相同的轴。这并不是破坏游戏,而是带来不便。
Linux
Linux 检测控制器为
ID 054c:0268 Sony Corp. Batoh Device / PlayStation 3 Controller
,有 13 个按钮和 6 个轴。某些按钮不会导致任何事件
/dev/input/jsX
不工作的按钮做导致 HID 事件,在 中检测到
/dev/hidrawX
。
现在,我明白这是一个错误,我应该等待驱动程序的修复。在修复过程中,我想找到解决方法。
我可以轻松编写一些 udev 规则来将原始 HID 事件映射到输入事件吗?
另外,我在windows下使用wireshark捕获了USB握手过程。
答案1
我通过使用让它工作用于Python的hidapi。最后这很简单,我只是缺少工具。
如果有人需要指导,下面是完整的脚本:
from __future__ import print_function
import hid
import time
import uinput
MAPPING = {
'green': uinput.BTN_0,
'red': uinput.BTN_1,
'yellow': uinput.BTN_2,
'blue': uinput.BTN_3,
'orange': uinput.BTN_4,
'tilt': uinput.BTN_SELECT, # SELECT
'start': uinput.BTN_START, # START
'select': uinput.BTN_SELECT, # SELECT
'whammy': uinput.ABS_X, # WHAMMY
'strumdown': uinput.BTN_DPAD_DOWN,
'strumup': uinput.BTN_DPAD_UP,
}
DEVICE = uinput.Device(MAPPING.values())
# enumerate USB devices
for d in hid.enumerate():
keys = list(d.keys())
keys.sort()
for key in keys:
print("%s : %s" % (key, d[key]))
print()
# try opening a device, then perform write and read
def diff(new_arr, old_arr):
for i in range(len(new_arr)):
if new_arr[i] != old_arr[i]:
yield (i, new_arr[i])
try:
print("Opening the device")
h = hid.device()
h.open(0x054c, 0x0268) # VendorID/ProductID
print("Manufacturer: %s" % h.get_manufacturer_string())
print("Product: %s" % h.get_product_string())
print("Serial No: %s" % h.get_serial_number_string())
# enable non-blocking mode
h.set_nonblocking(1)
# write some data to the device
print("Write the data")
h.write([0, 63, 35, 35] + [0] * 61)
# wait
time.sleep(0.05)
# read back the answer
print("Read the data")
previous = None
INPUT = {
'green': False,
'red': False,
'yellow': False,
'blue': False,
'orange': False,
'tilt': False,
'start': False,
'select': False,
'whammy': False,
'strumdown': False,
'strumup': False,
}
def set_input(key, val, tmp):
prev = INPUT[key]
if tmp >= val:
tmp -= val
INPUT[key] = True
else:
INPUT[key] = False
if prev != INPUT[key]:
# SEND EVENT
DEVICE.emit(MAPPING[key], INPUT[key])
pass
return tmp
counter = 0
while True:
try:
time.sleep(0.001)
d = h.read(64)
counter += 1
if d:
if previous and d != previous:
for ret in diff(d, previous):
if ret[0] == 7:
DEVICE.emit(MAPPING['whammy'], ret[1])
if ret[0] == 2:
tmp = ret[1] - 128
tmp = set_input('strumdown', 64, tmp)
tmp = set_input('strumup', 16, tmp)
tmp = set_input('start', 8, tmp)
tmp = set_input('select', 1, tmp)
if ret[0] == 3:
tmp = ret[1]
tmp = set_input('orange', 128, tmp)
tmp = set_input('blue', 64, tmp)
tmp = set_input('red', 32, tmp)
tmp = set_input('yellow', 16, tmp)
tmp = set_input('green', 2, tmp)
tmp = set_input('tilt', 1, tmp)
previous = d
else:
continue
except KeyboardInterrupt:
break
print(counter, end="\r")
# print(INPUT)
print("Closing the device")
h.close()
except IOError as ex:
print(ex)
print("You probably don't have the hard coded device. Update the hid.device line")
print("in this script with one from the enumeration list output above and try again.")
print("Done")