Loading...
Searching...
No Matches
health_store.c File Reference

The active health store: reads the watch's health service and holds the numbers. It owns the service accessors too so it is the single point of truth for health. More...

#include "io/stores/health_store.h"
#include "io/stores/store_cadence.h"
#include "io/stores/store_sum.h"
#include "io/stores/health_hrv.h"
#include "io/stores/health_store_parts.h"
#include "health/hrv_run.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>

Data Structures

struct  [struct].s_state

Macros

#define HRV_BUILT   0
 1 when the face builds heart rate variability in, 0 when it leaves it out.
#define HEALTH_PART_COUNT   3
 How many parts a face can name.
#define STEP_CATCHUP_HOURS   6
 How many hours a walk of the minute log asks for at a time.

Functions

bool health_metric_available (HealthMetric metric, time_t start, time_t end)
 Whether the watch actually has a reading for a metric over a time span.
static int read_hr (void)
 Peek the current heart rate.
static int read_sum_today (HealthMetric metric, int fallback)
 Sum a metric over today, or return the fallback when it is unavailable.
HealthMinuteData * health_scratch_take (int records)
 Allocate scratch room for a minute-history read, off the heap for the length of the read rather than sitting in the app's fixed footprint.
time_t health_hour_start (const struct tm *today, int hour)
 When a local hour of today starts.
bool health_walk_minute_log (const struct tm *local, int first_hour, int end_hour, time_t until, MinuteRecordFn each, void *context)
 Walks the watch's minute log over today's hours and hands each good record, with the hour it falls in, to the each callback.
static void refresh_hr (bool from_event)
 Refresh the heart rate and hand it to each part. A heart rate is never really 0, so -1 is kept as "no reading yet".
static void refresh_activity (bool force)
 Refresh the day's activity totals, steps, distance, calories, sleep, and active time, and hand the step count to each part.
static uint32_t state_sum (void)
 The readings and every named part's state summed, so a change anywhere shows.
static void notify_if_moved (uint32_t before)
 Asks the face for a repaint only when a reading really moved.
static void on_health_event (HealthEventType event, void *context)
 Health event handler: refresh only the metrics the event touched, then notify.
static void cadence_poll (void)
 The store's turn on the face's cadence, registered at init so no face wires it by hand.
void health_store_subscribe (void(*cb)(void))
 Hand it the function to call when the numbers change (the screen redraw).
void health_store_redraw (void)
 Asks the face to redraw, for a part whose own timer changed what it holds.
void health_store_deinit (void)
 Stops what the store asked of the watch. Call from the face's deinit.
void health_store_set_hrv_period (uint16_t period_sec)
 Changes how often the watch takes a burst of beat gaps, such as when the wearer changes a setting or places a panel. A period of 0 stops the sampling and puts the reading back to none.
void health_store_set_hrv_rules (uint16_t still_mg, uint16_t walk_steps, uint16_t settle_gaps, HrvCount count, bool keep_all, bool live_redraw)
 Changes which HRV bursts count and how they are measured, such as when the wearer changes a setting or places a panel.
void health_store_init (HealthConfig cfg, const HealthSeed *seed)
 Start the store with its rules. Pass seed = NULL for normal use.
bool health_store_has_hr (void)
 Whether this watch can give a heart rate at all, for a face that leaves its heart rate slot out rather than showing dashes forever.
int health_store_hr (void)
 The heart rate in beats per minute, or -1 if we have not got one yet.
int health_store_steps (void)
 The steps taken so far today, or -1 if we have not got one yet.
int health_store_calories (void)
 The active calories so far today, or -1 if we have not got one yet.
int health_store_sleep_min (void)
 The minutes of sleep, or -1 if we have not got one yet.
int health_store_active_min (void)
 The active minutes so far today, or -1 if we have not got one yet.
int health_store_distance_m (void)
 Today's walked distance in meters, or 0 if we cannot read it.
int health_store_hrv_ms (void)
 Heart rate variability as RMSSD in ms, or -1 if we have not got one, it went stale, or the face is built without HEALTH_HRV.
bool health_store_hrv_sampling (void)
 Whether the watch took the last request for beat gaps.
bool health_store_hrv_measuring (void)
 Whether a burst of beat gaps is being measured right now, with the sensor held on.
bool health_store_hrv_recent (int back, HrvBurst *out)
 One of the latest HRV bursts, for a readout that shows each burst, dropped ones included, newest first. Kept in memory only, so a face starts each launch with none.
bool health_store_hrv_live (int *score, int *steps, int *held_sec)
 The HRV burst being held right now, for a panel that shows it as it happens.
uint16_t health_store_hrv_still_mg (void)
 The still limit in use, in milli-g a sample, so a panel shows the same number as the setting.
