Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ESP32 Arduino Send Keystroke as BLE Keyboard
#include <M5StickC.h>
#include <BleKeyboard.h>
BleKeyboard bleKeyboard;
void setup() {
Serial.begin(115200);
Serial.println("Starting ...");
M5.begin();
M5.Axp.ScreenBreath(8);
M5.Lcd.setRotation(3);
drawPaused();
// bleKeyboard.begin();
}
bool paused = true;
int max_cnt = 0;
void drawPaused() {
M5.Lcd.fillScreen(TFT_BLACK);
M5.Lcd.setTextColor(TFT_WHITE);
M5.Lcd.setTextSize(2);
M5.Lcd.setCursor(50, 20, 4);
M5.Lcd.println("||");
}
bool setPaused() {
M5.update();
int last_max_cnt = max_cnt;
if (paused) {
if (M5.BtnA.pressedFor(1800))
max_cnt = 1000;
else if (M5.BtnA.pressedFor(1500))
max_cnt = 500;
else if (M5.BtnA.pressedFor(1200))
max_cnt = 400;
else if (M5.BtnA.pressedFor(900))
max_cnt = 300;
else if (M5.BtnA.pressedFor(600))
max_cnt = 200;
else if (M5.BtnA.pressedFor(300))
max_cnt = 100;
else if (M5.BtnA.isPressed())
max_cnt = 0;
if (last_max_cnt != max_cnt) {
M5.Lcd.fillScreen(TFT_BLACK);
M5.Lcd.setTextColor(TFT_WHITE);
M5.Lcd.setCursor(85, 50, 4);
M5.Lcd.setTextSize(1);
M5.Lcd.printf("%4d", max_cnt);
}
}
if (M5.BtnA.wasReleased()) {
paused = !paused;
Serial.print("paused = ");
Serial.println(paused);
if (paused) {
drawPaused();
ESP.restart();
} else {
bleKeyboard.begin();
}
}
return paused;
}
void loop() {
if (setPaused()) {
delay(50);
return;
}
if (bleKeyboard.isConnected()) {
M5.Lcd.fillScreen(TFT_BLUE);
Serial.println("Sending Ctrl...");
bleKeyboard.press(KEY_LEFT_CTRL);
delay(30);
bleKeyboard.releaseAll();
} else {
M5.Lcd.fillScreen(TFT_RED);
}
if (max_cnt > 0) {
if (--max_cnt == 0) {
paused = true;
drawPaused();
ESP.restart();
return;
}
}
Serial.println("Waiting 5 seconds...");
M5.Lcd.setTextColor(TFT_WHITE);
for (int i = 5; i > 0; i--) {
if (bleKeyboard.isConnected()) {
M5.Lcd.fillScreen(TFT_BLUE);
} else {
M5.Lcd.fillScreen(TFT_RED);
}
M5.Lcd.setCursor(30, 4, 8);
M5.Lcd.setTextSize(1);
M5.Lcd.print(i);
M5.Lcd.setCursor(85, 50, 4);
M5.Lcd.setTextSize(1);
M5.Lcd.printf("%4d", max_cnt);
for (int j = 0; j < 10; j++) {
if (setPaused())
return;
delay(100);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment