はじめに
先日作成した ESP32-C3 SuperMini オリジナルボード の一つを、Bluetooth接続のスリープブロッカー&ミニキーボードにしました。
Arduino環境にて、BLE Combo を使用します。
BLE Combo は、BLEキーボードと BLEマウスの両方が同時に使える便利なライブラリです。
ボードは esp32 → MakerGO ESP32 C3 SupreMini
を選択。
sketch
4つのボタンにそれぞれ、自由なキー送出を割り当てます。
複数キーの同時押しの判定を加えると、さらに多くのキー割り当てが可能( 2キー同時押しだと12パターンの割り当てが追加可能 )。
sketch.ino
// for ESP32-C3 SuperMini with original HAT
#include <BleCombo.h>
//#include <KeyboardOutputCallbacks.h>
typedef unsigned long int ulong;
const int sw1 = 1;
const int sw2 = 3;
const int sw3 = 0;
const int sw4 = 2;
bool sw1on, sw2on, sw3on, sw4on;
bool isConnected = false;
const ulong interval_ms = 10000; //10 seconds
inline bool isSw1On() { return isSwicthOn(sw1, LOW); }
inline bool isSw2On() { return isSwicthOn(sw2, LOW); }
inline bool isSw3On() { return isSwicthOn(sw3, LOW); }
inline bool isSw4On() { return isSwicthOn(sw4, LOW); }
inline void waitForBtnReleaseSw1() { waitForBtnRelease(sw1, LOW); }
inline void waitForBtnReleaseSw2() { waitForBtnRelease(sw2, LOW); }
inline void waitForBtnReleaseSw3() { waitForBtnRelease(sw3, LOW); }
inline void waitForBtnReleaseSw4() { waitForBtnRelease(sw4, LOW); }
inline void LEDOn() { digitalWrite(LED_BUILTIN, LOW); }
inline void LEDOff() { digitalWrite(LED_BUILTIN, HIGH); }
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
LEDOn();
pinMode(sw1, INPUT_PULLUP);
pinMode(sw2, INPUT_PULLUP);
pinMode(sw3, INPUT_PULLUP);
pinMode(sw4, INPUT_PULLUP);
Keyboard.deviceName = "SleepBlocker_Keyboard_Mouse";
Keyboard.begin();
Mouse.begin();
delay(1000); // wait for 1 second
waitForBtnReleaseSw1();
waitForBtnReleaseSw2();
waitForBtnReleaseSw3();
waitForBtnReleaseSw4();
Serial.println("Ready!");
}
void loop() {
static ulong lastTime = 0;
static int delta = 1; // -1 or 1
static bool toggle = false;
if (!Keyboard.isConnected()) {
if (isConnected) {
Serial.println("disconnected!");
isConnected = false;
}
digitalWrite(LED_BUILTIN, toggle); //blink led
toggle = !toggle;
delay(100);
return;
} else if (!isConnected) {
Serial.println("connected!");
isConnected = true;
LEDOff();
toggle = false;
}
sw1on = isSw1On();
sw2on = isSw2On();
sw3on = isSw3On();
sw4on = isSw4On();
if (sw1on) {
Keyboard.print("password");
delay(100);
waitForBtnReleaseSw1();
}
if (sw2on) { // for VDI
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_DELETE);
delay(100);
Keyboard.releaseAll();
waitForBtnReleaseSw2();
}
if (sw3on) {
Keyboard.print("```");
delay(100);
waitForBtnReleaseSw3();
}
if (sw4on) {
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press('L');
delay(100);
Keyboard.releaseAll();
waitForBtnReleaseSw4();
}
// sleep blocker -----------------------
delay(100);
ulong now = millis();
if (now < lastTime + interval_ms) return;
LEDOn();
Mouse.move(delta * 3, 0); //x-coordinate move 3 dots
delta *= -1;
lastTime = now;
delay(100);
LEDOff();
}
//local methods
inline bool isSwicthOn(int sw, int val) {
return digitalRead(sw) == val;
}
inline void waitForBtnRelease(int sw, int val) {
while (isSwicthOn(sw, val)) delay(10);
}
iPad の画面オフ抑止にも対応するため、時間間隔を 10秒と短くして、マウス移動はシンプルに 左右にのみ 3ドットにしています。
(セキュリティーポリシーにより、自動ロック(画面オフ)をオフにできない端末のため)
以上