Skip to content

Instantly share code, notes, and snippets.

@3110
Last active May 3, 2024 15:32
Show Gist options
  • Save 3110/161e803683fd14f2b6a533ca010ae5c5 to your computer and use it in GitHub Desktop.
Save 3110/161e803683fd14f2b6a533ca010ae5c5 to your computer and use it in GitHub Desktop.
ATOM Lite/MatrixをBLEキーボードにする
/*
* 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);
}
@3110
Copy link
Author

3110 commented May 26, 2023

Lite/Matrix両方で動くように変更

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment