1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
#ifndef HID_H
#define HID_H
struct HID_desc {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bcdHID;
uint8_t bCountryCode;
uint8_t bNumDescriptors;
uint8_t bDescriptorType0;
uint16_t wDescriptorLength0;
} __attribute__((packed));
constexpr HID_desc hid_desc(
uint16_t bcdHID,
uint8_t bCountryCode,
uint8_t bNumDescriptors,
uint8_t bDescriptorType,
uint16_t wDescriptorLength
) {
return {
sizeof(HID_desc),
0x21,
bcdHID,
bCountryCode,
bNumDescriptors,
bDescriptorType,
wDescriptorLength
};
}
template <typename T>
struct HID_Item {
uint8_t tag;
T data;
} __attribute__((packed));
template <>
struct HID_Item<void> {
uint8_t tag;
} __attribute__((packed));
constexpr HID_Item<void> hid_item(uint8_t tag) {
return {tag};
}
template <typename T>
constexpr uint8_t hid_item_data_size() {
static_assert(sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4, "item has invalid size");
return sizeof(T) == 1 ? 1 : sizeof(T) == 2 ? 2 : 3;
}
template <typename T>
constexpr HID_Item<T> hid_item(uint8_t tag, T data) {
return HID_Item<T>{uint8_t(tag | hid_item_data_size<T>()), data};
}
namespace UsagePage {
enum _8 : uint8_t {
Undefined,
Desktop,
Simulation,
VR,
Sport,
Game,
Generic,
Keyboard,
LED,
Button,
Ordinal,
};
}
constexpr HID_Item<UsagePage::_8> usage_page(UsagePage::_8 page) {
return hid_item(0x04, page);
}
constexpr HID_Item<uint16_t> usage_page(uint16_t page) {
return hid_item(0x04, page);
}
namespace DesktopUsage {
enum _8 : uint8_t {
Undefined,
Gamepad = 0x05,
X = 0x30,
Y,
Z,
Rx,
Ry,
Rz,
};
}
enum class Collection : uint8_t {
Physical,
Application,
Logical,
};
constexpr HID_Item<DesktopUsage::_8> usage(DesktopUsage::_8 usage) {
return hid_item(0x08, usage);
}
constexpr HID_Item<uint16_t> usage(uint16_t usage) {
return hid_item(0x08, usage);
}
template <typename... R>
constexpr auto collection(Collection type, R... r) -> decltype(pack(HID_Item<Collection>(), r..., HID_Item<void>())) {
return pack(hid_item(0xa0, type), r..., hid_item(0xc0));
}
constexpr HID_Item<int16_t> usage_minimum(int16_t x) {
return hid_item(0x18, x);
}
constexpr HID_Item<int16_t> usage_maximum(int16_t x) {
return hid_item(0x28, x);
}
constexpr HID_Item<int32_t> logical_minimum(int32_t x) {
return hid_item(0x14, x);
}
constexpr HID_Item<int32_t> logical_maximum(int32_t x) {
return hid_item(0x24, x);
}
constexpr HID_Item<uint8_t> report_count(uint8_t x) {
return hid_item(0x94, x);
}
constexpr HID_Item<uint8_t> report_size(uint8_t x) {
return hid_item(0x74, x);
}
constexpr HID_Item<uint8_t> input(uint8_t x) {
return hid_item(0x80, x);
}
constexpr HID_Item<uint8_t> output(uint8_t x) {
return hid_item(0x90, x);
}
constexpr HID_Item<uint8_t> feature(uint8_t x) {
return hid_item(0xb0, x);
}
constexpr HID_Item<uint8_t> report_id(uint8_t id) {
return hid_item(0x84, id);
}
template <typename... R>
constexpr auto gamepad(R... r) -> decltype(
pack(
usage_page(UsagePage::Desktop),
usage(DesktopUsage::Gamepad),
collection(Collection::Application,
collection(Collection::Physical,
r...
)
)
)) {
return pack(
usage_page(UsagePage::Desktop),
usage(DesktopUsage::Gamepad),
collection(Collection::Application,
collection(Collection::Physical,
r...
)
)
);
}
constexpr auto buttons(uint8_t num) -> decltype(
pack(
usage_page(UsagePage::Button),
usage_minimum(1),
usage_maximum(num),
logical_minimum(0),
logical_maximum(1),
report_count(num),
report_size(1),
input(0x02)
)) {
return pack(
usage_page(UsagePage::Button),
usage_minimum(1),
usage_maximum(num),
logical_minimum(0),
logical_maximum(1),
report_count(num),
report_size(1),
input(0x02)
);
}
constexpr auto padding_in(uint8_t size) -> decltype(
pack(
report_count(1),
report_size(size),
input(0x01)
)) {
return pack(
report_count(1),
report_size(size),
input(0x01)
);
}
constexpr auto padding_out(uint8_t size) -> decltype(
pack(
report_count(1),
report_size(size),
output(0x01)
)) {
return pack(
report_count(1),
report_size(size),
output(0x01)
);
}
#endif
|