Plaquette
 
Loading...
Searching...
No Matches
PqInputs.h
1/*
2 * PqInputs.h
3 *
4 * (c) 2015 Sofian Audry :: info(@)sofianaudry(.)com
5 * (c) 2015 Thomas O Fredericks :: tof(@)t-o-f(.)info
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#ifndef PQ_INPUTS_H_
22#define PQ_INPUTS_H_
23
24#include "PqCore.h"
25#include "MovingAverage.h"
26
27namespace pq {
28
30enum {
31 DEBOUNCE_STABLE,
32 DEBOUNCE_LOCK_OUT,
33 DEBOUNCE_PROMPT_DETECT
34};
35
36// Deprecated.
37#define EXTERNAL_PULLDOWN DIRECT
38#define EXTERNAL_PULLUP INVERTED
39#define ANALOG_DEFAULT DIRECT
40#define ANALOG_INVERTED INVERTED
41
44public:
45 Smoothable();
46
48 virtual void smooth(float smoothTime=PLAQUETTE_DEFAULT_SMOOTH_WINDOW) { timeWindow(smoothTime); }
49
51 virtual void noSmooth() { smooth(PLAQUETTE_NO_SMOOTH_WINDOW); }
52
54 virtual void timeWindow(float seconds) { _avg.timeWindow(seconds); }
55
57 float timeWindow() const { return _avg.timeWindow(); }
58
60 virtual void cutoff(float hz) { _avg.cutoff(hz); }
61
63 float cutoff() const { return _avg.cutoff(); }
64
65protected:
66 // Raw read function.
67 virtual float _read() = 0;
68
69 // Returns current sample rate in seconds.
70 virtual float _sampleRate() const = 0;
71
72 // Resets smoothing.
73 virtual void _begin();
74
75 // Performs update based on value returned by read().
76 virtual void _step();
77
78 // Returns smoothed value.
79 virtual float _smoothed() { return _avg.get(); }
80
81 // The moving average.
82 MovingAverage _avg;
83};
84
87public:
89
91 virtual void debounce(float debounceTime=PLAQUETTE_DEFAULT_DEBOUNCE_WINDOW) { timeWindow(debounceTime); }
92
94 virtual void noDebounce() { debounce(PLAQUETTE_NO_DEBOUNCE_WINDOW); }
95
97 virtual void smooth(float smoothTime=PLAQUETTE_DEFAULT_DEBOUNCE_WINDOW) { debounce(smoothTime); }
98
100 virtual void noSmooth() { noDebounce(); }
101
103 virtual void timeWindow(float seconds) { _interval = seconds; }
104
106 float timeWindow() const { return _interval; }
107
109 uint8_t debounceMode() const { return _debounceMode; }
110
115 void debounceMode(uint8_t mode) { _debounceMode = mode; }
116
117protected:
118 // Raw read function.
119 virtual bool _isOn() = 0;
120
121 // Returns current time in seconds.
122 virtual float _time() const = 0;
123
124 // Resets debouncing.
125 virtual void _begin();
126
127 // Performs update based on value returned by read().
128 virtual void _step();
129
130 // Returns debounced value.
131 virtual bool _debounced();
132
133 inline void _changeState();
134 inline void _setStateFlag(const uint8_t flag) { _state |= flag; }
135 inline void _unsetStateFlag(const uint8_t flag) { _state &= ~flag; }
136 inline void _toggleStateFlag(const uint8_t flag) { _state ^= flag; }
137 inline bool _getStateFlag(const uint8_t flag) { return((_state & flag) != 0); }
138
139 // The moving average.
140 float _interval;
141 float _startTime;
142 uint8_t _state;
143 uint8_t _debounceMode;
144};
145
147class AnalogIn : public Unit, public PinConfig, public Smoothable {
148public:
155 AnalogIn(uint8_t pin, uint8_t mode, Engine& engine = Engine::primary());
156 virtual ~AnalogIn() {}
157
159 virtual float get() { return _smoothed(); }
160
162 virtual float mapTo(float toLow, float toHigh);
163
165 float read() const;
166
168 int rawRead() const;
169
170protected:
171 virtual void begin();
172 virtual void step();
173
174 virtual float _read();
175 virtual float _sampleRate() const { return sampleRate(); }
176};
177
179class DigitalIn : public DigitalSource, public PinConfig, public Debounceable {
180public:
187 DigitalIn(uint8_t pin, uint8_t mode, Engine& engine = Engine::primary());
188 virtual ~DigitalIn() {}
189
191 virtual void mode(uint8_t mode);
192
194 float read() const;
195
197 int rawRead() const;
198
199protected:
200 virtual bool _isOn();
201 virtual float _time() const { return seconds(); }
202
203 virtual void _init();
204
205 virtual void begin();
206 virtual void step();
207};
208
209} // namespace pq
210
211#endif
A generic class representing a simple analog input.
Definition PqInputs.h:147
virtual float mapTo(float toLow, float toHigh)
Maps value to new range.
Definition PqInputs.cpp:148
virtual float get()
Returns value in [0, 1].
Definition PqInputs.h:159
int rawRead() const
Directly reads raw value from the pin (bypasses mode, smoothing, and engine).
Definition PqInputs.cpp:156
float read() const
Direcly reads value from the pin (bypasses mode, smoothing, and engine).
Definition PqInputs.cpp:152
Superclass for components that can be debounced.
Definition PqInputs.h:86
virtual void smooth(float smoothTime=PLAQUETTE_DEFAULT_DEBOUNCE_WINDOW)
Deprecated. Left for backwards compatibility.
Definition PqInputs.h:97
float timeWindow() const
Returns the debouncing window (expressed in seconds).
Definition PqInputs.h:106
void debounceMode(uint8_t mode)
Sets debounce mode.
Definition PqInputs.h:115
virtual void noDebounce()
Remove smoothing.
Definition PqInputs.h:94
virtual void noSmooth()
Remove smoothing.
Definition PqInputs.h:100
virtual void timeWindow(float seconds)
Changes the debouncing window (expressed in seconds).
Definition PqInputs.h:103
virtual void debounce(float debounceTime=PLAQUETTE_DEFAULT_DEBOUNCE_WINDOW)
Apply smoothing to object.
Definition PqInputs.h:91
uint8_t debounceMode() const
Returns the debounce mode.
Definition PqInputs.h:109
A generic class representing a simple digital input.
Definition PqInputs.h:179
float read() const
Directly reads value from the pin as 1 or 0 (bypasses mode, debounce, and engine).
Definition PqInputs.cpp:182
int rawRead() const
Directly reads raw value from the pin as HIGH or LOW (bypasses mode, debounce, and engine).
Definition PqInputs.cpp:186
A digital source that contains a true/false value.
Definition PqCore.h:462
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
float get()
Returns the value of the moving average. This is undefined if isValid() == false.
Definition MovingAverage.h:73
void timeWindow(float seconds)
Changes the smoothing window (expressed in seconds).
Definition MovingAverage.cpp:48
void cutoff(float hz)
Changes the smoothing window cutoff frequency (expressed in Hz).
Definition MovingAverage.cpp:52
Superclass for pin-based components.
Definition PqCore.h:647
uint8_t pin() const
Returns the pin this component is attached to.
Definition PqCore.h:653
uint8_t mode() const
Returns the mode of the component.
Definition PqCore.h:656
Superclass for components that can be smoothed.
Definition PqInputs.h:43
virtual void smooth(float smoothTime=PLAQUETTE_DEFAULT_SMOOTH_WINDOW)
Apply smoothing to object.
Definition PqInputs.h:48
virtual void timeWindow(float seconds)
Changes the smoothing window (expressed in seconds).
Definition PqInputs.h:54
virtual void noSmooth()
Remove smoothing.
Definition PqInputs.h:51
virtual void cutoff(float hz)
Changes the smoothing window cutoff frequency (expressed in Hz).
Definition PqInputs.h:60
float cutoff() const
Returns the smoothing window cutoff frequency (expressed in Hz).
Definition PqInputs.h:63
float timeWindow() const
Returns the smoothing window (expressed in seconds).
Definition PqInputs.h:57
A generic class representing a unit in the system.
Definition PqCore.h:335
float seconds() const
Returns engine time in seconds.
Definition PqCore.h:348
Engine * engine() const
Returns the engine that owns this unit.
Definition PqCore.h:382
float sampleRate() const
Returns engine sample rate.
Definition PqCore.h:360