diff --git a/README.md b/README.md index aaa7bb4..88eef64 100644 --- a/README.md +++ b/README.md @@ -30,13 +30,13 @@ This library allows you to make the ESP32 act as a Bluetooth Gamepad and control * This example turns the ESP32 into a Bluetooth LE gamepad that presses buttons and moves axis * * Possible buttons are: - * BUTTON_1 through to BUTTON_14* + * BUTTON_1 through to BUTTON_14 * * Possible DPAD/HAT switch position values are: * DPAD_CENTERED, DPAD_UP, DPAD_UP_RIGHT, DPAD_RIGHT, DPAD_DOWN_RIGHT, * DPAD_DOWN, DPAD_DOWN_LEFT, DPAD_LEFT, DPAD_UP_LEFT * - * bleGamepad.setAxes takes the following signed char parameters: + * bleGamepad.setAxes takes the following signed char parameters for the Left/Right Thumb X/Y, char for the Left/Right Triggers, and hat switch position as above: * (Left Thumb X, Left Thumb Y, Right Thumb X, Right Thumb Y, Left Trigger, Right Trigger, Hat switch position); */ @@ -53,14 +53,14 @@ void setup() { void loop() { if(bleGamepad.isConnected()) { Serial.println("Press buttons 1 and 14. Move all axes to max. Set DPAD to down right."); - bleGamepad.press(BUTTON_14); bleGamepad.press(BUTTON_1); - bleGamepad.setAxes(127, 127, 127, 127, 127, 127, DPAD_DOWN_RIGHT); + bleGamepad.press(BUTTON_14); + bleGamepad.setAxes(127, 127, 127, 127, 255, 255, DPAD_DOWN_RIGHT); delay(500); - Serial.println("Release button 14. Move all axes to min. Set DPAD to centred."); - bleGamepad.release(BUTTON_14); - bleGamepad.setAxes(-127, -127, -127, -127, -127, -127, DPAD_CENTERED); + Serial.println("Release button 1. Move all axes to min. Set DPAD to centred."); + bleGamepad.release(BUTTON_1); + bleGamepad.setAxes(-127, -127, -127, -127, 0, 0, DPAD_CENTERED); delay(500); } } diff --git a/examples/Gamepad/Gamepad.ino b/examples/Gamepad/Gamepad.ino index 2cc2b88..2e5f22d 100644 --- a/examples/Gamepad/Gamepad.ino +++ b/examples/Gamepad/Gamepad.ino @@ -2,13 +2,13 @@ * This example turns the ESP32 into a Bluetooth LE gamepad that presses buttons and moves axis * * Possible buttons are: - * BUTTON_1 through to BUTTON_14* + * BUTTON_1 through to BUTTON_14 * * Possible DPAD/HAT switch position values are: * DPAD_CENTERED, DPAD_UP, DPAD_UP_RIGHT, DPAD_RIGHT, DPAD_DOWN_RIGHT, * DPAD_DOWN, DPAD_DOWN_LEFT, DPAD_LEFT, DPAD_UP_LEFT * - * bleGamepad.setAxes takes the following signed char parameters: + * bleGamepad.setAxes takes the following signed char parameters for the Left/Right Thumb X/Y, char for the Left/Right Triggers, and hat switch position as above: * (Left Thumb X, Left Thumb Y, Right Thumb X, Right Thumb Y, Left Trigger, Right Trigger, Hat switch position); */ @@ -25,14 +25,14 @@ void setup() { void loop() { if(bleGamepad.isConnected()) { Serial.println("Press buttons 1 and 14. Move all axes to max. Set DPAD to down right."); - bleGamepad.press(BUTTON_14); bleGamepad.press(BUTTON_1); - bleGamepad.setAxes(127, 127, 127, 127, 127, 127, DPAD_DOWN_RIGHT); + bleGamepad.press(BUTTON_14); + bleGamepad.setAxes(127, 127, 127, 127, 255, 255, DPAD_DOWN_RIGHT); delay(500); - Serial.println("Release button 14. Move all axes to min. Set DPAD to centred."); - bleGamepad.release(BUTTON_14); - bleGamepad.setAxes(-127, -127, -127, -127, -127, -127, DPAD_CENTERED); + Serial.println("Release button 1. Move all axes to min. Set DPAD to centred."); + bleGamepad.release(BUTTON_1); + bleGamepad.setAxes(-127, -127, -127, -127, 0, 0, DPAD_CENTERED); delay(500); } } \ No newline at end of file diff --git a/examples/SingleButtonDebounce/SingleButtonDebounce.ino b/examples/SingleButtonDebounce/SingleButtonDebounce.ino new file mode 100644 index 0000000..b8b97b4 --- /dev/null +++ b/examples/SingleButtonDebounce/SingleButtonDebounce.ino @@ -0,0 +1,42 @@ +#include // https://github.com/thomasfredericks/Bounce2 +#include // https://github.com/lemmingDev/ESP32-BLE-Gamepad + +#define BUTTON_PIN 2 +#define LED_PIN 13 + +Bounce debouncer = Bounce(); // Instantiate a Bounce object +BleGamepad bleGamepad; // Instantiate a BleGamepad object + +void setup() +{ + bleGamepad.begin(); // Begin the gamepad + + pinMode(BUTTON_PIN, INPUT_PULLUP); // Setup the button with an internal pull-up + + debouncer.attach(BUTTON_PIN); // After setting up the button, setup the Bounce instance : + debouncer.interval(5); // interval in ms + + pinMode(LED_PIN, OUTPUT); // Setup the LED : +} + +void loop() +{ + if(bleGamepad.isConnected()) + { + debouncer.update(); // Update the Bounce instance + + int value = debouncer.read(); // Get the updated value + + // Press gamepad button and turn on or off the LED as determined by the state + if (value == LOW) + { + digitalWrite(LED_PIN, HIGH); + bleGamepad.press(BUTTON_1); + } + else + { + digitalWrite(LED_PIN, LOW); + bleGamepad.release(BUTTON_1); + } + } +}