English
English
简体中文
日本語

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

ToughC5 Sleep and Wakeup

ToughC5 sleep and wakeup APIs and example program.

Example Program

Compilation Requirements

  • M5Stack Board Manager Version >= 3.3.9
  • Board Selection = M5ToughC5
  • M5Unified Library Version >= 0.2.22
  • M5GFX Library Version >= 0.2.28
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
#include <M5Unified.h>

constexpr auto SLEEP_TIME_US = 5000000ULL;

void drawMessage(const char* title, const char* detail)
{
    M5.Display.clear(TFT_WHITE);
    M5.Display.setTextDatum(middle_center);
    M5.Display.setTextColor(TFT_BLACK, TFT_WHITE);
    M5.Display.setFont(&fonts::FreeSansBold12pt7b);
    M5.Display.drawString(title, M5.Display.width() / 2, 80);

    M5.Display.setFont(&fonts::FreeSansBold9pt7b);
    M5.Display.drawString(detail, M5.Display.width() / 2, 130);
}

void drawSleepMenu()
{
    M5.Display.clear(TFT_WHITE);
    M5.Display.setTextDatum(middle_center);
    M5.Display.setTextColor(TFT_BLACK, TFT_WHITE);
    M5.Display.setFont(&fonts::FreeSansBold12pt7b);
    M5.Display.drawString("ToughC5 Wakeup Test", M5.Display.width() / 2, 30);

    M5.Display.setFont(&fonts::FreeSansBold9pt7b);
    M5.Display.drawString("Touch left: Light Sleep", M5.Display.width() / 2, 80);
    M5.Display.drawString("Touch right: Deep Sleep", M5.Display.width() / 2, 120);
    M5.Display.drawString("Wakeup after 5 seconds", M5.Display.width() / 2, 165);
}

void setup()
{
    auto cfg = M5.config();
    cfg.clear_display = false;
    M5.begin(cfg);
    M5.Display.setRotation(1);

    drawSleepMenu();
}

void loop()
{
    M5.update();

    if (M5.Touch.getCount()) {
        const auto touch = M5.Touch.getDetail(0);
        if (touch.wasClicked()) {

            if (touch.x < M5.Display.width() / 2) {
                drawMessage("Light Sleep", "Sleeping for 5 seconds");
                delay(200);
                M5.Power.lightSleep(SLEEP_TIME_US, true);
                drawMessage("Wakeup complete", "Touch the screen to sleep again");
            } else {
                drawMessage("Deep Sleep", "Wake up after 5 seconds");
                delay(200);
                M5.Power.deepSleep(SLEEP_TIME_US, true);
            }
        }
    }

    delay(20);
}

After startup, the screen shows two sleep options. Touch the left side to enter light sleep for 5 seconds. On wakeup, execution continues from the line after lightSleep() and displays a completion message. Touch the right side to enter deep sleep for 5 seconds. On wakeup, ESP32-C5 resets and runs setup() again.

API

The ToughC5 sleep and wakeup example uses Power_Class from the M5Unified library. See the following documentation for more APIs:

Page Tools
PDF
On This Page