Skip to content

feat(hid-rp, gamepad_map, switch_pro, usb_device): descriptor-driven rumble building blocks - #927

Merged
finger563 merged 3 commits into
mainfrom
feat/rumble
Nov 24, 2026
Merged

finger563 merged 3 commits into
mainfrom
feat/rumble

Conversation

@finger563

@finger563 finger563 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

The first phase of generic rumble for controller bridges, in espp. Every piece is header-only and host-tested:

  • decode a host's rumble request into one normalised state;
  • drive a controller's motors from its own report descriptor, not a known VID/PID.

The dongle (esp-usb-ble-hid) is the first user, in a follow-up. It builds on the decode → latest-wins queue → sender structure from esp-usb-ble-hid#43.

What's here

hid-rp: Output fields and a descriptor-driven rumble encoder

  • Output fields: ReportMap now maps them too (output_fields(), output_report_bytes()), and ReportField carries its unit and unit exponent. The input side is untouched: a separate field list and bit cursors, and only input report IDs set uses_report_ids(), which split() relies on.
  • The encoder: RumbleEncoder::from_report_map() (new hid-rp-rumble.hpp) finds a Physical Interface Device style output report: DC Enable Actuators, one Magnitude per actuator, Duration, Start Delay and Loop Count. The Xbox Wireless controller declares exactly that over BLE. The descriptor gives each field's position, size, range and unit, so Duration is scaled correctly: 10 ms units for the Xbox, milliseconds when no time unit is declared.
  • Motor order: the descriptor doesn't say which Magnitude drives which motor, so that follows the convention Linux's hid-microsoft driver uses for Xbox controllers. Four actuators are left trigger, right trigger, strong, weak; enable bit i drives actuator n−1−i. Two actuators are strong, weak. set_motor() overrides the order for a device that differs.
  • Effect length: an effect plays once for its duration, with Start Delay and Loop Count 0. Callers re-send while the rumble lasts, so a lost stop report is bounded by one effect instead of running on.

hid-rp: Switch host decoding

  • decode_switch_rumble() / decode_switch_rumble_report() read the two motor levels from a Switch host's HD rumble. That's both bands, with the amplitude code inverted using dekuNukem's rumble table rather than read linearly.
  • Output reports 0x01 and 0x10 both lay out [id][packet number][8 rumble bytes]. This matches switch_pro's own subcommand offset of 10.

gamepad_map: RumbleState

  • rumble_state.hpp is the output counterpart of GamepadState: strong, weak and two trigger motors, 0..255.
  • to_rumble_levels() and to_rumble_state() bridge it to hid-rp, in gamepad_state_hid_rp.hpp.
  • It's self-contained, so it can move to its own rumble component later. It's also in the desktop umbrella header.

switch_pro: Config::on_rumble is called from on_hid_report() for every 0x01 and 0x10 report, which replaces the 0x10 TODO.

usb_device: xinput::decode_rumble() reads the X-Input OUT endpoint's rumble reports and returns nullopt for LED reports.

usb_device: UsbDevice::set_suspend_callback() / set_resume_callback() forward esp_tinyusb's suspend / resume events, which were dropped. A bridge stops the controller's rumble when a PC sleeps without sending a stop.

Tests

hid_rp_rumble_host_test, built with -Werror under g++ 16 and clang, covers:

  • the Xbox descriptor's output fields at their offsets;
  • the encoder's output byte for byte against espp's hand-written XboxRumbleOutputReport;
  • the trigger motors, duration clamping, the stop report, and set_motor();
  • a synthetic two-motor descriptor: 16-bit magnitudes, a 1-bit enable flag and Duration in milliseconds;
  • no encoder for the Switch Pro descriptor, whose outputs are constant vendor bytes;
  • Switch decoding: neutral, full, monotonic across the segment boundaries, and the packet-number offset.

The gamepad_map and xinput host tests are extended, and the four existing hid-rp tests still pass. The switch_pro, xinput_example, gamepad_map and hid-rp examples build for ESP32-S3 with IDF 6.1.

Next

  • The dongle: use RumbleEncoder instead of a hard-coded PID gate, plus a reusable sender (dedupe, short pulses re-sent while on, stop on disconnect / unmount / suspend, handle-based writes). The console gets "rumble supported" and a test button.
  • Rumble options in the button-map profiles: off, strength, swap motors.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU

…rumble building blocks

The first step of generic rumble for controller bridges: decode a host's rumble
request into one normalised state, and drive a controller's motors from its own
report descriptor rather than a known VID/PID.

- hid-rp ReportMap maps Output fields too (output_fields(), output_report_bytes()),
  each with its unit and unit exponent. Inputs are unchanged: their own field
  list and bit cursors, and only input report IDs set uses_report_ids().
- hid-rp-rumble.hpp (new):
  - RumbleEncoder::from_report_map() finds a Physical Interface Device style
    output report (DC Enable Actuators, a Magnitude per actuator, Duration,
    Start Delay, Loop Count), as the Xbox Wireless controller declares over
    BLE. The descriptor gives the layout. Which Magnitude drives which motor
    follows the Xbox convention (Linux hid-microsoft: LT, RT, strong, weak;
    enable bit i = actuator n-1-i), with set_motor() to override. An effect
    plays once for its duration (clamped to the field), so callers re-send
    while the rumble lasts and a lost stop is bounded.
  - decode_switch_rumble[_report]() reads a Switch host's HD rumble (both
    bands, the amplitude code inverted per dekuNukem's table) from output
    reports 0x01 and 0x10, both [id][packet number][8 rumble bytes].
