-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaButton.cpp
84 lines (77 loc) · 3.11 KB
/
aButton.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Alx2009 button interface
#include <Arduino.h>
#include "aButton.h"
aButtonGroup::aButtonGroup() {
for (int i=0; i <MAX_BUTTONS; i++) {
buttonPin[i] = 0xff;
callBack[i] = NULL;
buttonState[i] = BUTTON_IDLE_STATE;
lastButtonState[i] = BUTTON_IDLE_STATE;
lastDebounceTime[i] = 0L;
startPressed[i] = 0L;
lastReleased[i] = 0L;
}
}
boolean aButtonGroup::add(uint8_t pin, buttonCallBack pinCallBack) {
if (pinCallBack == NULL) {
return false;
}
for (int i=0; i <MAX_BUTTONS; i++) {
if ( callBack[i] == NULL ) { // we found a free slot...
buttonPin[i] = pin;
callBack[i] = pinCallBack;
return true;
}
}
return false;
}
void aButtonGroup::check(uint8_t i) { // Check Index i (note: we assume the caller function checks that i<MAX_BUTTONS, callBack[i] != NULL, etc.
unsigned long now = millis();
int btnow = digitalRead(buttonPin[i]);
if (btnow != lastButtonState[i]) {
// reset the debouncing timer
lastDebounceTime[i] = now;
//Serial.print(F("pin ")); Serial.print(buttonPin[i]); Serial.print(F(" now="));Serial.println(btnow);
}
if ((now - lastDebounceTime[i]) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (btnow != buttonState[i]) {
buttonState[i] = btnow;
// The following actions are taken at Button release
if (btnow == BUTTON_IDLE_STATE) { // button was just released
callBack[i](buttonPin[i], eventRelease);
if ( (now - startPressed[i]) > longPressTime ) {
callBack[i](buttonPin[i], eventLongPress);
} else if (startPressed[i] - lastReleased[i] < doubleClicktime) {
callBack[i](buttonPin[i], eventDoubleClick);
} else {
callBack[i](buttonPin[i], eventClick);
}
lastReleased[i] = now;
} else {//btnow == BUTTON_PRESSED_STATE // button was just pressed
callBack[i](buttonPin[i], eventPress);
startPressed[i] = now;
}
}
}
lastButtonState[i] = btnow;
}
void aButtonGroup::check(void) {
for (int i=0; i <MAX_BUTTONS; i++) {
if ( callBack[i] != NULL ) { // we found a valid slot
check(i);
}
}
}
void aButtonGroup::Serial_printEventName(uint8_t event) {
switch(event) {
case eventClick: Serial.print(F("eventClick")); break;
case eventDoubleClick: Serial.print(F("eventDoubleClick")); break;
case eventLongPress: Serial.print(F("eventLongPress")); break;
case eventPress: Serial.print(F("eventPress")); break;
case eventRelease: Serial.print(F("eventRelease")); break;
default: Serial.print(F("unknown ev. ")); Serial.print(event); break;
}
}