最近在弄 Donkey Car 的搖桿按鍵,需要讀按鍵值。
作法大概是這樣。
1. 使用 evdev 讀取搖桿資訊。
>>> import evdev >>> device = evdev.InputDevice('/dev/input/event0') >>> print(device) >>> device.capabilities() >>> device.capabilities(verbose=True) {('EV_SYN', 0): [('SYN_REPORT', 0), ('SYN_CONFIG', 1), ('SYN_DROPPED', 3), ('?', 4)], ('EV_KEY', 1): [(['BTN_A', 'BTN_GAMEPAD', 'BTN_SOUTH'], 304), (['BTN_B', 'BTN_EAST'], 305), ('BTN_C', 306), (['BTN_NORTH', 'BTN_X'], 307), (['BTN_WEST', 'BTN_Y'], 308), ('BTN_Z', 309), ('BTN_TL', 310), ('BTN_TR', 311), ('BTN_TL2', 312), ('BTN_TR2', 313), ('BTN_SELECT', 314), ('BTN_START', 315), ('BTN_MODE', 316)], ('EV_ABS', 3): [(('ABS_X', 0), AbsInfo(value=128, min=0, max=255, fuzz=0, flat=15, resolution=0)), (('ABS_Y', 1), AbsInfo(value=128, min=0, max=255, fuzz=0, flat=15, resolution=0)), (('ABS_Z', 2), AbsInfo(value=128, min=0, max=255, fuzz=0, flat=15, resolution=0)), (('ABS_RZ', 5), AbsInfo(value=128, min=0, max=255, fuzz=0, flat=15, resolution=0)), (('ABS_HAT0X', 16), AbsInfo(value=0, min=-1, max=1, fuzz=0, flat=0, resolution=0)), (('ABS_HAT0Y', 17), AbsInfo(value=0, min=-1, max=1, fuzz=0, flat=0, resolution=0))], ('EV_MSC', 4): [('MSC_SCAN', 4)]}
2. 將讀出來的資訊轉成 16 進位。
self.button_names = { 0x13a: 'select', # 8 314 0x13b: 'start', # 9 315 0x13c: 'PS', # a 316 }
3. 修改 donkeypart_ps3_controller 裡面的 part 即可。
#import evdev from evdev import InputDevice, categorize, ecodes #cree un objet gamepad | creates object gamepad gamepad = InputDevice('/dev/input/event2') #affiche la liste des device connectes | prints out device info at start print(gamepad) #affiche les codes interceptes | display codes for event in gamepad.read_loop(): #Boutons | buttons if event.type == ecodes.EV_KEY: print(event) #Gamepad analogique | Analog gamepad elif event.type == ecodes.EV_ABS: absevent = categorize(event) print ecodes.bytype[absevent.event.type][absevent.event.code], absevent.event.value使用 evdev 更容易。
import evdev device = evdev.InputDevice('/dev/input/event1') print(device) for event in device.read_loop(): if event.type == evdev.ecodes.EV_KEY: print(evdev.categorize(event))找到一篇最棒的,可以直接使用 evtest 讀取搖桿資訊。
沒有留言:
張貼留言