forked from SofaPirate/Plaquette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPqInputs.h
87 lines (70 loc) · 2.19 KB
/
PqInputs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* PqInputs.h
*
* (c) 2015 Sofian Audry :: info(@)sofianaudry(.)com
* (c) 2015 Thomas O Fredericks :: tof(@)t-o-f(.)info
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PQ_INPUTS_H_
#define PQ_INPUTS_H_
#include "PqCore.h"
#include "MovingAverage.h"
// Output constants.
#define INTERNAL_PULLUP 0x0
#define EXTERNAL_PULLUP 0x1
#define EXTERNAL_PULLDOWN 0x2
#define ANALOG_DEFAULT 0x4
#define ANALOG_INVERTED 0x8
/// Superclass for components that can be smoothed.
class PqSmoothable {
public:
virtual void smooth(float factor=0.1f);
virtual void noSmooth() { smooth(1); }
protected:
// Raw read function.
virtual float _read() = 0;
// Returns smoothed value.
virtual float _smoothed();
MovingAverage _avg;
};
/// A generic class representing a simple analog input.
class AnalogIn : public PqPinComponent, public PqSmoothable, public PqGetter {
public:
/// Constructor.
AnalogIn(uint8_t pin=A0, uint8_t mode=ANALOG_DEFAULT);
virtual ~AnalogIn() {}
/// Returns reading in [0, 1].
virtual float get() { return _value; }
protected:
virtual float _read();
virtual void setup();
virtual void update();
float _value;
};
/// A generic class representing a simple digital input.
class DigitalIn : public PqPinComponent, public PqSmoothable, public PqDigitalGetter {
public:
/// Constructor.
DigitalIn(uint8_t pin, uint8_t mode=INTERNAL_PULLUP);
virtual ~DigitalIn() {}
/// Returns true iff the input is "on".
virtual bool isOn() { return _isOn; }
protected:
virtual float _read();
virtual void setup();
virtual void update();
bool _isOn;
};
#endif