You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用纯Python控制SHANWAN Android Gamepad的振动马达?

Hey there! Let's figure out how to get that vibration going on your SHANWAN Android Gamepad in Linux. Generic Android gamepads can be tricky since they don't follow the Xbox API, but Linux's evdev system has you covered. Here's a step-by-step approach:

1. First, confirm your controller supports vibration

Before diving into code, let's verify your handle has Force Feedback (FF) support:

  • Install the evtest tool (on Debian/Ubuntu: sudo apt install evtest)
  • Run sudo evtest, select your "SHANWAN Android Gamepad" from the list
  • Look through the output for lines like Force Feedback or FF_RUMBLE — if you see these, your controller can vibrate!

2. Test vibration with a command-line tool

The fftest utility (included with evtest) lets you quickly test vibration without writing code:

  • Run sudo fftest /dev/input/eventX (replace eventX with your controller's device path from evtest)
  • Follow the prompts: type 1 to select the Rumble effect, then enter values for strong/weak magnitude (0-65535) and duration. If your controller vibrates, you're good to go!

3. Control vibration in code (Python example)

Since you already have a script reading controller data, here's how to add vibration using the evdev library (it's more straightforward than low-level ioctl calls):

  • Install the library: pip install evdev
  • Use this snippet to trigger vibration (adjust the device path, magnitude, and duration as needed):
from evdev import InputDevice, ecodes
import time

# Replace with your controller's device path
controller = InputDevice('/dev/input/eventX')

# Check if Force Feedback is supported
if ecodes.EV_FF not in controller.capabilities():
    print("Oops, your controller doesn't support vibration!")
    exit()

# Define a rumble effect: strong motor (max strength), weak motor (half strength), 1 second duration
rumble_effect = {
    "type": ecodes.FF_RUMBLE,
    "id": -1,
    "ff_effect": {
        "type": ecodes.FF_RUMBLE,
        "u": {
            "rumble": {
                "strong_magnitude": 0xffff,
                "weak_magnitude": 0x8000
            }
        },
        "replay": {
            "length": 1000,
            "delay": 0
        }
    }
}

# Upload the effect to the controller
effect_id = controller.upload_effect(rumble_effect)

# Trigger the vibration
controller.write(ecodes.EV_FF, effect_id, 1)

# Wait for the vibration to finish
time.sleep(1)

# Stop the vibration and clean up
controller.write(ecodes.EV_FF, effect_id, 0)
controller.erase_effect(effect_id)

4. Fix permission issues (so you don't need sudo)

To avoid running your script with sudo, add your user to the input group:

sudo usermod -aG input $USER

Log out and back in for this change to take effect.

Quick notes:

  • If evdev doesn't work, some Android gamepads use HID raw reports. You'd need to send HID feature reports directly, but that's less common — start with the evdev method first.
  • Adjust strong_magnitude and weak_magnitude to tweak vibration intensity (0 = off, 65535 = max).

内容的提问来源于stack exchange,提问作者Regis May

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:52:59