From 1fd1f9e2ec8064232dcc89e8c00a1ccf6ab4e40a Mon Sep 17 00:00:00 2001 From: Ahmet Inan Date: Tue, 12 Feb 2019 09:40:26 +0100 Subject: [PATCH] added Schmitt trigger and edge triggers --- README.md | 8 +++++++ trigger.hh | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 trigger.hh diff --git a/README.md b/README.md index cff171b..c046fe3 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,14 @@ The following [infinite impulse response](https://en.wikipedia.org/wiki/Infinite [atan](https://en.wikipedia.org/wiki/Inverse_trigonometric_functions) and [atan2](https://en.wikipedia.org/wiki/Atan2). +### [trigger.hh](trigger.hh) + +Implemented are the following [trigger functions](https://en.wikipedia.org/wiki/Flip-flop_(electronics)): + +* [Schmitt trigger](https://en.wikipedia.org/wiki/Schmitt_trigger) +* [Rising edge trigger](https://en.wikipedia.org/wiki/Signal_edge) +* [Falling edge trigger](https://en.wikipedia.org/wiki/Signal_edge) + ### [const.hh](const.hh) Some constants we need diff --git a/trigger.hh b/trigger.hh new file mode 100644 index 0000000..4ea6f2c --- /dev/null +++ b/trigger.hh @@ -0,0 +1,64 @@ +/* +Some trigger functions + +Copyright 2019 Ahmet Inan +*/ + +#pragma once + +namespace DSP { + +template +class SchmittTrigger +{ + TYPE treshold; + bool previous; +public: + constexpr SchmittTrigger(TYPE treshold, bool previous = false) : treshold(treshold), previous(previous) + { + } + bool operator() (TYPE input) + { + if (previous) { + if (input < -treshold) + previous = false; + } else { + if (input > treshold) + previous = true; + } + return previous; + } +}; + +class FallingEdgeTrigger +{ + bool previous; +public: + constexpr FallingEdgeTrigger(bool previous = false) : previous(previous) + { + } + bool operator() (bool input) + { + bool tmp = previous; + previous = input; + return tmp && !input; + } +}; + +class RisingEdgeTrigger +{ + bool previous; +public: + constexpr RisingEdgeTrigger(bool previous = false) : previous(previous) + { + } + bool operator() (bool input) + { + bool tmp = previous; + previous = input; + return !tmp && input; + } +}; + +} +