Skip to content

Commit

Permalink
Updated examples and Readme
Browse files Browse the repository at this point in the history
The examples and readme treated R/L triggers as signed chars, however they are now correctly referenced as chars (0 ~ 255)

The HID descriptor was correct, but now people using the library will have a better chance of using the triggers correctly ;)
  • Loading branch information
lemmingDev authored Nov 21, 2020
1 parent ce8cf9e commit 34fb08f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
*/

Expand All @@ -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);
}
}
Expand Down
14 changes: 7 additions & 7 deletions examples/Gamepad/Gamepad.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
*/

Expand All @@ -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);
}
}
42 changes: 42 additions & 0 deletions examples/SingleButtonDebounce/SingleButtonDebounce.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <Bounce2.h> // https://github.com/thomasfredericks/Bounce2
#include <BleGamepad.h> // 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);
}
}
}

0 comments on commit 34fb08f

Please sign in to comment.