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

The health store's heart rate graph: the last hour of heart rate, a reading a minute, saved to flash so a relaunch shows it straight away. A face turns it on with HEALTH_HR_HISTORY, and one that never names it carries none of this. More...

#include "io/stores/health_store.h"
#include "io/stores/health_store_parts.h"
#include "io/stores/store_persist.h"
#include "health/minute_window.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>

Data Structures

struct  HealthSaved
 The window as it goes to flash. More...
struct  [struct].s_window

Macros

#define HR_HISTORY_MINUTES   60
 How many minutes of heart rate the graph shows.

Functions

static void read_hr_history (time_t now_min)
 Fill the empty minutes of the heart rate window from the watch's own minute log.
static void persist_save (void)
 Stash the heart rate graph so a relaunch can restore it.
static void hr_history_advance (time_t now_min)
 Slide the rolling heart rate window forward to now_min, zeroing any minutes that passed with no reading. The last slot always holds the current minute, so the chart reads oldest on the left and newest on the right.
static void hr_history_start (const HealthConfig *cfg, const HealthSeed *seed)
 Empties the window, then takes the seed, or restores the saved window and fills the time away from the log for a live store.
static void hr_history_heart_rate (int hr, bool from_event)
 Slides the window to now and puts a new reading in its last slot.
static uint32_t hr_history_sum (void)
 The window summed, so the store can tell when it moved.
const uint8_t * health_store_hr_history (void)
 The last hour of heart rate readings, one per minute (a 60 slot buffer), for a face that named HEALTH_HR_HISTORY. The little charts read it back out. Empty for a face that did not.

Variables

static struct  s_window
 The rolling window and the minute its last slot holds.
static bool s_live
 True for a live store, so seed mode never writes to flash.
static uint32_t s_persist_key
 The persist slot the face handed us for the saved window.
const HealthPart health_part_hr_history
 The last hour of heart rate. Name it as HEALTH_HR_HISTORY.

Detailed Description

The health store's heart rate graph: the last hour of heart rate, a reading a minute, saved to flash so a relaunch shows it straight away. A face turns it on with HEALTH_HR_HISTORY, and one that never names it carries none of this.

Macro Definition Documentation

◆ HR_HISTORY_MINUTES

#define HR_HISTORY_MINUTES   60

How many minutes of heart rate the graph shows.

It has nothing to do with an hour having 60 minutes, even though it lands on the same number.

Function Documentation

◆ hr_history_advance()

void hr_history_advance ( time_t now_min)
static

Slide the rolling heart rate window forward to now_min, zeroing any minutes that passed with no reading. The last slot always holds the current minute, so the chart reads oldest on the left and newest on the right.

Parameters
now_minThe current wall-clock minute (time / SECONDS_PER_MINUTE).

◆ hr_history_heart_rate()

void hr_history_heart_rate ( int hr,
bool from_event )
static

Slides the window to now and puts a new reading in its last slot.

The chart reads from this window rather than the minute history: the watch only logs a per-minute heart_rate_bpm now and then in the background, so the minute history is mostly empty, while peeking the live value on each event fills the window with real readings.

Parameters
hrThe reading in beats per minute, or -1 for none.
from_eventUnused, since a peek is as good as an event for the graph.

◆ hr_history_start()

void hr_history_start ( const HealthConfig * cfg,
const HealthSeed * seed )
static

Empties the window, then takes the seed, or restores the saved window and fills the time away from the log for a live store.

Parameters
cfgThe store's rules.
seedThe store's seed, or NULL.

◆ hr_history_sum()

uint32_t hr_history_sum ( void )
static

The window summed, so the store can tell when it moved.

Returns
The sum.

◆ persist_save()

void persist_save ( void )
static

Stash the heart rate graph so a relaunch can restore it.

Only a live face writes, so seed mode never touches storage. A burst can land a reading a second and a flash write blocks, so the saves are held to one a minute, which is all the graph gains anyway.

One a minute is the rate to keep, not one to trim. The save runs from whichever comes first in a new minute, the face's cadence turn or a health event that refreshes the heart rate. Either way the CPU is already awake for it, so no extra wake is spent. The face is closed whenever the wearer opens a watchapp, even for a moment, and a graph saved less often comes back missing its last minutes every time.

◆ read_hr_history()

void read_hr_history ( time_t now_min)
static

Fill the empty minutes of the heart rate window from the watch's own minute log.

Used at launch, on an empty window or on a restored one slid forward over the time the face was away, so the minutes spent in a watchapp show what the watch logged. A minute that already holds a reading keeps it, since the live readings can be more frequent than the log. Each reading lands in the slot for its own minute.

Parameters
now_minThe minute the window's last slot holds, the same one the live readings write.