Things/things/DeviceRgb.cpp

129 lines
3.6 KiB
C++

#include "DeviceRgb.h"
void DeviceRgb::setRgb(int red, int green, int blue) {
analogWrite(pinRed, red);
analogWrite(pinGreen, green);
analogWrite(pinBlue, blue);
}
void DeviceRgb::publishStatus() {
DynamicJsonBuffer json_outBuffer;
JsonObject& json_out = json_outBuffer.createObject();
json_out["red"] = rgb_red;
json_out["green"] = rgb_green;
json_out["blue"] = rgb_blue;
String response;
json_out.printTo(response);
Serial.print("rgb state: ");
Serial.println(response);
Homie.setNodeProperty(rgbNode, "color", response);
}
void DeviceRgb::deviceSetup() {
pinMode(pinRed, OUTPUT);
pinMode(pinGreen, OUTPUT);
pinMode(pinBlue, OUTPUT);
setRgb(rgb_red, rgb_green, rgb_blue);
}
bool DeviceRgb::rgbOnHandler(String value) {
if (value == "true") {
setRgb(rgb_red, rgb_green, rgb_blue);
Homie.setNodeProperty(rgbNode, "on", "true");
Serial.println("rgb is on");
} else if (value == "false") {
setRgb(0, 0, 0);
Homie.setNodeProperty(rgbNode, "on", "false");
Serial.println("rgb is off");
} else {
return false;
}
return true;
}
bool DeviceRgb::rgbColorHandler(String message) {
DynamicJsonBuffer json_inBuffer;
JsonObject& json_in = json_inBuffer.parseObject(message);
if (json_in.success()) {
if (json_in.containsKey("red")) {
rgb_red = json_in["red"];
}
if (json_in.containsKey("green")) {
rgb_green = json_in["green"];
}
if (json_in.containsKey("blue")) {
rgb_blue = json_in["blue"];
}
setRgb(rgb_red, rgb_green, rgb_blue);
} else {
Serial.println("parsing of JSON failed");
}
publishStatus();
return true;
}
bool DeviceRgb::rgbFadeHandler(String message) {
DynamicJsonBuffer json_inBuffer;
JsonObject& json_in = json_inBuffer.parseObject(message);
if (json_in.success()) {
fade_from_red = rgb_red;
if (json_in.containsKey("red")) {
fade_to_red = json_in["red"];
} else {
fade_to_red = rgb_red;
}
fade_from_green = rgb_green;
if (json_in.containsKey("green")) {
fade_to_green = json_in["green"];
} else {
fade_to_green = rgb_green;
}
fade_from_blue = rgb_blue;
if (json_in.containsKey("blue")) {
fade_to_blue = json_in["blue"];
} else {
fade_to_blue = rgb_blue;
}
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 DeviceRgb::deviceRegister() {
rgbNode.subscribe("on", [this](String value) { return rgbOnHandler(value); });
rgbNode.subscribe("color", [this](String value) { return rgbColorHandler(value); });
rgbNode.subscribe("fade", [this](String value) { return rgbFadeHandler(value); });
Homie.registerNode(rgbNode);
}
void DeviceRgb::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)) {
rgb_red = fade_from_red + (fade_to_red - fade_from_red) * progress;
rgb_green = fade_from_green + (fade_to_green - fade_from_green) * progress;
rgb_blue = fade_from_blue + (fade_to_blue - fade_from_blue) * progress;
setRgb(rgb_red, rgb_green, rgb_blue);
progress_last = progress;
}
} else {
rgb_red = fade_to_red;
rgb_green = fade_to_green;
rgb_blue = fade_to_blue;
setRgb(rgb_red, rgb_green, rgb_blue);
publishStatus();
fading = false;
}
}
return;
}