Skip to content

Commit

Permalink
✨Modify Button behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurFDLR committed Jan 6, 2021
1 parent 1ef3973 commit bbaa778
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
26 changes: 23 additions & 3 deletions lib/M5StickC-Plus/src/utility/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Button::Button(uint8_t pin, uint8_t invert, uint32_t dbTime) {
_lastTime = _time;
_lastChange = _time;
_pressTime = _time;
_exitPressFor = false;
}

/*----------------------------------------------------------------------*
Expand Down Expand Up @@ -70,7 +71,10 @@ uint8_t Button::read(void) {
if (_state != _lastState) {
_lastChange = ms;
_changed = 1;
if (_state) { _pressTime = _time; }
if (_state) {
_pressTime = _time;
_exitPressFor = false;
}
}
else {
_changed = 0;
Expand Down Expand Up @@ -103,7 +107,13 @@ uint8_t Button::wasPressed(void) {
}

uint8_t Button::wasReleased(void) {
return !_state && _changed && millis() - _pressTime < _hold_time;
bool status = !_state && _changed && millis() - _pressTime < _hold_time;
if (_exitPressFor && status)
{
_exitPressFor = false;
return false;
}
return status;
}

uint8_t Button::wasReleasefor(uint32_t ms) {
Expand All @@ -117,7 +127,17 @@ uint8_t Button::wasReleasefor(uint32_t ms) {
* These functions do not cause the button to be read. *
*----------------------------------------------------------------------*/
uint8_t Button::pressedFor(uint32_t ms) {
return (_state == 1 && _time - _lastChange >= ms) ? 1 : 0;
if (_exitPressFor)
{
return false;
}

bool status = ((_state == 1 && _time - _lastChange >= ms) ? 1 : 0);
if (status)
{
_exitPressFor = true;
}
return status;
}

uint8_t Button::releasedFor(uint32_t ms) {
Expand Down
1 change: 1 addition & 0 deletions lib/M5StickC-Plus/src/utility/Button.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ class Button {
uint32_t _dbTime; //debounce time
uint32_t _pressTime; //press time
uint32_t _hold_time; //hold time call wasreleasefor
bool _exitPressFor; //state if we just record long press. Allow to skip associated release
};
#endif

0 comments on commit bbaa778

Please sign in to comment.