Arduino入門

2. デバイス&サンプル

5. 拡張モジュール&サンプル

アクセサリー

6. アプリケーション

CoreMatrix スリープとウェイクアップ

CoreMatrix のスリープとウェイクアップ関連 API とサンプルプログラムです。

サンプルプログラム

コンパイル要件

  • M5Stack ボードマネージャーのバージョン >= 3.3.10-a
  • 開発ボードの選択 = M5CoreMatrix
  • M5Unified ライブラリのバージョン >= 0.2.29
  • M5GFX ライブラリのバージョン >= 0.2.36

以下の例では、KEY1 で 5 秒間のライトスリープに、KEY2 で 5 秒間のディープスリープに入り、タイマーによるウェイクアップの流れを示します。

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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#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() から再起動し、マトリクスに中心の四角形が再表示されます。

API

CoreMatrix のスリープとウェイクアップ部分では、M5Unified ライブラリの Power_Class を使用しています。関連 API の詳細は以下のドキュメントを参照してください。

Page Tools
PDF
On This Page