-
Notifications
You must be signed in to change notification settings - Fork 5
/
MomentaryButton.h
43 lines (35 loc) · 1.03 KB
/
MomentaryButton.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
#pragma once
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
// Any button press >= HOLD_THRESHOLD milliseconds is considered a hold,
// not a click.
#define HOLD_THRESHOLD 2000
/**
* Encapsulate tracking pushes on a normally-open momentary button.
*
* After a call to check(), the button may report that it:
* - wasClicked(): tapped briefly
* - wasHeld(): pressed for a longer period of time (and perhaps still closed)
* The next call to check() will not report on that previous event.
*/
class MomentaryButton {
private:
// Was this button pushed (closed) when last checked?
boolean wasClosed;
// Only report a hold once (even though the condition
// is satisfied continuously).
boolean holdReported;
// When was this button pushed (closed)?
unsigned long closeTimeMillis;
const int pin;
boolean clicked, held; // At last check, was this button clicked (held)?
public:
MomentaryButton(int inputPin);
void setup();
void check();
boolean wasClicked();
boolean wasHeld();
};