Skip to content

Commit 224e778

Browse files
authored
Makes Gamepad example able to be tested with Windows 10/11 (espressif#8058)
* Makes sure it can be tested with Windows 10/11 Initial code had no effect with Win10/11 because BUTTON_START was not recognized. This change makes it visible in the Windows Game Controller Testing TAB. * Examples tests all USB gamepad APIs. It is possible to change the selected gamepad button when pressing BOOT (long/short press). The selected button is used as parameter to change R/L Stick and Trigger as well as the Hat.
1 parent c7eeeda commit 224e778

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

libraries/USB/examples/Gamepad/Gamepad.ino

+31-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,41 @@ void setup() {
1414
pinMode(buttonPin, INPUT_PULLUP);
1515
Gamepad.begin();
1616
USB.begin();
17+
Serial.begin(115200);
18+
Serial.println("\n==================\nUSB Gamepad Testing\n==================\n");
19+
Serial.println("Press BOOT Button to activate the USB gamepad.");
20+
Serial.println("Longer press will change the affected button and controls.");
21+
Serial.println("Shorter press/release just activates the button and controls.");
1722
}
1823

1924
void loop() {
25+
static uint8_t padID = 0;
26+
static long lastPress = 0;
27+
2028
int buttonState = digitalRead(buttonPin);
21-
if ((buttonState != previousButtonState) && (buttonState == LOW)) {
22-
Gamepad.pressButton(BUTTON_START);
23-
Gamepad.releaseButton(BUTTON_START);
29+
if (buttonState != previousButtonState) {
30+
if (buttonState == LOW) { // BOOT Button pressed
31+
Gamepad.pressButton(padID); // Buttons 1 to 32
32+
Gamepad.leftStick(padID << 3, padID << 3); // X Axis, Y Axis
33+
Gamepad.rightStick(-(padID << 2), padID << 2); // Z Axis, Z Rotation
34+
Gamepad.leftTrigger(padID << 4); // X Rotation
35+
Gamepad.rightTrigger(-(padID << 4)); // Y Rotation
36+
Gamepad.hat((padID & 0x7) + 1); // Point of View Hat
37+
log_d("Pressed PadID [%d]", padID);
38+
lastPress = millis();
39+
} else {
40+
Gamepad.releaseButton(padID);
41+
Gamepad.leftStick(0, 0);
42+
Gamepad.rightStick(0, 0);
43+
Gamepad.leftTrigger(0);
44+
Gamepad.rightTrigger(0);
45+
Gamepad.hat(HAT_CENTER);
46+
log_d("Released PadID [%d]\n", padID);
47+
if (millis() - lastPress > 300) {
48+
padID = (padID + 1) & 0x1F;
49+
log_d("Changed padID to %d\n", padID);
50+
}
51+
}
2452
}
2553
previousButtonState = buttonState;
2654
}

0 commit comments

Comments
 (0)