From fb010d66e4028a5f2336daa6a61cbce285d7649b Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Sun, 24 Oct 2021 23:38:16 -0400 Subject: [PATCH] Add onThreshold/offThreshold arguments to SchmittTrigger. --- include/dsp/digital.hpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/include/dsp/digital.hpp b/include/dsp/digital.hpp index 1e65e6ec..d03e6b65 100644 --- a/include/dsp/digital.hpp +++ b/include/dsp/digital.hpp @@ -22,7 +22,8 @@ struct BooleanTrigger { }; -/** Turns HIGH when value reaches 1.f, turns LOW when value reaches 0.f. */ +/** Turns HIGH when value reaches a threshold (default 0.f), turns LOW when value reaches a threshold (default 1.f). +*/ template struct TSchmittTrigger { T state; @@ -32,13 +33,16 @@ struct TSchmittTrigger { void reset() { state = T::mask(); } - T process(T in) { - T on = (in >= 1.f); - T off = (in <= 0.f); + T process(T in, T offThreshold = 0.f, T onThreshold = 1.f) { + T on = (in >= onThreshold); + T off = (in <= offThreshold); T triggered = ~state & on; state = on | (state & ~off); return triggered; } + T isHigh() { + return state; + } }; @@ -54,20 +58,20 @@ struct TSchmittTrigger { Returns true if triggered, i.e. the value increases from 0 to 1. If different trigger thresholds are needed, use - process(rescale(in, low, high, 0.f, 1.f)) + process(in, 0.1f, 2.f) for example. */ - bool process(float in) { + bool process(float in, float offThreshold = 0.f, float onThreshold = 1.f) { if (state) { // HIGH to LOW - if (in <= 0.f) { + if (in <= offThreshold) { state = false; } } else { // LOW to HIGH - if (in >= 1.f) { + if (in >= onThreshold) { state = true; return true; }