LoginSignup
1
4

M5StickCPlus1.1をBluetoothキーボード化しiPhoneのスクショボタンにすーる

Posted at

はじめに

M5StickCでBluetoothキーボード化しスクショボタンにしていきます

開発環境

  • Windows 11 PC
  • M5StickCPlus 1.1
  • Arduino IDE 2.2.1
  • iPhone 12 Pro

導入

1.Arduino IDEをインストール
https://www.arduino.cc/en/software

2.ESP32-BLE-Keyboard v0.3.0をダウンロード
https://github.com/T-vK/ESP32-BLE-Keyboard/releases

3.スケッチ->ライブラリをインクルード->.ZIP形式のライブラリをインストール…からESP32-BLE-Keyboard.zipを選択

4.ツール->ボード->ボードマネージャからesp32 2.0.11をインストール
20240106-144251-3725d061.png

5.スケッチ->ライブラリをインクルード->ライブラリを管理からM5StickCPlusをインストール
20240106-150128-67f8bf05.png

6.M5StickCPlusをUSBで接続し、COMポートを選択
20240106-150548-68fcbc29.png

7.プログラムを書き、M5StickCPlusにアップロード

#include <vector>
#include <M5StickCPlus.h>
#include <BleKeyboard.h>

// -----------------------------------------------------------------------------
// 定数
// -----------------------------------------------------------------------------
// 電源ボタンが1秒未満押された
const uint8_t AXP_WAS_PRESSED = 2;

// -----------------------------------------------------------------------------
// 変数
// -----------------------------------------------------------------------------
// ESP32-BLE-Keyboard
BleKeyboard bleKeyboard;

// -----------------------------------------------------------------------------
// 関数
// -----------------------------------------------------------------------------
// 画面を消去する
void clearScreen() {
    M5.Lcd.fillScreen(TFT_BLACK);
    M5.Lcd.setCursor(0, 0);
}

void setup() {
    M5.begin();

    // 画面の表示設定
    M5.Lcd.setRotation(1);
    M5.Lcd.setTextFont(2);
    clearScreen();
    M5.Lcd.println("ESP32-BLE-Keyboard");

    // Bluetooth接続
    bleKeyboard.begin();
}

void loop() {
    M5.update();

    // 電源ボタンが押されたらリセット
    if (M5.Axp.GetBtnPress() == AXP_WAS_PRESSED) {
        esp_restart();
    }

    if (bleKeyboard.isConnected()) {
        // Bluetooth接続済みの場合
        // ボタンAが押されたらキーを送信
        if (M5.BtnA.wasPressed()) {
            bleKeyboard.press(KEY_LEFT_GUI);
            bleKeyboard.press(KEY_LEFT_SHIFT);
            bleKeyboard.press('3');
            delay(100);
            bleKeyboard.releaseAll();
        }

        delay(10);
    }
    else {
        // Bluetooth未接続の場合は5秒待つ
        M5.Lcd.println("Waiting 5 seconds...");
        delay(5000);
    }
}

実行結果

iPhoneのBluetoothでESP32 Keyboardに接続し、M5StickCPlusのボタンを押すとスクショできました。

電源ボタンを6秒押すと電源が切れます。
お疲れさまでした。

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4