Skip to content

Commit

Permalink
Update examples for configurable HID
Browse files Browse the repository at this point in the history
  • Loading branch information
lemmingDev authored Apr 9, 2021
1 parent 0a8f8ea commit 53e11c7
Show file tree
Hide file tree
Showing 11 changed files with 358 additions and 79 deletions.
106 changes: 106 additions & 0 deletions examples/DrivingControllerTest/DrivingControllerTest.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Driving controller test
*/

#include <BleGamepad.h>

#define numOfButtons 10
#define numOfHatSwitches 0
#define enableX false
#define enableY false
#define enableZ false
#define enableRZ false
#define enableRX false
#define enableRY false
#define enableSlider1 false
#define enableSlider2 false
#define enableRudder false
#define enableThrottle false
#define enableAccelerator true
#define enableBrake true
#define enableSteering true

BleGamepad bleGamepad("BLE Driving Controller", "lemmingDev", 100);

void setup()
{
Serial.begin(115200);
Serial.println("Starting BLE work!");

//Setup controller with 10 buttons, accelerator, brake and steering
bleGamepad.setAutoReport(false);
bleGamepad.setControllerType(CONTROLLER_TYPE_GAMEPAD); //CONTROLLER_TYPE_JOYSTICK, CONTROLLER_TYPE_GAMEPAD (DEFAULT), CONTROLLER_TYPE_MULTI_AXIS
bleGamepad.begin(numOfButtons,numOfHatSwitches,enableX,enableY,enableZ,enableRZ,enableRX,enableRY,enableSlider1,enableSlider2,enableRudder,enableThrottle,enableAccelerator,enableBrake,enableSteering);

//Set accelerator and brake to min
bleGamepad.setAccelerator(-32767);
bleGamepad.setBrake(-32767);

//Set steering to center
bleGamepad.setSteering(0);
}

void loop()
{
if(bleGamepad.isConnected())
{
Serial.println("Press all buttons one by one");
for(int i = 1 ; i <= numOfButtons ; i += 1)
{
bleGamepad.press(i);
bleGamepad.sendReport();
delay(100);
bleGamepad.release(i);
bleGamepad.sendReport();
delay(25);
}

Serial.println("Move steering from center to max");
for(int i = 0 ; i > -32767 ; i -= 256)
{
bleGamepad.setSteering(i);
bleGamepad.sendReport();
delay(10);
}

Serial.println("Move steering from min to max");
for(int i = -32767 ; i < 32767 ; i += 256)
{
bleGamepad.setSteering(i);
bleGamepad.sendReport();
delay(10);
}

Serial.println("Move steering from max to center");
for(int i = 32767 ; i > 0 ; i -= 256)
{
bleGamepad.setSteering(i);
bleGamepad.sendReport();
delay(10);
}
bleGamepad.setSteering(0);
bleGamepad.sendReport();

Serial.println("Move accelerator from min to max");
//for(int i = 32767 ; i > -32767 ; i -= 256) //Use this for loop setup instead if accelerator is reversed
for(int i = -32767 ; i < 32767 ; i += 256)
{
bleGamepad.setAccelerator(i);
bleGamepad.sendReport();
delay(10);
}
bleGamepad.setAccelerator(-32767);
bleGamepad.sendReport();


Serial.println("Move brake from min to max");
for(int i = -32767 ; i < 32767 ; i += 256)
{
bleGamepad.setBrake(i);
bleGamepad.sendReport();
delay(10);
}
bleGamepad.setBrake(-32767);
bleGamepad.sendReport();
}
}
135 changes: 135 additions & 0 deletions examples/FlightControllerTest/FlightControllerTest.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Flight controller test
*/

#include <BleGamepad.h>

#define numOfButtons 32
#define numOfHatSwitches 0
#define enableX true
#define enableY true
#define enableZ false
#define enableRZ false
#define enableRX false
#define enableRY false
#define enableSlider1 false
#define enableSlider2 false
#define enableRudder true
#define enableThrottle true
#define enableAccelerator false
#define enableBrake false
#define enableSteering false

BleGamepad bleGamepad("BLE Flight Controller", "lemmingDev", 100);

