Skip to content

feat(xbox_host): Xbox and Switch Pro controllers on a USB host port (xbox_host, xinput, SwitchProHost) - #966

Open
finger563 wants to merge 2 commits into
mainfrom
feat/usb-host-gamepads
Open

finger563 wants to merge 2 commits into
mainfrom
feat/usb-host-gamepads

Conversation

@finger563

Copy link
Copy Markdown
Contributor

Summary

espp can emulate a Switch Pro controller (switch_pro) and an Xbox 360 controller (usb_device's X-Input function) to a host. A board with a USB host port could read neither kind of controller, though. A Switch Pro controller reports nothing useful until a host runs its handshake, and Xbox controllers are not HID devices at all. This PR adds both, as components other projects can use.

xbox_host (new) espp::XboxHost: wired Xbox controllers on a USB host port. It reads both X-Input pads (Xbox 360, and third-party controllers in X-input mode) and GIP pads (Xbox One / Series).
switch_pro espp::SwitchProHost: the host side of the existing emulator. It reads a real Pro controller.
xinput (new, header-only) The X-Input wire format, moved out of usb_device so the device and host sides share it.
hid-rp encode_switch_rumble(), the inverse of decode_switch_rumble().

Both drivers decode into gamepad_map's espp::GamepadState, and both take espp::RumbleState for rumble.

espp::XboxHost

Xbox controllers present a vendor-specific interface, not a HID one: X-Input is 0xFF/0x5D/0x01 and GIP is 0xFF/0x47/0xD0, each with interrupt IN and OUT endpoints. So UsbHost's HID driver never sees them.

  • A second client: XboxHost is a second USB Host Library client next to UsbHost's HID driver. Both share the same port, and controllers already attached when it starts are picked up too. It doesn't install the library itself: espp::UsbHost does that, or the application's own usb_host_install().
  • Starting the controller: it claims the interface and starts the controller. A GIP pad stays silent until the host powers it on, and wants its Xbox-button presses acknowledged. A Series pad's Share button maps to Capture.
  • Output: it drives rumble (two grips, plus the trigger motors on GIP) and, on X-Input pads, the player LED.
  • Tested formats: the wire formats are header-only and covered by host tests: gip.hpp, and xbox_gamepad_state.hpp for both directions of conversion.

espp::SwitchProHost

  • Handshake: the 0x80 init commands, then subcommands for:
    • full input mode;
    • the stick calibration from the controller's SPI flash (factory, then user);
    • vibration;
    • player lights.
  • Missing replies: a step whose reply is late is resent, and one that's never answered is skipped.
  • Calibration fallback: a calibration that can't be read, or that's implausible, is replaced with typical values. Some third-party controllers that present themselves as 057E:2009 store the whole 12-bit range, which would keep their sticks from ever reaching full deflection.
  • Transport: like SwitchPro, it is transport-light. UsbHost::HidDevice::send_output_report() matches its send hook. The handshake, calibration, decoding and rumble are host-tested against a fake controller.

Packaging

  • usb_device now requires xinput. The 16 examples that build usb_device list it among their component directories.
  • New examples: xbox_host/example and switch_pro/host_example, both ESP32-S3 with espp::UsbHost. Both are in the CI build matrix.
  • Uploads: xbox_host and xinput are in upload_components.yml.
  • Docs: a new Xbox host page, a host section on the Switch Pro page, and the Doxygen inputs.
  • Temporary overrides: until espp/xinput is on the registry, four examples carry a main/idf_component.yml that points at the in-repo copy, as lilygo-t5-47 did before. Three of them build with the component manager on: canopen/can_bridge_example, odrive_native/usb_example and switch_pro/example. The fourth is the new xbox_host/example. These files can be deleted after the release.

Testing

  • Host tests: new ones for xinput, xbox_host, switch_pro (the SwitchProHost handshake) and hid-rp (rumble round-trip). All 41 host-test programs pass with Apple clang and with g++-16, under the CI flags (-Wall -Wextra -Werror).
  • Example builds: the new examples build for the ESP32-S3. So do the affected existing examples, built the way CI builds them:
    • usb_device/xinput_example and canopen/can_bridge_example with the component manager off;
    • odrive_native/usb_example and switch_pro/example with it on.
  • On hardware (M5Stack Tab5, through tab5-emu, whose integration follows once this is released):
    • a Switch Pro-protocol controller: handshake, input and calibration fallback, played in Jedi Knight;
    • an 8BitDo Pro 2 in X-input mode (045e:028e) through XboxHost;
    • HID pads (PS5, Backbone, 8BitDo D-input) alongside it, through UsbHost.
  • Not yet tested on hardware: a genuine Xbox One / Series (GIP) controller. GIP is covered by host tests only.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY

…xbox_host, xinput, SwitchProHost)

