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

The heart rate variability half of the health store. More...

#include "io/stores/health_hrv.h"
#include "health/hrv_run.h"
#include "health/hrv_motion.h"
#include "io/stores/store_persist.h"
#include <string.h>
#include <time.h>

Data Structures

struct  HrvSaved
 The log as it goes to flash, with the tag store_restore checks in front. More...

Functions

static int32_t steps_now (void)
 The day's step count so far, or -1 when the watch cannot say.
static int steps_through (void)
 The steps taken since the hold started.
static HrvState burst_state (void)
 What the wearer is doing through the burst so far, from its steps and its movement.
static bool cannot_count (void)
 Whether the burst being held can no longer count for the main reading.
static void note_burst (int ms, uint32_t now_sec, bool counted)
 Keeps a finished or dropped burst in the tuning ring.
static void show (int ms, uint32_t now_sec)
 Puts a new reading up, or takes one down with -1, and redraws only when it moved.
static void save_log (void)
 Saves the log to flash, when the face gave it a slot.
static void local_day (uint32_t now_sec, int32_t *day, int *minute_of_day)
 The day and the minute of the day on the watch's own clock.
static void record (int ms, uint32_t now_sec)
 Files a finished reading and shows it, when it counts.
static void request (uint16_t period_sec, uint32_t now_sec)
 Asks the watch for beat gaps at a period, or tells it to stop with a period of 0.
static void on_accel (AccelData *data, uint32_t num_samples)
 Adds each accelerometer sample to the burst's movement, leaving out ones taken while the watch was vibrating, since the motor shakes it as hard as a step.
static void accel_start (void)
 Starts reading the accelerometer for a burst.
static void accel_stop (void)
 Stops reading the accelerometer once a burst is over.
static void hold_drop (void)
 Drops a hold without asking the watch for anything, for a caller about to ask for a period of its own.
static void hold_start (uint32_t now_sec)
 Holds the sensor on for the burst that just started, so its run can fill.
static void hold_end (uint32_t now_sec)
 Lets the sensor go back to the face's period once a burst is measured.
static void end_quiet_burst (uint32_t now_sec)
 Ends a burst whose beats went quiet: files what it measured, or notes it as dropped for a readout that shows each burst, and lets the sensor go.
void health_hrv_on_gap (void)
 Reads one beat gap from the watch. The health store calls it for each HRV event.
void health_hrv_poll (void)
 The HRV turn on the face's cadence, which the health store passes on while it is live.
static void show_latest_logged (uint32_t now_sec)
 Puts the log's newest reading that counted back up, when it is not stale yet.
void health_hrv_start (const HealthHrvConfig *cfg, int seed_ms, const HrvLog *seed_log, void(*changed)(void))
 Starts HRV with the health store's rules, or starts it again with new ones.
void health_hrv_set_period (uint16_t period_sec)
 Changes how often the watch takes a burst. A period of 0 stops the sampling and puts the reading back to none on a live store, and a seeded store keeps its seed.
void health_hrv_set_rules (uint16_t still_mg, uint16_t walk_steps, uint16_t settle_gaps, HrvCount count, bool keep_all, bool live_redraw)
 Changes the rules for which bursts count and how they are measured, such as when the wearer changes a setting or places a panel.
void health_hrv_stop (void)
 Stops the sampling, since the watch keeps an app's request going for an hour after it closes.
int health_hrv_ms (void)
 The latest reading as RMSSD in ms, or -1 if there is none or it went stale.
bool health_hrv_sampling (void)
 Whether the watch took the last request for beat gaps.
bool health_hrv_measuring (void)
 Whether a burst is being measured right now, with the sensor held on.
const HrvLog * health_hrv_log (void)
 The readings kept for the face's panels. Never NULL.
bool health_hrv_recent (int back, HrvBurst *out)
 One of the latest bursts, dropped ones included, newest first.
bool health_hrv_live (int *score, int *steps, int *held_sec)
 The burst being held right now.
uint16_t health_hrv_still_mg (void)
 The still limit in use, in milli-g a sample.
uint16_t health_hrv_walk_steps (void)
 The steps through a burst that make it a walk.
uint16_t health_hrv_settle_gaps (void)
 The gaps left out at the start of each burst.

Variables

static HrvRun s_run
 The run of beats being added up. It is kept apart from the reading, since a gap lands about once a beat and only a finished run changes what the face shows.
static int s_ms = -1
 The reading shown, or -1 for none.
static uint32_t s_measured_at
 When the reading shown was taken, in seconds.
static uint16_t s_period_sec
 How often the watch takes a burst, or 0 for not at all.
static bool s_live
 True once HRV asks the watch, so seed mode never does.
static bool s_sampling
 Whether the watch took the last request.
static uint32_t s_asked_at
 When the watch was last asked, in seconds.
static uint32_t s_gap_at
 When the last gap arrived, or the watch took a request, in seconds.
static bool s_holding
 True while a burst is held on with HRV_HOLD_PERIOD_SEC.
static uint8_t s_settle
 Gaps still to leave out at the start of this burst.
static uint16_t s_still_mg
 The most the wrist may move for a burst to be still.
static uint16_t s_walk_steps
 The steps through a burst that make it a walk.
static uint8_t s_settle_gaps
 The gaps a burst leaves out as it starts.
static HrvCount s_count
 Which bursts count for the main reading.
static bool s_accel_on
 Whether the accelerometer is being read for the burst.
static HrvMotion s_motion
 The movement seen through the burst being held.
