我正在使用 Evdev 读取 A、B、X、Y 的 XBOX ONE 输入并取得了巨大成功。然而,我正在努力拿起模拟棒输入。任何人都可以帮助我使用 Python 代码吗?
我正在尝试控制伺服。
到目前为止,这是我的代码,它“完美”地工作。我需要知道如何读取(xbox)操纵杆的输出,这样我就可以为伺服系统使用“GPIO.PWM”。
from evdev import InputDevice, categorize, ecodes, KeyEvent
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BOARD)
GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(7, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(11, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(12, GPIO.OUT, initial=GPIO.LOW)
gamepad = InputDevice('/dev/input/event2')
#evdev takes care of polling the controller in a loop
for event in gamepad.read_loop():
if event.type == ecodes.EV_KEY:
keyevent = categorize(event)
if keyevent.keystate == KeyEvent.key_down:
if keyevent.keycode[0] == "BTN_A":
print "Button A Pressed"
GPIO.output (8, GPIO.HIGH)
elif keyevent.keycode[0] == "BTN_B":
print "Button B Pressed"
GPIO.output (7, GPIO.HIGH)
elif keyevent.keycode[0] == "BTN_WEST":
print "Button Y Pressed"
GPIO.output (11, GPIO.HIGH)
elif keyevent.keycode[0] == "BTN_NORTH":
print "Button X Pressed"
GPIO.output (12, GPIO.HIGH)
if keyevent.keystate == KeyEvent.key_up:
if keyevent.keycode[0] == "BTN_A":
print "Button A Released"
GPIO.output (8, GPIO.LOW)
elif keyevent.keycode[0] == "BTN_B":
print "Button B Released"
GPIO.output (7, GPIO.LOW)
elif keyevent.keycode[0] == "BTN_WEST":
print "Button Y Released"
GPIO.output (11, GPIO.LOW)
elif keyevent.keycode[0] == "BTN_NORTH":
print "Button X Released"
GPIO.output (12, GPIO.LOW)