Xbox Controller Host#
Overview#
espp::XboxHost reads wired Xbox controllers plugged into an ESP32-S2 /
-S3 / -P4 USB host port: Xbox 360 controllers and the third-party controllers
that speak their protocol (X-Input, as most controllers’ “X-input” mode does),
and Xbox One / Xbox Series controllers (GIP). It decodes their input into
gamepad_map’s espp::GamepadState and drives their rumble (espp::RumbleState)
and, for X-Input, the player LED.
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 espp::UsbHost’s never sees them. XboxHost is a second
client of the ESP-IDF USB Host Library that claims those interfaces: it 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.
The wire formats are header-only and host-tested: xinput.hpp (the xinput
component, shared with espp::UsbDevice’s X-Input function), gip.hpp and
xbox_gamepad_state.hpp (the conversions to and from GamepadState /
RumbleState).
API Reference#
Header File#
Classes#
-
class XboxHost : public espp::BaseComponent#
Wired Xbox controllers on a USB host port: Xbox 360 (X-Input, and most third-party controllers in their X-input mode) and Xbox One / Series (GIP).
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 never sees them. XboxHost is a client of the ESP-IDF USB Host Library that claims those interfaces: it starts each controller (GIP controllers stay silent until the host powers them on), decodes its input into gamepad_map’s GamepadState, and sends it rumble (gamepad_map’s RumbleState) and, for X-Input, the player LED.
XboxHost does not install the USB Host Library: something else on the port does (espp::UsbHost, which also runs the library’s event task, or your own usb_host_install()), and XboxHost registers its client alongside, so HID devices (via UsbHost) and Xbox controllers work on the same port. Controllers already attached when XboxHost initializes are picked up too.
The callbacks run on XboxHost’s own task, the one that handles the USB events, so keep them short (copy the state somewhere and return); Controller::set_rumble() / set_player() may be called from any task.
Example#
// espp::UsbHost installs the USB Host Library (and runs its event task); // XboxHost registers its own client alongside it, so HID devices and Xbox // controllers can share the port espp::UsbHost usb_host({.log_level = espp::Logger::Verbosity::WARN}); std::error_code ec; if (!usb_host.initialize(ec)) { logger.error("Failed to initialize the USB host: {}", ec.message()); return; } using Controller = espp::XboxHost::Controller; espp::XboxHost xbox({ .on_connected = [&](const std::shared_ptr<Controller> &c) { const auto &info = c->info(); logger.info("{} controller connected: '{}' '{}' {:04x}:{:04x}, player {}", espp::XboxHost::name(info.protocol), info.manufacturer, info.product, info.vid, info.pid, c->player()); }, .on_disconnected = [&](const std::shared_ptr<Controller> &c) { logger.info("controller {:04x}:{:04x} disconnected", c->info().vid, c->info().pid); }, .on_state = [&](const std::shared_ptr<Controller> &c, const espp::GamepadState &state) { logger.info("player {}: {}", c->player(), state); // rumble with the triggers: the left one drives the strong motor, // the right one the weak motor using A = espp::GamepadState::Axis; using M = espp::RumbleState::Motor; espp::RumbleState rumble; rumble.set(M::Strong, static_cast<uint8_t>(state.axis(A::LeftTrigger) >> 7)); rumble.set(M::Weak, static_cast<uint8_t>(state.axis(A::RightTrigger) >> 7)); c->set_rumble(rumble); }, .log_level = espp::Logger::Verbosity::INFO, }); if (!xbox.initialize(ec)) { logger.error("Failed to initialize the Xbox host: {}", ec.message()); return; }
Public Types
Public Functions
-
explicit XboxHost(const Config &config)#
Construct an XboxHost. Call initialize() to register it.
-
~XboxHost()#
Releases every controller and the client (deinitialize()).
-
bool initialize(std::error_code &ec)#
Register the USB Host Library client and start the event task. The library must be installed already (espp::UsbHost::initialize() or usb_host_install()).
- Parameters:
ec – Set on failure (the library not installed: invalid state).
- Returns:
true on success (or if already initialized).
-
bool deinitialize(std::error_code &ec)#
Release every controller, deregister the client and stop the task. Call it before the USB Host Library is uninstalled.
- Parameters:
ec – Set on failure.
- Returns:
true on success (or if not initialized).
-
inline bool is_initialized() const#
Whether initialize() succeeded (and deinitialize() has not run).
-
std::vector<std::shared_ptr<Controller>> controllers() const#
The connected controllers.
-
inline const std::string &get_name() const#
Get the name of the component
Note
This is the tag of the logger
- Returns:
A const reference to the name of the component
-
template<typename T = void>
inline void set_log_tag(const std::string_view&)# Not available: the tag is fixed at construction (pass it to the constructor). Enable CONFIG_ESPP_LOGGER_MUTABLE_TAG (menuconfig: ESPP Logger -> Mutable tag) to change it afterwards.
-
inline espp::Logger::Verbosity get_log_level() const#
Get the log level for the logger
See also
See also
- Returns:
The verbosity level of the logger
-
inline void set_log_level(espp::Logger::Verbosity level)#
Set the log level for the logger
See also
See also
- Parameters:
level – The verbosity level to use for the logger
-
inline void set_log_verbosity(espp::Logger::Verbosity level)#
Set the log verbosity for the logger
See also
See also
See also
Note
This is a convenience method that calls set_log_level
- Parameters:
level – The verbosity level to use for the logger
-
inline espp::Logger::Verbosity get_log_verbosity() const#
Get the log verbosity for the logger
See also
See also
See also
Note
This is a convenience method that calls get_log_level
- Returns:
The verbosity level of the logger
-
inline void set_log_rate_limit(std::chrono::duration<float> rate_limit)#
Set the rate limit for the logger
See also
Note
Only calls to the logger that have _rate_limit suffix will be rate limited
- Parameters:
rate_limit – The rate limit to use for the logger
Public Static Functions
-
class Controller#
One connected controller.
Public Functions
-
inline bool connected() const#
Whether it is still attached.
-
GamepadState state() const#
Its latest state.
-
bool set_rumble(const RumbleState &rumble)#
Drive its rumble motors (X-Input: the two grips; GIP: the grips and the two impulse triggers) until the next call. Any task.
- Returns:
false if it is gone or the report could not be queued.
-
bool set_player(int player)#
Change its player number (X-Input: the LED ring shows it). Any task.
-
struct Info#
What the controller is.
-
inline bool connected() const#
-
struct Config#
Configuration.
Public Members
-
controller_fn on_connected = {nullptr}#
a controller attached and started
-
controller_fn on_disconnected = {nullptr}#
a controller detached
-
state_fn on_state = {nullptr}#
A controller’s state changed (every input report, if only_on_change is false).
-
bool only_on_change = {true}#
call on_state only when the state changes
-
int task_core_id = {-1}#
-1 = no affinity
-
uint32_t task_stack_alloc_caps = {0}#
ESP only: heap capabilities for the task’s stack (see espp::Task::BaseConfig::stack_alloc_caps); 0 = internal RAM. MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT puts it in PSRAM, as espp::UsbHost::Config::task_stack_alloc_caps does for its tasks.
-
controller_fn on_connected = {nullptr}#
-
explicit XboxHost(const Config &config)#
-
class Controller
One connected controller.
Public Functions
-
inline const Info &info() const
What the controller is.
-
inline bool connected() const
Whether it is still attached.
-
inline int player() const
Its player number (1..4; XboxHost hands them out in order).
-
GamepadState state() const
Its latest state.
-
bool set_rumble(const RumbleState &rumble)
Drive its rumble motors (X-Input: the two grips; GIP: the grips and the two impulse triggers) until the next call. Any task.
- Returns:
false if it is gone or the report could not be queued.
-
bool set_player(int player)
Change its player number (X-Input: the LED ring shows it). Any task.
-
struct Info
What the controller is.
Public Members
-
std::string manufacturer
from the string descriptor (ASCII), may be empty
-
std::string product
from the string descriptor (ASCII), may be empty
-
uint8_t address = {0}
USB device address.
-
std::string manufacturer
-
inline const Info &info() const
Header File#
Classes#
-
class Sequence#
The host’s packet sequence numbers: 1..255, then around again (0 is not used).