feat: add silent NTP time sync on boot via saved WiFi credentials

New "Auto Sync on Boot" toggle in Clock Settings. When enabled, a
background FreeRTOS task scans for saved WiFi networks at boot,
connects, syncs time via NTP, then tears down WiFi — all without
blocking boot or requiring user interaction. If no saved network is
found after two scan attempts (with a 3-second retry gap), it bails
silently.

Conflict guards (BootNtpSync::cancel()) added to all WiFi-using
activities so the background task cleans up before any user-initiated
WiFi flow. Also fixes clock not appearing in the header until a button
press by detecting the invalid→valid time transition after NTP sync.

Made-with: Cursor
This commit is contained in:
cottongin
2026-02-26 18:21:13 -05:00
parent 2eae521b6a
commit 19b6ad047b
22 changed files with 227 additions and 2 deletions

View File

@@ -32,6 +32,7 @@
#include "activities/util/FullScreenMessageActivity.h"
#include "components/UITheme.h"
#include "fontIds.h"
#include "util/BootNtpSync.h"
#include "util/ButtonNavigator.h"
HalDisplay display;
@@ -342,6 +343,7 @@ void setup() {
I18N.loadSettings();
KOREADER_STORE.loadFromFile();
BootNtpSync::start();
UITheme::getInstance().reload();
ButtonNavigator::setMappedInputManager(mappedInputManager);
@@ -459,14 +461,23 @@ void loop() {
// Refresh screen when the displayed minute changes (clock in header)
if (SETTINGS.clockFormat != CrossPointSettings::CLOCK_OFF && currentActivity) {
static int lastRenderedMinute = -1;
static bool sawInvalidTime = false;
time_t now = time(nullptr);
struct tm* t = localtime(&now);
if (t != nullptr && t->tm_year > 100) {
const int currentMinute = t->tm_hour * 60 + t->tm_min;
if (lastRenderedMinute >= 0 && currentMinute != lastRenderedMinute) {
if (lastRenderedMinute < 0) {
lastRenderedMinute = currentMinute;
if (sawInvalidTime) {
// Time just became valid (e.g. background NTP sync completed)
currentActivity->requestUpdate();
}
} else if (currentMinute != lastRenderedMinute) {
currentActivity->requestUpdate();
lastRenderedMinute = currentMinute;
}
lastRenderedMinute = currentMinute;
} else {
sawInvalidTime = true;
}
}