Last active
May 3, 2024 15:32
-
-
Save 3110/161e803683fd14f2b6a533ca010ae5c5 to your computer and use it in GitHub Desktop.
ATOM Lite/MatrixをBLEキーボードにする
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* ATOM Lite/MatrixをBLEキーボードにするサンプルプログラム | |
* | |
* 送信するキーを変更するにはSEND_KEYの値を変更してください。 | |
* | |
* コンパイルにはESP32 BLE Keyboard library(https://github.com/T-vK/ESP32-BLE-Keyboard)が必要です。 | |
* 事前にインストールしておいてください。 | |
*/ | |
#include <BleKeyboard.h> // https://github.com/T-vK/ESP32-BLE-Keyboard | |
#include <M5Atom.h> | |
const uint8_t SEND_KEY = 0x20; // 0x20: スペース | |
const std::string DEVICE_NAME("ATOM BLE Keyboard"); | |
const std::string DEVICE_MANUFACTURER("M5Stack"); | |
const CRGB CRGB_BLE_CONNECTED(0x00, 0x00, 0xf0); | |
const CRGB CRGB_BLE_DISCONNECTED(0xf0, 0x00, 0x00); | |
BleKeyboard bleKeyboard(DEVICE_NAME, DEVICE_MANUFACTURER); | |
bool isBleConnected = false; | |
bool isKeyPressed = false; | |
void setup() { | |
M5.begin(true, false, true); // Serial: Enable, I2C: Disable, Display: Enable | |
bleKeyboard.begin(); | |
M5.dis.fillpix(CRGB_BLE_DISCONNECTED); | |
Serial.println(DEVICE_NAME.c_str()); | |
} | |
void loop() { | |
M5.update(); | |
if (bleKeyboard.isConnected()) { | |
if (!isBleConnected) { | |
M5.dis.fillpix(CRGB_BLE_CONNECTED); | |
isBleConnected = true; | |
Serial.println("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.fillpix(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
Lite/Matrix両方で動くように変更