-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPin.cpp
53 lines (40 loc) · 830 Bytes
/
Pin.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
#include <Arduino.h>
#include "Pin.h"
Pin::Pin(short pin, short debounceDelay, event_cb onChange)
{
this->pin = pin;
this->debounceDelay = debounceDelay;
this->onChange = onChange;
currentState = LOW;
lastState = LOW;
lastReadTime = 0;
pinMode(pin, INPUT);
}
void Pin::Read()
{
int readState = digitalRead(pin);
if(readState != lastState) {
lastReadTime = millis();
}
if((millis() - lastReadTime) > debounceDelay && readState != currentState) {
currentState = readState;
HandleChange();
}
lastState = readState;
}
void Pin::HandleChange()
{
Event *event;
event = new Event();
event->id = pin;
event->time = lastReadTime;
if(currentState == HIGH) {
event->type = DOWN;
} else {
event->type = UP;
}
if(onChange) {
onChange(event);
}
delete event;
}