libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
hid_mouse.hpp
1#pragma once
2#include <cstdint>
3#include <cstring>
4
5#include "hid.hpp"
6
7namespace LibXR::USB
8{
9
10// 标准 HID Boot 鼠标报告描述符 / Standard HID Boot Mouse Report Descriptor
11static const constexpr uint8_t HID_MOUSE_REPORT_DESC[] = {
12 0x05, 0x01, // Usage Page (Generic Desktop)
13 0x09, 0x02, // Usage (Mouse)
14 0xA1, 0x01, // Collection (Application)
15 0x09, 0x01, // Usage (Pointer)
16 0xA1, 0x00, // Collection (Physical)
17 0x05, 0x09, // Usage Page (Buttons)
18 0x19, 0x01, // Usage Minimum (1)
19 0x29, 0x03, // Usage Maximum (3)
20 0x15, 0x00, // Logical Minimum (0)
21 0x25, 0x01, // Logical Maximum (1)
22 0x95, 0x03, // Report Count (3)
23 0x75, 0x01, // Report Size (1)
24 0x81, 0x02, // Input (Data, Variable, Absolute) ; Buttons
25 0x95, 0x01, // Report Count (1)
26 0x75, 0x05, // Report Size (5)
27 0x81, 0x03, // Input (Constant, Variable, Absolute) ; Padding
28 0x05, 0x01, // Usage Page (Generic Desktop)
29 0x09, 0x30, // Usage (X)
30 0x09, 0x31, // Usage (Y)
31 0x09, 0x38, // Usage (Wheel)
32 0x15, 0x81, // Logical Minimum (-127)
33 0x25, 0x7F, // Logical Maximum (127)
34 0x75, 0x08, // Report Size (8)
35 0x95, 0x03, // Report Count (3)
36 0x81, 0x06, // Input (Data, Variable, Relative) ; X, Y, Wheel
37 0xC0, // End Collection
38 0xC0 // End Collection
39};
40
45class HIDMouse : public HID<sizeof(HID_MOUSE_REPORT_DESC), 4, 0>
46{
47 public:
54 HIDMouse(Endpoint::EPNumber in_ep_num, uint8_t in_ep_interval = 1,
55 const char* interface_string =
56 HID<sizeof(HID_MOUSE_REPORT_DESC), 4, 0>::DEFAULT_INTERFACE_STRING)
57 : HID(in_ep_num, Endpoint::EPNumber::EP_INVALID, false, in_ep_interval, 1,
58 interface_string)
59 {
60 }
61
65 enum Button : uint8_t
66 {
67 LEFT = 0x01,
68 RIGHT = 0x02,
69 MIDDLE = 0x04
70 };
71
75 struct Report
76 {
77 uint8_t buttons;
78 int8_t x;
79 int8_t y;
80 int8_t wheel;
81 };
82
90 void Move(uint8_t buttons, int8_t x, int8_t y, int8_t wheel = 0)
91 {
92 Report report = {buttons, x, y, wheel};
93 SendInputReport(ConstRawData{&report, sizeof(report)});
94 }
95
99 void Release()
100 {
101 Report report = {0, 0, 0, 0};
102 SendInputReport(ConstRawData{&report, sizeof(report)});
103 }
104
105 protected:
107 {
109 header.data_.bDeviceSubClass = 1;
110 header.data_.bDeviceProtocol = 2; // Mouse
111 return ErrorCode::OK;
112 }
113
115 {
116 return ConstRawData{HID_MOUSE_REPORT_DESC, sizeof(HID_MOUSE_REPORT_DESC)};
117 }
118};
119
120} // namespace LibXR::USB
只读原始数据视图 / Immutable raw data view
USB描述符基类 USB descriptor base class.
Definition desc_dev.hpp:39
Data data_
设备描述符数据实例 / Internal data instance
Definition desc_dev.hpp:108
@ HID
人机接口类 / Human Interface Device
USB 端点基类 / USB Endpoint base class.
Definition ep.hpp:24
EPNumber
端点号 Endpoint number
Definition ep.hpp:42
USB HID(Human Interface Device)基类,支持可选 OUT 端点、自动生成描述符,适合键盘、鼠标、手柄等扩展。 USB HID (Human Interface Device)...
Definition hid.hpp:24
ErrorCode SendInputReport(ConstRawData report)
Definition hid.hpp:600
标准 USB HID 鼠标派生类 / Standard USB HID Mouse derived class
Definition hid_mouse.hpp:46
Button
鼠标按钮定义 / Mouse button definitions
Definition hid_mouse.hpp:66
HIDMouse(Endpoint::EPNumber in_ep_num, uint8_t in_ep_interval=1, const char *interface_string=HID< sizeof(HID_MOUSE_REPORT_DESC), 4, 0 >::DEFAULT_INTERFACE_STRING)
构造函数 / Constructor
Definition hid_mouse.hpp:54
ConstRawData GetReportDesc() override
获取 HID 报告描述符 Get HID Report Descriptor
ErrorCode WriteDeviceDescriptor(DeviceDescriptor &header) override
可选:覆盖设备描述符字段 / Optional: override device descriptor fields
void Release()
释放所有按钮 / Release all buttons
Definition hid_mouse.hpp:99
void Move(uint8_t buttons, int8_t x, int8_t y, int8_t wheel=0)
发送鼠标移动与按钮状态 / Send mouse movement and button state
Definition hid_mouse.hpp:90
ErrorCode
定义错误码枚举
@ OK
操作成功 | Operation successful
ClassID bDeviceClass
设备类代码 / Device class code
Definition desc_dev.hpp:92
uint8_t bDeviceSubClass
设备子类代码 / Device subclass code
Definition desc_dev.hpp:93
uint8_t bDeviceProtocol
协议代码 / Protocol code
Definition desc_dev.hpp:94
鼠标报告结构体 / Mouse input report structure
Definition hid_mouse.hpp:76
int8_t wheel
滚轮 / Wheel
Definition hid_mouse.hpp:80
uint8_t buttons
按键状态 / Button state
Definition hid_mouse.hpp:77
int8_t x
X 轴相对移动 / X movement.
Definition hid_mouse.hpp:78
int8_t y
Y 轴相对移动 / Y movement.
Definition hid_mouse.hpp:79