void setup()
{
Serial.begin(115200);
Serial.println("Starting BLE work!");

//Setup controller with 10 buttons, accelerator, brake and steering
bleGamepad.setAutoReport(false);
bleGamepad.setControllerType(CONTROLLER_TYPE_MULTI_AXIS); //CONTROLLER_TYPE_JOYSTICK, CONTROLLER_TYPE_GAMEPAD (DEFAULT), CONTROLLER_TYPE_MULTI_AXIS
bleGamepad.begin(numOfButtons,numOfHatSwitches,enableX,enableY,enableZ,enableRZ,enableRX,enableRY,enableSlider1,enableSlider2,enableRudder,enableThrottle,enableAccelerator,enableBrake,enableSteering);

//Set throttle to min
bleGamepad.setThrottle(-32767);

//Set x and y axes and rudder to center
bleGamepad.setX(0);
bleGamepad.setY(0);
bleGamepad.setRudder(0);
}

void loop()
{
if(bleGamepad.isConnected())
{
Serial.println("Press all buttons one by one");
for(int i = 1 ; i <= numOfButtons ; i += 1)
{
bleGamepad.press(i);
bleGamepad.sendReport();
delay(100);
bleGamepad.release(i);
bleGamepad.sendReport();
delay(25);
}

Serial.println("Move x axis from center to max");
for(int i = 0 ; i > -32767 ; i -= 256)
{
bleGamepad.setX(i);
bleGamepad.sendReport();
delay(10);
}

Serial.println("Move x axis from min to max");
for(int i = -32767 ; i < 32767 ; i += 256)
{
bleGamepad.setX(i);
bleGamepad.sendReport();
delay(10);
}

Serial.println("Move x axis from max to center");
for(int i = 32767 ; i > 0 ; i -= 256)
{
bleGamepad.setX(i);
bleGamepad.sendReport();
delay(10);
}
bleGamepad.setX(0);
bleGamepad.sendReport();


Serial.println("Move y axis from center to max");
for(int i = 0 ; i > -32767 ; i -= 256)
{
bleGamepad.setY(i);
bleGamepad.sendReport();
delay(10);
}

Serial.println("Move y axis from min to max");
for(int i = -32767 ; i < 32767 ; i += 256)
{
bleGamepad.setY(i);
bleGamepad.sendReport();
delay(10);
}

Serial.println("Move y axis from max to center");
for(int i = 32767 ; i > 0 ; i -= 256)
{
bleGamepad.setY(i);
bleGamepad.sendReport();
delay(10);
}
bleGamepad.setY(0);
bleGamepad.sendReport();


Serial.println("Move rudder from min to max");
//for(int i = 32767 ; i > -32767 ; i -= 256) //Use this for loop setup instead if rudder is reversed
for(int i = -32767 ; i < 32767 ; i += 256)
{
bleGamepad.setRudder(i);
bleGamepad.sendReport();
delay(10);
}
bleGamepad.setRudder(0);
bleGamepad.sendReport();


Serial.println("Move throttle from min to max");
for(int i = -32767 ; i < 32767 ; i += 256)
{
bleGamepad.setThrottle(i);
bleGamepad.sendReport();
delay(10);
}
bleGamepad.setThrottle(-32767);
bleGamepad.sendReport();
}
}
25 changes: 17 additions & 8 deletions examples/Gamepad/Gamepad.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
* This example turns the ESP32 into a Bluetooth LE gamepad that presses buttons and moves axis
*
* Possible buttons are:
* BUTTON_1 through to BUTTON_64
* BUTTON_1 through to BUTTON_16
* (16 buttons supported by default. Library can be configured to support up to 128)
*
* 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
* (or HAT_CENTERED, HAT_UP etc)
*
* bleGamepad.setAxes takes the following int16_t parameters for the Left/Right Thumb X/Y, uint16_t for the Left/Right Triggers plus slider1 and slider2, and hat switch position as above:
* (Left Thumb X, Left Thumb Y, Right Thumb X, Right Thumb Y, Left Trigger, Right Trigger, Hat switch positions (hat1, hat2, hat3, hat4));
* bleGamepad.setAxes takes the following int16_t parameters for the Left/Right Thumb X/Y, Left/Right Triggers plus slider1 and slider2, 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
^ (1 hat switch (dpad) supported by default. Library can be configured to support up to 4)
*
* Library can also be configured to support up to 5 simulation controls (can be set with setSimulationControls)
* (rudder, throttle, accelerator, brake, steering), but they are not enabled by default.
*/

