Skip to content

Instantly share code, notes, and snippets.

@moyashipan
Created January 18, 2020 21:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moyashipan/e58aaf9734aa083f6ac332ffeeba1bc1 to your computer and use it in GitHub Desktop.
Save moyashipan/e58aaf9734aa083f6ac332ffeeba1bc1 to your computer and use it in GitHub Desktop.
M5StickCで布団から手を出さずに読書アプリのページをめくるやつ
#include <M5StickC.h>
// https://github.com/T-vK/ESP32-BLE-Keyboard
#include <BleKeyboard.h>
BleKeyboard bleKeyboard;
bool connected = false;
struct KeyConfig {
String name;
String example;
uint8_t short_key;
uint8_t long_key;
};
KeyConfig configs[] = {
{ "Kindle\nMode\n", "Down, Up", KEY_DOWN_ARROW, KEY_UP_ARROW },
{ "Apple\nBooks\nMode", "Right, Left", KEY_RIGHT_ARROW, KEY_LEFT_ARROW },
{ "Manga\nMode\n", "Left, Right", KEY_LEFT_ARROW, KEY_RIGHT_ARROW }
};
int configs_length;
int mode = 0;
void setup() {
setCpuFrequencyMhz(80);
M5.begin();
M5.Lcd.fillScreen(BLACK);
M5.Axp.ScreenBreath(0);
Serial.begin(115200);
Serial.println("Starting BLE work!");
bleKeyboard.begin();
pinMode(M5_LED, OUTPUT);
configs_length = sizeof(configs) / sizeof(KeyConfig);
}
void loop() {
if (bleKeyboard.isConnected()) {
// 接続直後
if (!connected) {
showMode();
connected = true;
digitalWrite(M5_LED, HIGH);
}
m5.update();
if (m5.BtnB.wasReleasefor(300)) {
Serial.println("Mode Reset");
mode = 0;
showMode();
} else if (m5.BtnB.wasReleased()) {
Serial.println("Mode Change");
mode = (mode + 1) % configs_length;
showMode();
}
if (m5.BtnA.wasReleasefor(300)) {
// 長押しした
Serial.println("Long Key");
bleKeyboard.write(configs[mode].long_key);
M5.Axp.ScreenBreath(0);
} else if (m5.BtnA.wasReleased()) {
// 押した
Serial.println("Short Key");
bleKeyboard.write(configs[mode].short_key);
M5.Axp.ScreenBreath(0);
}
} else {
// 接続待ち
connected = false;
M5.Axp.ScreenBreath(0);
Serial.println("Waiting 1 seconds...");
digitalWrite(M5_LED, LOW);
delay(100);
digitalWrite(M5_LED, HIGH);
delay(900);
}
}
void showMode() {
M5.Axp.ScreenBreath(12);
M5.Lcd.fillScreen(BLACK);
delay(50);
M5.Lcd.setTextColor(WHITE, BLACK);
M5.Lcd.setCursor(0, 0, 4);
M5.Lcd.println(configs[mode].name);
M5.Lcd.setTextFont(2);
M5.Lcd.println(configs[mode].example);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment