Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions components/hid-rp/include/hid-rp-report-map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,13 +576,17 @@ class GamepadDecoder {
latest_.resize(report_ids_.size());
}

/// Whether a map looks like a gamepad (buttons plus a direction source).
/// Whether a map looks like a gamepad: buttons plus an absolute direction
/// source (an absolute X or Y axis, a hat or a d-pad). Relative X / Y is
/// pointer motion, so a
/// mouse, a keyboard with a built-in pointer or a SpaceMouse is not a
/// gamepad; a keyboard has no Button-page fields at all.
static bool looks_like_gamepad(const ReportMap &map) {
bool buttons = false, direction = false;
for (const auto &f : map.fields()) {
if (f.usage_page == usage::PAGE_BUTTON)
buttons = true;
if (f.usage_page == usage::PAGE_GENERIC_DESKTOP &&
if (f.usage_page == usage::PAGE_GENERIC_DESKTOP && !f.relative &&
(f.usage == usage::GD_HAT || f.usage == usage::GD_X || f.usage == usage::GD_Y ||
(f.usage >= usage::GD_DPAD_UP && f.usage <= usage::GD_DPAD_LEFT)))
direction = true;
Comment thread
finger563 marked this conversation as resolved.
Resolved
Expand Down
6 changes: 3 additions & 3 deletions components/hid-rp/test/hid_inspector_host_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ TEST_CASE("the Xbox descriptor: items, fields, a gamepad with the Android layout
CHECK(has(j, R"("output_fields":[{)")); // the rumble output report
}

// What the decoders conclude, not what the device is: the SpaceMouse's axes
// and buttons make it look like a gamepad and a mouse to them.
// What the decoders conclude, not what the device is: the SpaceMouse's
// relative axes read as pointer motion (a mouse), not as a gamepad's sticks.
TEST_CASE("the SpaceMouse, as the decoders classify it") {
const auto j = parse_json(preset("SpaceMouse").descriptor);
CHECK(has(j, "\"ok\":true"));
CHECK(has(j, R"("kind":{"gamepad":true,"keyboard":false,"mouse":true})"));
CHECK(has(j, R"("kind":{"gamepad":false,"keyboard":false,"mouse":true})"));
}

TEST_CASE("a real controller's report, as a pass-through sees it") {
Expand Down
87 changes: 87 additions & 0 deletions components/hid-rp/test/hid_rp_report_map_host_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <span>
Comment thread
finger563 marked this conversation as resolved.
Resolved
#include <utility>
#include <vector>

#include <doctest/doctest.h>

#include "hid-rp-3dconnexion.hpp"
#include "hid-rp-gamepad.hpp"
#include "hid-rp-playstation.hpp"
#include "hid-rp-ps4.hpp"
#include "hid-rp-report-map.hpp"
#include "hid-rp-switch-pro.hpp"
#include "hid-rp-xbox.hpp"
Expand Down Expand Up @@ -286,6 +290,89 @@ TEST_CASE("mouse") {
CHECK(m.wheel == -1);
}

// What each decoder's looks_like_*() concludes. Relative X / Y is pointer
// motion: a mouse, a keyboard with a built-in pointer and a SpaceMouse are not
// gamepads (they have buttons, but no absolute stick, hat or d-pad).
TEST_CASE("classification: gamepads, mice, keyboards, a SpaceMouse") {
using espp::hid_rp::GamepadDecoder;
using espp::hid_rp::KeyboardDecoder;
using espp::hid_rp::MouseDecoder;
using espp::hid_rp::ReportMap;
struct Kind {
bool gamepad, keyboard, mouse;
};
auto kind = [](std::span<const uint8_t> desc) {
Comment thread
finger563 marked this conversation as resolved.
Resolved
auto map = ReportMap::parse(desc);
REQUIRE(map.has_value());
return Kind{GamepadDecoder::looks_like_gamepad(*map),
KeyboardDecoder::looks_like_keyboard(*map), MouseDecoder::looks_like_mouse(*map)};
};
auto bytes = [](const auto &desc) { return std::vector<uint8_t>(desc.begin(), desc.end()); };

SUBCASE("hid-rp's controllers are gamepads, and nothing else") {
const std::vector<std::pair<const char *, std::vector<uint8_t>>> pads{
{"xbox", bytes(espp::xbox_descriptor())},
{"dualsense", bytes(espp::playstation_dualsense_ble_descriptor())},
{"dualshock4", bytes(espp::ps4_dualshock4_descriptor())},
{"switch pro", bytes(espp::switch_pro_descriptor())},
};
for (const auto &[name, desc] : pads) {
CAPTURE(name);
const auto k = kind(desc);
CHECK(k.gamepad);
CHECK_FALSE(k.keyboard);
CHECK_FALSE(k.mouse);
}
}

SUBCASE("a mouse is not a gamepad") {
// the HID 1.11 appendix B.2 boot mouse: 3 buttons, relative X / Y
const std::vector<uint8_t> mouse{0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x09, 0x01, 0xa1, 0x00,
0x05, 0x09, 0x19, 0x01, 0x29, 0x03, 0x15, 0x00, 0x25, 0x01,
0x95, 0x03, 0x75, 0x01, 0x81, 0x02, 0x95, 0x01, 0x75, 0x05,
0x81, 0x01, 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x15, 0x81,
0x25, 0x7f, 0x75, 0x08, 0x95, 0x02, 0x81, 0x06, 0xc0, 0xc0};
const auto k = kind(mouse);
CHECK_FALSE(k.gamepad);
CHECK_FALSE(k.keyboard);
CHECK(k.mouse);
}

SUBCASE("a SpaceMouse is not a gamepad") {
const auto k = kind(bytes(espp::spacemouse_descriptor()));
CHECK_FALSE(k.gamepad);
CHECK_FALSE(k.keyboard);
CHECK(k.mouse); // its relative axes read as pointer motion
}

SUBCASE("a keyboard is not a gamepad, with or without a built-in pointer") {
// the boot keyboard: modifiers, a reserved byte, six key slots
const std::vector<uint8_t> keyboard{
0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x05, 0x07, 0x19, 0xe0, 0x29, 0xe7, 0x15, 0x00, 0x25,
0x01, 0x75, 0x01, 0x95, 0x08, 0x81, 0x02, 0x95, 0x01, 0x75, 0x08, 0x81, 0x01, 0x95, 0x06,
0x75, 0x08, 0x15, 0x00, 0x25, 0x65, 0x05, 0x07, 0x19, 0x00, 0x29, 0x65, 0x81, 0x00, 0xc0};
const auto k = kind(keyboard);
CHECK_FALSE(k.gamepad);
CHECK(k.keyboard);
CHECK_FALSE(k.mouse);

// the same keyboard (report 1) plus a touchpad / trackpoint mouse (report
// 2) in one descriptor, as wireless keyboards often send
std::vector<uint8_t> combo{0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x85, 0x01};
combo.insert(combo.end(), keyboard.begin() + 6, keyboard.end());
const std::vector<uint8_t> pointer{
0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x85, 0x02, 0x09, 0x01, 0xa1, 0x00, 0x05,
0x09, 0x19, 0x01, 0x29, 0x03, 0x15, 0x00, 0x25, 0x01, 0x95, 0x03, 0x75, 0x01,
0x81, 0x02, 0x95, 0x01, 0x75, 0x05, 0x81, 0x01, 0x05, 0x01, 0x09, 0x30, 0x09,
0x31, 0x15, 0x81, 0x25, 0x7f, 0x75, 0x08, 0x95, 0x02, 0x81, 0x06, 0xc0, 0xc0};
combo.insert(combo.end(), pointer.begin(), pointer.end());
const auto kc = kind(combo);
CHECK_FALSE(kc.gamepad);
CHECK(kc.keyboard);
CHECK(kc.mouse);
}
}

// Report IDs, a Consumer-page array item, and DPad usages (the Xbox-style
// layout many mobile controllers use).
TEST_CASE("report ids consumer dpad") {
Expand Down
4 changes: 2 additions & 2 deletions components/usb_host/tab5_example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,5 @@ class driver come from the registry, as in the plain `usb_host` example).
interfaces.
- A SpaceMouse is recognised by 3Dconnexion's vendor id (`0x256F`), or by the
product ids of the early Logitech-branded SpaceNavigator / SpaceExplorer /
SpacePilot (`0x046D:C62x`), before the descriptor is looked at: its six axes
and buttons would otherwise pass for a gamepad.
SpacePilot (`0x046D:C62x`), before the descriptor is looked at: its six
relative axes and buttons would otherwise pass for a mouse.
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ extern "C" void app_main(void) {
.protocol = params.protocol,
.report_descriptor_bytes = device->report_descriptor().size(),
.kind = Gui::Kind::Unknown};
// A SpaceMouse first, by vendor id: its descriptor (six axes and
// buttons) would otherwise pass for a gamepad. Everything else is
// A SpaceMouse first, by vendor id: its descriptor (six relative
// axes and buttons) would otherwise pass for a mouse. Everything else is
// classified from its report descriptor; UsbHost has already asked a
// boot-subclass interface for the report protocol, so the descriptor
// describes what the device actually sends.
Expand Down
Loading