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:
53 HIDMouse(uint8_t in_ep_interval = 1,
55 : HID(false, in_ep_interval, 1, in_ep_num, Endpoint::EPNumber::EP_AUTO)
56 {
57 }
58
62 enum Button : uint8_t
63 {
64 LEFT = 0x01,
65 RIGHT = 0x02,
66 MIDDLE = 0x04
67 };
68
72 struct Report
73 {
74 uint8_t buttons;
75 int8_t x;
76 int8_t y;
77 int8_t wheel;
78 };
79
87 void Move(uint8_t buttons, int8_t x, int8_t y, int8_t wheel = 0)
88 {
89 Report report = {buttons, x, y, wheel};
90 SendInputReport(ConstRawData{&report, sizeof(report)});
91 }
92
96 void Release()
97 {
98 Report report = {0, 0, 0, 0};
99 SendInputReport(ConstRawData{&report, sizeof(report)});
100 }
101
102 protected:
103 ErrorCode WriteDeviceDescriptor(DeviceDescriptor& header) override
104 {
106 header.data_.bDeviceSubClass = 1;
107 header.data_.bDeviceProtocol = 2; // Mouse
108 return ErrorCode::OK;
109 }
110
112 {
113 return ConstRawData{HID_MOUSE_REPORT_DESC, sizeof(HID_MOUSE_REPORT_DESC)};
114 }
115};
116
117} // namespace LibXR::USB
常量原始数据封装类。 A class for encapsulating constant raw data.
USB描述符基类 USB descriptor base class.
Definition desc_dev.hpp:37
Data data_
设备描述符数据实例 / Internal data instance
Definition desc_dev.hpp:106
@ HID
人机接口类 / Human Interface Device
USB端点基类 / USB Endpoint base class.
Definition ep.hpp:22
EPNumber
端点号 / Endpoint number
Definition ep.hpp:39
@ EP_AUTO
自动分配端点号 / Auto allocate
USB HID(Human Interface Device)基类,支持可选 OUT 端点、自动生成描述符,适合键盘、鼠标、手柄等扩展。 USB HID (Human Interface Device)...
Definition hid.hpp:23
ErrorCode SendInputReport(ConstRawData report)
Definition hid.hpp:560
标准 USB HID 鼠标派生类 / Standard USB HID Mouse derived class
Definition hid_mouse.hpp:46
HIDMouse(uint8_t in_ep_interval=1, Endpoint::EPNumber in_ep_num=Endpoint::EPNumber::EP_AUTO)
构造函数 / Constructor
Definition hid_mouse.hpp:53
Button
鼠标按钮定义 / Mouse button definitions
Definition hid_mouse.hpp:63
ConstRawData GetReportDesc() override
获取 HID 报告描述符 Get HID Report Descriptor
ErrorCode WriteDeviceDescriptor(DeviceDescriptor &header) override
写入/补全设备描述符(非IAD情况下会被调用) Write device descriptor (non-IAD case will be called)
void Release()
释放所有按钮 / Release all buttons
Definition hid_mouse.hpp:96
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:87
ClassID bDeviceClass
设备类代码 / Device class code
Definition desc_dev.hpp:90
uint8_t bDeviceSubClass
设备子类代码 / Device subclass code
Definition desc_dev.hpp:91
uint8_t bDeviceProtocol
协议代码 / Protocol code
Definition desc_dev.hpp:92
鼠标报告结构体 / Mouse input report structure
Definition hid_mouse.hpp:73
int8_t wheel
滚轮 / Wheel
Definition hid_mouse.hpp:77
uint8_t buttons
按键状态 / Button state
Definition hid_mouse.hpp:74
int8_t x
X 轴相对移动 / X movement.
Definition hid_mouse.hpp:75
int8_t y
Y 轴相对移动 / Y movement.
Definition hid_mouse.hpp:76