muwerk mupplet Core Library
muwerk applets; mupplets: functional units that support specific hardware or reusable applications
Loading...
Searching...
No Matches
mup_light.h
1// mup_light.h - muwerk light applet
2
3#pragma once
4
5#include "helper/light_controller.h"
6#include "scheduler.h"
7
8namespace ustd {
9// clang-format off
55// clang-format on
56
57class Light {
58 public:
59 static const char *version; // = "0.1.0";
60
61 private:
62 // muwerk task management
63 Scheduler *pSched;
64 int tID;
65
66 // device configuration
67 String name;
68
69 // hardware configuration
70 uint8_t port;
71 bool activeLogic = false;
72 uint16_t pwmrange;
73 uint8_t channel;
74 LightController light;
75
76 public:
77 Light(String name, uint8_t port, bool activeLogic = false, uint8_t channel = 0)
78 : name(name), port(port), activeLogic(activeLogic), channel(channel) {
95 }
96
107 void begin(Scheduler *_pSched, bool initialState = false) {
108 pSched = _pSched;
109 tID = pSched->add([this]() { this->light.loop(); }, name, 50000L);
110
111 pSched->subscribe(tID, name + "/light/#", [this](String topic, String msg, String orig) {
112 this->light.commandParser(topic.substring(name.length() + 7), msg);
113 });
114
115 // prepare hardware
116#if defined(__ESP32__)
117 pinMode(port, OUTPUT);
118// use first channel of 16 channels (started from zero)
119#define LEDC_TIMER_BITS 10
120// use 5000 Hz as a LEDC base frequency
121#define LEDC_BASE_FREQ 5000
122 ledcSetup(channel, LEDC_BASE_FREQ, LEDC_TIMER_BITS);
123 ledcAttachPin(port, channel);
124#else
125 pinMode(port, OUTPUT);
126#endif
127#ifdef __ESP__
128 pwmrange = 1023;
129#else
130 pwmrange = 255;
131#endif
132
133 // start light controller
134 light.begin([this](bool state, double level, bool control,
135 bool notify) { this->onLightControl(state, level, control, notify); },
136 initialState);
137 }
138
142 void set(bool state) {
143 light.set(state);
144 }
145
161 void setMode(LightController::Mode mode, unsigned int interval_ms = 1000,
162 double phase_unit = 0.0, String pattern = "") {
163 light.setMode(mode, interval_ms, phase_unit, pattern);
164 }
165
173 void setMinMaxWaveBrightness(double minBrightness, double maxBrightness) {
174 light.setMinMaxWaveBrightness(minBrightness, maxBrightness);
175 }
176
177#ifdef USTD_FEATURE_HOMEASSISTANT
186 void registerHomeAssistant(HomeAssistant *pHass, String human = "", String icon = "",
187 String attribs = "", bool dimmable = true) {
188 pHass->addLight(name, human, dimmable ? HomeAssistant::LightDim : HomeAssistant::Light,
189 icon, attribs);
190 }
191#endif
192 private:
193 void onLightControl(bool state, double level, bool control, bool notify) {
194 if (control) {
195 if (state && level == 1.0) {
196 // led is on at maximum brightness
197#ifdef __ESP32__
198 ledcWrite(channel, activeLogic ? pwmrange : 0);
199#else
200 digitalWrite(port, activeLogic ? HIGH : LOW);
201#endif
202 } else if (state && level > 0.0) {
203 // led is dimmed
204 uint16_t bri = (uint16_t)(level * (double)pwmrange);
205 if (bri) {
206 if (!activeLogic)
207 bri = pwmrange - bri;
208#if defined(__ESP32__)
209 ledcWrite(channel, bri);
210#else
211 analogWrite(port, bri);
212#endif
213 } else {
214 light.forceState(false, 0.0);
215 onLightControl(false, 0.0, control, notify);
216 }
217 } else {
218 // led is off
219#ifdef __ESP32__
220 ledcWrite(channel, activeLogic ? 0 : pwmrange);
221#else
222 digitalWrite(port, activeLogic ? LOW : HIGH);
223#endif
224 }
225 }
226 if (notify) {
227#ifdef __ARDUINO__
228 pSched->publish(name + "/light/unitbrightness", String(level, 3));
229#else
230 char buf[32];
231 sprintf(buf, "%5.3f", level);
232 pSched->publish(name + "/light/unitbrightness", buf);
233#endif
234 pSched->publish(name + "/light/state", state ? "on" : "off");
235 }
236 }
237};
238
239const char *Light::version = "0.1.0";
240
241} // namespace ustd
Definition: home_assistant.h:86
void addLight(String name, String human="", DeviceType type=LightDim, String icon="", String attribs="", String effects="")
Definition: home_assistant.h:377
@ LightDim
A simple light that can only be switched on and off.
Definition: home_assistant.h:97
@ Light
A simple device that can only be switched on and off.
Definition: home_assistant.h:96
The Light Controller Class.
Definition: light_controller.h:16
void setMinMaxWaveBrightness(double minBrightness, double maxBrightness)
Definition: light_controller.h:321
void set(bool state)
Definition: light_controller.h:261
bool commandParser(String command, String args)
Definition: light_controller.h:153
void forceState(bool state, double brightlevel)
Definition: light_controller.h:339
void begin(T_CONTROL controller, bool initialState=false)
Definition: light_controller.h:75
void loop()
Definition: light_controller.h:88
Mode
Definition: light_controller.h:19
void setMode(Mode mode, unsigned int interval_ms=1000, double phase_unit=0.0, String pattern="")
Definition: light_controller.h:282
mupplet-core GPIO Light class
Definition: mup_light.h:57
Light(String name, uint8_t port, bool activeLogic=false, uint8_t channel=0)
Definition: mup_light.h:77
void setMinMaxWaveBrightness(double minBrightness, double maxBrightness)
Definition: mup_light.h:173
void set(bool state)
Definition: mup_light.h:142
void begin(Scheduler *_pSched, bool initialState=false)
Definition: mup_light.h:107
void setMode(LightController::Mode mode, unsigned int interval_ms=1000, double phase_unit=0.0, String pattern="")
Definition: mup_light.h:161
The muwerk namespace.
Definition: home_assistant.h:10