From e49edf0b4e6db0677b0ef83fd4751171fccd7e98 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 10 Apr 2023 11:44:34 -0300 Subject: [PATCH 1/2] 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. --- libraries/USB/examples/Gamepad/Gamepad.ino | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/libraries/USB/examples/Gamepad/Gamepad.ino b/libraries/USB/examples/Gamepad/Gamepad.ino index dad75723797..c5dd4e03db1 100644 --- a/libraries/USB/examples/Gamepad/Gamepad.ino +++ b/libraries/USB/examples/Gamepad/Gamepad.ino @@ -7,7 +7,9 @@ void loop(){} #include "USBHIDGamepad.h" USBHIDGamepad Gamepad; -const int buttonPin = 0; +// This sketch works correctly for the ESP32-S2 and ESP32-S3 in OTG mode (TinyUSB) + +const int buttonPin = 0; // GPIO 0 is the BOOT button of the board int previousButtonState = HIGH; void setup() { @@ -18,9 +20,14 @@ void setup() { void loop() { 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 + // changes many states + Gamepad.send(-100, 100, 50, 100, -100, -50, HAT_DOWN_RIGHT, BUTTON_TL); + } else { + // restores neutral states + Gamepad.send(0, 0, 0, 0, 0, 0, 0, 0); + } } previousButtonState = buttonState; } From 7dd22d14c7caaf64f1371a04fb89bfc67b4008d7 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 10 Apr 2023 15:58:08 -0300 Subject: [PATCH 2/2] 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. --- libraries/USB/examples/Gamepad/Gamepad.ino | 35 +++++++++++++++++----- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/libraries/USB/examples/Gamepad/Gamepad.ino b/libraries/USB/examples/Gamepad/Gamepad.ino index c5dd4e03db1..fa2a2a3bc6f 100644 --- a/libraries/USB/examples/Gamepad/Gamepad.ino +++ b/libraries/USB/examples/Gamepad/Gamepad.ino @@ -7,26 +7,47 @@ void loop(){} #include "USBHIDGamepad.h" USBHIDGamepad Gamepad; -// This sketch works correctly for the ESP32-S2 and ESP32-S3 in OTG mode (TinyUSB) - -const int buttonPin = 0; // GPIO 0 is the BOOT button of the board +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 - // changes many states - Gamepad.send(-100, 100, 50, 100, -100, -50, HAT_DOWN_RIGHT, BUTTON_TL); + 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 { - // restores neutral states - Gamepad.send(0, 0, 0, 0, 0, 0, 0, 0); + 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;