
Arduino入門
StickS3 のボタン入力関連 API とサンプルプログラムについて。
M5.update()関数を含めて状態更新を読み取る必要があります。また、可能な限りブロッキングを減らすようにしてください。そうしないと、ボタンの状態変化をタイムリーに取得できない可能性があります。#include "M5Unified.h"
#include "M5GFX.h"
static int32_t w;
static int32_t h;
static bool drawed = false;
void setup()
{
auto cfg = M5.config();
M5.begin(cfg);
M5.Lcd.setRotation(1);
w = M5.Lcd.width();
h = M5.Lcd.height();
M5.Lcd.setFont(&fonts::FreeMonoBold12pt7b);
M5.Lcd.drawString("Button Released", 0, 0);
}
void loop()
{
M5.update();
if(M5.BtnA.isPressed() || M5.BtnB.isPressed())
{
if (!drawed){
M5.Lcd.clear();
}
M5.Lcd.drawString("Button Detail:", 0, 0);
if (M5.BtnA.isPressed()) {
M5.Lcd.drawString("ButtonA Pressed", 0, 30);
}
else if (M5.BtnB.isPressed()) {
M5.Lcd.drawString("ButtonB Pressed", 0, 60);
}
drawed = true;
}
else if (drawed){
drawed = false;
M5.Lcd.clear();
M5.Lcd.drawString("Button Released", 0, 0);
}
vTaskDelay(1);
} ボタンを押すと、スクリーンに特定のボタン状態が表示されます。



StickS3 のボタン操作部分にはM5UnifiedライブラリのButton_Classを使用しています。ボタン関連のその他の API については、以下のドキュメントをご参照ください: