core/health/hrv_motion.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file hrv_motion.c | ||
| 3 | * @brief Tells whether the wearer kept still while a burst of beat gaps was measured. | ||
| 4 | * | ||
| 5 | * @ingroup lib_core | ||
| 6 | */ | ||
| 7 | #include "health/hrv_motion.h" | ||
| 8 | |||
| 9 | #include <string.h> | ||
| 10 | |||
| 11 | /** | ||
| 12 | * @brief How far one reading moved from the one before, as a positive number. | ||
| 13 | * | ||
| 14 | * @param now The reading now, in milli-g. | ||
| 15 | * @param before The reading before, in milli-g. | ||
| 16 | * @return The distance between them. | ||
| 17 | */ | ||
| 18 | 993 | static uint32_t moved(int16_t now, int16_t before) | |
| 19 | { | ||
| 20 | 993 | int32_t diff = (int32_t)now - (int32_t)before; | |
| 21 | 993 | return (uint32_t)(diff < 0 ? -diff : diff); | |
| 22 | } | ||
| 23 | |||
| 24 | 10 | void hrv_motion_reset(HrvMotion *motion) | |
| 25 | { | ||
| 26 | 10 | memset(motion, 0, sizeof(*motion)); | |
| 27 | 10 | } | |
| 28 | |||
| 29 | 341 | void hrv_motion_push(HrvMotion *motion, int16_t x_mg, int16_t y_mg, int16_t z_mg) | |
| 30 | { | ||
| 31 | // gravity is always there, so a watch held still reads the same tilt over and over. measuring | ||
| 32 | // the change from sample to sample leaves the tilt out and keeps only the movement | ||
| 33 |
3/4✓ Branch 2 → 3 taken 331 times.
✓ Branch 2 → 8 taken 10 times.
✓ Branch 3 → 4 taken 331 times.
✗ Branch 3 → 8 not taken.
|
341 | if (motion->primed && motion->samples < UINT16_MAX) |
| 34 | { | ||
| 35 | 331 | motion->sum += moved(x_mg, motion->last_x) + moved(y_mg, motion->last_y) + moved(z_mg, motion->last_z); | |
| 36 | 331 | motion->samples++; | |
| 37 | } | ||
| 38 | |||
| 39 | 341 | motion->last_x = x_mg; | |
| 40 | 341 | motion->last_y = y_mg; | |
| 41 | 341 | motion->last_z = z_mg; | |
| 42 | 341 | motion->primed = true; | |
| 43 | 341 | } | |
| 44 | |||
| 45 | 8 | int hrv_motion_score(const HrvMotion *motion) | |
| 46 | { | ||
| 47 |
2/2✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 4 taken 6 times.
|
8 | if (motion->samples < HRV_MOTION_MIN_SAMPLES) |
| 48 | { | ||
| 49 | 2 | return -1; | |
| 50 | } | ||
| 51 | |||
| 52 | 6 | return (int)(motion->sum / motion->samples); | |
| 53 | } | ||
| 54 | |||
| 55 | 4 | bool hrv_motion_over(const HrvMotion *motion, uint16_t max_mg) | |
| 56 | { | ||
| 57 | 4 | return hrv_motion_score(motion) > max_mg; | |
| 58 | } | ||
| 59 | |||
| 60 | 2 | bool hrv_motion_cannot_pass(const HrvMotion *motion, uint16_t max_mg, uint32_t max_samples) | |
| 61 | { | ||
| 62 | // the most movement the longest burst can hold and still average at the limit. the sum only | ||
| 63 | // grows, so once it is past that the burst is over the limit whatever comes next | ||
| 64 | 2 | return motion->sum > (uint32_t)max_mg * max_samples; | |
| 65 | } | ||
| 66 |