@@ -12,6 +12,8 @@ const AxesValueType kAxesMax = -kAxesMin - 1;
12
12
13
13
const size_t kNumButtonGroups = 3 ;
14
14
15
+ #define VIRTUAL_HAT
16
+
15
17
// Report data sent by the HID input device
16
18
struct ReportData {
17
19
AxesValueType axes[kNumAxes ];
@@ -24,6 +26,12 @@ constexpr uint8_t GetByte(int16_t x, uint8_t b) {
24
26
return (x >> (b << 3 ));
25
27
}
26
28
29
+ #ifdef VIRTUAL_HAT
30
+ const u8 kButtonCount = 20 ;
31
+ #else
32
+ const u8 kButtonCount = 24 ;
33
+ #endif /* VIRTUAL_HAT */
34
+
27
35
const u8 kReportId = 1 ;
28
36
static const u8 sHidDescriptorData [] PROGMEM = {
29
37
Global::UsagePage | 1 , usage::Page::GenericDesktop,
@@ -54,14 +62,14 @@ static const u8 sHidDescriptorData[] PROGMEM = {
54
62
// 24 1 bit buttons
55
63
Global::UsagePage | 1 , usage::Page::Button,
56
64
Local::UsageMinimum | 1 , 1 ,
57
- Local::UsageMaximum | 1 , 24 ,
65
+ Local::UsageMaximum | 1 , kButtonCount ,
58
66
Global::LogicalMinimum | 1 , 0 ,
59
67
Global::LogicalMaximum | 1 , 1 ,
60
68
Global::ReportSize | 1 , 1 ,
61
- Global::ReportCount | 1 , 24 ,
69
+ Global::ReportCount | 1 , kButtonCount ,
62
70
Main::Input | 1 , DataBits::Variable,
63
71
64
- #if 0
72
+ #ifdef VIRTUAL_HAT
65
73
/*
66
74
* 8 way hat switch
67
75
* Reports an angle in 45 degree increments by mapping the values 0-7
@@ -83,6 +91,28 @@ static const u8 sHidDescriptorData[] PROGMEM = {
83
91
Main::EndCollection | 0 ,
84
92
};
85
93
94
+ #ifdef VIRTUAL_HAT
95
+ const int8_t hat_null = -8 ;
96
+ int8_t button_hat_lut[16 ] = {
97
+ hat_null, // 0000
98
+ 0 , // 0001
99
+ 2 , // 0010
100
+ 1 , // 0011
101
+ 4 , // 0100
102
+ hat_null, // 0101
103
+ 3 , // 0110
104
+ hat_null, // 0111
105
+ 6 , // 1000
106
+ 7 , // 1001
107
+ hat_null, // 1010
108
+ hat_null, // 1011
109
+ 5 , // 1100
110
+ hat_null, // 1101
111
+ hat_null, // 1110
112
+ hat_null, // 1111
113
+ };
114
+ #endif /* VIRTUAL_HAT */
115
+
86
116
#define POLL_DELAY_MSEC 1
87
117
88
118
const hc165_config_t hc165_configs[kNumButtonGroups ] = {
@@ -185,10 +215,12 @@ u8 buttonMapping[kNumButtons] = {
185
215
12 ,
186
216
10 ,
187
217
8 ,
218
+ #ifndef VIRTUAL_HAT
188
219
13 ,
189
- 9 ,
190
220
15 ,
191
221
14 ,
222
+ 9 ,
223
+ #endif /* ! VIRTUAL_HAT */
192
224
193
225
19 ,
194
226
18 ,
@@ -198,6 +230,13 @@ u8 buttonMapping[kNumButtons] = {
198
230
20 ,
199
231
21 ,
200
232
22 ,
233
+
234
+ #ifdef VIRTUAL_HAT
235
+ 13 ,
236
+ 15 ,
237
+ 14 ,
238
+ 9 ,
239
+ #endif /* VIRTUAL_HAT */
201
240
};
202
241
203
242
/* Access button bits either as separate bytes or continuous bits. */
@@ -269,19 +308,26 @@ void loop()
269
308
reportData.buttons [i] = buttonsMapped >> (i * 8 );
270
309
}
271
310
311
+ #ifdef VIRTUAL_HAT
312
+ u8 & hat_button_group = reportData.buttons [kNumButtonGroups - 1 ];
313
+ uint8_t hat_buttons = hat_button_group >> 4 ;
314
+ uint8_t hat_angle = button_hat_lut[hat_buttons];
315
+ hat_button_group = (hat_button_group & 0xf ) | (hat_angle << 4 );
316
+ #endif /* VIRTUAL_HAT */
317
+
272
318
// Report data to host
273
319
HID ().SendReport (kReportId , &reportData, sizeof (reportData));
274
-
275
- #ifdef TIME_HISTOGRAM
276
- unsigned long micros_curr = micros ();
277
- time_histogram_update (micros_curr - micros_prev);
278
- micros_prev = micros_curr;
279
-
280
- if (((buttonsPrev ^ buttonsMapped) & 1 ) && (buttonsMapped ^ 1 )) {
281
- time_histogram_dump ();
282
- memset (histogram, 0 , sizeof (histogram));
283
- }
284
- #endif /* TIME_HISTOGRAM */
285
-
286
- buttonsPrev = buttonsMapped;
320
+
321
+ #ifdef TIME_HISTOGRAM
322
+ unsigned long micros_curr = micros ();
323
+ time_histogram_update (micros_curr - micros_prev);
324
+ micros_prev = micros_curr;
325
+
326
+ if (((buttonsPrev ^ buttonsMapped) & 1 ) && (buttonsMapped ^ 1 )) {
327
+ time_histogram_dump ();
328
+ memset (histogram, 0 , sizeof (histogram));
329
+ }
330
+ #endif /* TIME_HISTOGRAM */
331
+
332
+ buttonsPrev = buttonsMapped;
287
333
}
0 commit comments