spejsiot-sonoff_pow/soft/src/freq_counter.h

43 lines
1.3 KiB
C

#include <stdbool.h>
#include <stdint.h>
#ifndef FREQ_COUNTER_H
#define FREQ_COUNTER_H
#ifndef IRAM_ATTR
#define IRAM_ATTR __attribute__((section(".iram.text")))
#endif
struct freq_counter {
bool input_initialized; /* false before first update call */
bool input_state; /* last update input state */
uint32_t counter; /* input state changes counter */
uint32_t samples; /* input samples counter */
uint32_t cycle_len; /* number of samples in one cycle */
bool new_measurement; /* set to true after updating measurement */
uint32_t measurement; /* number of state changes in last complete cycle */
};
/**
* @brief initialize frequency counter structure
*
* @param cycle_len number of samples in one measurement cycle
*/
void freq_counter_init ( struct freq_counter *f, uint32_t cycle_len );
void IRAM_ATTR freq_counter_update ( struct freq_counter *f, bool input );
/**
* @brief get new measurement from freq_counter
*
* this function should be called at least twice per cycle in order to avoid
* concurrent rw acces to measurement variable
* @param measurement is set to new measurement value if returned true
* @return true if new measurement is available
*/
bool freq_counter_get_measurement( struct freq_counter *f, uint32_t *measurement);
#endif /* end of include guard: FREQ_COUNTER_H */