-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDigitalInputEvent.cpp
63 lines (39 loc) · 1.06 KB
/
DigitalInputEvent.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
/*
* DigitalInputEvent.cpp
*
* Created on: 24 áîøñ 2013
* Author: Miro
*/
#include "DigitalInputEvent.h"
#include "Log.h"
#include "Runtime.h"
#include <Arduino.h>
const uint8_t EVENT_TYPE_DIGITAL_INPUT = 30;
DigitalInputEvent::DigitalInputEvent(): _lastValue(LOW) {
}
DigitalInputEvent::~DigitalInputEvent() {
// TODO Auto-generated destructor stub
}
void DigitalInputEvent::init(uint8_t pin) {
_pin = pin;
pinMode(pin, INPUT);
// Receive runtime events
RUNTIME.Attach(this);
LOG_PRINTLN(DEBUG_INFO, "DigitalInputEvent initialized");
}
void DigitalInputEvent::onEvent(Event* o, uint16_t eventType, uint32_t param1, uint32_t param2) {
uint8_t val = (uint8_t) digitalRead(_pin);
if (val != _lastValue) {
_lastValue = val;
if (!_listeners.isEmpty()) {
// Do callback
for (_listeners.first(); !_listeners.isDone(); _listeners.next()) {
if (val == HIGH) {
((DigitalInputListener*)_listeners.currentItem())->onRising(this);
} else {
((DigitalInputListener*)_listeners.currentItem())->onFalling(this);
}
}
}
}
}