Save and restore LED state
This commit is contained in:
parent
e29f4c3896
commit
4c429d9ca5
81
src/LEDs.cpp
81
src/LEDs.cpp
|
@ -21,13 +21,19 @@ int led_1_cnt = DEFAULT_LED1_CNT, led_2_cnt, led_3_cnt;
|
||||||
ControlType led_1_cntrl = DEFAULT_LED1_CNTRL, led_2_cntrl, led_3_cntrl;
|
ControlType led_1_cntrl = DEFAULT_LED1_CNTRL, led_2_cntrl, led_3_cntrl;
|
||||||
std::vector<LED*> leds, statusLeds, countLeds, motionLeds;
|
std::vector<LED*> leds, statusLeds, countLeds, motionLeds;
|
||||||
bool online;
|
bool online;
|
||||||
|
unsigned long lastSave = 0;
|
||||||
|
|
||||||
LED* newLed(uint8_t index, ControlType cntrl, int type, int pin, int cnt) {
|
LED* newLed(uint8_t index, ControlType cntrl, int type, int pin, int cnt, String stateStr) {
|
||||||
if (pin == -1) return new LED(index, Control_Type_None);
|
LED* led;
|
||||||
if (type >= 2)
|
if (pin == -1) {
|
||||||
return new Addressable(index, cntrl, type - 2, pin, cnt);
|
led = new LED(index, Control_Type_None);
|
||||||
else
|
} else if (type >= 2) {
|
||||||
return new SinglePWM(index, cntrl, type == 1, pin);
|
led = new Addressable(index, cntrl, type - 2, pin, cnt);
|
||||||
|
} else {
|
||||||
|
led = new SinglePWM(index, cntrl, type == 1, pin);
|
||||||
|
}
|
||||||
|
led->setStateString(stateStr);
|
||||||
|
return led;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectToWifi() {
|
void ConnectToWifi() {
|
||||||
|
@ -38,20 +44,23 @@ void ConnectToWifi() {
|
||||||
led_1_pin = HeadlessWiFiSettings.integer("led_1_pin", -1, 39, DEFAULT_LED1_PIN, "Pin (-1 to disable)");
|
led_1_pin = HeadlessWiFiSettings.integer("led_1_pin", -1, 39, DEFAULT_LED1_PIN, "Pin (-1 to disable)");
|
||||||
led_1_cnt = HeadlessWiFiSettings.integer("led_1_cnt", -1, 39, DEFAULT_LED1_CNT, "Count (only applies to Addressable LEDs)");
|
led_1_cnt = HeadlessWiFiSettings.integer("led_1_cnt", -1, 39, DEFAULT_LED1_CNT, "Count (only applies to Addressable LEDs)");
|
||||||
led_1_cntrl = (ControlType)HeadlessWiFiSettings.dropdown("led_1_cntrl", ledControlTypes, DEFAULT_LED1_CNTRL, "LED Control");
|
led_1_cntrl = (ControlType)HeadlessWiFiSettings.dropdown("led_1_cntrl", ledControlTypes, DEFAULT_LED1_CNTRL, "LED Control");
|
||||||
|
String led_1_state = HeadlessWiFiSettings.string("led_1_state", true, "LED State");
|
||||||
|
|
||||||
led_2_type = HeadlessWiFiSettings.dropdown("led_2_type", ledTypes, 0, "LED Type");
|
led_2_type = HeadlessWiFiSettings.dropdown("led_2_type", ledTypes, 0, "LED Type");
|
||||||
led_2_pin = HeadlessWiFiSettings.integer("led_2_pin", -1, 39, -1, "Pin (-1 to disable)");
|
led_2_pin = HeadlessWiFiSettings.integer("led_2_pin", -1, 39, -1, "Pin (-1 to disable)");
|
||||||
led_2_cnt = HeadlessWiFiSettings.integer("led_2_cnt", -1, 39, 1, "Count (only applies to Addressable LEDs)");
|
led_2_cnt = HeadlessWiFiSettings.integer("led_2_cnt", -1, 39, 1, "Count (only applies to Addressable LEDs)");
|
||||||
led_2_cntrl = (ControlType)HeadlessWiFiSettings.dropdown("led_2_cntrl", ledControlTypes, 0, "LED Control");
|
led_2_cntrl = (ControlType)HeadlessWiFiSettings.dropdown("led_2_cntrl", ledControlTypes, 0, "LED Control");
|
||||||
|
String led_2_state = HeadlessWiFiSettings.string("led_2_state", true, "LED State");
|
||||||
|
|
||||||
led_3_type = HeadlessWiFiSettings.dropdown("led_3_type", ledTypes, 0, "LED Type");
|
led_3_type = HeadlessWiFiSettings.dropdown("led_3_type", ledTypes, 0, "LED Type");
|
||||||
led_3_pin = HeadlessWiFiSettings.integer("led_3_pin", -1, 39, -1, "Pin (-1 to disable)");
|
led_3_pin = HeadlessWiFiSettings.integer("led_3_pin", -1, 39, -1, "Pin (-1 to disable)");
|
||||||
led_3_cnt = HeadlessWiFiSettings.integer("led_3_cnt", -1, 39, 1, "Count (only applies to Addressable LEDs)");
|
led_3_cnt = HeadlessWiFiSettings.integer("led_3_cnt", -1, 39, 1, "Count (only applies to Addressable LEDs)");
|
||||||
led_3_cntrl = (ControlType)HeadlessWiFiSettings.dropdown("led_3_cntrl", ledControlTypes, 0, "LED Control");
|
led_3_cntrl = (ControlType)HeadlessWiFiSettings.dropdown("led_3_cntrl", ledControlTypes, 0, "LED Control");
|
||||||
|
String led_3_state = HeadlessWiFiSettings.string("led_3_state", true, "LED State");
|
||||||
|
|
||||||
leds.push_back(newLed(1, led_1_cntrl, led_1_type, led_1_pin, led_1_cnt));
|
leds.push_back(newLed(1, led_1_cntrl, led_1_type, led_1_pin, led_1_cnt, led_1_state));
|
||||||
leds.push_back(newLed(2, led_2_cntrl, led_2_type, led_2_pin, led_2_cnt));
|
leds.push_back(newLed(2, led_2_cntrl, led_2_type, led_2_pin, led_2_cnt, led_2_state));
|
||||||
leds.push_back(newLed(3, led_3_cntrl, led_3_type, led_3_pin, led_3_cnt));
|
leds.push_back(newLed(3, led_3_cntrl, led_3_type, led_3_pin, led_3_cnt, led_3_state));
|
||||||
std::copy_if(leds.begin(), leds.end(), std::back_inserter(statusLeds), [](LED* a) { return a->getControlType() == Control_Type_Status; });
|
std::copy_if(leds.begin(), leds.end(), std::back_inserter(statusLeds), [](LED* a) { return a->getControlType() == Control_Type_Status; });
|
||||||
std::copy_if(leds.begin(), leds.end(), std::back_inserter(countLeds), [](LED* a) { return a->getControlType() == Control_Type_Count; });
|
std::copy_if(leds.begin(), leds.end(), std::back_inserter(countLeds), [](LED* a) { return a->getControlType() == Control_Type_Count; });
|
||||||
std::copy_if(leds.begin(), leds.end(), std::back_inserter(motionLeds), [](LED* a) { return a->getControlType() == Control_Type_Motion; });
|
std::copy_if(leds.begin(), leds.end(), std::back_inserter(motionLeds), [](LED* a) { return a->getControlType() == Control_Type_Motion; });
|
||||||
|
@ -66,15 +75,21 @@ bool sendState(LED* bulb) {
|
||||||
auto slug = slugify(bulb->getName());
|
auto slug = slugify(bulb->getName());
|
||||||
auto state = bulb->getState();
|
auto state = bulb->getState();
|
||||||
doc["state"] = state ? MQTT_STATE_ON_PAYLOAD : MQTT_STATE_OFF_PAYLOAD;
|
doc["state"] = state ? MQTT_STATE_ON_PAYLOAD : MQTT_STATE_OFF_PAYLOAD;
|
||||||
if (state) {
|
doc["color_mode"] = bulb->hasRgbw() ? "rgbw" : bulb->hasRgb() ? "rgb": "brightness";
|
||||||
doc["brightness"] = bulb->getBrightness();
|
doc["brightness"] = bulb->getBrightness();
|
||||||
|
if (bulb->hasRgbw()) {
|
||||||
|
auto color = doc.createNestedObject("color");
|
||||||
|
auto c = bulb->getColor();
|
||||||
|
color["r"] = c.red;
|
||||||
|
color["g"] = c.green;
|
||||||
|
color["b"] = c.blue;
|
||||||
|
color["w"] = c.white;
|
||||||
|
} else if (bulb->hasRgb()) {
|
||||||
auto color = doc.createNestedObject("color");
|
auto color = doc.createNestedObject("color");
|
||||||
auto c = bulb->getColor();
|
auto c = bulb->getColor();
|
||||||
color["r"] = c.red;
|
color["r"] = c.red;
|
||||||
color["g"] = c.green;
|
color["g"] = c.green;
|
||||||
color["b"] = c.blue;
|
color["b"] = c.blue;
|
||||||
// doc["white_value"] = bulb->getColor().white;
|
|
||||||
// doc["color_temp"] = bulb->getColorTemperature();
|
|
||||||
}
|
}
|
||||||
serializeJson(doc, buffer);
|
serializeJson(doc, buffer);
|
||||||
String setTopic = Sprintf("%s/%s", roomsTopic.c_str(), slug.c_str());
|
String setTopic = Sprintf("%s/%s", roomsTopic.c_str(), slug.c_str());
|
||||||
|
@ -83,17 +98,30 @@ bool sendState(LED* bulb) {
|
||||||
|
|
||||||
void Setup() {
|
void Setup() {
|
||||||
for (auto& led : leds)
|
for (auto& led : leds)
|
||||||
led->begin();
|
led->update();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Save() {
|
||||||
|
for (auto& led : leds)
|
||||||
|
if (led->getDirty()) {
|
||||||
|
led->setDirty(false);
|
||||||
|
Serial.printf("Saving %s: %s\r\n", led->getStateFilename().c_str(), led->getStateString().c_str());
|
||||||
|
spurt(led->getStateFilename(), led->getStateString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Loop() {
|
void Loop() {
|
||||||
for (auto& led : leds)
|
for (auto& led : leds)
|
||||||
led->service();
|
led->service();
|
||||||
|
if (millis() - lastSave > 60000) {
|
||||||
|
lastSave = millis();
|
||||||
|
Save();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SendDiscovery() {
|
bool SendDiscovery() {
|
||||||
for (auto& led : leds)
|
for (auto& led : leds)
|
||||||
if (led->getControlType() == Control_Type_MQTT && !sendLightDiscovery(led->getName(), EC_NONE, led->hasRgb()))
|
if (led->getControlType() == Control_Type_MQTT && !sendLightDiscovery(led->getName(), EC_NONE, led->hasRgb(), led->hasRgbw()))
|
||||||
return false;
|
return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -155,12 +183,12 @@ LED* findBulb(String& command) {
|
||||||
return led;
|
return led;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return NULL;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Command(String& command, String& pay) {
|
bool Command(String& command, String& pay) {
|
||||||
auto bulb = findBulb(command);
|
auto bulb = findBulb(command);
|
||||||
if (bulb == NULL) return false;
|
if (bulb == nullptr) return false;
|
||||||
DynamicJsonDocument root(pay.length() + 100);
|
DynamicJsonDocument root(pay.length() + 100);
|
||||||
auto err = deserializeJson(root, pay);
|
auto err = deserializeJson(root, pay);
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -198,9 +226,8 @@ bool Command(String& command, String& pay) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int count = 0, lastCount = 0;
|
int count = 0, lastCount = 0;
|
||||||
void Counting(bool added)
|
void Counting(bool added) {
|
||||||
{
|
|
||||||
if (added) {
|
if (added) {
|
||||||
count++;
|
count++;
|
||||||
} else {
|
} else {
|
||||||
|
@ -211,18 +238,16 @@ bool Command(String& command, String& pay) {
|
||||||
for (auto& led : countLeds)
|
for (auto& led : countLeds)
|
||||||
led->setState(count > 0);
|
led->setState(count > 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Count(unsigned int countVal)
|
void Count(unsigned int countVal) {
|
||||||
{
|
|
||||||
count = countVal;
|
count = countVal;
|
||||||
for (auto& led: countLeds)
|
for (auto& led : countLeds)
|
||||||
led->setState(count > 0);
|
led->setState(count > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Motion(bool pir, bool radar)
|
void Motion(bool pir, bool radar) {
|
||||||
{
|
for (auto& led : motionLeds)
|
||||||
for (auto& led: motionLeds)
|
|
||||||
led->setState(pir || radar);
|
led->setState(pir || radar);
|
||||||
}
|
}
|
||||||
} // namespace LEDs
|
} // namespace LEDs
|
||||||
|
|
|
@ -22,59 +22,39 @@ neoPixelType getNeoPixelType(int type) {
|
||||||
return NEO_GRB + NEO_KHZ800;
|
return NEO_GRB + NEO_KHZ800;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Addressable::begin() {
|
void Addressable::update() {
|
||||||
if (ws2812fx == NULL) {
|
if (ws2812fx == nullptr) {
|
||||||
ws2812fx = new WS2812FX(cnt, pin, getNeoPixelType(type), 1, 1);
|
ws2812fx = new WS2812FX(cnt, pin, getNeoPixelType(type), 1, 1);
|
||||||
ws2812fx->init();
|
ws2812fx->init();
|
||||||
ws2812fx->setColor(255, 255, 128);
|
|
||||||
ws2812fx->setBrightness(64);
|
|
||||||
ws2812fx->setMode(FX_MODE_STATIC);
|
ws2812fx->setMode(FX_MODE_STATIC);
|
||||||
ws2812fx->start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Color color = LED::getColor();
|
||||||
|
ws2812fx->setColor(color.red, color.green, color.blue);
|
||||||
|
ws2812fx->setBrightness(mapBrightness(LED::getBrightness()));
|
||||||
|
if (LED::getState())
|
||||||
|
ws2812fx->start();
|
||||||
|
else
|
||||||
|
ws2812fx->stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Addressable::service() {
|
void Addressable::service() {
|
||||||
if (ws2812fx == NULL) begin();
|
if (ws2812fx != nullptr)
|
||||||
ws2812fx->service();
|
ws2812fx->service();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Addressable::setColor(uint8_t p_red, uint8_t p_green, uint8_t p_blue) {
|
uint8_t Addressable::mapBrightness(uint8_t brightness) {
|
||||||
if (!LED::setColor(p_red, p_green, p_blue)) return false;
|
// Special case for zero brightness
|
||||||
if (ws2812fx == NULL) begin();
|
if (brightness == 0) return 0;
|
||||||
ws2812fx->setColor(p_red, p_green, p_blue);
|
|
||||||
ws2812fx->setBrightness(LED::getBrightness());
|
// For non-zero values, ensure we have at least brightness level 1
|
||||||
LED::setState(true);
|
// and map the rest of the range proportionally
|
||||||
return true;
|
long const result = 1 + ((long)(brightness - 1) * (MAX_BRIGHTNESS - 1)) / 254;
|
||||||
|
|
||||||
|
// Ensure we stay within byte range
|
||||||
|
return (uint8_t)min(result, (long)MAX_BRIGHTNESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Addressable::setBrightness(uint8_t p_brightness) {
|
bool Addressable::hasRgbw() {
|
||||||
if (!LED::setBrightness(p_brightness)) return false;
|
return this->type == 1 || this->type == 3;
|
||||||
if (ws2812fx == NULL) begin();
|
|
||||||
ws2812fx->setBrightness(map(p_brightness, 0, 255, 0, MAX_BRIGHTNESS));
|
|
||||||
LED::setState(p_brightness > 0);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Addressable::setState(bool p_state) {
|
|
||||||
if (!LED::setState(p_state)) return false;
|
|
||||||
if (ws2812fx == NULL) begin();
|
|
||||||
ws2812fx->setBrightness(map(p_state ? LED::getBrightness() : 0, 0, 255, 0, MAX_BRIGHTNESS));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Addressable::setWhite(uint8_t p_white) {
|
|
||||||
Serial.printf("Addressable::setWhite: p_white=%d\r\n", p_white);
|
|
||||||
if (ws2812fx == NULL) begin();
|
|
||||||
ws2812fx->setColor(p_white, p_white, p_white);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Addressable::setEffect(const char* p_effect) {
|
|
||||||
// ws2812fx->setMode(p_effect);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Addressable::hasRgbw()
|
|
||||||
{
|
|
||||||
return this->type==1 || this->type==3;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,14 +6,9 @@
|
||||||
class Addressable : public LED {
|
class Addressable : public LED {
|
||||||
public:
|
public:
|
||||||
Addressable(uint8_t index, ControlType controlType, int type, int pin, int cnt);
|
Addressable(uint8_t index, ControlType controlType, int type, int pin, int cnt);
|
||||||
void begin() override;
|
void update() override;
|
||||||
void service() override;
|
void service() override;
|
||||||
|
|
||||||
bool setColor(uint8_t p_red, uint8_t p_green, uint8_t p_blue) override;
|
|
||||||
bool setWhite(uint8_t p_white) override;
|
|
||||||
bool setBrightness(uint8_t brightness) override;
|
|
||||||
bool setState(bool p_state) override;
|
|
||||||
bool setEffect(const char* p_effect) override;
|
|
||||||
bool hasRgb() override { return true; }
|
bool hasRgb() override { return true; }
|
||||||
bool hasRgbw() override;
|
bool hasRgbw() override;
|
||||||
|
|
||||||
|
@ -23,4 +18,5 @@ class Addressable : public LED {
|
||||||
int pin;
|
int pin;
|
||||||
int cnt;
|
int cnt;
|
||||||
int cntrl;
|
int cntrl;
|
||||||
|
uint8_t mapBrightness(uint8_t brightness);
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,10 +2,10 @@
|
||||||
|
|
||||||
#include "string_utils.h"
|
#include "string_utils.h"
|
||||||
|
|
||||||
void LED::begin() {}
|
void LED::update() {}
|
||||||
void LED::service() {}
|
void LED::service() {}
|
||||||
|
|
||||||
uint8_t LED::getBrightness(void) {
|
uint8_t LED::getBrightness() {
|
||||||
return brightness;
|
return brightness;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,12 +14,12 @@ bool LED::setBrightness(uint8_t p_brightness) {
|
||||||
if (p_brightness == brightness) return false;
|
if (p_brightness == brightness) return false;
|
||||||
if (p_brightness > 0)
|
if (p_brightness > 0)
|
||||||
brightness = p_brightness;
|
brightness = p_brightness;
|
||||||
else
|
dirty = true;
|
||||||
LED::setState(false);
|
update();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Color LED::getColor(void) {
|
const Color LED::getColor() {
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,20 +27,24 @@ bool LED::setColor(uint32_t color) {
|
||||||
return LED::setColor((color & 0xFF0000) >> 16, (color & 0x00FF00) >> 8, (color & 0x0000FF));
|
return LED::setColor((color & 0xFF0000) >> 16, (color & 0x00FF00) >> 8, (color & 0x0000FF));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LED::setColor(uint8_t p_red, uint8_t p_green, uint8_t p_blue) {
|
bool LED::setColor(uint8_t p_red, uint8_t p_green, uint8_t p_blue, uint8_t p_white) {
|
||||||
if (p_red == color.red && p_green == color.green && p_blue == color.blue) {
|
if (p_red == color.red && p_green == color.green && p_blue == color.blue && p_white == color.white) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// Serial.printf("LED::setColor(%d, %d, %d)\r\n", p_red, p_green, p_blue);
|
// Serial.printf("LED::setColor(%d, %d, %d)\r\n", p_red, p_green, p_blue);
|
||||||
color.red = p_red;
|
color.red = p_red;
|
||||||
color.green = p_green;
|
color.green = p_green;
|
||||||
color.blue = p_blue;
|
color.blue = p_blue;
|
||||||
|
color.white = p_white;
|
||||||
|
dirty = true;
|
||||||
|
update();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LED::setWhite(uint8_t p_white) {
|
bool LED::setWhite(uint8_t p_white) {
|
||||||
Serial.printf("LED::setWhite(%d)\r\n", p_white);
|
//Serial.printf("LED::setWhite(%d)\r\n", p_white);
|
||||||
return false;
|
if (!LED::setColor(255, 255, 255) && !LED::setBrightness(p_white)) return false;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t LED::getColorTemperature(void) {
|
uint16_t LED::getColorTemperature(void) {
|
||||||
|
@ -48,16 +52,18 @@ uint16_t LED::getColorTemperature(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LED::setColorTemperature(uint16_t p_colorTemperature) {
|
bool LED::setColorTemperature(uint16_t p_colorTemperature) {
|
||||||
Serial.printf("LED::setColorTemperature(%d)\r\n", p_colorTemperature);
|
//Serial.printf("LED::setColorTemperature(%d)\r\n", p_colorTemperature);
|
||||||
|
dirty = true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LED::setEffect(const char *p_effect) {
|
bool LED::setEffect(const char *p_effect) {
|
||||||
Serial.printf("LED::setEffect(%s)\r\n", p_effect);
|
//Serial.printf("LED::setEffect(%s)\r\n", p_effect);
|
||||||
|
dirty = true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LED::getState(void) {
|
bool LED::getState() {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,6 +71,8 @@ bool LED::setState(bool p_state) {
|
||||||
if (state == p_state) return false;
|
if (state == p_state) return false;
|
||||||
// Serial.printf("LED::setState(%s)\r\n", p_state ? "true" : "false");
|
// Serial.printf("LED::setState(%s)\r\n", p_state ? "true" : "false");
|
||||||
state = p_state;
|
state = p_state;
|
||||||
|
dirty = true;
|
||||||
|
update();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,3 +88,38 @@ LED::LED(uint8_t index, ControlType controlType) {
|
||||||
const String LED::getId() {
|
const String LED::getId() {
|
||||||
return Sprintf("led_%d", index);
|
return Sprintf("led_%d", index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const String LED::getStateFilename() {
|
||||||
|
return Sprintf("/led_%d_state", index);
|
||||||
|
}
|
||||||
|
|
||||||
|
const String LED::getStateString() {
|
||||||
|
// Format: BBRGGBBWW (B=brightness, R=red, G=green, B=blue, W=white)
|
||||||
|
char stateStr[11];
|
||||||
|
sprintf(stateStr, "%02X%02X%02X%02X%02X",
|
||||||
|
brightness,
|
||||||
|
color.red,
|
||||||
|
color.green,
|
||||||
|
color.blue,
|
||||||
|
color.white);
|
||||||
|
return String(stateStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LED::setStateString(String stateStr) {
|
||||||
|
if (stateStr.length() == 10) {
|
||||||
|
// Parse hex values - each value is 2 hex digits
|
||||||
|
uint8_t brightness = strtol(stateStr.substring(0, 2).c_str(), NULL, 16);
|
||||||
|
uint8_t r = strtol(stateStr.substring(2, 4).c_str(), NULL, 16);
|
||||||
|
uint8_t g = strtol(stateStr.substring(4, 6).c_str(), NULL, 16);
|
||||||
|
uint8_t b = strtol(stateStr.substring(6, 8).c_str(), NULL, 16);
|
||||||
|
uint8_t w = strtol(stateStr.substring(8, 10).c_str(), NULL, 16);
|
||||||
|
|
||||||
|
if (hasRgbw()) {
|
||||||
|
setColor(r, g, b, w);
|
||||||
|
} else if (hasRgb()) {
|
||||||
|
setColor(r, g, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
setBrightness(brightness);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ struct Color {
|
||||||
class LED {
|
class LED {
|
||||||
public:
|
public:
|
||||||
LED(uint8_t index, ControlType controlType);
|
LED(uint8_t index, ControlType controlType);
|
||||||
virtual void begin();
|
virtual void update();
|
||||||
virtual void service();
|
virtual void service();
|
||||||
|
|
||||||
virtual uint8_t getBrightness(void);
|
virtual uint8_t getBrightness(void);
|
||||||
|
@ -28,7 +28,7 @@ class LED {
|
||||||
|
|
||||||
const virtual Color getColor(void);
|
const virtual Color getColor(void);
|
||||||
virtual bool setColor(uint32_t color);
|
virtual bool setColor(uint32_t color);
|
||||||
virtual bool setColor(uint8_t red, uint8_t green, uint8_t blue);
|
virtual bool setColor(uint8_t red, uint8_t green, uint8_t blue, uint8_t white = 0);
|
||||||
|
|
||||||
virtual bool setWhite(uint8_t white);
|
virtual bool setWhite(uint8_t white);
|
||||||
|
|
||||||
|
@ -45,6 +45,13 @@ class LED {
|
||||||
const String getId();
|
const String getId();
|
||||||
const String getName();
|
const String getName();
|
||||||
|
|
||||||
|
bool getDirty() { return this->dirty; }
|
||||||
|
void setDirty(bool dirty) { this->dirty = dirty; }
|
||||||
|
|
||||||
|
const String getStateFilename();
|
||||||
|
const String getStateString();
|
||||||
|
void setStateString(String encoded);
|
||||||
|
|
||||||
virtual bool hasRgb() { return false; }
|
virtual bool hasRgb() { return false; }
|
||||||
virtual bool hasRgbw() { return false; }
|
virtual bool hasRgbw() { return false; }
|
||||||
|
|
||||||
|
@ -53,5 +60,6 @@ class LED {
|
||||||
uint8_t index;
|
uint8_t index;
|
||||||
Color color = {255, 255, 128, 128};
|
Color color = {255, 255, 128, 128};
|
||||||
bool state = true;
|
bool state = true;
|
||||||
uint8_t brightness = 64;
|
uint8_t brightness = 128;
|
||||||
|
bool dirty = false;
|
||||||
};
|
};
|
||||||
|
|
|
@ -12,8 +12,8 @@ void SinglePWM::init() {
|
||||||
ledcAttachPin(pin, getIndex());
|
ledcAttachPin(pin, getIndex());
|
||||||
}
|
}
|
||||||
|
|
||||||
void SinglePWM::begin() {
|
void SinglePWM::update() {
|
||||||
setDuty(LED::getBrightness());
|
setDuty(LED::getState() ? LED::getBrightness() : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SinglePWM::setDuty(uint32_t x) {
|
void SinglePWM::setDuty(uint32_t x) {
|
||||||
|
@ -25,15 +25,3 @@ void SinglePWM::setDuty(uint32_t x) {
|
||||||
|
|
||||||
void SinglePWM::service() {
|
void SinglePWM::service() {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SinglePWM::setState(bool state) {
|
|
||||||
if (!LED::setState(state)) return false;
|
|
||||||
setDuty(state ? LED::getBrightness() : 0);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SinglePWM::setBrightness(uint8_t brightness) {
|
|
||||||
if (!LED::setBrightness(brightness)) return false;
|
|
||||||
setDuty(brightness);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
|
@ -6,12 +6,9 @@ class SinglePWM : public LED {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
SinglePWM(uint8_t index, ControlType controlType, bool inverted, int pin);
|
SinglePWM(uint8_t index, ControlType controlType, bool inverted, int pin);
|
||||||
void begin() override;
|
void update() override;
|
||||||
void service() override;
|
void service() override;
|
||||||
|
|
||||||
bool setState(bool state) override;
|
|
||||||
bool setBrightness(uint8_t brightness) override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void init();
|
void init();
|
||||||
void setDuty(uint32_t value);
|
void setDuty(uint32_t value);
|
||||||
|
|
12
src/mqtt.cpp
12
src/mqtt.cpp
|
@ -192,7 +192,7 @@ bool sendNumberDiscovery(const String &name, const String &entityCategory)
|
||||||
return pub(discoveryTopic.c_str(), 0, true, buffer.c_str());
|
return pub(discoveryTopic.c_str(), 0, true, buffer.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool sendLightDiscovery(const String &name, const String &entityCategory, bool rgb)
|
bool sendLightDiscovery(const String &name, const String &entityCategory, bool rgb, bool rgbw)
|
||||||
{
|
{
|
||||||
auto slug = slugify(name);
|
auto slug = slugify(name);
|
||||||
|
|
||||||
|
@ -204,7 +204,15 @@ bool sendLightDiscovery(const String &name, const String &entityCategory, bool r
|
||||||
doc["stat_t"] = "~/" + slug;
|
doc["stat_t"] = "~/" + slug;
|
||||||
doc["cmd_t"] = "~/" + slug + "/set";
|
doc["cmd_t"] = "~/" + slug + "/set";
|
||||||
doc["brightness"] = true;
|
doc["brightness"] = true;
|
||||||
doc["rgb"] = rgb;
|
|
||||||
|
if (rgbw) {
|
||||||
|
doc["supported_color_modes"][0] = "rgbw";
|
||||||
|
} else if (rgb) {
|
||||||
|
doc["supported_color_modes"][0] = "rgb";
|
||||||
|
} else {
|
||||||
|
doc["supported_color_modes"][0] = "brightness";
|
||||||
|
}
|
||||||
|
|
||||||
if (!entityCategory.isEmpty()) doc["entity_category"] = entityCategory;
|
if (!entityCategory.isEmpty()) doc["entity_category"] = entityCategory;
|
||||||
|
|
||||||
String buffer = String();
|
String buffer = String();
|
||||||
|
|
|
@ -21,7 +21,7 @@ bool sendSensorDiscovery(const String &name, const String &entityCategory, const
|
||||||
bool sendButtonDiscovery(const String &name, const String &entityCategory);
|
bool sendButtonDiscovery(const String &name, const String &entityCategory);
|
||||||
bool sendSwitchDiscovery(const String &name, const String &entityCategory);
|
bool sendSwitchDiscovery(const String &name, const String &entityCategory);
|
||||||
bool sendNumberDiscovery(const String &name, const String &entityCategory);
|
bool sendNumberDiscovery(const String &name, const String &entityCategory);
|
||||||
bool sendLightDiscovery(const String &name, const String &entityCategory, bool rgb);
|
bool sendLightDiscovery(const String &name, const String &entityCategory, bool rgb, bool rgbw);
|
||||||
|
|
||||||
bool sendDeleteDiscovery(const String &domain, const String &name);
|
bool sendDeleteDiscovery(const String &domain, const String &name);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue