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

基于Raspberry Pi Pico和CircuitPython的脚踏板定制问题

Raspberry Pi Pico脚踏板:将Gamepad改为Footpedal的解决方案

1. 修改设备识别为Footpedal系列

要让PC将设备识别为Footpedal而非Gamepad,需修改boot.py中的USB HID配置,包括自定义报告描述符和设备名称:

修改后的boot.py示例

import usb_hid

# 自定义Footpedal的HID报告描述符(对应4个脚踏板按键)
FOOTPEDAL_REPORT_DESCRIPTOR = bytes((
    0x05, 0x01,        # Usage Page (Generic Desktop)
    0x09, 0x05,        # Usage (Game Pad)
    0xA1, 0x01,        # Collection (Application)
    0x15, 0x00,        # Logical Minimum (0)
    0x25, 0x01,        # Logical Maximum (1)
    0x35, 0x00,        # Physical Minimum (0)
    0x45, 0x01,        # Physical Maximum (1)
    0x75, 0x01,        # Report Size (1)
    0x95, 0x04,        # Report Count (4) -> 对应FOOTPEDAL1到FOOTPEDAL4
    0x05, 0x09,        # Usage Page (Button)
    0x19, 0x01,        # Usage Minimum (Button 1)
    0x29, 0x04,        # Usage Maximum (Button 4)
    0x81, 0x02,        # Input (Data,Var,Abs)
    0xC0,              # End Collection
))

# 创建自定义Footpedal HID设备
footpedal = usb_hid.Device(
    report_descriptor=FOOTPEDAL_REPORT_DESCRIPTOR,
    usage_page=0x01,
    usage=0x05,
    report_ids=(0,),
    in_report_lengths=(1,),
    out_report_lengths=(0,),
)

# 启用自定义Footpedal设备(替换原Gamepad)
usb_hid.enable((footpedal,))

# 设置USB设备显示名称
usb_hid.set_device_name("Footpedal Controller")

核心修改:

  • 自定义报告描述符,限定仅支持4个脚踏板按键
  • 替换默认Gamepad设备为自定义Footpedal设备
  • 修改设备名称为Footpedal相关,确保系统识别时显示正确

2. 取消自动循环,绑定GP0到FOOTPEDAL1

修改main.py,移除自动循环输出逻辑,改为检测GP0引脚状态触发FOOTPEDAL1信号:

修改后的main.py示例

import board
import digitalio
import usb_hid
from adafruit_hid.gamepad import Gamepad

# 初始化Footpedal(复用Gamepad类适配自定义报告描述符)
footpedal = Gamepad(usb_hid.devices)

# 配置GP0为输入引脚(带内部上拉,适配脚踏板按下接地的逻辑)
gp0_switch = digitalio.DigitalInOut(board.GP0)
gp0_switch.direction = digitalio.Direction.INPUT
gp0_switch.pull = digitalio.Pull.UP

# 记录脚踏板状态,避免重复发送信号
last_state = True

while True:
    current_state = gp0_switch.value
    # 脚踏板按下(状态从高变低)
    if not current_state and last_state:
        footpedal.press_buttons(1)  # 发送FOOTPEDAL1按下信号
    # 脚踏板松开(状态从低变高)
    elif current_state and not last_state:
        footpedal.release_buttons(1)  # 发送FOOTPEDAL1松开信号
    last_state = current_state

核心修改:

  • 删除原循环遍历输出GAMEPAD1-16的代码
  • 将GP0配置为带内部上拉的输入引脚,适配脚踏板的物理触发逻辑
  • 仅在脚踏板状态变化时发送按键信号,避免无效重复触发

验证效果

  1. 保存修改后的boot.py和main.py到Pico
  2. 重新连接Pico到PC,系统会识别为Footpedal Controller设备
  3. 使用HID测试工具(如Windows设备管理器、Linux的evtest)验证:按下GP0对应的脚踏板时,仅触发FOOTPEDAL1的按键事件,无自动循环输出

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 04:45:01