From 6a42762e2a3ad3ec32904b267c4bd7541dbb23d0 Mon Sep 17 00:00:00 2001 From: Correl Date: Fri, 20 Jan 2023 16:28:31 -0500 Subject: [PATCH] Add a power button --- src/main.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 3463fcb..ec604d5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,10 +8,12 @@ const char* ssid = "REPLACE_WITH_YOUR_SSID"; const char* password = "REPLACE_WITH_YOUR_PASSWORD"; AsyncWebServer server(80); +LGFX_Button button_power; void setup(void) { auto cfg = M5.config(); M5.begin(cfg); + M5.Display.init(); M5.Display.setTextSize(3); WiFi.mode(WIFI_STA); @@ -37,6 +39,7 @@ void setup(void) { server.begin(); Serial.println("HTTP server started"); + M5.Display.startWrite(); M5.Display.printf("SSID: %s\n", ssid); M5.Display.printf("IP Address: %s\n", WiFi.localIP().toString()); M5.Display.printf("\n\n"); @@ -44,9 +47,28 @@ void setup(void) { M5.Display.printf("ElegantOTA available at\n" "http://%s/update\n", WiFi.localIP().toString()); + + button_power.initButton(&M5.Display, M5.Display.width() / 2, M5.Display.height() / 2, 200, 100, TFT_BLACK, TFT_LIGHTGRAY, TFT_BLACK, "Power", 3, 3); + button_power.drawButton(); + M5.Display.endWrite(); } void loop() { + M5.update(); + auto t = M5.Touch.getDetail(); + if (t.wasPressed()) { + if (button_power.contains(t.x, t.y)) { + button_power.press(true); + } else { + button_power.press(false); + } + } else { + button_power.press(false); + } + if (button_power.justReleased()) { + M5.Display.clearDisplay(TFT_WHITE); + M5.Power.powerOff(); + } yield(); }