5#include "mupplet_core.h"
6#include "helper/light_controller.h"
7#include <Adafruit_PWMServoDriver.h>
60 static const char *version;
71 Adafruit_PWMServoDriver *pPwm;
90 LightsPCA9685(String name, uint8_t addr = 0x40,
bool activeLogic =
false)
91 : name(name), addr(addr), activeLogic(activeLogic) {
106 void begin(Scheduler *_pSched, TwoWire *_pWire =
nullptr,
bool initialState =
false) {
109 tID = pSched->add([
this]() { this->loop(); }, name, 80000L);
112 pPwm =
new Adafruit_PWMServoDriver(addr, _pWire ==
nullptr ? Wire : *_pWire);
114 pPwm->setPWMFreq(1000);
117 pSched->subscribe(tID, name +
"/light/#", [
this](String topic, String msg, String orig) {
118 topic = topic.substring(name.length() + 7);
119 int iPos = topic.indexOf(
'/');
125 if (index < 0 || index > 15) {
129 this->light[index].
commandParser(topic.substring(iPos + 1), msg);
133 for (uint8_t channel = 0; channel < 16; channel++) {
134 light[channel].
begin(
135 [
this, channel](
bool state,
double level,
bool control,
bool notify) {
136 this->onLightControl(channel, state, level, control, notify);
146 void set(int8_t channel,
bool state) {
148 for (channel = 0; channel < 16; channel++) {
149 light[channel].
set(state);
151 }
else if (channel < 16) {
152 light[channel].
set(state);
173 double phase_unit = 0.0, String pattern =
"") {
175 for (channel = 0; channel < 16; channel++) {
176 light[channel].
setMode(mode, interval_ms, phase_unit, pattern);
178 }
else if (channel < 16) {
179 light[channel].
setMode(mode, interval_ms, phase_unit, pattern);
193 for (channel = 0; channel < 16; channel++) {
196 }
else if (channel < 16) {
201#ifdef USTD_FEATURE_HOMEASSISTANT
209 void registerHomeAssistant(
HomeAssistant *pHass, String human =
"", String icon =
"",
210 String attribs =
"") {
222 void registerHomeAssistant(HomeAssistant *pHass,
int channel, String human =
"",
223 String icon =
"", String attribs =
"") {
224 if (channel >= 0 && channel < 16) {
232 for (
int channel = 0; channel < 16; channel++) {
233 light[channel].
loop();
237 void onLightControl(uint8_t channel,
bool state,
double level,
bool control,
bool notify) {
239 uint16_t intensity = level * 4096;
240 if (intensity == 0 || state ==
false) {
242 gpioSet(channel,
false);
243 }
else if (intensity == 4096) {
245 gpioSet(channel,
true);
248 pwmSet(channel, intensity);
252 pSched->publish(name +
"/light/" + String(channel) +
"/unitbrightness",
254 pSched->publish(name +
"/light/" + String(channel) +
"/state", state ?
"on" :
"off");
258 void gpioSet(uint8_t channel,
bool on) {
259 if (activeLogic && on) {
260 pPwm->setPWM(channel, 4096, 0);
261 }
else if (activeLogic) {
262 pPwm->setPWM(channel, 0, 4096);
264 pPwm->setPWM(channel, 0, 4096);
266 pPwm->setPWM(channel, 4096, 0);
270 void pwmSet(uint8_t channel, uint16_t intensity) {
272 pPwm->setPWM(channel, 0, intensity);
274 pPwm->setPWM(channel, 0, 4096 - intensity);
279const char *LightsPCA9685::version =
"0.1.0";
Definition: home_assistant.h:86
@ LightDim
A simple light that can only be switched on and off.
Definition: home_assistant.h:97
void addMultiLight(String name, int count, String human="", DeviceType type=LightDim, String icon="", String attribs="")
Definition: home_assistant.h:422
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 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 PCA 9685 Light class
Definition: mup_light_pca9685.h:58
void setMinMaxWaveBrightness(int8_t channel, double minBrightness, double maxBrightness)
Definition: mup_light_pca9685.h:191
void begin(Scheduler *_pSched, TwoWire *_pWire=nullptr, bool initialState=false)
Definition: mup_light_pca9685.h:106
LightsPCA9685(String name, uint8_t addr=0x40, bool activeLogic=false)
Definition: mup_light_pca9685.h:90
void setMode(int8_t channel, LightController::Mode mode, unsigned int interval_ms=1000, double phase_unit=0.0, String pattern="")
Definition: mup_light_pca9685.h:172
void set(int8_t channel, bool state)
Definition: mup_light_pca9685.h:146
The muwerk namespace.
Definition: home_assistant.h:10
long parseLong(String arg, long defaultVal)
Definition: mupplet_core.h:101