espp could emulate a Switch Pro controller (switch_pro) and an Xbox 360
controller (usb_device's X-Input function) to a host, but a board with a USB
host port could read neither kind of controller: a Switch Pro controller
reports nothing useful until a host runs its handshake, and Xbox controllers
are not HID devices at all.

- xinput (new, header-only): the X-Input wire format, moved out of usb_device
  so the device and host sides share it. It now also parses a controller's
  input report and builds the rumble and LED-ring reports a host sends.
  usb_device requires it; the examples that build usb_device list it.
- xbox_host (new): espp::XboxHost, a USB Host Library client beside
  espp::UsbHost's HID driver that claims X-Input (Xbox 360, third-party
  X-input mode) and GIP (Xbox One / Series) interfaces, starts each
  controller (GIP power-on, acknowledging the Xbox button), decodes its input
  into gamepad_map's GamepadState and drives rumble and the player LED.
  gip.hpp and xbox_gamepad_state.hpp are header-only and host-tested.
- switch_pro: espp::SwitchProHost, the host side of the emulator. It runs the
  USB handshake, reads the stick calibration from the controller's SPI flash
  (typical values for one it cannot read or that is implausible, as some
  third-party controllers store the whole range), decodes the reports into
  GamepadState, and drives rumble and the player lights. It is
  transport-light like SwitchPro (UsbHost::HidDevice is the intended
  pairing) and host-tested against a fake controller.
- hid-rp: encode_switch_rumble(), the inverse of decode_switch_rumble().
- Examples: xbox_host/example and switch_pro/host_example (ESP32-S3, with
  espp::UsbHost), in the CI build matrix; docs pages and Doxygen inputs.
- Until espp/xinput is on the registry, the examples that build usb_device
  with the component manager on carry a main/idf_component.yml that points
  at the in-repo copy (delete them after the release, as for lilygo-t5-47).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
Copilot AI balanced review requested due to automatic review settings December 2, 2026 04:18
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

✅Static analysis result - no issues found! ✅

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

1 open issue
What changed in this PR

Adds USB host-side support for reading real Nintendo Switch Pro controllers and wired Xbox controllers (X-Input + GIP) on ESP32 USB host ports, and factors out X-Input wire format helpers into a reusable header-only component shared by device + host.

Changes:

  • Add new xbox_host component (library + docs + example + host tests) for wired Xbox 360/X-Input and Xbox One/Series GIP controllers.
  • Add SwitchProHost to switch_pro (library + docs + host example + host tests) to perform the Pro controller handshake/calibration and decode input.
  • Introduce new header-only xinput component (wire format + host tests) and extend hid-rp with encode_switch_rumble() + round-trip tests.