#include <BleGamepad.h>
Expand All @@ -21,22 +26,26 @@ void setup()
Serial.begin(115200);
Serial.println("Starting BLE work!");
bleGamepad.begin();
// The default bleGamepad.begin() above is the same as bleGamepad.begin(16, 1, true, true, true, true, true, true, true, true, false, false, false, false, false);
// which enables a gamepad with 16 buttons, 1 hat switch, enabled x, y, z, rZ, rX, rY, slider 1, slider 2 and disabled rudder, throttle, accelerator, brake, steering
// Auto reporting is enabled by default.
// Use bleGamepad.setAutoReport(false); to disable auto reporting, and then use bleGamepad.sendReport(); as needed
}

void loop()
{
if(bleGamepad.isConnected())
{
Serial.println("Press buttons 5 and 32. Move all axes to max. Set DPAD (hat 1) to down right.");
Serial.println("Press buttons 5 and 16. Move all enabled axes to max. Set DPAD (hat 1) to down right.");
bleGamepad.press(BUTTON_5);
bleGamepad.press(BUTTON_32);
bleGamepad.setAxes(32767, 32767, 32767, 32767, 65535, 65535, 65535, 65535, DPAD_DOWN_RIGHT); //(can also optionally set hat2/3/4 after DPAD/hat1 as seen below)
// All axes, sliders, hats can also be set independently. See the IndividualAxes.ino example
bleGamepad.press(BUTTON_16);
bleGamepad.setAxes(32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, DPAD_DOWN_RIGHT);
// All axes, sliders, hats etc can also be set independently. See the IndividualAxes.ino example
delay(500);

Serial.println("Release button 5. Move all axes to min. Set DPAD (hat 1) to centred.");
bleGamepad.release(BUTTON_5);
bleGamepad.setAxes(-32767, -32767, -32767, -32767, 0, 0, 0, 0, DPAD_CENTERED, HAT_CENTERED, HAT_CENTERED, HAT_CENTERED);
bleGamepad.setAxes(-32767, -32767, -32767, -32767, -32767, -32767, -32767, -32767, DPAD_CENTERED);
delay(500);
}
}
45 changes: 25 additions & 20 deletions examples/IndividualAxes/IndividualAxes.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@
* This example turns the ESP32 into a Bluetooth LE gamepad that presses buttons and moves axis
*
* Possible buttons are:
* BUTTON_1 through to BUTTON_64 (Windows gamepad tester only visualised the first 32)
* BUTTON_1 through to BUTTON_128 (Windows gamepad tester only visualises the first 32)
^ Use http://www.planetpointy.co.uk/joystick-test-application/ to visualise all of them
* Whenever you adjust the amount of buttons/axes etc, make sure you unpair and repair the BLE device
*
* 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 int16_t parameters for the Left/Right Thumb X/Y, uint16_t for the Left/Right Triggers plus slider1 and slider2, and hat1/hat2/hat3/hat4 switch positions as above:
* bleGamepad.setAxes takes the following int16_t parameters for the Left/Right Thumb X/Y, Left/Right Triggers plus slider1 and slider2, and hat1/hat2/hat3/hat4 switch positions as above:
* (Left Thumb X, Left Thumb Y, Right Thumb X, Right Thumb Y, Left Trigger, Right Trigger, Slider 1, Slider 2, Hat1 switch position, Hat2 switch position, Hat3 switch position, Hat4 switch position);
*
* bleGamepad.setLeftThumb (or setRightThumb) takes 2 int16_t parameters for x and y axes (or z and rZ axes)
*
* bleGamepad.setLeftTrigger (or setRightTrigger) takes 1 uint16_t parameter for rX axis (or rY axis)
* bleGamepad.setLeftTrigger (or setRightTrigger) takes 1 int16_t parameter for rX axis (or rY axis)
*
* bleGamepad.setSlider1 (or setSlider2) takes 1 uint16_t parameter for slider 1 (or slider 2)
* bleGamepad.setSlider1 (or setSlider2) takes 1 int16_t parameter for slider 1 (or slider 2)
*
* bleGamepad.setHat1 takes a hat position as above (or 0 = centered and 1~8 are the 8 possible directions)
*
* setHats amd setSliders functions are also available for setting all hats/sliders at once
* setHats, setTriggers and setSliders functions are also available for setting all hats/triggers/sliders at once
*
* The example shows that you can set axes/hats independantly, or together.
*
Expand All @@ -34,40 +36,43 @@ void setup()
{
Serial.begin(115200);
Serial.println("Starting BLE work!");
bleGamepad.begin();
bleGamepad.setAutoReport(false); //This is true by default. You can also set this in the line above --> bleGamepad.begin(false);
bleGamepad.begin(128, 2); //Creates a gamepad with 128 buttons, 2 hat switches and x, y, z, rZ, rX, rY and 2 sliders (no simulation controls enabled by default)
bleGamepad.setAutoReport(false); //This is true by default
}

