Skip to content

Makes Gamepad example able to be tested with Windows 10/11 #8058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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