Skip to content

Commit 71c9112

Browse files
authored
Update HIDGamepad.ino
1 parent 76746bf commit 71c9112

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

libraries/USB/examples/HIDGamepad/HIDGamepad.ino

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ uint8_t readHat() {
183183
int8_t readAxis(uint8_t pin) {
184184
uint16_t raw = analogRead(pin); // 0-4096 from microcontroller
185185
int mapped = map(raw, 0, 4095, -127, 127); // maps 12 bit analog from esp32 to 8 bit signed integer for packaging
186+
mapped = max(-127, min(127, mapped));
186187
return (int8_t)mapped;
187188
};
188189

@@ -191,19 +192,19 @@ void setup() {
191192
Serial.setDebugOutput(true);
192193

193194
// BUTTON PIN SETUP
194-
for (int i = 0; i < sizeof(buttonPins) / buttonPins[0]; i++) {
195+
for (int i = 0; i < sizeof(buttonPins) / sizeof(buttonPins[0]); i++) {
195196
if (buttonPins[i] == 255) {
196197
continue;
197198
} else {
198199
pinMode(buttonPins[i], INPUT_PULLUP); // Active LOW buttons
199200
}
200201
}
201202
// HAT PIN SETUP
202-
for (int i = 0; i < sizeof(hatPins) / hatPins[0]; i++) {
203+
for (int i = 0; i < sizeof(hatPins) / sizeof(hatPins[0]); i++) {
203204
pinMode(hatPins[i], INPUT_PULLUP); // Active LOW buttons
204205
}
205206
// AXIS PIN SETUP
206-
for (int i = 0; i < sizeof(axisPins) / axisPins[0]; i++) {
207+
for (int i = 0; i < sizeof(axisPins) / sizeof(axisPins[0]); i++) {
207208
pinMode(axisPins[i], INPUT); // Active LOW buttons
208209
}
209210
memset(&report, 0, sizeof(report));
@@ -216,9 +217,9 @@ void setup() {
216217
// ---------------- LOOP ----------------
217218
void loop() {
218219
// Read buttons
219-
for (int i = 0; i < sizeof(buttonPins) / buttonPins[0]; i++) {
220+
for (int i = 0; i < sizeof(buttonPins) / sizeof(buttonPins[0]); i++) {
220221
if (buttonPins[i] == 255) {
221-
continue
222+
continue;
222223
} else {
223224
bool state = digitalRead(buttonPins[i]);
224225
if (state == LOW) {
@@ -229,7 +230,7 @@ void loop() {
229230
}
230231
}
231232
// Fill report
232-
for (int i = 0; i < sizeof(axisPins) / axisPins[0]; i++) {report.axis[i] = readAxis(axisPins[i])}
233+
for (int i = 0; i < sizeof(axisPins) / sizeof(axisPins[0]); i++) {report.axis[i] = readAxis(axisPins[i]);}
233234
report.hat = readHat(); // neutral hat
234235
report.padding = 0;
235236
memcpy(report.buttons, buttons, 3);

0 commit comments

Comments
 (0)