
Arduino Quick Start
APIs and example programs related to button input on StickC.
M5.update() function in the main loop to update button states, and minimize blocking operations; otherwise, button changes may not be detected promptly.#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");
}
}After button A or B is pressed and released, the program displays the corresponding button state in the center of the screen.
