
Arduino Quick Start
ToughC5 sleep and wakeup APIs and example program.
3.3.9M5ToughC50.2.220.2.28#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.
The ToughC5 sleep and wakeup example uses Power_Class from the M5Unified library. See the following documentation for more APIs: