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#include "TimeWindowable.h"
27
28namespace pq {
29
31enum {
32 DEBOUNCE_STABLE,
33 DEBOUNCE_LOCK_OUT,
34 DEBOUNCE_PROMPT_DETECT
35};
36
37// Deprecated.
38#define EXTERNAL_PULLDOWN DIRECT
39#define EXTERNAL_PULLUP INVERTED
40#define ANALOG_DEFAULT DIRECT
41#define ANALOG_INVERTED INVERTED
42
44class Smoothable : public TimeWindowable {
45protected:
46 Smoothable();
47
48public:
50 virtual void smooth(float smoothTime=PLAQUETTE_DEFAULT_SMOOTH_WINDOW) { timeWindow(smoothTime); }
51
53 virtual void noSmooth() { noTimeWindow(); }
54
55protected:
56 // Raw read function.
57 virtual float _read() = 0;
58
59 // Returns current sample rate in seconds.
60 virtual float _sampleRate() const = 0;
61
62 // Resets smoothing.
63 virtual void _begin();
64
65 // Performs update based on value returned by read().
66 virtual void _step();
67
68 // Returns smoothed value.
69 virtual float _smoothed() { return _avg.get(); }
70
71 // The moving average.
72 MovingAverage _avg;
73
74 // The number of samples to average.
75 unsigned int _nSamples;
76};
77
80protected:
82
83public:
85 virtual void debounce(float debounceTime=PLAQUETTE_DEFAULT_DEBOUNCE_WINDOW) { timeWindow(debounceTime); }
86
88 virtual void noDebounce() { noTimeWindow(); }
89
92 [[deprecated("Use debounce() instead")]]
93 virtual void smooth(float smoothTime=PLAQUETTE_DEFAULT_DEBOUNCE_WINDOW) { debounce(smoothTime); }
94
97 [[deprecated("Use noDebounce() instead")]]
98 virtual void noSmooth() { noDebounce(); }
99
101 uint8_t debounceMode() const { return _debounceMode; }
102
107 void debounceMode(uint8_t mode) { _debounceMode = mode; }
108
109protected:
110 // Raw read function.
111 virtual bool _isOn() = 0;
112
113 // Returns current time in seconds.
114 virtual float _time() const = 0;
115
116 // Resets debouncing.
117 virtual void _begin();
118
119 // Performs update based on value returned by read().
120 virtual void _step();
121
122 // Returns debounced value.
123 virtual bool _debounced();
124
125 // Flag helper functions.
126 inline void _changeState();
127 inline void _setStateFlag(const uint8_t flag) { _state |= flag; }
128 inline void _unsetStateFlag(const uint8_t flag) { _state &= ~flag; }
129 inline void _toggleStateFlag(const uint8_t flag) { _state ^= flag; }
130 inline bool _getStateFlag(const uint8_t flag) { return((_state & flag) != 0); }
131
132 // The moving average.
133 float _startTime;
134 uint8_t _state;
135 uint8_t _debounceMode;
136};
137
139class AnalogIn : public Unit, public PinConfig, public Smoothable {
140public:
147 AnalogIn(uint8_t pin, uint8_t mode, Engine& engine = Engine::primary());
148 virtual ~AnalogIn() {}
149
151 virtual float get() { return _smoothed(); }
152
154 virtual float mapTo(float toLow, float toHigh);
155
157 float read() const;
158
160 int rawRead() const;
161
162protected:
163 virtual void begin();
164 virtual void step();
165
166 virtual float _read();
167 virtual float _sampleRate() const { return sampleRate(); }
168};
169
171class DigitalIn : public DigitalSource, public PinConfig, public Debounceable {
172public:
179 DigitalIn(uint8_t pin, uint8_t mode, Engine& engine = Engine::primary());
180 virtual ~DigitalIn() {}
181
183 virtual void mode(uint8_t mode);
184
186 float read() const;
187
189 int rawRead() const;
190
191protected:
192 virtual bool _isOn();
193 virtual float _time() const { return seconds(); }
194
195 virtual void _init();
196
197 virtual void begin();
198 virtual void step();
199};
200
201} // namespace pq
202
203#endif
A generic class representing a simple analog input.
Definition PqInputs.h:139
virtual float mapTo(float toLow, float toHigh)
Maps value to new range.
Definition PqInputs.cpp:150
virtual float get()
Returns value in [0, 1].
Definition PqInputs.h:151
int rawRead() const
Directly reads raw value from the pin (bypasses mode, smoothing, and engine).
Definition PqInputs.cpp:158
float read() const
Direcly reads value from the pin (bypasses mode, smoothing, and engine).
Definition PqInputs.cpp:154
Superclass for components that can be debounced.
Definition PqInputs.h:79
virtual void smooth(float smoothTime=PLAQUETTE_DEFAULT_DEBOUNCE_WINDOW)
Deprecated.
Definition PqInputs.h:93
void debounceMode(uint8_t mode)
Sets debounce mode.
Definition PqInputs.h:107
virtual void noDebounce()
Remove smoothing.
Definition PqInputs.h:88
virtual void noSmooth()
Remove smoothing.
Definition PqInputs.h:98
virtual void debounce(float debounceTime=PLAQUETTE_DEFAULT_DEBOUNCE_WINDOW)
Apply smoothing to object.
Definition PqInputs.h:85
uint8_t debounceMode() const
Returns the debounce mode.
Definition PqInputs.h:101
A generic class representing a simple digital input.
Definition PqInputs.h:171
float read() const
Directly reads value from the pin as 1 or 0 (bypasses mode, debounce, and engine).
Definition PqInputs.cpp:184
int rawRead() const
Directly reads raw value from the pin as HIGH or LOW (bypasses mode, debounce, and engine).
Definition PqInputs.cpp:188
A digital source that contains a true/false value.
Definition PqCore.h:500
The main Plaquette static class containing all the units.
Definition PqCore.h:63
static Engine & primary()
Returns the main instance of Plaquette.
Definition PqCore.cpp:30
float get()
Returns the value of the moving average. This is undefined if isValid() == false.
Definition MovingAverage.h:56
Superclass for pin-based components.
Definition PqCore.h:724
uint8_t pin() const
Returns the pin this component is attached to.
Definition PqCore.h:730
uint8_t mode() const
Returns the mode of the component.
Definition PqCore.h:733
Superclass for components that can be smoothed.
Definition PqInputs.h:44
virtual void smooth(float smoothTime=PLAQUETTE_DEFAULT_SMOOTH_WINDOW)
Apply smoothing to object.
Definition PqInputs.h:50
virtual void noSmooth()
Remove smoothing.
Definition PqInputs.h:53
Abstract class for time-based objects.
Definition TimeWindowable.h:28
virtual float timeWindow() const
Returns the time window (expressed in seconds).
Definition TimeWindowable.h:45
virtual void noTimeWindow()
Sets time window to no time window.
Definition TimeWindowable.cpp:36
A generic class representing a unit in the system.
Definition PqCore.h:373
float seconds() const
Returns engine time in seconds.
Definition PqCore.h:386
Engine * engine() const
Returns the engine that owns this unit.
Definition PqCore.h:420
float sampleRate() const
Returns engine sample rate.
Definition PqCore.h:398