forked from tigoe/Button
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Button.h
executable file
·73 lines (59 loc) · 2 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
70
71
72
73
/* $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, bool _debounceMode=true, int _debounceDuration=20);
uint8_t pin;
void pullup(uint8_t buttonMode);
void pulldown();
void process();
bool isPressed(bool proc=true);
bool wasPressed(bool proc=true);
bool stateChanged(bool proc=true);
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 mode;
uint8_t state;
bool debounceMode;
unsigned long pressedStartTime;
unsigned int holdEventThreshold;
unsigned long debounceStartTime;
int debounceDuration;
buttonEventHandler cb_onPress;
buttonEventHandler cb_onRelease;
buttonEventHandler cb_onClick;
buttonEventHandler cb_onHold;
unsigned int numberOfPresses;
bool triggeredHoldEvent;
};
#endif