-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbutton.cpp
More file actions
65 lines (59 loc) · 2 KB
/
button.cpp
File metadata and controls
65 lines (59 loc) · 2 KB
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
#include "Button.h"
void check_buttons(Button *fbuttons, uint8_t buttonCount)
{
uint8_t i;
uint8_t buttonState;
// check the buttons
for ( i = 0 ; i < buttonCount ; i ++ )
{
buttonState = digitalRead(fbuttons[i].pin);
if ( buttonState != fbuttons[i].lastState )
{
if ( millis() < fbuttons[i].debounceTime )
{
fbuttons[i].debounceTime = millis();
}
if ( buttonState == fbuttons[i].debounceState )
{
if ( millis() - fbuttons[i].debounceTime > BUTTON_DEBOUNCE_DELAY )
{
if ( fbuttons[i].lastState == LOW && buttonState == HIGH )
{
if ( millis() - fbuttons[i].pressTime > BUTTON_LONGPRESS_THRESHOLD )
{
button_onLongClick(&fbuttons[i]);
}
else
{
button_onClick(&fbuttons[i]);
}
}
button_onRelease(&fbuttons[i]);
if ( fbuttons[i].lastState == HIGH && buttonState == LOW )
{
button_onPress(&fbuttons[i]);
fbuttons[i].pressTime = millis();
}
fbuttons[i].lastState = buttonState;
}
}
else
{
fbuttons[i].debounceState = buttonState;
fbuttons[i].debounceTime = millis();
}
}
}
}
void setup_buttons(Button *fbuttons, uint8_t buttonCount)
{
uint8_t i;
for ( i = 0 ; i < buttonCount ; i ++ )
{
Serial.print("setting up ");
Serial.println(fbuttons[i].name);
pinMode(fbuttons[i].pin, INPUT_PULLUP);
fbuttons[i].debounceState = HIGH;
fbuttons[i].lastState = HIGH;
}
}