static uint32_t s_held_since
 When the hold started, in seconds.
static void(*) s_changed (void)
 Called whenever the reading changes, so the face can redraw.
static bool s_keep_all
 Whether a burst that cannot count is measured anyway, for a panel that shows it.
static bool s_live_redraw
 Whether the face is redrawn once a second while a burst is held, for a readout that shows each burst.
static int32_t s_steps_start = -1
 The day's step count when the hold started, or -1 when not known.
static uint8_t s_hold_gaps
 Gaps added to the run since the hold started.
static HrvBurst s_recent [HRV_RECENT]
 The latest bursts, for a readout that shows each burst, kept in memory only.
static uint8_t s_recent_head
 The slot the next burst goes in.
static uint32_t s_persist_key
 The persist slot the log is saved in, or 0 for none.
static HrvLog s_log
 The readings kept for the face's panels.

Detailed Description

The heart rate variability half of the health store.

The watch reports one gap between two heartbeats at a time, as a health event. The health store holds the app's one health subscription, so it hands each of those events here, along with its turn on the face's cadence. The arithmetic, which beats count and how a run adds up to RMSSD, is in core/health/hrv_run.c, and the movement score is in core/health/hrv_motion.c.

Function Documentation

◆ accel_start()

void accel_start ( void )
static

Starts reading the accelerometer for a burst.

Every held burst is scored, since the log tags each reading still or busy and an unscored one would go in as still whatever the wrist did.

◆ cannot_count()

bool cannot_count ( void )
static

Whether the burst being held can no longer count for the main reading.

Steps never come back off within a burst, so a walk decides it at once. The still level also gives up once the wrist has moved more than a whole burst at the limit could hold, rather than as soon as the average crosses it, since a scratch early on does not mean the burst will not pass.

◆ end_quiet_burst()

void end_quiet_burst ( uint32_t now_sec)
static

Ends a burst whose beats went quiet: files what it measured, or notes it as dropped for a readout that shows each burst, and lets the sensor go.

Beats that came outside a hold have no score and never make a reading, so their run is only emptied.

Parameters
now_secThe time now, in seconds.

◆ hold_end()

void hold_end ( uint32_t now_sec)
static

Lets the sensor go back to the face's period once a burst is measured.

Parameters
now_secThe time now, in seconds.

◆ hold_start()

void hold_start ( uint32_t now_sec)
static

Holds the sensor on for the burst that just started, so its run can fill.

The watch turns the sensor off on its first good heart rate, which is only a handful of beats in. A period shorter than the sensor's warm up keeps it on, so the burst is asked for at HRV_HOLD_PERIOD_SEC until its run is done. That costs up to HRV_HOLD_MAX_SEC of sensor time a burst, and is what gives a run its 20 beats at all.

Parameters
now_secThe time now, in seconds.

◆ local_day()

void local_day ( uint32_t now_sec,
int32_t * day,
int * minute_of_day )
static

The day and the minute of the day on the watch's own clock.

The log only needs a day that changes at local midnight, so the year and the day of the year make one without any date sums.

Parameters
now_secThe time now, in seconds.
[out]dayThe day.
[out]minute_of_dayThe minute of the day, 0 to 1439.

◆ note_burst()

void note_burst ( int ms,
uint32_t now_sec,
bool counted )
static

Keeps a finished or dropped burst in the tuning ring.

Parameters
msIts reading, or -1 when it was dropped or gave none.
now_secThe time now, in seconds.
countedWhether it became the main reading.

◆ on_accel()

void on_accel ( AccelData * data,
uint32_t num_samples )
static

Adds each accelerometer sample to the burst's movement, leaving out ones taken while the watch was vibrating, since the motor shakes it as hard as a step.

Parameters
dataThe samples.
num_samplesHow many there are.

◆ record()

void record ( int ms,
uint32_t now_sec )
static

Files a finished reading and shows it, when it counts.

Every reading goes into the log with what the wearer was doing. One the face's count level leaves out, such as a walking one, is logged but does not replace the main reading, which stays a resting one.

Parameters
msThe reading in ms.
now_secThe time now, in seconds.

◆ request()

void request ( uint16_t period_sec,
uint32_t now_sec )
static

Asks the watch for beat gaps at a period, or tells it to stop with a period of 0.

Parameters
period_secHow often the watch takes a burst, or 0 to stop.
now_secThe time now, in seconds, which the next ask is spaced from.

◆ save_log()

void save_log ( void )
static

Saves the log to flash, when the face gave it a slot.

It runs once a reading, which at a five minute period is a few hundred writes of a quarter of a kilobyte a day. The CPU is already awake for the burst, and a log saved only when the face closes would lose the day whenever the watch resets.

◆ show()

void show ( int ms,
uint32_t now_sec )
static

Puts a new reading up, or takes one down with -1, and redraws only when it moved.

Parameters
msThe reading in ms, or -1 for none.
now_secThe time now, in seconds, which the staleness check counts from.

◆ show_latest_logged()

void show_latest_logged ( uint32_t now_sec)
static

Puts the log's newest reading that counted back up, when it is not stale yet.

Without it the readout shows dashes after every relaunch until the next burst finishes, which is up to a sample period plus the hold, while the panels that read the log still show it.

Parameters
now_secThe time now, in seconds.

◆ steps_through()

int steps_through ( void )
static

The steps taken since the hold started.

The day's count starts over at midnight, and the sleep tracker can take steps back off minutes it later judges asleep, so a count that went down says nothing and reads as not known.

Returns
The steps, or -1 when not known.