publish color after fading

This commit is contained in:
Ronald Schaten 2016-04-27 21:40:33 +02:00
parent 17d862049e
commit e15a65c69d
2 changed files with 20 additions and 15 deletions

View File

@ -6,6 +6,19 @@ void DeviceLed::setLed(int red, int green, int blue) {
analogWrite(pinBlue, blue); analogWrite(pinBlue, blue);
} }
void DeviceLed::publishStatus() {
DynamicJsonBuffer json_outBuffer;
JsonObject& json_out = json_outBuffer.createObject();
json_out["red"] = led_red;
json_out["green"] = led_green;
json_out["blue"] = led_blue;
String response;
json_out.printTo(response);
Serial.print("led state: ");
Serial.println(response);
Homie.setNodeProperty(ledNode, "color", response);
}
void DeviceLed::deviceSetup() { void DeviceLed::deviceSetup() {
pinMode(pinRed, OUTPUT); pinMode(pinRed, OUTPUT);
pinMode(pinGreen, OUTPUT); pinMode(pinGreen, OUTPUT);
@ -45,17 +58,7 @@ bool DeviceLed::ledColorHandler(String message) {
} else { } else {
Serial.println("parsing of JSON failed"); Serial.println("parsing of JSON failed");
} }
publishStatus();
DynamicJsonBuffer json_outBuffer;
JsonObject& json_out = json_outBuffer.createObject();
json_out["red"] = led_red;
json_out["green"] = led_green;
json_out["blue"] = led_blue;
String response;
json_out.printTo(response);
Serial.print("led state: ");
Serial.println(response);
Homie.setNodeProperty(ledNode, "color", response);
return true; return true;
} }
@ -113,10 +116,11 @@ void DeviceLed::deviceLoop() {
progress_last = progress; progress_last = progress;
} }
} else { } else {
led_red = fade_to_red; led_red = fade_to_red;
led_green = fade_to_green; led_green = fade_to_green;
led_blue = fade_to_blue; led_blue = fade_to_blue;
setLed(led_red, led_green, led_blue); setLed(led_red, led_green, led_blue);
publishStatus();
fading = false; fading = false;
} }
} }

View File

@ -28,6 +28,7 @@ class DeviceLed : public Device {
unsigned long fade_end = 0; unsigned long fade_end = 0;
float progress_last = 0; float progress_last = 0;
void setLed(int red, int green, int blue); void setLed(int red, int green, int blue);
void publishStatus();
bool ledOnHandler(String value); bool ledOnHandler(String value);
bool ledColorHandler(String message); bool ledColorHandler(String message);
bool ledFadeHandler(String message); bool ledFadeHandler(String message);