5#include "ustd_platform.h"
6#include "mupplet_core.h"
41#if defined(__ESP__) || defined(__UNIXOID__)
42 typedef std::function<void(
bool state,
double level,
bool control,
bool notify)>
T_CONTROL;
43#elif defined(__ATTINY__)
44 typedef void (*
T_CONTROL)(
bool state,
double level,
bool control,
bool notify);
46 typedef ustd::function<void(
bool state,
double level,
bool control,
bool notify)>
T_CONTROL;
56 unsigned long interval = 1000;
58 double minWaveBrightness = 0.0;
59 double maxWaveBrightness = 1.0;
62 unsigned long uPhase = 0;
63 unsigned long oPeriod = 0;
64 unsigned long startPulse = 0;
65 unsigned int patternPointer = 0;
82 this->controller = controller;
84 state = !initialState;
93 if (mode == Mode::Passive)
95 unsigned long period = (millis() + uPhase) % (2 * interval);
97 if (millis() - startPulse < interval) {
105 if (period < oPeriod) {
108 if (period > interval && oPeriod < interval) {
114 unsigned long period = (millis() + uPhase) % (2 * interval);
116 if (period < interval) {
117 br = (double)period / (
double)interval;
119 br = (double)(2 * interval - period) / (double)interval;
121 br = br * (maxWaveBrightness - minWaveBrightness) + minWaveBrightness;
125 if (period < oPeriod) {
126 if (patternPointer < pattern.length()) {
127 char c = pattern[patternPointer];
130 c = pattern[patternPointer];
138 if (c >=
'0' && c <=
'9') {
139 double br = (double)(c -
'0') * 0.1111;
196 if (command ==
"set") {
202 if (command ==
"mode/set") {
204 memset(msgbuf, 0, 128);
205 strncpy(msgbuf, args.c_str(), 127);
206 char *p = strchr(msgbuf,
' ');
216 p3 = strchr(p2,
',');
225 if (!strcmp(msgbuf,
"passive")) {
227 }
else if (!strcmp(msgbuf,
"pulse")) {
231 }
else if (!strcmp(msgbuf,
"blink")) {
237 }
else if (!strcmp(msgbuf,
"wave")) {
243 }
else if (!strcmp(msgbuf,
"pattern")) {
244 if (p && strlen(p) > 0) {
254 if (command ==
"unitbrightness/get") {
255 controller(state, brightlevel,
false,
true);
282 void setMode(
Mode mode,
unsigned int interval_ms = 1000,
double phase_unit = 0.0,
283 String pattern =
"") {
300 if (mode == Mode::Passive)
307 interval = interval_ms;
310 if (interval > 100000)
312 startPulse = millis();
313 uPhase = (
unsigned long)(2.0 * (
double)interval * phase);
314 oPeriod = (millis() + uPhase) % interval;
316 this->pattern = pattern;
327 if (minBrightness < 0.0 || minBrightness > 1.0)
329 if (maxBrightness < 0.0 || maxBrightness > 1.0)
331 if (minBrightness >= maxBrightness) {
335 minWaveBrightness = minBrightness;
336 maxWaveBrightness = maxBrightness;
368 this->brightlevel = brightlevel < 0.0 ? 0.0 : brightlevel > 1.0 ? 1.0
373 void set(
bool _state,
bool _automatic) {
377 mode = Mode::Passive;
379 brightlevel = _state ? 1.0 : 0.0;
381 controller(state, brightlevel,
true, !_automatic);
384 void brightness(
double _brightlevel,
bool _automatic) {
385 if (_brightlevel < 0.0)
387 if (_brightlevel > 1.0)
389 if (brightlevel == _brightlevel)
392 mode = Mode::Passive;
393 brightlevel = _brightlevel;
394 state = _brightlevel > 0.0;
395 controller(state, brightlevel,
true, !_automatic);
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
void brightness(double level)
Definition: light_controller.h:275
std::function< void(bool state, double level, bool control, bool notify)> T_CONTROL
Hardware Control Function.
Definition: light_controller.h:42
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
@ Pattern
Definition: light_controller.h:26
@ Blink
Definition: light_controller.h:22
@ Pulse
Definition: light_controller.h:25
@ Wave
Definition: light_controller.h:24
void setMode(Mode mode, unsigned int interval_ms=1000, double phase_unit=0.0, String pattern="")
Definition: light_controller.h:282
LightController()
Definition: light_controller.h:68
The muwerk namespace.
Definition: home_assistant.h:10
double parseUnitLevel(String arg)
Definition: mupplet_core.h:154