English
English
简体中文
日本語

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

Chain Captain Wakeup

Chain Captain sleep and wakeup related APIs and sample programs.

Example

Compilation Requirements

  • M5Stack Board Manager Version >= 3.3.10-a
  • Board Option = M5ChainCaptain
  • M5Unified Library Version >= 0.3.0
  • M5GFX Library Version >= 0.3.0
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#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.

API

The Chain Captain sleep and wakeup section uses the Power_Class from the M5Unified library. For more related APIs, refer to the following document:

Page Tools
PDF
On This Page