-
Notifications
You must be signed in to change notification settings - Fork 0
/
ButtonHandler.cpp
53 lines (43 loc) · 1.43 KB
/
ButtonHandler.cpp
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
/*
ButtonHandler.cpp - ButtonHandler library for Arduino.
Copyright (C) 2017 Tom Rosenback ([email protected]). All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
See file LICENSE.txt for further informations on licensing terms.
*/
#include "ButtonHandler.h"
ButtonHandler::ButtonHandler(bool invertState, unsigned int holdTime) {
_dirty = false;
_lastState = false;
_lastStateTime = 0;
_invertState = invertState;
_holdTime = holdTime;
};
int ButtonHandler::handle(int currentState, unsigned long currentTime) {
if(_invertState) {
currentState = !currentState;
}
int currEvent = BH_EVENT_NONE;
unsigned long interval = currentTime - _lastStateTime;
if(currentState != _lastState && interval > BH_DEBOUNCE_TIME || currentState == _lastState) {
if(currentState && !_lastState) { // LOW => HIGH
// nothing to do
}
else if(currentState && _lastState && interval > _holdTime && !_dirty) { // HIGH => HIGH
currEvent = BH_EVENT_HOLD;
_dirty = true;
} else if(!currentState && _lastState) { // HIGH => LOW
if(!_dirty) {
currEvent = BH_EVENT_CLICK;
}
_dirty = false;
}
if(currentState != _lastState) {
_lastState = currentState;
_lastStateTime = currentTime;
}
}
return currEvent;
}