5#include "helper/light_controller.h"
59 static const char *version;
71 bool activeLogic =
false;
77 Light(String name, uint8_t port,
bool activeLogic =
false, uint8_t channel = 0)
78 : name(name), port(port), activeLogic(activeLogic), channel(channel) {
107 void begin(Scheduler *_pSched,
bool initialState =
false) {
109 tID = pSched->add([
this]() { this->light.
loop(); }, name, 50000L);
111 pSched->subscribe(tID, name +
"/light/#", [
this](String topic, String msg, String orig) {
112 this->light.
commandParser(topic.substring(name.length() + 7), msg);
116#if defined(__ESP32__)
117 pinMode(port, OUTPUT);
119#define LEDC_TIMER_BITS 10
121#define LEDC_BASE_FREQ 5000
122 ledcSetup(channel, LEDC_BASE_FREQ, LEDC_TIMER_BITS);
123 ledcAttachPin(port, channel);
125 pinMode(port, OUTPUT);
134 light.
begin([
this](
bool state,
double level,
bool control,
135 bool notify) { this->onLightControl(state, level, control, notify); },
162 double phase_unit = 0.0, String pattern =
"") {
163 light.
setMode(mode, interval_ms, phase_unit, pattern);
177#ifdef USTD_FEATURE_HOMEASSISTANT
186 void registerHomeAssistant(
HomeAssistant *pHass, String human =
"", String icon =
"",
187 String attribs =
"",
bool dimmable =
true) {
193 void onLightControl(
bool state,
double level,
bool control,
bool notify) {
195 if (state && level == 1.0) {
198 ledcWrite(channel, activeLogic ? pwmrange : 0);
200 digitalWrite(port, activeLogic ? HIGH : LOW);
202 }
else if (state && level > 0.0) {
204 uint16_t bri = (uint16_t)(level * (
double)pwmrange);
207 bri = pwmrange - bri;
208#if defined(__ESP32__)
209 ledcWrite(channel, bri);
211 analogWrite(port, bri);
215 onLightControl(
false, 0.0, control, notify);
220 ledcWrite(channel, activeLogic ? 0 : pwmrange);
222 digitalWrite(port, activeLogic ? LOW : HIGH);
228 pSched->publish(name +
"/light/unitbrightness", String(level, 3));
231 sprintf(buf,
"%5.3f", level);
232 pSched->publish(name +
"/light/unitbrightness", buf);
234 pSched->publish(name +
"/light/state", state ?
"on" :
"off");
239const char *Light::version =
"0.1.0";
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