muwerk mupplet Core Library
muwerk applets; mupplets: functional units that support specific hardware or reusable applications
Loading...
Searching...
No Matches
mup_digital_out.h
1// mup_digital_out.h - muwerk digital out applet
2#pragma once
3
4#include "scheduler.h"
5#include "mupplet_core.h"
6
7namespace ustd {
8
9// clang-format off
48// clang-format on
50 public:
51#if USTD_FEATURE_MEMORY > USTD_FEATURE_MEM_512B
52 static const char *version; // = "0.2.0";
53#endif
54
55 private:
56 Scheduler *pSched;
57 int tID;
58 String name;
59 uint8_t port;
60 bool activeLogic = false;
61 const char *topic;
62 bool state;
63
64 public:
65 DigitalOut(String name, uint8_t port, bool activeLogic = false, const char *topic = "relay")
66 : name(name), port(port), activeLogic(activeLogic), topic(topic) {
75 }
76
77 ~DigitalOut() {
78 }
79
80 void begin(Scheduler *_pSched) {
85 pSched = _pSched;
86 pinMode(port, OUTPUT);
87
88 setOff();
89#if USTD_FEATURE_MEMORY > USTD_FEATURE_MEM_512B
90 auto ft = [=]() { this->loop(); };
91 tID = pSched->add(ft, name, 50000);
92 auto fnall = [=](String topic, String msg, String originator) {
93 this->subsMsg(topic, msg, originator);
94 };
95 pSched->subscribe(tID, name + "/" + topic + "/#", fnall);
96 pSched->subscribe(tID, "mqtt/state");
97 publishState();
98#endif
99 }
100
101 void set(bool state) {
106 if (this->state == state)
107 return;
108 if (state)
109 setOn();
110 else
111 setOff();
112#if USTD_FEATURE_MEMORY > USTD_FEATURE_MEM_512B
113 publishState();
114#endif
115 }
116
117 private:
118 void setOn() {
119 state = true;
120 if (activeLogic) {
121 digitalWrite(port, true);
122 } else {
123 digitalWrite(port, false);
124 }
125 }
126 void setOff() {
127 state = false;
128 if (activeLogic) {
129 digitalWrite(port, false);
130 } else {
131 digitalWrite(port, true);
132 }
133 }
134
135#if USTD_FEATURE_MEMORY > USTD_FEATURE_MEM_512B
136 void publishState() {
137 if (state) {
138 pSched->publish(name + "/" + topic + "/state", "on");
139 } else {
140 pSched->publish(name + "/" + topic + "/state", "off");
141 }
142 }
143#endif
144
145#if USTD_FEATURE_MEMORY > USTD_FEATURE_MEM_512B
146 void loop() {
147 }
148#endif
149
150#if USTD_FEATURE_MEMORY > USTD_FEATURE_MEM_512B
151 void subsMsg(String topic, String msg, String originator) {
152 char msgbuf[128];
153 memset(msgbuf, 0, 128);
154 strncpy(msgbuf, msg.c_str(), 127);
155 msg.toLowerCase();
156 if (topic == name + "/" + topic + "/set") {
157 set((msg == "on" || msg == "1"));
158 } else if (topic == "mqtt/state") {
159 publishState();
160 }
161 }
162#endif
163}; // DigitalOut
164
165// version
166#if USTD_FEATURE_MEMORY > USTD_FEATURE_MEM_512B
167const char *DigitalOut::version = "0.2.0";
168#endif
169
170} // namespace ustd
mupplet-core DigitalOut class
Definition: mup_digital_out.h:49
DigitalOut(String name, uint8_t port, bool activeLogic=false, const char *topic="relay")
Definition: mup_digital_out.h:65
void set(bool state)
Definition: mup_digital_out.h:101
void begin(Scheduler *_pSched)
Definition: mup_digital_out.h:80
The muwerk namespace.
Definition: home_assistant.h:10