-
Notifications
You must be signed in to change notification settings - Fork 7
/
UsbGamepad.cpp
258 lines (240 loc) · 5.22 KB
/
UsbGamepad.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include "UsbGamepad.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "tusb.h"
#include "usb_descriptors.h"
#include "class/hid/hid.h"
#include "class/hid/hid_device.h"
#include "utils.h"
UsbGamepad::UsbGamepad(uint8_t interfaceId, uint8_t reportId) :
interfaceId(interfaceId),
reportId(reportId),
currentDpad(),
currentButtons(0),
buttonsUpdated(true)
{
updateAllReleased();
}
bool UsbGamepad::isButtonPressed()
{
return (
currentDpad[DPAD_UP]
|| currentDpad[DPAD_DOWN]
|| currentDpad[DPAD_LEFT]
|| currentDpad[DPAD_RIGHT]
|| currentButtons != 0
|| isAnalogPressed(currentLeftAnalog[0])
|| isAnalogPressed(currentLeftAnalog[1])
|| currentLeftAnalog[2] > (MIN_ANALOG_VALUE + ANALOG_PRESSED_TOL)
|| isAnalogPressed(currentRightAnalog[0])
|| isAnalogPressed(currentRightAnalog[1])
|| currentRightAnalog[2] > (MIN_ANALOG_VALUE + ANALOG_PRESSED_TOL));
}
//--------------------------------------------------------------------+
// EXTERNAL API
//--------------------------------------------------------------------+
void UsbGamepad::setAnalogThumbX(bool isLeft, int8_t x)
{
x = limit_value(x, MIN_ANALOG_VALUE, MAX_ANALOG_VALUE);
int8_t lastX = 0;
if (isLeft)
{
lastX = currentLeftAnalog[0];
currentLeftAnalog[0] = x;
}
else
{
lastX = currentRightAnalog[0];
currentRightAnalog[0] = x;
}
buttonsUpdated = buttonsUpdated || (x != lastX);
}
void UsbGamepad::setAnalogThumbY(bool isLeft, int8_t y)
{
y = limit_value(y, MIN_ANALOG_VALUE, MAX_ANALOG_VALUE);
int8_t lastY = 0;
if (isLeft)
{
lastY = currentLeftAnalog[1];
currentLeftAnalog[1] = y;
}
else
{
lastY = currentRightAnalog[1];
currentRightAnalog[1] = y;
}
buttonsUpdated = buttonsUpdated || (y != lastY);
}
void UsbGamepad::setAnalogTrigger(bool isLeft, int8_t z)
{
z = limit_value(z, MIN_ANALOG_VALUE, MAX_ANALOG_VALUE);
int8_t lastZ = 0;
if (isLeft)
{
lastZ = currentLeftAnalog[2];
currentLeftAnalog[2] = z;
}
else
{
lastZ = currentRightAnalog[2];
currentRightAnalog[2] = z;
}
buttonsUpdated = buttonsUpdated || (z != lastZ);
}
int8_t UsbGamepad::getAnalogThumbX(bool isLeft)
{
if (isLeft)
{
return currentLeftAnalog[0];
}
else
{
return currentRightAnalog[0];
}
}
int8_t UsbGamepad::getAnalogThumbY(bool isLeft)
{
if (isLeft)
{
return currentLeftAnalog[1];
}
else
{
return currentRightAnalog[1];
}
}
int8_t UsbGamepad::getAnalogTrigger(bool isLeft)
{
if (isLeft)
{
return currentLeftAnalog[2];
}
else
{
return currentRightAnalog[2];
}
}
void UsbGamepad::setDigitalPad(UsbGamepad::DpadButtons button, bool isPressed)
{
bool oldValue = currentDpad[button];
currentDpad[button] = isPressed;
buttonsUpdated = buttonsUpdated || (oldValue != currentDpad[button]);
}
void UsbGamepad::setButtonMask(uint32_t mask, bool isPressed)
{
uint32_t lastButtons = currentButtons;
if (isPressed)
{
currentButtons |= mask;
}
else
{
currentButtons &= ~mask;
}
buttonsUpdated = buttonsUpdated || (lastButtons != currentButtons);
}
void UsbGamepad::setButton(uint8_t button, bool isPressed)
{
setButtonMask(1 << button, isPressed);
}
void UsbGamepad::updateAllReleased()
{
if (isButtonPressed())
{
currentLeftAnalog[0] = 0;
currentLeftAnalog[1] = 0;
currentLeftAnalog[2] = MIN_ANALOG_VALUE;
currentRightAnalog[0] = 0;
currentRightAnalog[1] = 0;
currentRightAnalog[2] = MIN_ANALOG_VALUE;
currentDpad[DPAD_UP] = false;
currentDpad[DPAD_DOWN] = false;
currentDpad[DPAD_LEFT] = false;
currentDpad[DPAD_RIGHT] = false;
currentButtons = 0;
buttonsUpdated = true;
}
}
uint8_t UsbGamepad::getHatValue()
{
if (currentDpad[DPAD_UP])
{
if (currentDpad[DPAD_LEFT])
{
return GAMEPAD_HAT_UP_LEFT;
}
else if (currentDpad[DPAD_RIGHT])
{
return GAMEPAD_HAT_UP_RIGHT;
}
else
{
return GAMEPAD_HAT_UP;
}
}
else if (currentDpad[DPAD_DOWN])
{
if (currentDpad[DPAD_LEFT])
{
return GAMEPAD_HAT_DOWN_LEFT;
}
else if (currentDpad[DPAD_RIGHT])
{
return GAMEPAD_HAT_DOWN_RIGHT;
}
else
{
return GAMEPAD_HAT_DOWN;
}
}
else if (currentDpad[DPAD_LEFT])
{
return GAMEPAD_HAT_LEFT;
}
else if (currentDpad[DPAD_RIGHT])
{
return GAMEPAD_HAT_RIGHT;
}
else
{
return GAMEPAD_HAT_CENTERED;
}
}
bool UsbGamepad::send(bool force)
{
if (buttonsUpdated || force)
{
bool sent = sendReport(interfaceId, reportId);
if (sent)
{
buttonsUpdated = false;
}
return sent;
}
else
{
return true;
}
}
uint8_t UsbGamepad::getReportSize()
{
return sizeof(hid_gamepad_report_t);
}
uint16_t UsbGamepad::getReport(uint8_t *buffer, uint16_t reqlen)
{
// Build the report
hid_gamepad_report_t report;
report.x = currentLeftAnalog[0];
report.y = currentLeftAnalog[1];
report.z = currentLeftAnalog[2];
report.rz = currentRightAnalog[2];
report.rx = currentRightAnalog[0];
report.ry = currentRightAnalog[1];
report.hat = getHatValue();
report.buttons = currentButtons;
// Copy report into buffer
uint16_t setLen = (sizeof(report) <= reqlen) ? sizeof(report) : reqlen;
memcpy(buffer, &report, setLen);
return setLen;
}