From e8fc76ca8b7fd571b086db3df44014d19ffde0f5 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Mon, 15 Apr 2019 00:19:57 -0400 Subject: [PATCH] Change dsp::Counter to dsp::ClockDivider --- include/dsp/digital.hpp | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/include/dsp/digital.hpp b/include/dsp/digital.hpp index ecc6083b..b8cce2fd 100644 --- a/include/dsp/digital.hpp +++ b/include/dsp/digital.hpp @@ -130,36 +130,35 @@ struct Timer { }; -/** Counts the number of `process()` calls. -If `period > 0`, `count` is reset to 0 when that number is reached. -Useful for clock dividing and waiting to fill a fixed buffer. -*/ -struct Counter { - int count; - int period = 0; - - Counter() { +struct ClockDivider { + int clock; + int division = 1; + + ClockDivider() { reset(); } void reset() { - count = 0; + clock = 0; } - void setPeriod(int period) { - this->period = period; - reset(); + void setDivision(int division) { + this->division = division; + } + + int getDivision() { + return division; } - int getCount() { - return count; + int getClock() { + return clock; } - /** Returns true when the counter reaches `period` and resets. */ + /** Returns true when the clock reaches `division` and resets. */ bool process() { - count++; - if (count == period) { - count = 0; + clock++; + if (clock >= division) { + clock = 0; return true; } return false;