展示HN:我写了一个小库,可以将USB游戏手柄转换为蓝牙手柄。
Show HN: I wrote a small lib to turn a USB gamepad into a Bluetooth one

原始链接: https://github.com/skorokithakis/bluetooth-gamepad

该ESP32-S3固件将有线USB游戏手柄桥接到无线蓝牙低功耗(BLE)连接。只需将USB游戏手柄插入Seeed Studio XIAO ESP32S3板(如果需要,使用USB-C OTG适配器或供电集线器),固件就会广播一个BLE信号,允许与手机、平板电脑或电脑配对。 该项目使用PlatformIO构建,处理不同游戏手柄HID报告格式的复杂性。它将按钮布局标准化为一致的内部表示,然后将其转换为标准的BLE HID游戏手柄报告。这种解耦允许广泛的游戏手柄兼容性,而无需特定的设备驱动程序。 开发通过PlatformIO命令进行简化,用于构建、烧录和串口监视。可以通过调整`hid_parser.cpp`中的设置来映射独特的游戏手柄按钮配置,并借助调试日志来识别按钮映射。本质上,它从几乎任何有线控制器创建通用的BLE游戏手柄信号。

一位开发者在Hacker News分享了一个项目,使用ESP32-S3微控制器将USB游戏手柄转换为蓝牙。该方案允许用户无线连接旧式有线手柄到电脑,实现远程控制。它涉及通过USB OTG线将游戏手柄连接到ESP32-S3,并通过蓝牙配对ESP32。 该项目引发了讨论,一位用户询问了ESP32开发的初学者资源,作为树莓派的替代方案。其他人称赞其巧妙实用的设计,特别是其对不同手柄的适应性和简单的硬件设置——基本上就是将USB端口焊接到ESP32-S3上。重要的是,开发者报告说与Xbox One手柄相比,没有明显延迟,并且有人建议使用视频录制来测量延迟。该项目的代码可在GitHub上找到。
相关文章

原文

ESP32-S3 firmware that turns a USB HID gamepad into a Bluetooth LE (BLE) gamepad. Plug a wired controller into the board (USB Host), and the firmware advertises as a BLE gamepad so a phone/tablet/PC can connect to it wirelessly.

  • Seeed Studio XIAO ESP32S3 (the current PlatformIO target: seeed_xiao_esp32s3)
  • A USB HID gamepad/controller (wired USB)
  • A USB-C OTG adapter (USB-C male → USB-A female), or a powered USB hub + suitable cables
  • Optional: a 3.3V UART adapter for debug logs on pins GPIO12 (TX) / GPIO13 (RX) (see platformio.ini)

Power note: some controllers draw more current than the XIAO can comfortably supply. If your controller doesn’t enumerate or resets, use a powered hub.

  1. Flashing / development: connect the XIAO ESP32S3 to your computer via USB-C.
  2. Runtime (USB Host): connect your USB controller to the XIAO’s USB-C port through an OTG adapter (or powered hub).
  3. Pair the BLE device from your phone/tablet/PC like a normal Bluetooth controller.

This is a PlatformIO project (platformio.ini). The default environment enables USB Host (OTG) and routes debug output to a hardware UART.

  • Build: pio run -e seeed_xiao_esp32s3
  • Flash: pio run -e seeed_xiao_esp32s3 -t upload
  • Serial monitor (if you use UART): pio device monitor -b 115200

There is also a development environment that disables USB Host and keeps USB CDC enabled (useful for quick flashing/monitoring over the built-in USB serial):

  • Flash (dev): pio run -e seeed_xiao_esp32s3_dev -t upload

The project uses a custom partition table referenced by platformio.ini:

At a high level, the firmware runs a USB Host stack to enumerate a connected HID controller, parses the HID reports into a normalized internal GamepadState, then maps that state into BLE HID gamepad reports exposed via the BLE gamepad library. The USB side and BLE side are decoupled through this state representation so HID parsing and BLE report generation stay independent and easy to evolve.

USB HID gamepads are not required to agree on which physical button is “Button 1/2/3/4”, and many devices use different ordering for face buttons and meta buttons (Start/Select/L3/R3/etc). Operating systems often “recognize” a controller because their HID drivers and controller databases (quirks/hwdb/SDL mappings) contain device-specific mappings.

This firmware is acting as a bridge: it reads the controller’s raw HID reports and then exposes a generic BLE HID gamepad report descriptor. The project normalizes all controllers into a canonical internal button layout (GamepadButton in src/gamepad_state.h), then maps that canonical layout into the BLE button indices that Linux maps to BTN_* codes (canonical_button_to_ble_button in src/ble_gamepad.cpp). If your controller’s HID “Button N” ordering differs, adjust the HID→canonical mapping in src/hid_parser.cpp (hid_button_number_to_canonical_mask). Use the debug log “Button bit N pressed” output to see which internal bit toggled.

  • src/usb_host.*: USB Host setup and device/report handling
  • src/hid_parser.*: HID report parsing into a usable controller state
  • src/gamepad_state.h: shared representation of buttons/axes
  • src/ble_gamepad.*: BLE gamepad setup and report sending
  • src/main.cpp: initialization and main loop orchestration
联系我们 contact @ memedata.com