Skip to content

Implement sending typical sleep to the watch#194

Open
aveao wants to merge 6 commits into
coredevices:masterfrom
aveao:fix/typical-sleep
Open

Implement sending typical sleep to the watch#194
aveao wants to merge 6 commits into
coredevices:masterfrom
aveao:fix/typical-sleep

Conversation

@aveao

@aveao aveao commented May 16, 2026

Copy link
Copy Markdown
Contributor

See #78

Draft as this builds on #192, that should be merged first

image

aveao added 6 commits June 5, 2026 23:40
Adds the four-field data class and file-private constants
(history weeks, min-history threshold, min-session-seconds filter,
seconds-per-day) that the upcoming typical-sleep writer will use.
No behavior change yet.
Adds the helper signature, empty-input fast path, and test fixtures
(session, nightSleepMulti, nightSleep) that subsequent tests will use.
Subsequent commits fill in the per-weekday reducer test-by-test.
Implements buildWeekdaySleepTypicalsFromData: filters sessions ≥30min,
partitions by weekday, computes arithmetic-mean durations and
circular-mean bedtime/wake (so 23:00+01:00 averages to ~00:00, not noon).
Weekdays below the 2-day minimum-history threshold are omitted.

Adds the secondsOfDay and circularMeanSecondsOfDay helpers.

Tests cover: empty input, below-threshold skip, two-day basic case,
nap-only filtering, the critical circular-mean midnight-wrap case,
per-weekday independence, split-sleep last-session-wake, and
mixed-validity within a single day.
Wires computeAllWeekdayTypicalSleep into updateHealthStatsInDatabase
so the four typical_* fields of each <weekday>_sleepData blob (which
have been hardcoded to 0u since the writer was first added) now carry
real values: arithmetic-mean total/deep sleep durations and
circular-mean bedtime/wake, computed over the user's past 7 weeks of
same-weekday history.

This populates the firmware fields read by health_db_get_typical_value
(blob_db/health_db.c) and consumed by the sleep summary card
(apps/system/health/data.c) and the activity_insights sleep
notification path.
The existing daily 6-day loop deliberately skips today (incomplete
current-day daily fields shouldn't overwrite the watch's
accelerometer-tracked values), but the firmware reads
`<today's weekday>_sleepData` typicals on the sleep summary card.
Without this extra write the typicals for today's weekday stay
stale for a day after the new typical-sleep code starts running.

After the daily loop, add a typicals-only blob for today's weekday
with last_processed_timestamp=0. Firmware's prv_notify_health_listeners
gates the in-memory daily-metric update on a valid timestamp and bails
out, so daily fields=0 don't corrupt today's tracked values, but
health_db_insert still stores the blob — making the typicals readable
via health_db_get_typical_value.

This brings sleep typicals in line with the step typicals, which
already cover all 7 weekdays via their separate per-weekday loop.
Mirrors the typical-steps debug section added on fix/typical-steps,
showing the four values the watch reads from each <weekday>_sleepData
blob's typical tail: duration / deep duration / bedtime → wakeup.
Weekdays below the 2-day minimum-history threshold show "--".

WeekdaySleepTypicals visibility bumped from internal to public so the
new HealthDebugStats field can expose it across modules. The data
class is just four Ints and was always going to be reachable by the
debug dialog by design.
@aveao aveao force-pushed the fix/typical-sleep branch from 1174f79 to 9aeea86 Compare June 5, 2026 21:40
@aveao aveao marked this pull request as ready for review June 5, 2026 21:40
@aveao

aveao commented Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

Rebased, should be ready to review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant