Skip to content

Commit ad6c593

Browse files
authored
Merge pull request #89 from sakabin/dev
Add button hold press event
2 parents 3fb90ef + d8e9b5e commit ad6c593

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

src/M5Stack.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class M5Stack {
114114
void powerOFF();
115115

116116
// Button API
117-
#define DEBOUNCE_MS 5
117+
#define DEBOUNCE_MS 10
118118
Button BtnA = Button(BUTTON_A_PIN, true, DEBOUNCE_MS);
119119
Button BtnB = Button(BUTTON_B_PIN, true, DEBOUNCE_MS);
120120
Button BtnC = Button(BUTTON_C_PIN, true, DEBOUNCE_MS);

src/utility/Button.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ Button::Button(uint8_t pin, uint8_t invert, uint32_t dbTime)
4040
_time = millis();
4141
_lastState = _state;
4242
_changed = 0;
43+
_hold_time = -1;
4344
_lastTime = _time;
4445
_lastChange = _time;
46+
_pressTime = _time;
4547
}
4648

4749
/*----------------------------------------------------------------------*
@@ -64,12 +66,13 @@ uint8_t Button::read(void)
6466
}
6567
else {
6668
_lastTime = _time;
69+
_time = ms;
6770
_lastState = _state;
6871
_state = pinVal;
69-
_time = ms;
7072
if (_state != _lastState) {
7173
_lastChange = ms;
7274
_changed = 1;
75+
if (_state) { _pressTime = _time; }
7376
}
7477
else {
7578
_changed = 0;
@@ -83,13 +86,11 @@ uint8_t Button::read(void)
8386
* read, and return false (0) or true (!=0) accordingly. *
8487
* These functions do not cause the button to be read. *
8588
*----------------------------------------------------------------------*/
86-
uint8_t Button::isPressed(void)
87-
{
89+
uint8_t Button::isPressed(void) {
8890
return _state == 0 ? 0 : 1;
8991
}
9092

91-
uint8_t Button::isReleased(void)
92-
{
93+
uint8_t Button::isReleased(void) {
9394
return _state == 0 ? 1 : 0;
9495
}
9596

@@ -99,35 +100,35 @@ uint8_t Button::isReleased(void)
99100
* true (!=0) accordingly. *
100101
* These functions do not cause the button to be read. *
101102
*----------------------------------------------------------------------*/
102-
uint8_t Button::wasPressed(void)
103-
{
103+
uint8_t Button::wasPressed(void) {
104104
return _state && _changed;
105105
}
106106

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;
110114
}
111115
/*----------------------------------------------------------------------*
112116
* pressedFor(ms) and releasedFor(ms) check to see if the button is *
113117
* pressed (or released), and has been in that state for the specified *
114118
* time in milliseconds. Returns false (0) or true (1) accordingly. *
115119
* These functions do not cause the button to be read. *
116120
*----------------------------------------------------------------------*/
117-
uint8_t Button::pressedFor(uint32_t ms)
118-
{
121+
uint8_t Button::pressedFor(uint32_t ms) {
119122
return (_state == 1 && _time - _lastChange >= ms) ? 1 : 0;
120123
}
121124

122-
uint8_t Button::releasedFor(uint32_t ms)
123-
{
125+
uint8_t Button::releasedFor(uint32_t ms) {
124126
return (_state == 0 && _time - _lastChange >= ms) ? 1 : 0;
125127
}
126128
/*----------------------------------------------------------------------*
127129
* lastChange() returns the time the button last changed state, *
128130
* in milliseconds. *
129131
*----------------------------------------------------------------------*/
130-
uint32_t Button::lastChange(void)
131-
{
132+
uint32_t Button::lastChange(void) {
132133
return _lastChange;
133134
}

src/utility/Button.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Button
2626
uint8_t wasReleased();
2727
uint8_t pressedFor(uint32_t ms);
2828
uint8_t releasedFor(uint32_t ms);
29+
uint8_t wasReleasefor(uint32_t ms);
2930
uint32_t lastChange();
3031

3132
private:
@@ -39,5 +40,7 @@ class Button
3940
uint32_t _lastTime; //time of previous state
4041
uint32_t _lastChange; //time of last state change
4142
uint32_t _dbTime; //debounce time
43+
uint32_t _pressTime; //press time
44+
uint32_t _hold_time; //hold time call wasreleasefor
4245
};
4346
#endif

0 commit comments

Comments
 (0)