espp/xbox_host

1.5.0

Latest
uploaded 10 hours ago
Wired Xbox controllers on an ESP32-S2/-S3/-P4 USB host port: Xbox 360 / X-Input and Xbox One / Series (GIP), decoded into gamepad_map's GamepadState, with rumble and the player LED

Readme

# Xbox Host Component

[![Badge](https://components.espressif.com/components/espp/xbox_host/badge.svg)](https://components.espressif.com/components/espp/xbox_host)

`espp::XboxHost` reads **wired Xbox controllers** plugged into an ESP32-S2 /
-S3 / -P4 USB host port:

- **X-Input**: Xbox 360 controllers, and the third-party controllers that speak
  their protocol (most controllers' "X-input" mode, e.g. 8BitDo's);
- **GIP**: Xbox One and Xbox Series controllers.

It decodes their input into gamepad_map's `espp::GamepadState`, drives their
rumble (`espp::RumbleState`: the two grips, and on GIP controllers the two
impulse triggers too) and, for X-Input, the player LED.

## Why a separate component

Neither family is a USB HID device: both present a vendor-specific interface
(X-Input: class `0xFF` / subclass `0x5D` / protocol `0x01`; GIP: `0xFF` /
`0x47` / `0xD0`) with an interrupt IN and an interrupt OUT endpoint, so a HID
host driver such as [`usb_host`](../usb_host)'s never sees them. `XboxHost` is
a second client of the ESP-IDF USB Host Library that claims those interfaces,
starts each controller (a GIP controller stays silent until the host powers it
on, and asks for its Xbox button presses to be acknowledged), decodes its
packets and sends it rumble.

`XboxHost` does **not** install the USB Host Library: `espp::UsbHost` (or your
own `usb_host_install()`) does, and `XboxHost` registers its client alongside,
so HID devices and Xbox controllers share the port. Controllers already
attached when `XboxHost` initializes are picked up too.

## Usage

```cpp
espp::UsbHost usb_host({});            // installs the USB Host Library
std::error_code ec;
usb_host.initialize(ec);

espp::XboxHost xbox({
    .on_connected = [](const auto &c) { /* c->info(): vid, pid, protocol, product */ },
    .on_disconnected = [](const auto &c) { /* ... */ },
    .on_state = [](const auto &c, const espp::GamepadState &state) {
      // buttons by position (south = A), sticks -32767..32767 (Y down),
      // triggers 0..32767
    },
});
xbox.initialize(ec);
// later, from any task: controller->set_rumble(rumble); controller->set_player(2);
```

The callbacks run on XboxHost's task (the one that handles the USB events), so
keep them short. See the [example](./example).

## Wire formats

Header-only and host-tested (`test/`):

- `xinput.hpp`, from the [`xinput`](../xinput) component (shared with
  `espp::UsbDevice`'s X-Input function): the 20-byte input report, rumble and
  LED-ring reports;
- `gip.hpp`: GIP packet headers, input / Xbox button packets, the power-on,
  acknowledgement, rumble and LED packets;
- `xbox_gamepad_state.hpp`: both families to `GamepadState`, `RumbleState` to
  both families' rumble levels.

## Requirements

- An ESP32-S2 / -S3 / -P4 (USB-OTG host) whose port can source VBUS to the
  controller.
- The USB Host Library installed by something else (`espp::UsbHost` or
  `usb_host_install()`) before `XboxHost::initialize()`.

Links

Supports all targets

Maintainer

  • William Emfinger <waemfinger@gmail.com>
To add this component to your project, run:

idf.py add-dependency "espp/xbox_host^1.5.0"

download archive

Stats

  • Archive size
    Archive size ~ 21.70 KB
  • Downloaded in total
    Downloaded in total 0 times
  • Downloaded this version
    This version: 0 times

Badge

espp/xbox_host version: 1.5.0
|