/*
Test program to use game controller buttons as Bluetooth keyboard input
*/
#define USE_NIMBLE
#include <BleKeyboard.h>
BleKeyboard bleKeyboard;
#define GP_Space 16
#define GP_Y 17
#define GP_A 18
#define GP_D 19
bool keyStates[4] = {false, false, false, false};
int keyPins[4] = {GP_Space, GP_Y, GP_A, GP_D};
uint8_t keyCodes[4] = {' ', 'y', 'a', 'd'};
void setup() {
Serial.begin(115200);
Serial.println("Code running...");
setInputs();
bleKeyboard.begin();
}
bool connectNotificationSent = false;
void loop() {
int counter;
if (bleKeyboard.isConnected()) {
Serial.println("is connected...");
if (!connectNotificationSent) {
Serial.println("Code connected...");
connectNotificationSent = true;
}
for (counter = 0; counter < 4; counter ++) {
handleButton(counter);
}
}
}
void setInputs() {
pinMode(GP_Space, INPUT_PULLUP);
pinMode(GP_Y, INPUT_PULLUP);
pinMode(GP_A, INPUT_PULLUP);
pinMode(GP_D, 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]);
}
}
}