void loop()
{
if(bleGamepad.isConnected())
{
Serial.println("Press buttons 1, 32 and 64. Set DPAD to down right.");
Serial.println("Press buttons 1, 32, 64 and 128. Set hat 1 to down right and hat 2 to up left");

//Press buttons 5, 32 and 64
//Press buttons 5, 32, 64 and 128
bleGamepad.press(BUTTON_5);
bleGamepad.press(BUTTON_32);
bleGamepad.press(BUTTON_64);
bleGamepad.press(BUTTON_128);

//Move all axes to max.
bleGamepad.setLeftThumb(32767, 32767);
bleGamepad.setRightThumb(32767, 32767);
bleGamepad.setLeftTrigger(65535);
bleGamepad.setRightTrigger(65535);
bleGamepad.setSlider1(65535);
bleGamepad.setSlider2(65535);
bleGamepad.setLeftThumb(32767, 32767); //or bleGamepad.setX(32767); and bleGamepad.setY(32767);
bleGamepad.setRightThumb(32767, 32767); //or bleGamepad.setZ(32767); and bleGamepad.setRZ(32767);
bleGamepad.setLeftTrigger(32767); //or bleGamepad.setRX(32767);
bleGamepad.setRightTrigger(32767); //or bleGamepad.setRY(32767);
bleGamepad.setSlider1(32767);
bleGamepad.setSlider2(32767);

//Set hat 1 to down right (hats are otherwise centered by default)
bleGamepad.setHat1(DPAD_DOWN_RIGHT);

//Set hat 1 to down right and hat 2 to up left (hats are otherwise centered by default)
bleGamepad.setHat1(DPAD_DOWN_RIGHT); //or bleGamepad.setHat1(HAT_DOWN_RIGHT);
bleGamepad.setHat2(DPAD_UP_LEFT); //or bleGamepad.setHat2(HAT_UP_LEFT);
//Or bleGamepad.setHats(DPAD_DOWN_RIGHT, DPAD_UP_LEFT);

//Send the gamepad report
bleGamepad.sendReport();
delay(500);

Serial.println("Release button 5 and 64. Move all axes to min. Set DPAD to centred.");
Serial.println("Release button 5 and 64. Move all axes to min. Set hat 1 and 2 to centred.");
bleGamepad.release(BUTTON_5);
bleGamepad.release(BUTTON_64);
bleGamepad.setAxes(-32767, -32767, -32767, -32767, 0, 0, 0, 0, DPAD_CENTERED);
bleGamepad.setAxes(-32767, -32767, -32767, -32767, -32767, -32767, -32767, -32767, DPAD_CENTERED, HAT_CENTERED);
bleGamepad.sendReport();
delay(500);
}
Expand Down
Loading

0 comments on commit 53e11c7

Please sign in to comment.