101 lines
2.6 KiB
C++
101 lines
2.6 KiB
C++
|
#include "DeviceLed.h"
|
||
|
|
||
|
void DeviceLed::setLed(int value) {
|
||
|
analogWrite(pinLed, value);
|
||
|
}
|
||
|
|
||
|
void DeviceLed::publishStatus() {
|
||
|
DynamicJsonBuffer json_outBuffer;
|
||
|
JsonObject& json_out = json_outBuffer.createObject();
|
||
|
json_out["brightness"] = brightness;
|
||
|
String response;
|
||
|
json_out.printTo(response);
|
||
|
Serial.print("led state: ");
|
||
|
Serial.println(response);
|
||
|
Homie.setNodeProperty(ledNode, "brightness", response);
|
||
|
}
|
||
|
|
||
|
void DeviceLed::deviceSetup() {
|
||
|
pinMode(pinLed, OUTPUT);
|
||
|
setLed(brightness);
|
||
|
}
|
||
|
|
||
|
bool DeviceLed::ledOnHandler(String value) {
|
||
|
if (value == "true") {
|
||
|
setLed(brightness);
|
||
|
Homie.setNodeProperty(ledNode, "on", "true");
|
||
|
Serial.println("led is on");
|
||
|
} else if (value == "false") {
|
||
|
setLed(0);
|
||
|
Homie.setNodeProperty(ledNode, "on", "false");
|
||
|
Serial.println("led is off");
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool DeviceLed::ledBrightnessHandler(String message) {
|
||
|
DynamicJsonBuffer json_inBuffer;
|
||
|
JsonObject& json_in = json_inBuffer.parseObject(message);
|
||
|
if (json_in.success()) {
|
||
|
if (json_in.containsKey("brightness")) {
|
||
|
brightness = json_in["brightness"];
|
||
|
}
|
||
|
setLed(brightness);
|
||
|
} else {
|
||
|
Serial.println("parsing of JSON failed");
|
||
|
}
|
||
|
publishStatus();
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool DeviceLed::ledFadeHandler(String message) {
|
||
|
DynamicJsonBuffer json_inBuffer;
|
||
|
JsonObject& json_in = json_inBuffer.parseObject(message);
|
||
|
if (json_in.success()) {
|
||
|
fade_from = brightness;
|
||
|
if (json_in.containsKey("brightness")) {
|
||
|
fade_to = json_in["brightness"];
|
||
|
} else {
|
||
|
fade_to = brightness;
|
||
|
}
|
||
|
fade_start = millis();
|
||
|
if (json_in.containsKey("seconds")) {
|
||
|
fade_end = fade_start + 1000 * (int)json_in["seconds"];
|
||
|
} else {
|
||
|
fade_end = 0;
|
||
|
}
|
||
|
fading = true;
|
||
|
} else {
|
||
|
Serial.println("parsing of JSON failed");
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
void DeviceLed::deviceRegister() {
|
||
|
ledNode.subscribe("on", [this](String value) { return ledOnHandler(value); });
|
||
|
ledNode.subscribe("brightness", [this](String value) { return ledBrightnessHandler(value); });
|
||
|
ledNode.subscribe("fade", [this](String value) { return ledFadeHandler(value); });
|
||
|
Homie.registerNode(ledNode);
|
||
|
}
|
||
|
|
||
|
void DeviceLed::deviceLoop() {
|
||
|
if (fading) {
|
||
|
if (fade_end > millis()) {
|
||
|
float progress = 1.0 * (millis() - fade_start) / (fade_end - fade_start);
|
||
|
if ((int)(progress * 100) != (int)(progress_last * 100)) {
|
||
|
brightness = fade_from + (fade_to - fade_from) * progress;
|
||
|
setLed(brightness);
|
||
|
progress_last = progress;
|
||
|
}
|
||
|
} else {
|
||
|
brightness = fade_to;
|
||
|
setLed(brightness);
|
||
|
publishStatus();
|
||
|
fading = false;
|
||
|
}
|
||
|
}
|
||
|
return;
|
||
|
}
|