Control vibration motors of game controller in pure Python?

↘锁芯ラ 提交于 2020-04-30 05:39:29

问题


I've bought a standard game controller that has vibration motors. It identifies itself as "SHANWAN Android Gamepad" but seems to be compatible to others as it works well on Linux out of the box.

I found a script that enables me to read data from the game controller device path at https://gist.github.com/rdb/8864666 . I am now wondering: How can I control the vibration motors? I could find only XBox API code samples which are not helpful for obvious reasons. Has anyone an idea about how to controle the vibration motors?


回答1:


The script you linked only looks for joydev devices. On Linux, joydev is the older gamepad interface and doesn't support vibration. The path to the joydev device node is probably something like /dev/input/js0.

The newer evdev-based interface supports force feedback. Check /proc/bus/input/devices to find the evdev node that corresponds to the joydev node. As an example, I have a wired Xbox 360 gamepad connected and get the following output:

I: Bus=0003 Vendor=045e Product=028e Version=0110
N: Name="Microsoft X-Box 360 pad"
P: Phys=usb-0000:00:14.0-1.1/input0
S: Sysfs=/devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1.1/2-1.1:1.0/input/input27
U: Uniq=
H: Handlers=event16 js0 
B: PROP=0
B: EV=20000b
B: KEY=7cdb000000000000 0 0 0 0
B: ABS=3003f
B: FF=107030000 0

The line starting with "H:" indicates that /dev/input/js0 and /dev/input/event16 both refer to the Xbox gamepad. /dev/input/event16 is the evdev node.

To test vibration on the device, use the fftest command with the path to the evdev node. If it supports vibration it should give an OK result for one or more of the effects it tries to upload to the device:

$ fftest /dev/input/event16
Force feedback test program.
HOLD FIRMLY YOUR WHEEL OR JOYSTICK TO PREVENT DAMAGES

Device /dev/input/event16 opened
Features:
  * Absolute axes: X, Y, Z, RX, RY, RZ, Hat 0 X, Hat 0 Y, 
    [3F 00 03 00 00 00 00 00 ]
  * Relative axes: 
    [00 00 ]
  * Force feedback effects types: Periodic, Rumble, Gain, 
    Force feedback periodic effects: Square, Triangle, Sine, 
    [00 00 00 00 00 00 00 00 00 00 03 07 01 00 00 00 ]
  * Number of simultaneous effects: 16

Setting master gain to 75% ... OK
Uploading effect #0 (Periodic sinusoidal) ... OK (id 0)
Uploading effect #1 (Constant) ... Error: Invalid argument
Uploading effect #2 (Spring) ... Error: Invalid argument
Uploading effect #3 (Damper) ... Error: Invalid argument
Uploading effect #4 (Strong rumble, with heavy motor) ... OK (id 1)
Uploading effect #5 (Weak rumble, with light motor) ... OK (id 2)
Enter effect number, -1 to exit

If you get a Permission Denied error, it may mean the device does not have a compatible driver installed or the driver doesn't support force feedback. You shouldn't need to run this command as root to test force feedback.

I'm not familiar with the SHANWAN Android Gamepad. I have one SHANWAN device that works on Linux, is it the same?

https://www.amazon.com/Gaming-Controller-Gamepad-Windows-Android/dp/B00OAYHIRA

IIRC, the vibration features require that the device is in XInput mode which allows it to work with the xpad kernel driver. If your gamepad has a similar mode-switching feature, try the XInput mode.

Once you've verified that the force feedback features are working for your device, you should be able to send FF commands with python-evdev:

http://python-evdev.readthedocs.io/en/latest/



来源:https://stackoverflow.com/questions/49968540/control-vibration-motors-of-game-controller-in-pure-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!