File Description
doc/​en/​hid/​switch_pro_host_example.md Adds docs page that includes the host example README.
doc/​en/​hid/​switch_pro.rst Documents SwitchProHost, links host example + API include.
doc/​en/​buses/​xbox_host_example.md Adds docs page that includes the Xbox host example README.
doc/​en/​buses/​xbox_host.rst Adds new Xbox host driver documentation + toctree + API includes.
doc/​en/​buses/​index.rst Adds xbox_host to buses doc index.
doc/​Doxyfile Adds new examples/inputs for SwitchProHost, XboxHost, xinput wiring headers.
components/​xinput/​test/​xinput_host_test.cpp Expands xinput host tests (parse input report + build rumble/LED reports).
components/​xinput/​test/​CMakeLists.txt Registers xinput host test target.
components/​xinput/​include/​xinput.hpp Adds host-side parsing plus host-built rumble/LED report builders.
components/​xinput/​idf_component.yml Adds component manager manifest for xinput.
components/​xinput/​README.md Documents xinput component usage and wire format.
components/​xinput/​CMakeLists.txt Registers header-only component.
components/​xbox_host/​test/​xbox_host_test.cpp Adds host tests for GIP parsing/formatting and conversions to/from Gamepad/Rumble state.
components/​xbox_host/​test/​CMakeLists.txt Registers xbox_host host test target.
components/​xbox_host/​src/​xbox_host.cpp Implements XboxHost USB Host Library client, device claiming, transfers, parsing, rumble/LED output.
components/​xbox_host/​include/​xbox_host.hpp Declares XboxHost API, controller info/state callbacks, and configuration.
components/​xbox_host/​include/​xbox_gamepad_state.hpp Adds conversion helpers between xinput/gip wire states and GamepadState/RumbleState.
components/​xbox_host/​include/​gip.hpp Adds dependency-free GIP wire parsing/building helpers.
components/​xbox_host/​idf_component.yml Adds component manager manifest for xbox_host.
components/​xbox_host/​example/​sdkconfig.defaults Sets ESP32-S3 host-console defaults for example.
components/​xbox_host/​example/​main/​xbox_host_example.cpp Adds XboxHost example using UsbHost + rumble demo.
components/​xbox_host/​example/​main/​idf_component.yml Adds temporary override for in-repo xinput until registry release.
components/​xbox_host/​example/​main/​CMakeLists.txt Registers example main component requirements.
components/​xbox_host/​example/​README.md Documents Xbox host example setup and output.
components/​xbox_host/​example/​CMakeLists.txt Defines example project + component directories.
components/​xbox_host/​README.md Documents XboxHost usage, rationale, and wire formats.
components/​xbox_host/​CMakeLists.txt Registers xbox_host component and dependencies.
components/​wdi/​usb_example/​CMakeLists.txt Adds xinput to example component dirs (usb_device dependency change).
components/​vl53l/​vl53l5cx_webapp_example/​CMakeLists.txt Adds xinput to example component dirs (usb_device dependency change).
components/​usb_device/​xinput_example/​CMakeLists.txt Adds xinput to example component dirs.
components/​usb_device/​test/​CMakeLists.txt Removes xinput host test from usb_device tests (moved to xinput component).
components/​usb_device/​msc_example/​CMakeLists.txt Adds xinput to example component dirs.
components/​usb_device/​idf_component.yml Declares new dependency on espp/xinput.
components/​usb_device/​example/​CMakeLists.txt Adds xinput to example component dirs.
components/​usb_device/​README.md Updates docs to reference xinput component for X-Input wire definitions.
components/​usb_device/​CMakeLists.txt Adds xinput to usb_device component requirements.
components/​telemetry/​example/​CMakeLists.txt Adds xinput to example component dirs (usb_device dependency change).
components/​system/​example/​CMakeLists.txt Adds xinput to example component dirs (usb_device dependency change).
components/​switch_pro/​test/​switch_pro_host_test.cpp Adds host tests for SwitchProHost handshake, calibration, decoding, rumble, lights.
components/​switch_pro/​test/​CMakeLists.txt Registers switch_pro host test target.
components/​switch_pro/​src/​switch_pro_host.cpp Implements SwitchProHost handshake/state decoding/rumble/lights.
components/​switch_pro/​include/​switch_pro_host.hpp Declares SwitchProHost API and documents protocol/usage.
components/​switch_pro/​idf_component.yml Updates switch_pro manifest to include host example + gamepad_map dependency.
components/​switch_pro/​host_example/​sdkconfig.defaults Sets ESP32-S3 host-console defaults for host example.
components/​switch_pro/​host_example/​main/​switch_pro_host_example.cpp Adds SwitchProHost + UsbHost integration example.
components/​switch_pro/​host_example/​main/​CMakeLists.txt Registers host example main component requirements.
components/​switch_pro/​host_example/​README.md Documents Switch Pro host example setup and output.
components/​switch_pro/​host_example/​CMakeLists.txt Defines host example project + component directories.
components/​switch_pro/​example/​main/​idf_component.yml Adds temporary override for in-repo xinput until registry release.
components/​switch_pro/​example/​CMakeLists.txt Adds required component dirs (including xinput) for updated deps.
components/​switch_pro/​README.md Adds SwitchProHost documentation and requirements.
components/​switch_pro/​CMakeLists.txt Adds gamepad_map dependency for SwitchProHost public headers.
components/​ota/​example/​CMakeLists.txt Adds xinput to example component dirs (usb_device dependency change).
components/​odrive_native/​usb_example/​main/​idf_component.yml Adds temporary override for in-repo xinput until registry release.
components/​odrive_native/​usb_example/​CMakeLists.txt Adds xinput to example component dirs (usb_device dependency change).
components/​mcp266/​webapp_example/​CMakeLists.txt Adds xinput to example component dirs (usb_device dependency change).
components/​hid-rp/​test/​hid_rp_rumble_host_test.cpp Adds round-trip tests for Switch rumble encode/decode.
components/​hid-rp/​include/​hid-rp-rumble.hpp Adds encode_switch_rumble() inverse helper for host rumble packets.
components/​gamepad_map/​webapp_example/​CMakeLists.txt Adds xinput to example component dirs (usb_device dependency change).
components/​desktop/​example/​CMakeLists.txt Adds xinput to example component dirs (usb_device dependency change).
components/​coredump/​example/​CMakeLists.txt Adds xinput to example component dirs (usb_device dependency change).
components/​canopen/​can_bridge_example/​main/​idf_component.yml Adds temporary override for in-repo xinput until registry release.
components/​canopen/​can_bridge_example/​CMakeLists.txt Adds xinput to example component dirs (usb_device dependency change).
components/​bldc_haptics/​example/​CMakeLists.txt Adds xinput to example component dirs (usb_device dependency change).
.github/​workflows/​upload_components.yml Adds xbox_host and xinput to upload list.
.github/​workflows/​build.yml Adds build matrix entries for new host examples.

Review effort: Lite

const int32_t range = v >= 0 ? a.above : a.below;
if (range <= 0)
return 0;
return v * GamepadState::AXIS_MAX / range;
…omponents

- switch_pro_host.cpp: the SPI reply's debug log writes its bytes as hex by
  hand; fmt's range formatter tripped MSVC's unreachable-code warning (C4702,
  an error under /WX) in fmt/ranges.h. An inline suppression for the 'ready'
  flag, which finish() sets under the lock where cppcheck does not follow it.
- gip.hpp: Sequence::next() without a condition (1..255, then 1 again).
- xbox_host.cpp: std::copy_if / any_of / find_if instead of raw loops.
- switch_pro_host_test.cpp: swap instead of move-then-clear, count_if /
  transform; and, as for the WDI host tests, file-level suppressions for the
  vectors its fake controller's callbacks fill, which cppcheck reports as
  always empty.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants