pdf-icon

Arduino入門

2. デバイス&サンプル

Air Quality ウェイクアップ

Air Quality のスリープおよびウェイクアップ機能に関する API とサンプルプログラム。

サンプルプログラム

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>
#include <esp_sleep.h>
void setup() {
auto cfg = M5.config();
M5.begin(cfg); // ディスプレイ、ボタン、RTC などを初期化
M5.Display.fillScreen(TFT_WHITE);
M5.Display.setTextSize(2);
M5.Display.setTextColor(TFT_BLACK);
M5.Display.setTextDatum(middle_center);
esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
int cx = M5.Display.width() / 2;
int cy = M5.Display.height() / 2;
if (cause == ESP_SLEEP_WAKEUP_TIMER) {
M5.Display.drawString("Woken up!", cx, cy - 20);
M5.Display.drawString("Press BtnA", cx, cy + 20);
} else {
M5.Display.drawString("Press BtnA", cx, cy - 20);
M5.Display.drawString("to sleep 5s", cx, cy + 20);
}
}
void loop() {
M5.update(); // ボタン状態を更新
// BtnA を押すと 5 秒のタイマー付きスリープへ移行
if (M5.BtnA.wasPressed()) {
int cx = M5.Display.width() / 2;
int cy = M5.Display.height() / 2;
M5.Display.fillScreen(TFT_WHITE);
M5.Display.setTextDatum(middle_center);
M5.Display.drawString("Sleeping...", cx, cy);
delay(500);
M5.Power.timerSleep(5); // 5 秒後にウェイクアップ
}
}

アップロード後、ボタンを押すと以下のように表示されます:

API

電源セクションでは M5Unified ライブラリの Power_Class を使用しています。その他の関連 API は以下のドキュメントを参照してください:

On This Page