27template<int16_t LOG_MIN = 0,
28 int16_t LOG_MAX = 2047,
29 uint8_t IN_EP_INTERVAL_MS = 1>
32 static_assert(LOG_MIN <= LOG_MAX,
"LOG_MIN must be <= LOG_MAX");
33 static_assert(LOG_MIN >= -32768 && LOG_MAX <= 32767,
"Axis logical range must fit in int16_t");
42 :
HID<50, 9, 0>(false, IN_EP_INTERVAL_MS, 1,
43 in_ep_num,
Endpoint::EPNumber::EP_AUTO)
58 BTN1 = 0x01, BTN2 = 0x02, BTN3 = 0x04, BTN4 = 0x08,
59 BTN5 = 0x10, BTN6 = 0x20, BTN7 = 0x40, BTN8 = 0x80,
77 static_assert(
sizeof(
Report) == 9,
"Report size must be 9 bytes");
89 ErrorCode
Send(
int x,
int y,
int z,
int rx, uint8_t buttons)
138 return ErrorCode::OK;
153 static constexpr uint8_t U8(uint16_t v) {
return static_cast<uint8_t
>(v & 0xFF); }
154 static constexpr uint8_t U8H(uint16_t v) {
return static_cast<uint8_t
>((v >> 8) & 0xFF); }
156 static constexpr uint8_t desc_[] = {
171 0x16, U8(
static_cast<uint16_t
>(LOG_MIN)), U8H(
static_cast<uint16_t
>(LOG_MIN)),
172 0x26, U8(
static_cast<uint16_t
>(LOG_MAX)), U8H(
static_cast<uint16_t
>(LOG_MAX)),
192 static_assert(
sizeof(desc_) == 50,
"HID report descriptor size changed; update base template arg!");
197 static constexpr int16_t
Clamp(
int v)
199 return (v < LOG_MIN) ? LOG_MIN : (v > LOG_MAX ? LOG_MAX :
static_cast<int16_t
>(v));
205 static constexpr int16_t
Mid()
207 return static_cast<int16_t
>((
static_cast<int32_t
>(LOG_MIN) +
static_cast<int32_t
>(LOG_MAX)) / 2);
216using HIDGamepad = HIDGamepadT<0, 2047, 1>;
221using HIDGamepadBipolar = HIDGamepadT<-2048, 2047, 1>;
常量原始数据封装类。 A class for encapsulating constant raw data.
USB描述符基类 USB descriptor base class.
Data data_
设备描述符数据实例 / Internal data instance
@ PER_INTERFACE
每个接口自定义类 / Per-interface
USB端点基类 / USB Endpoint base class.
EPNumber
端点号 / Endpoint number
@ EP_AUTO
自动分配端点号 / Auto allocate
模板化 4 轴 + 8 按钮 HID 手柄 / Templated 4-axis + 8-button HID gamepad
Button
按钮位掩码(8 个) / Button bit masks (8 buttons)
ErrorCode SendAxes(int x, int y, int z, int rx)
仅更新轴(保持按钮不变) / Update axes only (buttons unchanged)
ErrorCode SendButtons(uint8_t buttons)
仅更新按钮(保持轴不变) / Update buttons only (axes unchanged)
ErrorCode Send(int x, int y, int z, int rx, uint8_t buttons)
发送完整输入报告(轴 + 按钮) / Send a full input report (axes + buttons)
static constexpr int16_t Mid()
轴逻辑中点 / Logical midpoint of axes
ConstRawData GetReportDesc() override
获取报告描述符 / Get report descriptor
static constexpr int16_t Clamp(int v)
将值夹到 [LOG_MIN, LOG_MAX] / Clamp a value to [LOG_MIN, LOG_MAX]
Report last_
最近一次发送的输入报告 / Last-sent input report
HIDGamepadT(Endpoint::EPNumber in_ep_num=Endpoint::EPNumber::EP_AUTO)
构造函数 / Constructor
ErrorCode WriteDeviceDescriptor(DeviceDescriptor &header) override
写入设备描述符(Per-Interface) / Write device descriptor (Per-Interface)
USB HID(Human Interface Device)基类,支持可选 OUT 端点、自动生成描述符,适合键盘、鼠标、手柄等扩展。 USB HID (Human Interface Device)...
ErrorCode SendInputReport(ConstRawData report)
ClassID bDeviceClass
设备类代码 / Device class code
uint8_t bDeviceSubClass
设备子类代码 / Device subclass code
uint8_t bDeviceProtocol
协议代码 / Protocol code
输入报告结构(9 字节) / Input report structure (9 bytes)
int16_t y
Y 轴 / Y axis (LOG_MIN..LOG_MAX)
int16_t x
X 轴 / X axis (LOG_MIN..LOG_MAX)
int16_t rx
Rx 轴 / Rx axis (LOG_MIN..LOG_MAX)
int16_t z
Z 轴 / Z axis (LOG_MIN..LOG_MAX)
uint8_t buttons
按钮位 / Button bits (8)