uint16_t health_store_hrv_walk_steps (void)
 The steps through a burst that make it a walk, so a panel shows the same number as the setting.
uint16_t health_store_hrv_settle_gaps (void)
 The gaps left out at the start of each burst, after the hold to HRV_SETTLE_GAPS_MAX, so a panel shows the count in use rather than the one asked for.
const HrvLog * health_store_hrv_log (void)
 The HRV readings kept for the panels: the latest few with what the wearer was doing, today by the hour, last night's average, and the latest of each kind.

Variables

static struct  s_state
 Every health number the store holds.
static void(*) s_cb (void)
 Called whenever a reading changes, so the face can redraw.
static bool s_live
 True once subscribed to the live health service, so the minute poll does nothing in seed mode.
static const HealthPart * s_parts [HEALTH_PART_COUNT]
 The parts the face named, the heart rate graph, the hourly steps, and the heart rate day, with NULL for one it left out.
Costly readings

These four cost a flash read every time they are asked for, so only a face that shows one pays.

static bool s_sleep
 Whether the face shows sleep.
static bool s_active
 Whether the face shows active minutes.
static bool s_calories
 Whether the face shows calories.
static bool s_distance
 Whether the face shows distance.

Detailed Description

The active health store: reads the watch's health service and holds the numbers. It owns the service accessors too so it is the single point of truth for health.

Macro Definition Documentation

◆ HRV_BUILT

#define HRV_BUILT   0

1 when the face builds heart rate variability in, 0 when it leaves it out.

It is built in only for a face that sets HEALTH_HRV in its appinfo's defines. With it off nothing here calls into health_hrv.c, so the linker leaves that out of the face.

◆ STEP_CATCHUP_HOURS

#define STEP_CATCHUP_HOURS   6

How many hours a walk of the minute log asks for at a time.

The records come into a buffer borrowed from the heap, so a batch this size keeps it a few KB. A read costs the same whatever window it asks for, so this still takes a day in four reads.

Function Documentation

◆ cadence_poll()

void cadence_poll ( void )
static

The store's turn on the face's cadence, registered at init so no face wires it by hand.

The events alone are not enough. Heart rate events are sparse, so the graph would be dots not a line, and movement events stop the moment the wearer goes still, stranding the step count. The gates inside the two refreshes make this free on a turn an event already covered.

◆ notify_if_moved()

void notify_if_moved ( uint32_t before)
static

Asks the face for a repaint only when a reading really moved.

The minute tick and most movement events bring back the same numbers, and a repaint for nothing still reruns every health panel. A face graphing the heart rate window still gets one a minute, since the window slides. It compares sums rather than a copy of every reading, which keeps the readings off the stack.

Parameters
beforeThe sum of the readings before the refresh.

◆ on_health_event()

void on_health_event ( HealthEventType event,
void * context )
static

Health event handler: refresh only the metrics the event touched, then notify.

A movement event does not change the heart rate history and an hr event does not change the step count, so each reads only its own group. The minute gate inside refresh_activity does the rest, turning most movement events into nothing at all.

Parameters
eventThe health event type.
contextContext (unused).

◆ read_hr()

int read_hr ( void )
static

Peek the current heart rate.

Returns
The reading in beats per minute, or 0 when there is none.

◆ read_sum_today()

int read_sum_today ( HealthMetric metric,
int fallback )
static

Sum a metric over today, or return the fallback when it is unavailable.

Distance passes 0 as its fallback, and a panel tells no data from a real zero by the steps count. The rest, steps included, pass -1 so a panel can tell "no data yet" apart from a real zero and show a placeholder.

Parameters
metricThe health metric to sum.
fallbackReturned when the metric is unavailable.
Returns
The sum for today, or fallback.

◆ refresh_activity()

void refresh_activity ( bool force)
static

Refresh the day's activity totals, steps, distance, calories, sleep, and active time, and hand the step count to each part.

These are whole-day sums that move once a minute at most, but movement events arrive every few seconds while walking, and every metric bar steps costs a blocking flash read. So the reads are held to one round a minute, and the four a face has to ask for are skipped unless it has.

Parameters
forceRead now regardless of the minute gate, for the seed read and for the significant update that calls every number stale.

◆ refresh_hr()

void refresh_hr ( bool from_event)
static

Refresh the heart rate and hand it to each part. A heart rate is never really 0, so -1 is kept as "no reading yet".

Parameters
from_eventTrue when a heart rate event brought a new reading, rather than a peek that can hand back the same one for up to fifteen minutes.

◆ state_sum()

uint32_t state_sum ( void )
static

The readings and every named part's state summed, so a change anywhere shows.

Returns
The sum.