简体中文
English
简体中文
日本語

Arduino 上手教程

2. 设备开发 & 案例程序

5. 扩展模块

6. 应用案例

StickC Button 按键

StickC 按键输入相关 API 与案例程序。

注意事项
使用时需要在主循环中包含 M5.update() 函数读取按键状态,并尽可能减少阻塞,否则可能无法及时获取按键变化。

案例程序

编译要求

  • M5Stack 板管理版本 >= 3.3.8
  • 开发板选项 = M5StickC
  • M5Unified 库版本 >= 0.2.18
  • M5GFX 库版本 >= 0.2.24
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include <M5Unified.h>

constexpr uint8_t TEXT_SIZE = 2;

void showMessage(const char* message) {
    M5.Display.fillScreen(TFT_BLACK);
    M5.Display.setTextSize(TEXT_SIZE);
    M5.Display.setTextColor(TFT_YELLOW, TFT_BLACK);

    int16_t textWidth = M5.Display.textWidth(message);
    int16_t textHeight = M5.Display.fontHeight();
    int16_t x = (M5.Display.width() - textWidth) / 2;
    int16_t y = (M5.Display.height() - textHeight) / 2;

    M5.Display.setCursor(x, y);
    M5.Display.print(message);
}

void setup() {
    auto cfg = M5.config();
    M5.begin(cfg);

    M5.Display.setRotation(1);
    M5.Display.fillScreen(TFT_BLACK);
    M5.Display.setTextColor(TFT_YELLOW, TFT_BLACK);
    M5.Display.setTextSize(TEXT_SIZE);
}

void loop() {
    M5.update();

    if (M5.BtnA.wasReleased()) {
        showMessage("A Btn Pressed");
    }
    if (M5.BtnB.wasReleased()) {
        showMessage("B Btn Pressed");
    }
}

该程序会在按下并释放 A 或 B 按键后,在屏幕中央显示对应的按键状态。

Page Tools
PDF
On This Page