
Arduino Quick Start
Chain Captain sleep and wakeup related APIs and sample programs.
3.3.10-aM5ChainCaptain0.3.00.3.0#include <M5Unified.h>
// #include <esp_sleep.h>
static constexpr uint64_t SLEEP_TIME_US = 5000000ULL;
static const char* SLEEP_MODE_NAMES[] = {
"Light sleep 5s",
"Deep sleep 5s",
};
static const size_t MODE_COUNT = sizeof(SLEEP_MODE_NAMES) / sizeof(SLEEP_MODE_NAMES[0]);
static size_t modeIndex = 0;
static void drawMenu(const char* status)
{
M5.Display.startWrite();
M5.Display.fillScreen(TFT_BLACK);
M5.Display.setTextDatum(middle_center);
M5.Display.setFont(&fonts::FreeSansBold18pt7b);
M5.Display.setTextColor(TFT_CYAN, TFT_BLACK);
M5.Display.drawString("Wakeup", 120, 40);
M5.Display.setFont(&fonts::FreeSans9pt7b);
for (size_t i = 0; i < MODE_COUNT; ++i) {
M5.Display.setTextColor(i == modeIndex ? TFT_GREEN : TFT_WHITE, TFT_BLACK);
M5.Display.drawString(SLEEP_MODE_NAMES[i], 120, 100 + i * 36);
}
M5.Display.setTextColor(TFT_DARKGREY, TFT_BLACK);
M5.Display.drawString("B: prev C: next", 120, 180);
M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
M5.Display.drawString("A: OK", 120, 205);
if (status) {
M5.Display.setTextColor(TFT_ORANGE, TFT_BLACK);
M5.Display.drawString(status, 120, 228);
}
M5.Display.endWrite();
}
void setup()
{
delay(500);
auto cfg = M5.config();
cfg.serial_baudrate = 115200;
M5.begin(cfg);
M5.Display.setRotation(0);
M5.Display.setBrightness(128);
drawMenu(nullptr);
}
void loop()
{
M5.update();
if (M5.BtnB.wasClicked()) {
modeIndex = (modeIndex + MODE_COUNT - 1) % MODE_COUNT;
drawMenu(nullptr);
}
if (M5.BtnC.wasClicked()) {
modeIndex = (modeIndex + 1) % MODE_COUNT;
drawMenu(nullptr);
}
if (M5.BtnA.wasClicked()) {
if (modeIndex == 0) {
drawMenu("Light sleep now");
delay(200);
M5.Power.lightSleep(SLEEP_TIME_US, false);
// Light sleep resumes here.
Serial.printf("Wakeup cause: %d\n",
static_cast<int>(esp_sleep_get_wakeup_cause()));
drawMenu("Light sleep wakeup");
Serial.println("Woke up from light sleep");
} else {
drawMenu("Deep sleep now");
delay(200);
M5.Power.deepSleep(SLEEP_TIME_US, false);
}
}
delay(20);
}After uploading, the screen shows a sleep mode menu. Press KEYB / KEYC to select light sleep or deep sleep, and press KEYA to execute. After light sleep wakeup, Light sleep wakeup is displayed. After deep sleep wakeup, the ESP32-S3 resets and the initial menu is shown again.
The Chain Captain sleep and wakeup section uses the Power_Class from the M5Unified library. For more related APIs, refer to the following document: