1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

M5Stack用デュアルボタンユニットで、bleキーボード(onekey)を作成して、スイッチコントロールでiPadを操作

Last updated at Posted at 2022-04-10

M5Stack用デュアルボタンユニットで、bleキーボード(onekey)を作成して、スイッチコントロールでiPadを操作してみました。

たなかまさゆき氏のLang-shipのページを参考にさせてもらいました。
https://lang-ship.com/blog/work/m5stickc-iphone/

BleKeyboard.hをincludeして、M5stickCをBLEキーボードとして認識させることができます。

bleKeyboard.write(0x20);の部分で、
ASCIIコードから、青ボタンをスペースキー(0x20)、

bleKeyboard.write(0x68);も同様に、
赤ボタンをHキー(0x68)に設定しています。

M5stickCにプログラムをインストール

#include <M5StickC.h>
#include <BleKeyboard.h>

BleKeyboard bleKeyboard;
const int buttonred = 32;
const int buttonblue = 33;

double vbat = 0.0;
int8_t bat_charge_p = 0;

void show_battery_info() {
  // バッテリー電圧表示
  // GetVbatData()の戻り値はバッテリー電圧のステップ数で、
  // AXP192のデータシートによると1ステップは1.1mV
  vbat = M5.Axp.GetVbatData() * 1.1 / 1000;
  // バッテリー残量表示
  // 簡易的に、線形で4.2Vで100%、3.0Vで0%とする
  bat_charge_p = int8_t((vbat - 3.0) / 1.2 * 100);
  if (bat_charge_p > 100) {
    bat_charge_p = 100;
  } else if (bat_charge_p < 0) {
    bat_charge_p = 0;
  }
  M5.Lcd.setCursor(45, 2, 1);
  M5.Lcd.printf("Charge: %3d%%", bat_charge_p);
}
void setup() {
  Serial.begin(115200);
  Serial.println("Starting BLE work!");
  bleKeyboard.begin();
  M5.begin();
  M5.Lcd.setRotation(1);
  pinMode(33, INPUT);
  pinMode(32, INPUT);
}
void loop() {
  M5.update();
  if ( M5.BtnA.wasPressed() ) {
    M5.Axp.PowerOff();
  }
  show_battery_info();
  Serial.println(digitalRead(32), digitalRead(33));


  M5.begin();
  if (bleKeyboard.isConnected()) {
    M5.Lcd.drawCentreString("connected", 80, 60, 2);
  }
  // read the value of BUTTON
  if (digitalRead(32) == 0) {
    Serial.println("Button Status: red pressed");

    M5.Lcd.drawCentreString("   red pressed   ", 80, 20, 2);
    bleKeyboard.write(0x68); // Hキーに設定
    delay(100);
  } else if (digitalRead(32) == 1) {
    Serial.println("Button Status:red released");
    M5.Lcd.drawCentreString("   red released   ", 80, 20, 2);
    delay(20);
  }
  // read the value of BUTTON
  if (digitalRead(33) == 0) {
    Serial.println("Button Status: blue pressed ");
    M5.Lcd.drawCentreString("   blue pressed   ", 80, 40, 2);
    bleKeyboard.write(0x20); //スペースキーに設定
    delay(100);
  } else if (digitalRead(33) == 1) {
    Serial.println("Button Status:blue released");
    M5.Lcd.drawCentreString("   blue released   ", 80, 40, 2);
    delay(20);
  }
}
1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?