forked from plex3r/EggDuino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.h
59 lines (43 loc) · 794 Bytes
/
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
53
54
55
56
57
58
/*
* button.h
*
* Created: 04.05.2015 21:38:42
* Author: Yogi
*/
#ifndef __BUTTON_H__
#define __BUTTON_H__
#include <Arduino.h>
#define debounceDelay 50
typedef void (*ActionCb)(void);
class Button
{
private:
long debounce;
byte state:1;
byte lastState:1;
byte pin;
ActionCb action;
Button( const Button &c );
Button& operator=( const Button &c );
public:
Button(byte p, ActionCb a): debounce(0), state(1), lastState(1), action(a), pin(p) {
pinMode(pin, INPUT_PULLUP);
}
void check() {
byte b = digitalRead(pin);
long t = millis();
if (b != lastState) {
debounce = t;
}
if ((t - debounce) > debounceDelay) {
if (b != state) {
state = b;
if (!state) {
(*action)();
}
}
}
lastState = b;
}
}; //button
#endif //__BUTTON_H__