From e15a65c69d4abab955323a5a18a9555027507a77 Mon Sep 17 00:00:00 2001 From: Ronald Schaten Date: Wed, 27 Apr 2016 21:40:33 +0200 Subject: [PATCH] publish color after fading --- things/DeviceLed.cpp | 34 +++++++++++++++++++--------------- things/DeviceLed.h | 1 + 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/things/DeviceLed.cpp b/things/DeviceLed.cpp index 69b453c..7081d0b 100644 --- a/things/DeviceLed.cpp +++ b/things/DeviceLed.cpp @@ -6,6 +6,19 @@ void DeviceLed::setLed(int red, int green, int 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() { pinMode(pinRed, OUTPUT); pinMode(pinGreen, OUTPUT); @@ -45,17 +58,7 @@ bool DeviceLed::ledColorHandler(String message) { } else { Serial.println("parsing of JSON failed"); } - - 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); + publishStatus(); return true; } @@ -113,10 +116,11 @@ void DeviceLed::deviceLoop() { progress_last = progress; } } else { - led_red = fade_to_red; - led_green = fade_to_green; - led_blue = fade_to_blue; - setLed(led_red, led_green, led_blue); + led_red = fade_to_red; + led_green = fade_to_green; + led_blue = fade_to_blue; + setLed(led_red, led_green, led_blue); + publishStatus(); fading = false; } } diff --git a/things/DeviceLed.h b/things/DeviceLed.h index 21f9cc0..09a1023 100644 --- a/things/DeviceLed.h +++ b/things/DeviceLed.h @@ -28,6 +28,7 @@ class DeviceLed : public Device { unsigned long fade_end = 0; float progress_last = 0; void setLed(int red, int green, int blue); + void publishStatus(); bool ledOnHandler(String value); bool ledColorHandler(String message); bool ledFadeHandler(String message);