Browse Source

Add onThreshold/offThreshold arguments to SchmittTrigger.

tags/v2.0.0
Andrew Belt 3 years ago
parent
commit
fb010d66e4
1 changed files with 12 additions and 8 deletions
  1. +12
    -8
      include/dsp/digital.hpp

+ 12
- 8
include/dsp/digital.hpp View File

@@ -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 <typename T = float> template <typename T = float>
struct TSchmittTrigger { struct TSchmittTrigger {
T state; T state;
@@ -32,13 +33,16 @@ struct TSchmittTrigger {
void reset() { void reset() {
state = T::mask(); 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; T triggered = ~state & on;
state = on | (state & ~off); state = on | (state & ~off);
return triggered; return triggered;
} }
T isHigh() {
return state;
}
}; };




@@ -54,20 +58,20 @@ struct TSchmittTrigger<float> {
Returns true if triggered, i.e. the value increases from 0 to 1. Returns true if triggered, i.e. the value increases from 0 to 1.
If different trigger thresholds are needed, use If different trigger thresholds are needed, use


process(rescale(in, low, high, 0.f, 1.f))
process(in, 0.1f, 2.f)


for example. for example.
*/ */
bool process(float in) {
bool process(float in, float offThreshold = 0.f, float onThreshold = 1.f) {
if (state) { if (state) {
// HIGH to LOW // HIGH to LOW
if (in <= 0.f) {
if (in <= offThreshold) {
state = false; state = false;
} }
} }
else { else {
// LOW to HIGH // LOW to HIGH
if (in >= 1.f) {
if (in >= onThreshold) {
state = true; state = true;
return true; return true;
} }


Loading…
Cancel
Save