-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaseInputs.h
90 lines (77 loc) · 2.47 KB
/
BaseInputs.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
88
89
90
#ifndef BASEINPUTS_H_
#define BASEINPUTS_H_
#include "ArduinoIOPins.h"
/**
* Base class for making on/off switched input
*/
class SwitchedInputBase : public DigitalInputPin{
public :
SwitchedInputBase () : DigitalInputPin(){}
SwitchedInputBase(uint8_t pin) : DigitalInputPin(pin){}
virtual ~SwitchedInputBase(){}
/** Check if it's on */
virtual bool isOn() = 0;
/** Check if it's off */
virtual bool isOff() = 0;
};
/**
* An easier way of handling digital inputs.
* Choose the value to consider this input on at @initInput.
*/
class SwitchedInput : public SwitchedInputBase{
private :
bool highOn;
public :
/** Default constructor */
SwitchedInput () : SwitchedInputBase(){}
/** Pin setting included constructor. */
SwitchedInput(uint8_t pin, uint8_t onLevel = true) : SwitchedInputBase(pin){highOn = onLevel;}
virtual ~SwitchedInput(){}
/** Input pin initialization.
* @param pin Input pin
* @param onLevel Digital level for considering the input on,opened,whatever.
* HIGH,true or any number bigger than 0 mean the opposite to LOW,false or 0.
*/
void initInput(uint8_t pin, uint8_t onLevel = true){
highOn = onLevel;
DigitalInputPin::initInput(pin);
}
/** Check if it's on */
virtual bool isOn(){ return ((highOn) ? isHigh():isLow());}
/** Check if it's off */
virtual bool isOff(){ return !this->isOn();}
};
/**
* An easier way of handling digital inputs considered ON when input is LOW .
* Use this instead of SwitchedInput if you want it lighter.
*/
class LowOnSwitchedInput : public SwitchedInputBase {
public :
/** Default constructor */
LowOnSwitchedInput () : SwitchedInputBase(){}
/** Pin setting included constructor. */
LowOnSwitchedInput(uint8_t pin) : SwitchedInputBase(pin){}
virtual ~LowOnSwitchedInput(){}
/** Check if it's on */
virtual bool isOn() {return isLow();}
/** Check if it's off */
virtual bool isOff() {return isHigh();}
};
/**
* HighOnSwitchedInput class
* An easier way of handling digital inputs considered ON when input is HIGH.
* Use this instead of SwitchedInput if you want it lighter.
*/
class HighOnSwitchedInput : public SwitchedInputBase {
public :
/** Default constructor */
HighOnSwitchedInput () : SwitchedInputBase(){}
/** Pin setting included constructor. */
HighOnSwitchedInput(uint8_t pin) : SwitchedInputBase(pin){}
virtual ~HighOnSwitchedInput(){}
/** Check if it's on */
virtual bool isOn() {return isHigh();}
/** Check if it's off */
virtual bool isOff() {return isLow();}
};
#endif /* BASEINPUTS_H_ */