Skip to content

Implement sending typical sleep to the watch#194

Draft
aveao wants to merge 11 commits into
coredevices:masterfrom
aveao:fix/typical-sleep
Draft

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

Conversation

@aveao
Copy link
Copy Markdown
Contributor

@aveao aveao commented May 16, 2026

See #78

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

image

aveao added 11 commits May 19, 2026 12:26
Adds file-private constants (bin count, bin width, history window,
minimum-history threshold, unknown sentinel) and the weekday→key map
that the upcoming typical-step writer will use. No behavior change yet.
Adds the helper signature and empty-input fast path. Subsequent commits
will fill in the binning algorithm test-by-test.
Implements buildWeekdayTypicalsFromData: single-pass binning of
HealthDataEntity rows into 7×96 (weekday × 15-min slot) aggregates,
with per-slot day-count tracking so a slot that was uncovered on
some matching days isn't underweighted. Weekdays below the 2-day
minimum-history threshold are omitted; per-slot gaps emit the
0xFFFF sentinel the watch firmware sum-skips.

Tests cover empty input, below-threshold skip, sentinel emission,
and the per-slot-vs-per-day averaging distinction.
Wires computeAllWeekdayTypicalSteps into updateHealthStatsInDatabase
so monday_steps..sunday_steps rows land in BlobDatabase.HealthStats
alongside the existing *_movementData / *_sleepData rows. The
@GenerateRoomEntity infrastructure on HealthStat pushes them to the
watch on next sync.

This fixes the missing percentage in the end-of-day activity summary
notification on PebbleOS watches (firmware activity_insights.c
prv_calc_percent_tier returns OnAverage when the typical sums to 0,
and the corresponding copy variants have no "%d%%" placeholder).
Surfaces the seven <weekday>_steps typical totals computed by
computeAllWeekdayTypicalSteps in the Health Debug Stats dialog so
the values pushed to the watch can be sanity-checked from the phone.
Each row sums the 96 15-min bins of that weekday's payload
(matching the firmware's prv_cur_step_avg sum semantics), or
shows "--" for weekdays below the 2-day minimum-history threshold.

New decodeTypicalStepTotal helper in HealthStatsSync encapsulates the
wire-format knowledge; HealthDebugStats gains a weekdayTypicalSteps
field; Health.getHealthDebugStats and FakeLibPebble.getHealthDebugStats
populate it; the iOS healthDebugStatsToJson includes it.
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 2993bef to 1174f79 Compare May 19, 2026 10:26
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