-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputs.cpp
66 lines (53 loc) · 1.36 KB
/
inputs.cpp
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
#include "inputs.h"
// ------------- base class for all inputs -----------
InputBase::InputBase() :
DListNode<InputBase*>(this)
{
}
InputBase::~InputBase()
{
}
// -------------- A simple digital button class, reads from a single pin --------------
DigitalInputButton::DigitalInputButton(uint8_t buttonPin, const char *name /*= 0*/, uint8_t debounceTime /*= 70*/) :
InputBase(),
_state(false),
_startedAt(0),
_pinNr(buttonPin),
_debounceTime(debounceTime),
_name(name)
{
pinMode(_pinNr, INPUT_PULLUP);
}
bool DigitalInputButton::isPressed(){
//Serial.print("checkpressed:");
int btnState = digitalRead(_pinNr);
//Serial.print(btnState); Serial.print(" pin:");Serial.println(_pinNr);
if (btnState != _state) {
if (_startedAt == 0){
_startedAt = millis(); // start new debounce
} else if (millis() - _debounceTime > _startedAt){
_state = btnState;
_startedAt = 0;
EventInfo evt;
evt.cstr = _name;
if (_state == true){
onPressed();
pressedEvent.send(&evt);
} else {
onReleased();
releasedEvent.send(&evt);
}
}
} else if (_startedAt > 0) {
_startedAt = 0;
}
return _state;
}
void DigitalInputButton::eventLoop() {
isPressed();
}
// subclasses may override
void DigitalInputButton::onPressed(){
}
void DigitalInputButton::onReleased() {
}