forked from JChristensen/JC_Button
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.h
60 lines (45 loc) · 1.55 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
#pragma once
#include <Arduino.h>
class BaseButton {
public:
BaseButton(unsigned long dbTime)
: dbTime(dbTime), state(false), changed(false), lastChange(-dbTime) {}
void update() {
changed = false;
if (hasElapsed(dbTime)) {
bool reading = read();
if (state != reading) {
state = reading;
changed = true;
lastChange = millis();
}
}
}
virtual bool read() const = 0;
inline bool isPressed() const {return state;}
inline bool isReleased() const {return !isPressed();}
inline bool wasPressed() const {return isPressed() && changed;}
inline bool wasReleased() const {return isReleased() && changed;}
inline bool pressedFor(unsigned long ms) const {return isPressed() && hasElapsed(ms);}
inline bool releasedFor(unsigned long ms) const {return isReleased() && hasElapsed(ms);}
protected:
const unsigned long dbTime;
bool state;
bool changed;
unsigned long lastChange;
private:
inline bool hasElapsed(unsigned long ms) const {return millis() - lastChange >= ms;}
};
// global scope to match scoping of pinMode's INPUT, OUTPUT and INPUT_PULLUP
enum config_t {INTERNAL_PULLUP, EXTERNAL_PULLUP, EXTERNAL_PULLDOWN};
class Button : public BaseButton {
public:
Button(uint8_t pin, config_t config, unsigned long dbTime)
: BaseButton(dbTime), pin(pin), config(config) {
pinMode(pin, config == INTERNAL_PULLUP ? INPUT_PULLUP : INPUT);
}
inline bool read() const {return digitalRead(pin) ^ (config != EXTERNAL_PULLDOWN);}
private:
const uint8_t pin;
const config_t config;
};