Plaquette
 
Loading...
Searching...
No Matches
PeakDetector.h
1/*
2 * PeakDetector.h
3 *
4 * (c) 2022 Sofian Audry :: info(@)sofianaudry(.)com
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef PEAK_DETECTOR_H_
21#define PEAK_DETECTOR_H_
22
23#include "PqCore.h"
24
26enum {
27 PEAK_RISING,
28 PEAK_FALLING,
29 PEAK_MAX,
30 PEAK_MIN
31};
32
33namespace pq {
34
38class PeakDetector : public DigitalUnit {
39public:
46
58 virtual ~PeakDetector() {}
59
62
64 float triggerThreshold() const { return _triggerThreshold; }
65
71
73 float reloadThreshold() const { return _reloadThreshold; }
74
80
82 float fallbackTolerance() const { return _fallbackTolerance; }
83
85 bool modeInverted() const;
86
88 bool modeCrossing() const;
89
91 bool modeApex() const;
92
94 void mode(uint8_t mode);
95
97 uint8_t mode() const { return _mode; }
98
104 virtual float put(float value);
105
107 virtual bool isOn() { return _onValue; }
108
110 virtual void onBang(EventCallback callback);
111
112protected:
113 // Resets peak detection flags.
114 void _reset();
115
116 // Returns true if event is triggered.
117 virtual bool eventTriggered(EventType eventType);
118
119 // Threshold values.
120 float _triggerThreshold;
121 float _reloadThreshold;
122 float _fallbackTolerance;
123 float _peakValue;
124
125 // Thresholding mode.
126 bool _onValue : 1;
127 uint8_t _mode : 2;
128
129 // Booleans used to keep track of signal value.
130 bool _isHigh : 1;
131 bool _wasLow : 1;
132 bool _crossed : 1;
133 bool _firstRun : 1;
134
135 // Unused extra space.
136 uint8_t _data : 1;
137};
138
139}
140
141#endif
A generic class representing a simple digital (true/false)unit.
Definition PqCore.h:386
The main Plaquette static class containing all the units.
Definition PqCore.h:60
static Engine & primary()
Returns the main instance of Plaquette.
Definition PqCore.cpp:31
Emits a "bang" signal when another signal peaks.
Definition PeakDetector.h:38
float fallbackTolerance() const
Returns minimal relative "drop" after peak to trigger detection in peak modes.
Definition PeakDetector.h:82
virtual float put(float value)
Pushes value into the unit.
Definition PeakDetector.cpp:99
bool modeCrossing() const
Returns true if mode is PEAK_RISING or PEAK_FALLING.
Definition PeakDetector.cpp:91
float reloadThreshold() const
Returns minimal value "drop" for reset.
Definition PeakDetector.h:73
virtual bool eventTriggered(EventType eventType)
Returns true iff an event of a certain type has been triggered.
Definition PeakDetector.cpp:165
virtual void onBang(EventCallback callback)
Registers event callback on peak detection.
Definition PeakDetector.cpp:161
float triggerThreshold() const
Returns triggerThreshold.
Definition PeakDetector.h:64
bool modeApex() const
Returns true if mode is PEAK_MAX or PEAK_MIN.
Definition PeakDetector.cpp:95
uint8_t mode() const
Returns mode.
Definition PeakDetector.h:97
virtual bool isOn()
Returns true iff the triggerThreshold is crossed.
Definition PeakDetector.h:107
bool modeInverted() const
Returns true if mode is PEAK_FALLING or PEAK_MIN.
Definition PeakDetector.cpp:87
Engine * engine() const
Returns the engine that owns this unit.
Definition PqCore.h:382