-
Notifications
You must be signed in to change notification settings - Fork 0
/
digital_out.h
108 lines (93 loc) · 2.61 KB
/
digital_out.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// led.h
#pragma once
#include "scheduler.h"
#include "mup_util.h"
#include "home_assistant.h"
namespace ustd {
class DigitalOut {
public:
String DIGITALOUT_VERSION = "0.1.0";
Scheduler *pSched;
int tID;
String name;
uint8_t port;
bool activeLogic = false;
bool state;
#ifdef __ESP__
HomeAssistant *pHA;
#endif
DigitalOut(String name, uint8_t port, bool activeLogic = false)
: name(name), port(port), activeLogic(activeLogic) {
}
~DigitalOut() {
}
void begin(Scheduler *_pSched) {
pSched = _pSched;
pinMode(port, OUTPUT);
setOff();
auto ft = [=]() { this->loop(); };
tID = pSched->add(ft, name, 50000);
auto fnall = [=](String topic, String msg, String originator) {
this->subsMsg(topic, msg, originator);
};
pSched->subscribe(tID, name + "/switch/#", fnall);
}
#ifdef __ESP__
void registerHomeAssistant(String homeAssistantFriendlyName, String projectName = "",
String homeAssistantDiscoveryPrefix = "homeassistant") {
pHA = new HomeAssistant(name, tID, homeAssistantFriendlyName, projectName,
DIGITALOUT_VERSION, homeAssistantDiscoveryPrefix);
pHA->addSwitch();
pHA->begin(pSched);
}
#endif
void setOn() {
this->state = true;
if (activeLogic) {
digitalWrite(port, true);
} else {
digitalWrite(port, false);
}
}
void setOff() {
this->state = false;
if (!activeLogic) {
digitalWrite(port, true);
} else {
digitalWrite(port, false);
}
}
void set(bool state) {
if (state == this->state)
return;
this->state = state;
if (state) {
setOn();
pSched->publish(name + "/switch/state", "on");
} else {
setOff();
pSched->publish(name + "/switch/state", "off");
}
}
void publishState() {
if (state) {
pSched->publish(name + "/switch/state", "on");
this->state = true;
} else {
pSched->publish(name + "/switch/state", "off");
this->state = false;
}
}
void loop() {
}
void subsMsg(String topic, String msg, String originator) {
char msgbuf[128];
memset(msgbuf, 0, 128);
strncpy(msgbuf, msg.c_str(), 127);
msg.toLowerCase();
if (topic == name + "/switch/set") {
set((msg == "on" || msg == "1"));
}
};
}; // DigitalOut
} // namespace ustd