-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.h
110 lines (95 loc) · 2.71 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
||
|| @author Alexander Brevig <abrevig@wiring.org.co>
|| @url http://wiring.org.co/
|| @url http://alexanderbrevig.com/
|| @contribution Brett Hagman <bhagman@wiring.org.co>
|| @contribution https://github.com/cyborgsimon
|| @contribution Chris van Marle (DebounceButton Library)
||
|| @description
|| | Hardware Abstraction Library for Buttons.
|| | It provides an easy way of handling buttons.
|| |
|| | Wiring Cross-platform Library
|| #
||
|| @license Please see cores/Common/License.txt.
||
*/
#ifndef BUTTON_H
#define BUTTON_H
#include <stdint.h>
#define BUTTON_PULLUP HIGH
#define BUTTON_PULLUP_INTERNAL 2
#define BUTTON_PULLDOWN LOW
class Button;
typedef void (*buttonEventHandler)(Button&);
class Button
{
public:
// Constructor
Button(uint8_t buttonPin, uint8_t buttonMode = BUTTON_PULLUP_INTERNAL);
// Public Member
uint8_t ID;
// Methods
void pullup(uint8_t buttonMode);
void pulldown();
bool scan();
bool isPressed() const;
bool stateChanged() const;
bool uniquePress() const;
unsigned int clicked();
bool held(unsigned long time = 0);
bool heldFor(unsigned long time) const;
// Properties
uint8_t getPin(void) const
{
return pin;
}
void setDebounceDelay(unsigned int debounceDelay);
void setHoldThreshold(unsigned int holdTime);
void setHoldRepeat(unsigned int repeatTime);
void setMultiClickThreshold(unsigned int multiClickTime);
void pressHandler(buttonEventHandler handler);
void releaseHandler(buttonEventHandler handler);
void clickHandler(buttonEventHandler handler);
void holdHandler(buttonEventHandler handler, unsigned long holdTime = 0);
unsigned long holdTime() const;
unsigned long heldTime() const;
inline unsigned long presses() const
{
return numberOfPresses;
}
inline unsigned int getClickCount() const
{
return clickCount;
}
inline unsigned int getHoldRepeatCount() const
{
return holdEventRepeatCount;
}
bool operator==(Button &rhs);
private:
uint8_t pin;
uint8_t mode;
uint8_t state;
unsigned long pressedStartTime;
unsigned long debounceStartTime;
unsigned long debounceDelayTime;
unsigned long holdEventThresholdTime;
unsigned long holdEventRepeatTime;
unsigned long holdEventPreviousTime;
unsigned long lastReleaseTime;
unsigned long multiClickThresholdTime;
buttonEventHandler cb_onPress;
buttonEventHandler cb_onRelease;
buttonEventHandler cb_onClick;
buttonEventHandler cb_onHold;
unsigned long numberOfPresses;
unsigned int clickCount;
unsigned int holdEventRepeatCount;
bool triggeredHoldEvent;
};
#endif
// BUTTON_H