diff --git a/src/counter.c b/src/counter.c index 88269a4..e82d34e 100644 --- a/src/counter.c +++ b/src/counter.c @@ -22,12 +22,11 @@ const int INTR_IN = 19; const int SPI_CHAN = 0; const int SPI_SPEED = 1000000; +const uint32_t PRECISION = 1000; +const uint32_t COUNTER_FREQUENCY = 1e6; + config_t cfg; -const char EPSILON_KEY[] = "epsilon"; -const double DEFAULT_EPSILON = 0.01; - -uint32_t skipped = 0; void isr() { @@ -81,22 +80,33 @@ int main (void) { struct timespec timestamp; uint32_t last_seconds = 0; uint32_t last_milliseconds = 0; - + uint32_t mainsCntSum = 0; + uint32_t mainsCntCnt = 0; while (1) { uint32_t period = ringbufferGet(); + clock_gettime(CLOCK_REALTIME, ×tamp); uint32_t current_seconds = timestamp.tv_sec; uint32_t current_milliseconds = timestamp.tv_nsec / 1e6; - - // add averaging and forming the struct here uint32_t duration = ((current_seconds - last_seconds) * 1000) + (current_milliseconds - last_milliseconds); - logmsg(LOG_DEBUG, "s: %lu, ns: %lu, d: %lu, p: %lu", current_seconds, current_milliseconds, duration, period); + // logmsg(LOG_DEBUG, "s: %lu, ns: %lu, d: %lu, p: %lu", current_seconds, current_milliseconds, duration, period); + + mainsCntSum += period; + mainsCntCnt += 1; if (duration >= 1000) { logmsg(LOG_DEBUG, "one second is over"); last_seconds = current_seconds; last_milliseconds = current_milliseconds; + + uint32_t cnt = mainsCntSum / mainsCntCnt; + mainsCntSum = 0; + mainsCntCnt = 0; + uint32_t freq = PRECISION * COUNTER_FREQUENCY / cnt; + + logmsg(LOG_DEBUG, "s: %lu, f: %lu", current_seconds, freq); + }