mod: fix clock bugs, add NTP auto-sync, show clock in all headers

- Fix SetTimeActivity immediately dismissing by changing wasReleased to
  wasPressed for all button inputs (matching other subactivities)
- Extract NTP sync into shared TimeSync utility (startNtpSync,
  waitForNtpSync, stopNtpSync) and trigger non-blocking NTP sync on
  every WiFi connection
- Move clock rendering into drawHeader (BaseTheme + LyraTheme) so it
  appears on all screens with a header, positioned symmetrically with
  the battery icon (12px margin, same Y offset, SMALL_FONT_ID)
- Add per-minute auto-refresh on home screen so clock updates without
  button press
- Add RTC time debug log on boot to verify time persistence across
  deep sleep

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cottongin
2026-02-17 02:13:10 -05:00
parent ab4540b26f
commit 38a87298f3
10 changed files with 145 additions and 54 deletions

50
src/util/TimeSync.cpp Normal file
View File

@@ -0,0 +1,50 @@
#include "TimeSync.h"
#include <Logging.h>
#include <esp_sntp.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
namespace TimeSync {
void startNtpSync() {
if (esp_sntp_enabled()) {
esp_sntp_stop();
}
esp_sntp_setoperatingmode(ESP_SNTP_OPMODE_POLL);
esp_sntp_setservername(0, "pool.ntp.org");
esp_sntp_init();
LOG_DBG("NTP", "SNTP service started");
}
bool waitForNtpSync(int timeoutMs) {
startNtpSync();
const int intervalMs = 100;
const int maxRetries = timeoutMs / intervalMs;
int retry = 0;
while (sntp_get_sync_status() != SNTP_SYNC_STATUS_COMPLETED && retry < maxRetries) {
vTaskDelay(intervalMs / portTICK_PERIOD_MS);
retry++;
}
if (retry < maxRetries) {
LOG_DBG("NTP", "Time synced after %d ms", retry * intervalMs);
return true;
}
LOG_DBG("NTP", "Sync timeout after %d ms", timeoutMs);
return false;
}
void stopNtpSync() {
if (esp_sntp_enabled()) {
esp_sntp_stop();
LOG_DBG("NTP", "SNTP service stopped");
}
}
} // namespace TimeSync

17
src/util/TimeSync.h Normal file
View File

@@ -0,0 +1,17 @@
#pragma once
namespace TimeSync {
// Start NTP time synchronization (non-blocking).
// Configures and starts the SNTP service; time will be updated
// automatically when the NTP response arrives.
void startNtpSync();
// Start NTP sync and block until complete or timeout.
// Returns true if time was synced, false on timeout.
bool waitForNtpSync(int timeoutMs = 5000);
// Stop the SNTP service. Call before disconnecting WiFi.
void stopNtpSync();
} // namespace TimeSync