Skip to content

Instantly share code, notes, and snippets.

@NeoCat
Last active October 4, 2019 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NeoCat/b709cdd9899386bf956b01df084c4d7a to your computer and use it in GitHub Desktop.
Save NeoCat/b709cdd9899386bf956b01df084c4d7a to your computer and use it in GitHub Desktop.
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