Browse Source

Refactor dsp/digital.hpp

tags/v1.0.0
Andrew Belt 5 years ago
parent
commit
d6fd5f62a3
1 changed files with 28 additions and 19 deletions
  1. +28
    -19
      include/dsp/digital.hpp

+ 28
- 19
include/dsp/digital.hpp View File

@@ -9,18 +9,20 @@ namespace dsp {
/** Turns HIGH when value reaches 1.f, turns LOW when value reaches 0.f. */ /** Turns HIGH when value reaches 1.f, turns LOW when value reaches 0.f. */
struct SchmittTrigger { struct SchmittTrigger {
enum State { enum State {
UNKNOWN,
LOW, LOW,
HIGH
HIGH,
UNKNOWN
}; };
State state; State state;


SchmittTrigger() { SchmittTrigger() {
reset(); reset();
} }

void reset() { void reset() {
state = UNKNOWN; state = UNKNOWN;
} }

/** Updates the state of the Schmitt Trigger given a value. /** Updates the state of the Schmitt Trigger given a value.
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
@@ -51,24 +53,28 @@ struct SchmittTrigger {
} }
return false; return false;
} }

bool isHigh() { bool isHigh() {
return state == HIGH; return state == HIGH;
} }
}; };




/** Detects when a boolean changes from false to true */
struct BooleanTrigger { struct BooleanTrigger {
bool lastState;
bool state;


BooleanTrigger() { BooleanTrigger() {
reset(); reset();
} }

void reset() { void reset() {
lastState = true;
state = true;
} }

bool process(bool state) { bool process(bool state) {
bool triggered = (state && !lastState);
lastState = state;
bool triggered = (state && !this->state);
this->state = state;
return triggered; return triggered;
} }
}; };
@@ -76,28 +82,31 @@ struct BooleanTrigger {


/** When triggered, holds a high value for a specified time before going low again */ /** When triggered, holds a high value for a specified time before going low again */
struct PulseGenerator { struct PulseGenerator {
float time;
float triggerDuration;
float remaining;


PulseGenerator() { PulseGenerator() {
reset(); reset();
} }
/** Immediately resets the state to LOW */

/** Immediately disables the pulse */
void reset() { void reset() {
time = 0.f;
triggerDuration = 0.f;
remaining = 0.f;
} }

/** Advances the state by `deltaTime`. Returns whether the pulse is in the HIGH state. */ /** Advances the state by `deltaTime`. Returns whether the pulse is in the HIGH state. */
bool process(float deltaTime) { bool process(float deltaTime) {
time += deltaTime;
return time < triggerDuration;
if (remaining > 0.f) {
remaining -= deltaTime;
return true;
}
return false;
} }
/** Begins a trigger with the given `triggerDuration`. */
void trigger(float triggerDuration) {
// Keep the previous triggerDuration if the existing pulse would be held longer than the currently requested one.
if (time + triggerDuration >= this->triggerDuration) {
time = 0.f;
this->triggerDuration = triggerDuration;
/** Begins a trigger with the given `duration`. */
void trigger(float duration = 1e-3f) {
// Keep the previous pulse if the existing pulse will be held longer than the currently requested one.
if (duration > remaining) {
remaining = duration;
} }
} }
}; };


Loading…
Cancel
Save