-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLedBlinker.cpp
74 lines (62 loc) · 1.19 KB
/
LedBlinker.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
66
67
68
69
70
71
72
73
74
/*
* LedBlinker.cpp
*
* Created on: Jul 1, 2016
* Author: lieven
*/
#include "LedBlinker.h"
#define PIN 16
LedBlinker::LedBlinker() :
Actor("Led")
{
_interval = 10;
_isOn = true;
}
LedBlinker::~LedBlinker()
{
}
void LedBlinker::setup()
{
init();
timeout(100);
eb.onSrc(_wifi).call(this);
eb.onSrc(_mqtt).call(this);
}
void LedBlinker::init()
{
pinMode(PIN, OUTPUT);
digitalWrite(PIN, 1);
}
void LedBlinker::onEvent(Cbor& cbor)
{
if (timeout()) {
if (_isOn) {
_isOn = false;
digitalWrite(PIN, 1);
} else {
_isOn = true;
digitalWrite(PIN, 0);
}
timeout(_interval);
} else if ( eb.isEvent(_wifi,H("connected"))) {
setInterval(100);
} else if ( eb.isEvent(_wifi,H("disconnected"))) {
setInterval(10);
} else if ( eb.isEvent(_mqtt,H("connected"))) {
setInterval(1000);
} else if ( eb.isEvent(_mqtt,H("disconnected"))) {
setInterval(100);
}
}
void LedBlinker::setInterval(uint32_t interval)
{
_interval=interval;
}
void LedBlinker::setWifi(uid_t wifi)
{
_wifi=wifi;
}
void LedBlinker::setMqtt(uid_t mqtt)
{
_mqtt=mqtt;
}