English
English
简体中文
日本語

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

CoreP4X Wakeup Sleep and Wakeup

APIs and example programs related to timed sleep and wakeup of CoreP4X.

Example Program

Compilation Requirements

  • M5Stack Board Manager version >= 3.3.10-a
  • Board selection = M5CoreP4X
  • M5Unified library version >= 0.3.1
  • M5GFX library version >= 0.3.1

CoreP4X uses the ESP32-P4 chip. M5Unified does not support M5.Power.lightSleep() on the ESP32-P4; calling it reports unsupported and returns immediately.

This example provides two sleep entries:

  • M5.Power.timerSleep(): uses the M5Unified timed sleep flow; wakes up after 5 seconds and restarts.
  • M5.Power.deepSleep(): enters ESP32-P4 deep sleep directly and wakes up using a 5-second ESP timer.

Both methods reset on wakeup and run again from setup().

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 86 87 88 89 90 91 92 93 94
#include <M5Unified.h>

static constexpr int SLEEP_SECONDS = 5;
static constexpr uint64_t SLEEP_TIME_US = 5000000ULL;

M5Canvas canvas(&M5.Display);


static void drawCard(int y, const char* title, const char* detail, uint16_t color)
{
  canvas.fillRoundRect(60, y, 360, 96, 16, color);
  canvas.drawRoundRect(60, y, 360, 96, 16, TFT_WHITE);

  canvas.setTextDatum(middle_center);
  canvas.setTextColor(TFT_WHITE, color);
  canvas.setFont(&fonts::FreeSansBold18pt7b);
  canvas.drawString(title, 240, y + 36);

  canvas.setFont(&fonts::FreeSans12pt7b);
  canvas.drawString(detail, 240, y + 70);
}

static void drawScreen(const char* status)
{
  canvas.fillSprite(TFT_BLACK);
  canvas.setTextDatum(middle_center);
  canvas.setTextColor(TFT_WHITE, TFT_BLACK);

  canvas.setFont(&fonts::FreeSansBold24pt7b);
  canvas.drawString("Wakeup", 240, 52);

  canvas.setFont(&fonts::FreeSans12pt7b);
  canvas.setTextColor(TFT_CYAN, TFT_BLACK);
  canvas.drawString("Select sleep mode", 240, 112);

  drawCard(164, "Timer Sleep 5s", "M5.Power.timerSleep", TFT_BLUE);
  drawCard(298, "Deep Sleep 5s", "M5.Power.deepSleep", TFT_DARKGREEN);

  canvas.setTextColor(TFT_ORANGE, TFT_BLACK);
  canvas.setFont(&fonts::FreeSans12pt7b);
  canvas.drawString(status, 240, 426);

  canvas.setTextColor(TFT_DARKGREY, TFT_BLACK);
  canvas.setFont(&fonts::FreeSans9pt7b);
  canvas.drawString("Tap a card to start", 240, 456);
  canvas.pushSprite(0, 0);
}

void setup()
{
  auto cfg = M5.config();
  cfg.serial_baudrate = 115200;
  M5.begin(cfg);

  M5.Display.setBrightness(128);
  canvas.createSprite(M5.Display.width(), M5.Display.height());

  drawScreen("Waiting for input");
}

void loop()
{
  M5.update();

  if (!M5.Touch.getDetail().wasClicked()) {
    delay(20);
    return;
  }

  const auto touch = M5.Touch.getDetail();

  if (touch.y >= 164 && touch.y <= 260) {
    drawScreen("Starting timer sleep");
    Serial.println("Starting timer sleep");
    delay(100);

    // timerSleep returns only when the sleep request fails.
    if (!M5.Power.timerSleep(SLEEP_SECONDS)) {
      drawScreen("Timer sleep failed");
      Serial.println("Timer sleep failed");
    }
  } else if (touch.y >= 298 && touch.y <= 394) {
    drawScreen("Starting deep sleep");
    Serial.println("Starting deep sleep");
    delay(100);

    // Deep sleep wakes after 5 seconds and restarts the program.
    M5.Power.deepSleep(SLEEP_TIME_US, false);
    drawScreen("Deep sleep returned");
    Serial.println("Deep sleep returned without reset");
  }

  delay(20);
}

After uploading, the screen displays two sleep entries. Tap Timer Sleep 5s to call M5.Power.timerSleep(), and tap Deep Sleep 5s to call M5.Power.deepSleep(). Both methods restart after 5 seconds and display the sleep menu again.

API

The sleep and wakeup section of CoreP4X uses the Power_Class in the M5Unified library. For more APIs, refer to the documentation below:

Page Tools
PDF
On This Page