2016-05-17 22:14:03 +02:00
|
|
|
#include "DeviceRgb.h"
|
2016-04-22 22:26:52 +02:00
|
|
|
|
2016-05-17 22:14:03 +02:00
|
|
|
void DeviceRgb::setRgb(int red, int green, int blue) {
|
2016-04-27 21:33:58 +02:00
|
|
|
analogWrite(pinRed, red);
|
|
|
|
analogWrite(pinGreen, green);
|
|
|
|
analogWrite(pinBlue, blue);
|
|
|
|
}
|
|
|
|
|
2016-05-17 22:14:03 +02:00
|
|
|
void DeviceRgb::publishStatus() {
|
2016-04-27 21:40:33 +02:00
|
|
|
DynamicJsonBuffer json_outBuffer;
|
|
|
|
JsonObject& json_out = json_outBuffer.createObject();
|
2016-05-17 22:14:03 +02:00
|
|
|
json_out["red"] = rgb_red;
|
|
|
|
json_out["green"] = rgb_green;
|
|
|
|
json_out["blue"] = rgb_blue;
|
2016-04-27 21:40:33 +02:00
|
|
|
String response;
|
|
|
|
json_out.printTo(response);
|
2016-05-17 22:14:03 +02:00
|
|
|
Serial.print("rgb state: ");
|
2016-04-27 21:40:33 +02:00
|
|
|
Serial.println(response);
|
2016-05-17 22:14:03 +02:00
|
|
|
Homie.setNodeProperty(rgbNode, "color", response);
|
2016-04-27 21:40:33 +02:00
|
|
|
}
|
|
|
|
|
2016-05-17 22:14:03 +02:00
|
|
|
void DeviceRgb::deviceSetup() {
|
2016-04-22 22:26:52 +02:00
|
|
|
pinMode(pinRed, OUTPUT);
|
|
|
|
pinMode(pinGreen, OUTPUT);
|
|
|
|
pinMode(pinBlue, OUTPUT);
|
2016-05-17 22:14:03 +02:00
|
|
|
setRgb(rgb_red, rgb_green, rgb_blue);
|
2016-04-22 22:26:52 +02:00
|
|
|
}
|
|
|
|
|
2016-05-17 22:14:03 +02:00
|
|
|
bool DeviceRgb::rgbOnHandler(String value) {
|
2016-04-22 22:26:52 +02:00
|
|
|
if (value == "true") {
|
2016-05-17 22:14:03 +02:00
|
|
|
setRgb(rgb_red, rgb_green, rgb_blue);
|
|
|
|
Homie.setNodeProperty(rgbNode, "on", "true");
|
|
|
|
Serial.println("rgb is on");
|
2016-04-22 22:26:52 +02:00
|
|
|
} else if (value == "false") {
|
2016-05-17 22:14:03 +02:00
|
|
|
setRgb(0, 0, 0);
|
|
|
|
Homie.setNodeProperty(rgbNode, "on", "false");
|
|
|
|
Serial.println("rgb is off");
|
2016-04-22 22:26:52 +02:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-17 22:14:03 +02:00
|
|
|
bool DeviceRgb::rgbColorHandler(String message) {
|
2016-04-22 22:26:52 +02:00
|
|
|
DynamicJsonBuffer json_inBuffer;
|
|
|
|
JsonObject& json_in = json_inBuffer.parseObject(message);
|
|
|
|
if (json_in.success()) {
|
|
|
|
if (json_in.containsKey("red")) {
|
2016-05-17 22:14:03 +02:00
|
|
|
rgb_red = json_in["red"];
|
2016-04-22 22:26:52 +02:00
|
|
|
}
|
|
|
|
if (json_in.containsKey("green")) {
|
2016-05-17 22:14:03 +02:00
|
|
|
rgb_green = json_in["green"];
|
2016-04-22 22:26:52 +02:00
|
|
|
}
|
|
|
|
if (json_in.containsKey("blue")) {
|
2016-05-17 22:14:03 +02:00
|
|
|
rgb_blue = json_in["blue"];
|
2016-04-22 22:26:52 +02:00
|
|
|
}
|
2016-05-17 22:14:03 +02:00
|
|
|
setRgb(rgb_red, rgb_green, rgb_blue);
|
2016-04-22 22:26:52 +02:00
|
|
|
} else {
|
|
|
|
Serial.println("parsing of JSON failed");
|
|
|
|
}
|
2016-04-27 21:40:33 +02:00
|
|
|
publishStatus();
|
2016-04-22 22:26:52 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-17 22:14:03 +02:00
|
|
|
bool DeviceRgb::rgbFadeHandler(String message) {
|
2016-04-27 21:24:55 +02:00
|
|
|
DynamicJsonBuffer json_inBuffer;
|
|
|
|
JsonObject& json_in = json_inBuffer.parseObject(message);
|
|
|
|
if (json_in.success()) {
|
2016-05-17 22:14:03 +02:00
|
|
|
fade_from_red = rgb_red;
|
2016-04-27 21:24:55 +02:00
|
|
|
if (json_in.containsKey("red")) {
|
|
|
|
fade_to_red = json_in["red"];
|
|
|
|
} else {
|
2016-05-17 22:14:03 +02:00
|
|
|
fade_to_red = rgb_red;
|
2016-04-27 21:24:55 +02:00
|
|
|
}
|
2016-05-17 22:14:03 +02:00
|
|
|
fade_from_green = rgb_green;
|
2016-04-27 21:24:55 +02:00
|
|
|
if (json_in.containsKey("green")) {
|
|
|
|
fade_to_green = json_in["green"];
|
|
|
|
} else {
|
2016-05-17 22:14:03 +02:00
|
|
|
fade_to_green = rgb_green;
|
2016-04-27 21:24:55 +02:00
|
|
|
}
|
2016-05-17 22:14:03 +02:00
|
|
|
fade_from_blue = rgb_blue;
|
2016-04-27 21:24:55 +02:00
|
|
|
if (json_in.containsKey("blue")) {
|
|
|
|
fade_to_blue = json_in["blue"];
|
|
|
|
} else {
|
2016-05-17 22:14:03 +02:00
|
|
|
fade_to_blue = rgb_blue;
|
2016-04-27 21:24:55 +02:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-17 22:14:03 +02:00
|
|
|
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);
|
2016-04-22 22:26:52 +02:00
|
|
|
}
|
|
|
|
|
2016-05-17 22:14:03 +02:00
|
|
|
void DeviceRgb::deviceLoop() {
|
2016-04-27 21:24:55 +02:00
|
|
|
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)) {
|
2016-05-17 22:14:03 +02:00
|
|
|
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);
|
2016-04-27 21:24:55 +02:00
|
|
|
progress_last = progress;
|
|
|
|
}
|
|
|
|
} else {
|
2016-05-17 22:14:03 +02:00
|
|
|
rgb_red = fade_to_red;
|
|
|
|
rgb_green = fade_to_green;
|
|
|
|
rgb_blue = fade_to_blue;
|
|
|
|
setRgb(rgb_red, rgb_green, rgb_blue);
|
2016-04-27 21:40:33 +02:00
|
|
|
publishStatus();
|
2016-04-27 21:24:55 +02:00
|
|
|
fading = false;
|
|
|
|
}
|
|
|
|
}
|
2016-04-22 22:26:52 +02:00
|
|
|
return;
|
|
|
|
}
|