Stopwatch counts in real time from a start date, kept over a restart

The stopwatch counted with millis(), so it started at 0 after every
restart and would overflow after 49 days. It now keeps the real start
time (UTC ms) and the time counted before a pause, stored in NVS
(system/stopwatch_store) on every change, so it keeps counting while
the display is off or restarting. Stop pauses, Start continues, and the
Stopwatch tab can set the start to a date and time (local, summer time
by mktime) to count from, e.g. days since an event. Start, stop and set
need the synced time.

New page formats: 12d 4:05:09 and 3w 2d.

Version 2.19.0, tested on the display over OTA (set start, future and
invalid dates refused, pause, restart).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mangavalkandClaude Opus 5.5 committed 2026-10-20 22:22:32 +02:00
1 parent f5e77c7eca
commit 0166d0ee46
20 files changed
+278 -67

No files matched your search

+44 -3
View File
@@ -15,6 +15,7 @@
#include "net/ha_api.h"
#include "system/schedule_store.h"
#include "system/settings_store.h"
#include "system/stopwatch_store.h"
static void copyName(char *out, const char *name)
{
@@ -119,6 +120,7 @@ void appLoadSettings()
{
loadSettings(settings);
loadSchedule(page_schedule);
loadStopwatch(stopwatch);
selector.setSchedule(&page_schedule);
selector.setChosen(settings.page);
haApiWatchRules(&page_schedule);
@@ -486,15 +488,54 @@ const char *appDeleteFont(const char *name)
return deleteFont(name);
}
void appStopwatch(StopwatchAction action)
static const char *NO_TIME = "The time is not synced yet";
const char *appStopwatch(StopwatchAction action)
{
// It counts in real time: before the sync, the clock still says 1970
if (!time_known && action != StopwatchAction::Reset)
return NO_TIME;
int64_t now = epochMs();
if (action == StopwatchAction::Start)
stopwatch.start(millis());
stopwatch.start(now);
else if (action == StopwatchAction::Stop)
stopwatch.stop(millis());
stopwatch.stop(now);
else
stopwatch.reset();
saveStopwatch(stopwatch);
controller.pageChanged();
return nullptr;
}
const char *appStopwatchSetStart(const char *local)
{
uint32_t minutes;
if (!parseTarget(local, minutes))
return "Invalid start: use YYYY-MM-DDTHH:MM";
if (!time_known)
return NO_TIME;
struct tm t = {};
sscanf(local, "%4d-%2d-%2dT%2d:%2d", &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min);
t.tm_year -= 1900;
t.tm_mon -= 1;
t.tm_isdst = -1; // the time zone decides (summer time at that date)
time_t start = mktime(&t);
if (start == (time_t)-1 || !stopwatch.setStart((int64_t)start * 1000, epochMs()))
return "The start is in the future";
saveStopwatch(stopwatch);
controller.pageChanged();
return nullptr;
}
bool appStopwatchStart(char *out)
{
if (!time_known || !stopwatch.running())
return false;
time_t start = (time_t)((epochMs() - (int64_t)stopwatch.elapsed(epochMs())) / 1000);
struct tm t;
localtime_r(&start, &t);
strftime(out, 20, "%Y-%m-%dT%H:%M:%S", &t);
return true;
}
void appShowLive(const uint8_t *bitmap)