-
Notifications
You must be signed in to change notification settings - Fork 0
/
StevesAwesomeButton.cpp
71 lines (59 loc) · 1.57 KB
/
StevesAwesomeButton.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
#include "Arduino.h"
#include "StevesAwesomeButton.h"
StevesAwesomeButton::StevesAwesomeButton(int _buttonPin, int _buttonNum, int _buttonType)
{
buttonPin = _buttonPin;
buttonNum = _buttonNum;
buttonState = 0;
lastButtonState = 0;
buttonType = _buttonType;
if (buttonType == INPUT) {
onState = HIGH;
offState = LOW;
} else {
onState = LOW;
offState = HIGH;
}
pinMode(buttonPin, buttonType);
}
StevesAwesomeButton::StevesAwesomeButton(int _buttonPin, int _buttonNum)
{
buttonPin = _buttonPin;
buttonNum = _buttonNum;
buttonState = 0;
lastButtonState = 0;
// this constructor has no buttonType arg so we assume its an INPUT
buttonType = INPUT;
onState = HIGH;
offState = LOW;
pinMode(buttonPin, buttonType);
}
void StevesAwesomeButton::process()
{
lastButtonState = buttonState;
buttonState = digitalRead(buttonPin);
if (lastButtonState == offState && buttonState == onState) {
if (pressCallback != NULL) pressCallback(buttonNum);
delay(5);
}
else if (lastButtonState == onState && buttonState == onState) {
if (holdCallback != NULL) holdCallback(buttonNum);
delay(5);
}
else if (lastButtonState == onState && buttonState == offState) {
if (releaseCallback != NULL) releaseCallback(buttonNum);
delay(5);
}
}
void StevesAwesomeButton::pressHandler(void (*f)(int)) //button down
{
pressCallback = *f;
}
void StevesAwesomeButton::holdHandler(void (*f)(int)) //button hold
{
holdCallback = *f;
}
void StevesAwesomeButton::releaseHandler(void (*f)(int)) //button release
{
releaseCallback = *f;
}