@@ -40,8 +40,10 @@ Button::Button(uint8_t pin, uint8_t invert, uint32_t dbTime)
40
40
_time = millis ();
41
41
_lastState = _state;
42
42
_changed = 0 ;
43
+ _hold_time = -1 ;
43
44
_lastTime = _time;
44
45
_lastChange = _time;
46
+ _pressTime = _time;
45
47
}
46
48
47
49
/* ----------------------------------------------------------------------*
@@ -64,12 +66,13 @@ uint8_t Button::read(void)
64
66
}
65
67
else {
66
68
_lastTime = _time;
69
+ _time = ms;
67
70
_lastState = _state;
68
71
_state = pinVal;
69
- _time = ms;
70
72
if (_state != _lastState) {
71
73
_lastChange = ms;
72
74
_changed = 1 ;
75
+ if (_state) { _pressTime = _time; }
73
76
}
74
77
else {
75
78
_changed = 0 ;
@@ -83,13 +86,11 @@ uint8_t Button::read(void)
83
86
* read, and return false (0) or true (!=0) accordingly. *
84
87
* These functions do not cause the button to be read. *
85
88
*----------------------------------------------------------------------*/
86
- uint8_t Button::isPressed (void )
87
- {
89
+ uint8_t Button::isPressed (void ) {
88
90
return _state == 0 ? 0 : 1 ;
89
91
}
90
92
91
- uint8_t Button::isReleased (void )
92
- {
93
+ uint8_t Button::isReleased (void ) {
93
94
return _state == 0 ? 1 : 0 ;
94
95
}
95
96
@@ -99,35 +100,35 @@ uint8_t Button::isReleased(void)
99
100
* true (!=0) accordingly. *
100
101
* These functions do not cause the button to be read. *
101
102
*----------------------------------------------------------------------*/
102
- uint8_t Button::wasPressed (void )
103
- {
103
+ uint8_t Button::wasPressed (void ) {
104
104
return _state && _changed;
105
105
}
106
106
107
- uint8_t Button::wasReleased (void )
108
- {
109
- return !_state && _changed;
107
+ uint8_t Button::wasReleased (void ) {
108
+ return !_state && _changed && millis () - _pressTime < _hold_time;
109
+ }
110
+
111
+ uint8_t Button::wasReleasefor (uint32_t ms) {
112
+ _hold_time = ms;
113
+ return !_state && _changed && millis () - _pressTime >= ms;
110
114
}
111
115
/* ----------------------------------------------------------------------*
112
116
* pressedFor(ms) and releasedFor(ms) check to see if the button is *
113
117
* pressed (or released), and has been in that state for the specified *
114
118
* time in milliseconds. Returns false (0) or true (1) accordingly. *
115
119
* These functions do not cause the button to be read. *
116
120
*----------------------------------------------------------------------*/
117
- uint8_t Button::pressedFor (uint32_t ms)
118
- {
121
+ uint8_t Button::pressedFor (uint32_t ms) {
119
122
return (_state == 1 && _time - _lastChange >= ms) ? 1 : 0 ;
120
123
}
121
124
122
- uint8_t Button::releasedFor (uint32_t ms)
123
- {
125
+ uint8_t Button::releasedFor (uint32_t ms) {
124
126
return (_state == 0 && _time - _lastChange >= ms) ? 1 : 0 ;
125
127
}
126
128
/* ----------------------------------------------------------------------*
127
129
* lastChange() returns the time the button last changed state, *
128
130
* in milliseconds. *
129
131
*----------------------------------------------------------------------*/
130
- uint32_t Button::lastChange (void )
131
- {
132
+ uint32_t Button::lastChange (void ) {
132
133
return _lastChange;
133
134
}
0 commit comments