forked from espressif/arduino-esp32
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGamepad.ino
55 lines (51 loc) · 1.8 KB
/
Gamepad.ino
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
#if ARDUINO_USB_MODE
#warning This sketch should be used when USB is in OTG mode
void setup(){}
void loop(){}
#else
#include "USB.h"
#include "USBHIDGamepad.h"
USBHIDGamepad Gamepad;
const int buttonPin = 0;
int previousButtonState = HIGH;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
Gamepad.begin();
USB.begin();
Serial.begin(115200);
Serial.println("\n==================\nUSB Gamepad Testing\n==================\n");
Serial.println("Press BOOT Button to activate the USB gamepad.");
Serial.println("Longer press will change the affected button and controls.");
Serial.println("Shorter press/release just activates the button and controls.");
}
void loop() {
static uint8_t padID = 0;
static long lastPress = 0;
int buttonState = digitalRead(buttonPin);
if (buttonState != previousButtonState) {
if (buttonState == LOW) { // BOOT Button pressed
Gamepad.pressButton(padID); // Buttons 1 to 32
Gamepad.leftStick(padID << 3, padID << 3); // X Axis, Y Axis
Gamepad.rightStick(-(padID << 2), padID << 2); // Z Axis, Z Rotation
Gamepad.leftTrigger(padID << 4); // X Rotation
Gamepad.rightTrigger(-(padID << 4)); // Y Rotation
Gamepad.hat((padID & 0x7) + 1); // Point of View Hat
log_d("Pressed PadID [%d]", padID);
lastPress = millis();
} else {
Gamepad.releaseButton(padID);
Gamepad.leftStick(0, 0);
Gamepad.rightStick(0, 0);
Gamepad.leftTrigger(0);
Gamepad.rightTrigger(0);
Gamepad.hat(HAT_CENTER);
log_d("Released PadID [%d]\n", padID);
if (millis() - lastPress > 300) {
padID = (padID + 1) & 0x1F;
log_d("Changed padID to %d\n", padID);
}
}
}
previousButtonState = buttonState;
}
#endif /* ARDUINO_USB_MODE */