
Arduino入門
CoreMatrix のスリープとウェイクアップ関連 API とサンプルプログラムです。
3.3.10-aM5CoreMatrix0.2.290.2.36以下の例では、KEY1 で 5 秒間のライトスリープに、KEY2 で 5 秒間のディープスリープに入り、タイマーによるウェイクアップの流れを示します。
#include <M5Unified.h>
static constexpr uint64_t SLEEP_TIME_US = 5000000ULL;
static void drawPattern(uint8_t pattern)
{
M5.Display.startWrite();
M5.Display.fillScreen(TFT_BLACK);
M5.Display.drawRect(0, 0, 16, 16, TFT_WHITE);
if (pattern == 0) {
M5.Display.fillRect(7, 7, 2, 2, TFT_WHITE);
} else if (pattern == 1) {
M5.Display.fillRect(5, 5, 6, 6, TFT_WHITE);
} else {
M5.Display.drawRect(3, 3, 10, 10, TFT_WHITE);
}
M5.Display.endWrite();
M5.Display.display();
}
void setup()
{
auto cfg = M5.config();
cfg.serial_baudrate = 115200;
M5.begin(cfg);
M5.Display.setColorDepth(8);
M5.Display.setRotation(1);
M5.Display.setBrightness(128);
drawPattern(0);
}
void loop()
{
M5.update();
if (M5.BtnA.wasClicked()) {
Serial.println("KEY1: light sleep for 5 seconds");
drawPattern(1);
delay(200);
M5.Power.lightSleep(SLEEP_TIME_US);
Serial.println("Light sleep woke up");
drawPattern(2);
}
if (M5.BtnB.wasClicked()) {
Serial.println("KEY2: deep sleep for 5 seconds");
drawPattern(1);
delay(200);
M5.Power.deepSleep(SLEEP_TIME_US);
}
delay(20);
}起動後、マトリクスの中央に 2 x 2 の四角形が表示されます。KEY1 を押すとライトスリープに入り、5 秒後に呼び出し位置から実行が再開されて四角形が表示されます。KEY2 を押すとディープスリープに入り、5 秒後にリセットされて setup() から再起動し、マトリクスに中心の四角形が再表示されます。
CoreMatrix のスリープとウェイクアップ部分では、M5Unified ライブラリの Power_Class を使用しています。関連 API の詳細は以下のドキュメントを参照してください。