/* Keystroke inputs for controlling VLC via bluetooth
* Test program to use game controller buttons as Bluetooth keyboard input
20240513 Final program works
Cannot find BLEKeyboard.h to compile on Wokwi
https://github.com/T-vK/ESP32-BLE-Keyboard ?? possibly
*/
#define USE_NIMBLE
#include <BleKeyboard.h>
BleKeyboard bleKeyboard;
#define GP_VOL_UP 16
#define GP_VOL_DOWN 17
#define GP_PAUSE_PLAY 18
#define GP_REWIND_10s 19
#define GP_SLOW_SPEED 21
#define GP_NORMAL_SPEED 22
bool keyStates[6] = {false, false, false, false, false, false};
int keyPins[6] = {GP_VOL_UP, GP_VOL_DOWN, GP_PAUSE_PLAY, GP_REWIND_10s, GP_SLOW_SPEED, GP_NORMAL_SPEED};
uint8_t keyCodes[6] = {'2', '3', ' ', '1', '--', '='};
void setup() {
Serial.begin(115200);
Serial.println("Code running...");
setInputs();
bleKeyboard.begin();
}
bool connectNotificationSent = false;
void loop() {
int counter;
if(bleKeyboard.isConnected()) {
if (!connectNotificationSent) {
Serial.println("Code connected...");
connectNotificationSent = true;
}
for(counter = 0; counter < 6; counter ++){
handleButton(counter);
}
}
}
void setInputs() {
pinMode(GP_VOL_UP, INPUT_PULLUP);
pinMode(GP_VOL_DOWN, INPUT_PULLUP);
pinMode(GP_PAUSE_PLAY, INPUT_PULLUP);
pinMode(GP_REWIND_10s, INPUT_PULLUP);
pinMode(GP_SLOW_SPEED, INPUT_PULLUP);
pinMode(GP_NORMAL_SPEED, INPUT_PULLUP);
}
void handleButton(int keyIndex){
// handle the button press
if (!digitalRead(keyPins[keyIndex])){
// button pressed
if (!keyStates[keyIndex]){
// key not currently pressed
keyStates[keyIndex] = true;
bleKeyboard.press(keyCodes[keyIndex]);
}
}
else {
// button not pressed
if (keyStates[keyIndex]){
// key currently pressed
keyStates[keyIndex] = false;
bleKeyboard.release(keyCodes[keyIndex]);
}
}
}