-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
GamepadReporter.cpp
154 lines (134 loc) · 4.61 KB
/
GamepadReporter.cpp
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
#include "MicroBitConfig.h"
#if CONFIG_ENABLED(DEVICE_BLE)
#include "GamepadReporter.h"
// Report Map from https://stackoverflow.com/questions/41147796/hid-gamepad-report-descriptor-issue
// (Generated by descriptor tool); Modified a bit
static const uint8_t gamepadReportMap[] =
{
0x05, 0x01, //USAGE_PAGE (Generic Desktop)
0x09, 0x05, //USAGE (Game Pad)
0xa1, 0x01, //COLLECTION (APPLICATION)
0x85, 0x00, // Report ID OFFSET: 7
0xa1, 0x00, // COLLECTION (Physical)
0x05, 0x09, //USAGE_PAGE (Button)
0x19, 0x01, //USAGE_MINIMUM (Button1)
0x29, 0x10, //USAGE_MAXIMUM (Button 16)
0x15, 0x00, //LOGICAL_MINIMUM (0)
0x25, 0x01, //LOGICAL_MAXIMUM(1)
0x95, 0x10, //REPORT_COUNT (16)
0x75, 0x01, //REPORT_SIZE (1)
0x81, 0x02, //INPUT(Data, Var, Abs)
0x05, 0x01, //USAGE_PAGE (Generic Desktop)
0x09, 0x30, //USAGE (X)
0x09, 0x31, //USAGE (Y)
0x09, 0x32, //USAGE (Rx) 33 // Or z / 32
0x09, 0x33, //USAGE (Ry) 34 // Or Rx / 33
0x15, 0x81, //LOGICAL_MINIMUM(-127)
0x25, 0x7f, //LOGICAL_MAXIMUM(127)
0x75, 0x08, //REPORT_SIZE(8)
0x95, 0x04, //REPORT_COUNT(4)
0x81, 0x02, //INPUT(Data,Var,Abs)
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
0x09, 0x39, /* USAGE (Hat switch) */
0x09, 0x39, /* USAGE (Hat switch) */
0x15, 0x01, /* LOGICAL_MINIMUM (1) */
0x25, 0x08, /* LOGICAL_MAXIMUM (8) */
0x95, 0x02, /* REPORT_COUNT (2) */
0x75, 0x04, /* REPORT_SIZE (4) */
0x81, 0x02, /* INPUT (Data,Var,Abs) */
0xc0, //END_Collection
/*
Android Buttons
0 = A
1 = B
2 = -
3 = X
4 = Y
5 = -
6 = Left Shoulder
7 = Right Shoulder
8 = -
9 = -
10 = back
11 = start
12 = guide
13 = left stick
14 = right stick
15 = -
dpad
1 = dpup
2 = dpright + dpup
3 = dpright
4 = dpright + dpdown
5 = dpdown
6 = dpdown+doleft
7 = dpleft
8 = dpleft + dpup
*/
// Other attempts at Hats / D-pad
// macOS sees the below as 4 additional switches (after the first 16)
// Android doesn't work (shows up pressed)
// 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
// 0x09, 0x01, /* USAGE (Pointer) */
// 0xA1, 0X00, /* COLLECTION (Physical) */
// 0x75, 0x01, /* REPORT_SIZE (1) */
// 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
// 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
// 0x35, 0x00, /* PHYSICAL_MINIMUM (0) */
// 0x45, 0x01, /* PHYSICAL__MAXIMUM (1) */
// 0x95, 0x04, /* REPORT_COUNT (4) */
// 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
// 0x09, 0x90, /* USAGE (D-PAD UP) */
// 0x09, 0x91, /* USAGE (D-PAD DOWN) */
// 0x09, 0x93, /* USAGE (D-PAD LEFT) */
// 0x09, 0x92, /* USAGE (D-PAD RIGHT) */
// 0x81, 0x02, /* INPUT (Data,Var,Abs) */
// 0xc0, /* END_COLLECTION */
// /* Padding */
// 0x75, 0x01,
// 0x95, 0x04,
// 0x81, 0x01,
// 0x95, 0x01, // Report Count (1) // Padding
// 0x75, 0x04, // Report Size (4)
// 0x81, 0x01, // Input (Constant) Reserved byte
// 0x09, 0x90, // USAGE (D Pad Up)
// 0x09, 0x91, // USAGE (D Pad Down)
// 0x09, 0x92, // USAGE (D Pad Right)
// 0x09, 0x93, // USAGE (D Pad Left)
// 0x15, 0x00, // LOGICAL_MINIMUM (0)
// 0x25, 0x01, // LOGICAL_MAXIMUM (1)
// 0x75, 0x01, // REPORT_SIZE (1)
// 0x95, 0x04, // REPORT_COUNT (4)
// 0x81, 0x02, // INPUT (Data,Var,Abs)
0xc0
};
GamepadReporter *GamepadReporter::reporter = NULL; // Singleton reference to the service
/**
*/
GamepadReporter *GamepadReporter::getInstance()
{
if (reporter == NULL)
{
reporter = new GamepadReporter();
}
return reporter;
}
GamepadReporter::GamepadReporter() :
HIDReporter("Gamepad", 7, gamepadReportMap, sizeof(gamepadReportMap), 7, 110) // Name and report size
{
}
void GamepadReporter::send(uint16_t buttons, uint8_t x, uint8_t y, uint8_t z, uint8_t rx, uint8_t dpad) {
// Little endian
// x/y/z/rx are absolute
memset(report, 0, reportSize);
report[0] = buttons&0xff;
report[1] = (buttons>>8)&0xff;
report[2] = x;
report[3] = y;
report[4] = z;
report[5] = rx;
// D-pad // 1-8 = directions (north going clockwise) Up Down Right Left
report[6] = dpad;
sendReport();
}
#endif