Skip to content

Instantly share code, notes, and snippets.

@3110
Last active July 12, 2020 04:41
Show Gist options
  • Save 3110/6bd91e7b252f8290723b693c15351aef to your computer and use it in GitHub Desktop.
Save 3110/6bd91e7b252f8290723b693c15351aef to your computer and use it in GitHub Desktop.
ATOM Liteでスクリーンセーバーを起動しないようにする(BLEキーボード版)
/*
* ATOM LiteをBLEキーボードにして,定期的にキーを送ることで
* スクリーンセーバーを起動しないようにする。
*
* KILL_INTERVAL_SEC: 定期的にキーを送る頻度(秒)
* KILL_KEY: 定期的に送るキー
*
* ATOM Liteのボタンを押すことで,SEND_KEYに設定されたキーを
* 送る。
*
* SEND_KEY: ボタンを押したときに送るキー
*
* LEDの色:
* 赤:BLEで接続していない
* 青:BLEで接続している
* 黄:スクリーンセーバーを起動しないようにキーを送った(0.1秒光る)
*/
#include <BleKeyboard.h> // https://github.com/T-vK/ESP32-BLE-Keyboard
#include <M5Atom.h>
#include <utility/M5Timer.h>
// スクリーンセーバーを起動させないためにキーを送る頻度(秒)
const long KILL_INTERVAL_SEC = 30;
// スクリーンセーバーを起動させないために送るキー
const uint8_t KILL_KEY = KEY_ESC;
// ボタンを押したときに送るキー
const uint8_t SEND_KEY = 0x20; // 0x20: スペース
const std::string DEVICE_NAME("ATOM Lite BLE Keyboard");
const std::string DEVICE_MANUFACTURER("M5Stack");
const CRGB CRGB_BLE_CONNECTED(0x00, 0x00, 0xf0);
// 数値は緑だが Lite では赤く光る https://github.com/m5stack/M5Atom/issues/5
const CRGB CRGB_BLE_DISCONNECTED(0x00, 0xf0, 0x00);
const CRGB CRGB_KILLED(0xf0, 0xf0, 0x00);
BleKeyboard bleKeyboard(DEVICE_NAME, DEVICE_MANUFACTURER);
bool isBleConnected = false;
bool isKeyPressed = false;
M5Timer killTimer;
bool isEmitKiller = false;
void emitKiller() {
isEmitKiller = true;
}
void setup() {
Serial.begin(115200);
M5.begin(true, false, true); // Serial: Enable, I2C: Disable, Display: Enable
bleKeyboard.begin();
M5.dis.drawpix(0, CRGB_BLE_DISCONNECTED);
killTimer.setInterval(KILL_INTERVAL_SEC * 1000, emitKiller);
Serial.println("Screen Saver Killer started: interval = " + String(KILL_INTERVAL_SEC) + "sec.");
}
void loop() {
M5.update();
killTimer.run();
if (bleKeyboard.isConnected()) {
if (!isBleConnected) {
M5.dis.drawpix(0, CRGB_BLE_CONNECTED);
isBleConnected = true;
Serial.println("Connected");
}
if (isEmitKiller) {
isEmitKiller = false;
if (!isKeyPressed) {
bleKeyboard.write(KILL_KEY);
Serial.println("Killed!");
M5.dis.drawpix(0, CRGB_KILLED);
delay(100);
M5.dis.drawpix(0, CRGB_BLE_CONNECTED);
}
}
if (isKeyPressed) {
if (M5.Btn.isReleased()) {
bleKeyboard.release(SEND_KEY);
isKeyPressed = false;
Serial.println("Released");
}
} else {
if (M5.Btn.isPressed()) {
bleKeyboard.press(SEND_KEY);
isKeyPressed = true;
Serial.println("Pressed");
}
}
} else {
if (isBleConnected) {
M5.dis.drawpix(0, CRGB_BLE_DISCONNECTED);
isBleConnected = false;
isKeyPressed = false;
Serial.println("Disconnected");
}
}
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment