-
Notifications
You must be signed in to change notification settings - Fork 35
/
Button.h
executable file
·69 lines (55 loc) · 1.7 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* $Id$
||
|| @file Button.cpp
|| @author Alexander Brevig <[email protected]>
|| @url http://alexanderbrevig.com
||
|| @description
|| | This is a Hardware Abstraction Library for Buttons
|| | It providea an easy way of handling buttons
|| #
||
|| @license LICENSE_REPLACE
||
*/
#ifndef Button_h
#define Button_h
#include <inttypes.h>
#define BUTTON_PULLUP HIGH
#define BUTTON_PULLUP_INTERNAL 2
#define BUTTON_PULLDOWN LOW
class Button;
typedef void (*buttonEventHandler)(Button&);
class Button {
public:
Button(uint8_t buttonPin, uint8_t buttonMode=BUTTON_PULLUP_INTERNAL);
void pullup(uint8_t buttonMode);
void pulldown();
bool isPressed();
bool wasPressed();
bool stateChanged();
bool uniquePress();
void setHoldThreshold(unsigned int holdTime);
bool held(unsigned int time=0);
bool heldFor(unsigned int time);
void pressHandler(buttonEventHandler handler);
void releaseHandler(buttonEventHandler handler);
void clickHandler(buttonEventHandler handler);
void holdHandler(buttonEventHandler handler, unsigned int holdTime=0);
unsigned int holdTime() const;
inline unsigned int presses() const { return numberOfPresses; }
bool operator==(Button &rhs);
private:
uint8_t pin;
uint8_t mode;
uint8_t state;
unsigned int pressedStartTime;
unsigned int holdEventThreshold;
buttonEventHandler cb_onPress;
buttonEventHandler cb_onRelease;
buttonEventHandler cb_onClick;
buttonEventHandler cb_onHold;
unsigned int numberOfPresses;
bool triggeredHoldEvent;
};
#endif