44 lines
1.1 KiB
C
44 lines
1.1 KiB
C
#include <freq_counter.h>
|
|
|
|
void freq_counter_init ( struct freq_counter *f, uint32_t cycle_len ) {
|
|
f->input_initialized = false;
|
|
f->input_state = false;
|
|
|
|
f->counter = 0;
|
|
f->samples = 0;
|
|
f->cycle_len = cycle_len;
|
|
|
|
f->new_measurement = false;
|
|
f->measurement = 0;
|
|
}
|
|
|
|
void IRAM_ATTR freq_counter_update ( struct freq_counter *f, bool input ) {
|
|
if ( f->input_initialized == false ) {
|
|
// ignore first input value
|
|
f->input_state = input;
|
|
f->input_initialized = true;
|
|
}
|
|
if ( f->input_state != input) {
|
|
f->counter += 1;
|
|
f->input_state = input;
|
|
}
|
|
f->samples += 1;
|
|
if ( f->samples >= f->cycle_len ) {
|
|
/* cycle complete */
|
|
f->measurement = f->counter;
|
|
f->new_measurement = true;
|
|
f->samples = 0;
|
|
f->counter = 0;
|
|
}
|
|
}
|
|
|
|
bool freq_counter_get_measurement( struct freq_counter *f, uint32_t *measurement) {
|
|
if ( f->new_measurement == true ) {
|
|
*measurement = f->measurement;
|
|
f->new_measurement = false;
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|