Files
ESP32-BLE-Gamepad/examples/MultipleButtons/MultipleButtons.ino
dexterdy acee5ab5ea Seperated configuration from base class for convenience. Added support for special buttons (#97)
* added support for start and select button

* Added the things I forgot

* changed header

* edited the main file to add support for the different special buttons

* added the press and release functions

* added stuff to make it more complete. not done yet

* this should do it

* added back button

* made the indentation consistent

* small commit so i can continue on my other pc

* finished the header for the config class

* started working on converting existing code with new config class

* replaced all _include variable with calls to the config class

* fixed constructor

* removed setAutoReport function

* fixed a bunch of dumb mistakes

* fixed a bunch of dumb mistakes

* made the indentation consistent

* small commit so i can continue on my other pc

* finished the header for the config class

* started working on converting existing code with new config class

* replaced all _include variable with calls to the config class

* removed setAutoReport function

* fixed a bunch of dumb mistakes

* added report id to list of configurable options

* actually finished all the getters and setters from the config class

* finished the configuration class

* forgot controllertype config

* updated the examples

* added my own example

* added home test to example

* ran an autoformatter

* added a comment (examples)

* more readable copy-by-value

* fixed circular dependancy problem

* spelling mistake

Co-authored-by: Hagedoorn, J.P. (pieter) <j.p.hagedoorn@students.uu.nl>
2022-05-01 02:14:13 +10:00

72 lines
2.2 KiB
C++

/*
* This code programs a number of pins on an ESP32 as buttons on a BLE gamepad
*
* It uses arrays to cut down on code
*
* Before using, adjust the numOfButtons, buttonPins and physicalButtons to suit your senario
*
*/
#include <Arduino.h>
#include <BleGamepad.h> // https://github.com/lemmingDev/ESP32-BLE-Gamepad
BleGamepad bleGamepad;
#define numOfButtons 10
byte previousButtonStates[numOfButtons];
byte currentButtonStates[numOfButtons];
byte buttonPins[numOfButtons] = {0, 35, 17, 18, 19, 23, 25, 26, 27, 32};
byte physicalButtons[numOfButtons] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
void setup()
{
for (byte currentPinIndex = 0; currentPinIndex < numOfButtons; currentPinIndex++)
{
pinMode(buttonPins[currentPinIndex], INPUT_PULLUP);
previousButtonStates[currentPinIndex] = HIGH;
currentButtonStates[currentPinIndex] = HIGH;
}
BleGamepadConfiguration bleGamepadConfig;
bleGamepadConfig.setAutoReport(false);
bleGamepadConfig.setButtonCount(numOfButtons);
bleGamepad.begin(bleGamepadConfig);
// changing bleGamepadConfig after the begin function has no effect, unless you call the begin function again
}
void loop()
{
if (bleGamepad.isConnected())
{
for (byte currentIndex = 0; currentIndex < numOfButtons; currentIndex++)
{
currentButtonStates[currentIndex] = digitalRead(buttonPins[currentIndex]);
if (currentButtonStates[currentIndex] != previousButtonStates[currentIndex])
{
if (currentButtonStates[currentIndex] == LOW)
{
bleGamepad.press(physicalButtons[currentIndex]);
}
else
{
bleGamepad.release(physicalButtons[currentIndex]);
}
}
}
if (currentButtonStates != previousButtonStates)
{
for (byte currentIndex = 0; currentIndex < numOfButtons; currentIndex++)
{
previousButtonStates[currentIndex] = currentButtonStates[currentIndex];
}
bleGamepad.sendReport();
}
delay(20);
}
}