-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToggleButton.h
71 lines (68 loc) · 1.6 KB
/
ToggleButton.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
#ifndef ToggleButton_h
#define ToggleButton_h
class ToggleButton
{
public:
ToggleButton(int pin)
: _pin(pin),
_isChanged(false),
_isDown(false),
_isPressed(false),
_isReleased(false),
_inverted(false),
_delayUpdate(0),
_lastMillis(0),
pause(false)
{ pinMode(_pin,INPUT); };
void update()
{
unsigned long currMillis = millis();
boolean currState = digitalRead(_pin) ^ _inverted;
if(((currState && _isDown) || (!currState && !_isDown)) && !pause)
{
_isChanged = false;
_isPressed = false;
_isReleased = false;
}else if(currState && !_isDown && !pause)
{
_isPressed = true;
_isReleased = false;
_isChanged = true;
}else if(!currState && _isDown && !pause)
{
_isPressed = false;
_isReleased = true;
_isChanged = true;
}
if(_delayUpdate > 0 && _isPressed)
{
if(currMillis-_lastMillis < _delayUpdate)
pause = true;
else
{
_lastMillis = currMillis;
pause = false;
}
}
if(!pause)
_lastMillis = currMillis;
_isDown = currState;
};
boolean isButtonDown(){return _isDown;};
void setButtonPressedTime(unsigned long millis){_delayUpdate = millis;};
void setInvertedButton(){_inverted = true;};
boolean isButtonChanged(){return _isChanged;};
boolean isButtonPressed(){return _isPressed;};
boolean isButtonReleased(){return _isReleased;};
private:
boolean _isDown;
boolean _isChanged;
boolean _isPressed;
boolean _isReleased;
boolean _inverted;
boolean pause;
unsigned long _delayUpdate;
unsigned long _lastMillis;
int _pin;
};
#endif