Files
TableWatch/src/main.cpp

77 lines
1.8 KiB
C++

#include <Arduino.h>
#include <esp_sleep.h>
#include "app_state.h"
#include "config.h"
#include "display.h"
#include "hardware.h"
#include "network.h"
#include "web.h"
void setup() {
Serial.begin(115200);
uint32_t serialWaitStart = millis();
while (!Serial && (millis() - serialWaitStart) < 1500) {
delay(10);
}
Serial.println("Boot");
delay(200);
initDisplay();
initAnalog();
int batteryPercent = readBatteryPercent();
time_t now = time(nullptr);
bool timeValid = now >= kMinValidEpoch;
bool needNetSync = !timeValid || gLastNetSyncEpoch == 0 ||
(timeValid && (now - gLastNetSyncEpoch) >= gNetSyncSec);
int wifiLevel = 0;
if (needNetSync) {
performNetSync(batteryPercent, &wifiLevel);
}
gShowTable = false;
renderClock(wifiLevel, batteryPercent);
gLastScreenSwitchMs = millis();
if (kDeepSleepEnabled) {
esp_sleep_enable_timer_wakeup(static_cast<uint64_t>(kWakeIntervalSec) * 1000000ULL);
esp_deep_sleep_start();
}
}
void loop() {
delay(1000);
if (kDeepSleepEnabled) {
return;
}
webLoop();
uint32_t nowMs = millis();
uint32_t currentScreenSec = gShowTable ? gTableScreenSec : gClockScreenSec;
if ((nowMs - gLastScreenSwitchMs) < (currentScreenSec * 1000UL)) {
return;
}
gLastScreenSwitchMs = nowMs;
time_t now = time(nullptr);
bool timeValid = now >= kMinValidEpoch;
bool needNetSync = !timeValid || gLastNetSyncEpoch == 0 ||
(timeValid && (now - gLastNetSyncEpoch) >= gNetSyncSec);
int batteryPercent = readBatteryPercent();
int wifiLevel = 0;
if (needNetSync) {
performNetSync(batteryPercent, &wifiLevel);
}
if (gNotionCount <= 0) {
gShowTable = false;
} else {
gShowTable = !gShowTable;
}
if (gShowTable) {
renderTable(wifiLevel, batteryPercent);
} else {
renderClock(wifiLevel, batteryPercent);
}
}