main
John Lauer
Publish assumption-based R11 assembly study, locked firmware and new review footage
d61d6cb
#include "measurement_cache.h"
#include <math.h>
#include <stddef.h>
void load_measurement_begin(load_measurement_cache *c,uint32_t now,bool ok){
*c=(load_measurement_cache){.healthy=ok,.trigger_ms=now};
}
load_ina_result load_measurement_poll(load_measurement_cache *c,const load_i2c *b,uint32_t now){
if(!c->healthy)return LOAD_INA_ERROR;
if(c->have_sample&&(uint32_t)(now-c->sample_ms)>50)c->have_sample=false;
float v,a;
load_ina_result r=load_ina_poll(b,&v,&a);
if(r==LOAD_INA_ERROR){c->healthy=false;c->have_sample=false;return r;}
if(r==LOAD_INA_READY){
c->sample_ms=c->trigger_ms;c->trigger_ms=now;
c->load_terminal_v=v;c->amps=a;c->have_sample=isfinite(v)&&isfinite(a);
if(!c->have_sample){c->healthy=false;return LOAD_INA_ERROR;}
}
return r;
}
bool load_measurement_get(const load_measurement_cache *c,uint32_t now,float *v,float *a,uint32_t *stamp){
if(!c||!v||!a||!stamp||v==a||!c->healthy||!c->have_sample||(uint32_t)(now-c->sample_ms)>50)return false;
*v=c->load_terminal_v;*a=c->amps;*stamp=c->sample_ms;return true;
}