-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.h
52 lines (42 loc) · 1.08 KB
/
button.h
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
/*
* BUTTON
*
* Manages buttons being pressed
*
*/
class Button {
private:
byte Pin;
bool pullup;
bool state;
public:
bool changed_state = false;
uint32_t pressedTime = 0;
uint32_t releasedTime = 0;
Button(byte InitPin, bool InitPullup = true) {
Pin = InitPin; // Setting the pin at init
pullup = InitPullup;
pinMode(Pin, pullup ? INPUT_PULLUP : INPUT); // Setting the correct pin mode
state = pullup ? !digitalRead(Pin) : digitalRead(Pin); // Register state of the switch on boot; ASSUMING NOT PRESSED!
}
bool update(uint32_t momentTime = millis()) {
bool newstate = pullup ? !digitalRead(Pin) : digitalRead(Pin); // Handle pullup here
if ( state != newstate ) {
state = newstate;
changed_state = true;
if ( state ) {
pressedTime = momentTime;
} else {
releasedTime = momentTime;
}
return true;
}
return false;
}
bool read() {
changed_state = false;
return state;
}
};
Button FLASH(FLASH_PIN);
Button PRINT(PRINT_PIN);