Add "app" switching

This commit is contained in:
Correl Roush 2023-01-21 21:07:50 -05:00
parent d7010c54d5
commit c9abc1ce67

View file

@ -7,8 +7,74 @@
const char* ssid = "REPLACE_WITH_YOUR_SSID"; const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD"; const char* password = "REPLACE_WITH_YOUR_PASSWORD";
AsyncWebServer server(80); class App {
public:
virtual void loop() = 0;
};
class OTA: public App {
public:
OTA() {
M5.Display.startWrite();
M5.Display.clearDisplay(TFT_WHITE);
M5.Display.setCursor(0, 10);
M5.Display.printf("SSID: %s\n", ssid);
M5.Display.printf("IP Address: %s\n", WiFi.localIP().toString());
M5.Display.printf("\n\n");
M5.Display.printf("Web server running on port 80\n");
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() {
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();
}
protected:
LGFX_Button button_power; LGFX_Button button_power;
};
class Clock: public App {
public:
Clock() {
M5.Display.clearDisplay(TFT_WHITE);
}
void loop() {
static constexpr const char* const wd[7] = {"Sun","Mon","Tue","Wed","Thr","Fri","Sat"};
auto dt = M5.Rtc.getDateTime();
M5.Display.setCursor(0, 15);
M5.Display.printf("RTC : %04d/%02d/%02d (%s) %02d:%02d:%02d"
, dt.date.year
, dt.date.month
, dt.date.date
, wd[dt.date.weekDay]
, dt.time.hours
, dt.time.minutes
, dt.time.seconds
);
delay(500);
}
};
AsyncWebServer server(80);
App* app;
void setup(void) { void setup(void) {
auto cfg = M5.config(); auto cfg = M5.config();
@ -43,38 +109,19 @@ void setup(void) {
server.begin(); server.begin();
Serial.println("HTTP server started"); Serial.println("HTTP server started");
M5.Display.startWrite(); app = new OTA;
M5.Display.clearDisplay(TFT_WHITE);
M5.Display.setCursor(0, 10);
M5.Display.printf("SSID: %s\n", ssid);
M5.Display.printf("IP Address: %s\n", WiFi.localIP().toString());
M5.Display.printf("\n\n");
M5.Display.printf("Web server running on port 80\n");
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() void loop()
{ {
M5.update(); M5.update();
auto t = M5.Touch.getDetail(); if (M5.BtnA.wasClicked()) {
if (t.wasPressed()) { delete app;
if (button_power.contains(t.x, t.y)) { app = new Clock;
button_power.press(true); } else if (M5.BtnC.wasClicked()) {
} else { delete app;
button_power.press(false); app = new OTA;
}
} else {
button_power.press(false);
}
if (button_power.justReleased()) {
M5.Display.clearDisplay(TFT_WHITE);
M5.Power.powerOff();
} }
app->loop();
yield(); yield();
} }