@@ -183,6 +183,7 @@ uint8_t readHat() {
183
183
int8_t readAxis (uint8_t pin) {
184
184
uint16_t raw = analogRead (pin); // 0-4096 from microcontroller
185
185
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));
186
187
return (int8_t )mapped;
187
188
};
188
189
@@ -191,19 +192,19 @@ void setup() {
191
192
Serial.setDebugOutput (true );
192
193
193
194
// 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++) {
195
196
if (buttonPins[i] == 255 ) {
196
197
continue ;
197
198
} else {
198
199
pinMode (buttonPins[i], INPUT_PULLUP); // Active LOW buttons
199
200
}
200
201
}
201
202
// 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++) {
203
204
pinMode (hatPins[i], INPUT_PULLUP); // Active LOW buttons
204
205
}
205
206
// 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++) {
207
208
pinMode (axisPins[i], INPUT); // Active LOW buttons
208
209
}
209
210
memset (&report, 0 , sizeof (report));
@@ -216,9 +217,9 @@ void setup() {
216
217
// ---------------- LOOP ----------------
217
218
void loop () {
218
219
// 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++) {
220
221
if (buttonPins[i] == 255 ) {
221
- continue
222
+ continue ;
222
223
} else {
223
224
bool state = digitalRead (buttonPins[i]);
224
225
if (state == LOW) {
@@ -229,7 +230,7 @@ void loop() {
229
230
}
230
231
}
231
232
// 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]); }
233
234
report.hat = readHat (); // neutral hat
234
235
report.padding = 0 ;
235
236
memcpy (report.buttons , buttons, 3 );
0 commit comments