- gamepad_map: RumbleState (rumble_state.hpp), the output counterpart of
  GamepadState (strong / weak / two trigger motors, 0..255), plus the
  to_rumble_levels() / to_rumble_state() bridges in gamepad_state_hid_rp.hpp.
  It is also in the desktop umbrella header.
- switch_pro: Config::on_rumble, called from on_hid_report() for every 0x01 /
  0x10 report (replaces the 0x10 TODO).
- usb_device: xinput::decode_rumble() for the X-Input OUT endpoint (rumble
  reports; LED reports are not rumble).

Tests (host, -Werror, g++ and clang):
- hid_rp_rumble_host_test: the Xbox descriptor's output fields at their
  offsets, its encoder byte for byte against espp's hand-written
  XboxRumbleOutputReport, a synthetic 2-motor layout (16-bit magnitudes, a
  1-bit enable flag, ms durations), no encoder for the Switch Pro descriptor
  (constant vendor outputs), Switch decoding (neutral, full, monotonic, the
  packet-number offset);
- gamepad_map and xinput tests extended.

The existing hid-rp tests still pass. The switch_pro, xinput, gamepad_map and
hid-rp examples build for ESP32-S3 (IDF 6.1). Docs: READMEs, docs pages and
the Doxyfile entries for the new headers.

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

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

✅Static analysis result - no issues found! ✅

finger563 and others added 2 commits November 23, 2026 15:29
UsbDevice::set_suspend_callback() / set_resume_callback() forward esp_tinyusb's
TINYUSB_EVENT_SUSPENDED / RESUMED, which the event callback used to drop. On
suspend the device stays powered and configured, so an app can stop what the
host was driving: a controller bridge stops the controller's rumble when a PC
goes to sleep without sending a stop. Same pattern as the mount / unmount
callbacks (run in the TinyUSB task, cb_mutex_-guarded).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU
- hid-rp ReportMap::Parser: the local field list was named `fields`, which
  shadowed ReportMap::fields(). It is now `dest_fields`.
- xinput host test: the decoded rumble no longer shadows the outer report `r`.
- hid-rp rumble test: find_output() uses std::find_if instead of a raw loop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU
@finger563
finger563 merged commit 39050fe into main Nov 24, 2026
191 checks passed
@finger563
finger563 deleted the feat/rumble branch November 24, 2026 21:58
finger563 added a commit to finger563/esp-usb-ble-hid that referenced this pull request Nov 25, 2026
* feat: descriptor-driven rumble on any controller that declares it

Generalises the rumble forwarding from #43 (decode on the TinyUSB task, a
latest-wins sender with keep-alive, handle-based writes, stop on unmount /
suspend) with espp's new rumble pieces (esp-cpp/espp#927):

- Which controllers rumble: an espp::hid_rp::RumbleEncoder built from the
  controller's OWN report map when it connects, replacing the 045E:0B13
  gate. Any controller whose map declares a PID-style rumble report (DC Enable
  Actuators, a Magnitude per motor, Duration...) is driven: the Xbox Wireless
  Series / One S / Elite over BLE, and lookalikes. A map that could not be read
  (decoded as an Xbox controller) is not assumed to rumble like one, so
  nothing is written to an output report the controller did not declare.
- Host side: the Switch's rumble through SwitchPro::Config::on_rumble, with
  HD rumble decoded by espp hid-rp using the amplitude table (replaces the
  local switch_rumble.hpp + its host test, which CI never ran); the PC's
  X-Input rumble through xinput::decode_rumble. Both become an
  espp::RumbleState.
- main/rumble.cpp, the sender (replaces the ble.cpp output-report queue):
  - the newest target wins, an unchanged one is not re-sent, writes are at
    least 30 ms apart;
  - each report plays a 300 ms pulse, re-sent every 150 ms while the motors
    run; the stop is sent three times;
  - the motors stop when the controller changes or disconnects, and when the
    USB host unmounts or suspends (UsbDevice suspend callback from espp#927,
    replacing the tud_suspended() poll in the HID sender).
- BLE: ble_has_output_report() / ble_write_output_report() (write without
  response when the characteristic takes it, otherwise an async write; only
  writable output characteristics are recorded).
- Console: INFO carries an optional rumble_supported byte (absent from older
  firmware: "not reported"). The Device tab shows a Rumble row. ACTION 0x04
  TEST_RUMBLE plays a 400 ms pulse (Test rumble button), with ERROR when the
  controller cannot rumble.
- Docs: README, controllers / hosts / troubleshooting / web-console /
  button-mapping / architecture describe the descriptor-driven behaviour.

Tests: device_config host test (rumble byte present / absent, TestRumble id)
and the console node test (16 groups) pass. On hardware (T-Dongle-S3 in Xbox
360 mode, against espp#927 via a local override_path, not committed), with
espp's hid_service example as the BLE gamepad logging every output report it
receives: rumble_supported from its report map; Test rumble, and X-Input
strong-only / weak-only / both / mixed levels arrive as the right enable bits
and magnitudes (200 -> 78, 120 -> 47, 255 -> 100, 64 -> 25), 300 ms pulses
re-sent every ~150 ms for the length of the hold, each stop sent 3 times.

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

* build: espp 1.4.7 (descriptor-driven rumble pieces)

hid-rp, switch_pro, usb_device and gamepad_map >= 1.4.7: RumbleEncoder,
Switch HD rumble decode, xinput::decode_rumble, UsbDevice suspend / resume
callbacks and RumbleState all landed in espp 1.4.7 (esp-cpp/espp#927).
dependencies.lock regenerated from the registry.

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

* fix(rumble): match rumble_set_encoder's parameter name (cppcheck funcArgNamesDifferent)

The declaration said `encoder`, the definition `e` (the file already has an
`encoder` variable, which the parameter must not shadow): both are now
`new_encoder`.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant