Skip to content

Commit a56efee

Browse files
committed
examples: phoenix-arduino-joystick: add support for virtual hat
Create a virtual POV HAT control from 4 separate buttons.
1 parent 3a92166 commit a56efee

File tree

1 file changed

+63
-17
lines changed

1 file changed

+63
-17
lines changed

examples/phoenix-arduino-joystick/phoenix-arduino-joystick.ino

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const AxesValueType kAxesMax = -kAxesMin - 1;
1212

1313
const size_t kNumButtonGroups = 3;
1414

15+
#define VIRTUAL_HAT
16+
1517
// Report data sent by the HID input device
1618
struct ReportData {
1719
AxesValueType axes[kNumAxes];
@@ -24,6 +26,12 @@ constexpr uint8_t GetByte(int16_t x, uint8_t b) {
2426
return (x >> (b << 3));
2527
}
2628

29+
#ifdef VIRTUAL_HAT
30+
const u8 kButtonCount = 20;
31+
#else
32+
const u8 kButtonCount = 24;
33+
#endif /* VIRTUAL_HAT */
34+
2735
const u8 kReportId = 1;
2836
static const u8 sHidDescriptorData[] PROGMEM = {
2937
Global::UsagePage | 1, usage::Page::GenericDesktop,
@@ -54,14 +62,14 @@ static const u8 sHidDescriptorData[] PROGMEM = {
5462
// 24 1 bit buttons
5563
Global::UsagePage | 1, usage::Page::Button,
5664
Local::UsageMinimum | 1, 1,
57-
Local::UsageMaximum | 1, 24,
65+
Local::UsageMaximum | 1, kButtonCount,
5866
Global::LogicalMinimum | 1, 0,
5967
Global::LogicalMaximum | 1, 1,
6068
Global::ReportSize | 1, 1,
61-
Global::ReportCount | 1, 24,
69+
Global::ReportCount | 1, kButtonCount,
6270
Main::Input | 1, DataBits::Variable,
6371

64-
#if 0
72+
#ifdef VIRTUAL_HAT
6573
/*
6674
* 8 way hat switch
6775
* Reports an angle in 45 degree increments by mapping the values 0-7
@@ -83,6 +91,28 @@ static const u8 sHidDescriptorData[] PROGMEM = {
8391
Main::EndCollection | 0,
8492
};
8593

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+
86116
#define POLL_DELAY_MSEC 1
87117

88118
const hc165_config_t hc165_configs[kNumButtonGroups] = {
@@ -185,10 +215,12 @@ u8 buttonMapping[kNumButtons] = {
185215
12,
186216
10,
187217
8,
218+
#ifndef VIRTUAL_HAT
188219
13,
189-
9,
190220
15,
191221
14,
222+
9,
223+
#endif /* ! VIRTUAL_HAT */
192224

193225
19,
194226
18,
@@ -198,6 +230,13 @@ u8 buttonMapping[kNumButtons] = {
198230
20,
199231
21,
200232
22,
233+
234+
#ifdef VIRTUAL_HAT
235+
13,
236+
15,
237+
14,
238+
9,
239+
#endif /* VIRTUAL_HAT */
201240
};
202241

203242
/* Access button bits either as separate bytes or continuous bits. */
@@ -269,19 +308,26 @@ void loop()
269308
reportData.buttons[i] = buttonsMapped >> (i * 8);
270309
}
271310

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+
272318
// Report data to host
273319
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;
287333
}

0 commit comments

Comments
 (0)