我正在为我的电脑构建自己的脚踏板,并让它或多或少地工作。我可以发送 he 代码,使其充当键盘,但我希望它显示为 FOOTPEDAL1 .. 2 .. 3 .. 4。我下载了此代码,其中有 16 个按钮,称为 GAMEPAD1 .. 2...
第一个问题:代码只是每秒输出一个 GAMEPAD1,然后计数到 16,然后重新开始。但我想用 GP0 引脚设置 FOOTPEDAL1。
第二个问题:代码将其命名为 GAMEPAD1...但我想要它 FOOTPEDAL1...
这是启动代码:
import usb_hid
# This is only one example of a gamepad descriptor, and may not suit your needs.
GAMEPAD_REPORT_DESCRIPTOR = bytes((
0x05, 0x01, # Usage Page (Generic Desktop Ctrls)
0x09, 0x05, # Usage (Game Pad)
0xA1, 0x01, # Collection (Application)
0x85, 0x04, # Report ID (4)
0x05, 0x09, # Usage Page (Button)
0x19, 0x01, # Usage Minimum (Button 1)
0x29, 0x10, # Usage Maximum (Button 16)
0x15, 0x00, # Logical Minimum (0)
0x25, 0x01, # Logical Maximum (1)
0x75, 0x01, # Report Size (1)
0x95, 0x10, # Report Count (16) ## 2 bytes
0x81, 0x02, # Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0xC0, # End Collection ## total 2 bytes
))
gamepad = usb_hid.Device(
report_descriptor=GAMEPAD_REPORT_DESCRIPTOR,
usage_page=0x01, # Generic Desktop Control ## Must match Usage Page above!
usage=0x05, # Gamepad ## Must match Usage above!
report_ids=(4,), # Descriptor uses report ID 4. ## Must match Report ID above!
in_report_lengths=(2,), # This gamepad sends 2 bytes in its report. ## Must match number of bytes above!
out_report_lengths=(0,), # It does not receive any reports.
)
usb_hid.enable(
(usb_hid.Device.KEYBOARD,
usb_hid.Device.MOUSE,
usb_hid.Device.CONSUMER_CONTROL,
gamepad)
)
这是主要代码:
import time
import struct
import usb_hid
import adafruit_hid
gamepad_device = adafruit_hid.find_device(usb_hid.devices, usage_page=0x1, usage=0x05)
print("gamepad_device:",gamepad_device)
def send_gamepad_report(button_state):
report = bytearray(2) ## must be same size as specified in HID Report Descriptor in boot.py
#report_id = 4 ## must match what's in HID Report Descriptor in boot.py
struct.pack_into(
"<H", # little endian, one 2-byte value
report,
0,
button_state
)
print(["%02x" % x for x in report])
gamepad_device.send_report(report)
# utility to help us set & clear bits in the button_state
def set_bit(v,i):
v |= (1 << i)
return v
def clr_bit(v,i):
v &= ~(1 << i)
return v
button_state = 0x00 # holds the state of our buttons
button_num = 0 # which button are we current toggling
# while True:
print("pushing button ", (button_num+1)) # human buttons start at one not zero
# push button
button_state = set_bit(button_state, button_num)
send_gamepad_report(button_state)
time.sleep(1)
print("releasing button ", (button_num+1)) # human buttons start at one not zero
# release button
button_state = clr_bit(button_state, button_num)
send_gamepad_report(button_state)
time.sleep(1)
# go to next button
button_num = (button_num+1) % 16
我会很高兴得到回复!
它会给出从 1 到 16 的所有 GAMEPAD。它每秒加 1。 只要我将 3.3V 引脚连接到任何 GP 引脚,它就会输出当前的 GAMEPAD(数字)。
嗯,代码按预期工作。主循环仅用于测试启动代码与 send_gamepad_report() 函数的功能。
目前还没有代码可以检查真实的按钮。这就是你必须提供的。
您的新主循环应如下所示:
HTH