Skip to content

Commit

Permalink
Makes Gamepad example able to be tested with Windows 10/11 (espressif…
Browse files Browse the repository at this point in the history
…#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.
  • Loading branch information
SuGlider authored Apr 10, 2023
1 parent c7eeeda commit 224e778
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions libraries/USB/examples/Gamepad/Gamepad.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,41 @@ 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) && (buttonState == LOW)) {
Gamepad.pressButton(BUTTON_START);
Gamepad.releaseButton(BUTTON_START);
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;
}
Expand Down

0 comments on commit 224e778

Please sign in to comment.