From b055578759bb7a1f81f08b94c7a6cf8fca90aa31 Mon Sep 17 00:00:00 2001 From: Tim Niggemann Date: Wed, 16 Jul 2014 11:49:34 +0200 Subject: [PATCH] Added config switch for turing off the lamps if the color black is written. Former-commit-id: bb4f4bc74c035c10a8dc678a11052ea276ea0149 --- libsrc/leddevice/LedDeviceFactory.cpp | 3 ++- libsrc/leddevice/LedDevicePhilipsHue.cpp | 23 ++++++++++++++++------- libsrc/leddevice/LedDevicePhilipsHue.h | 7 ++++++- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/libsrc/leddevice/LedDeviceFactory.cpp b/libsrc/leddevice/LedDeviceFactory.cpp index 76eb0137..5a1bd123 100755 --- a/libsrc/leddevice/LedDeviceFactory.cpp +++ b/libsrc/leddevice/LedDeviceFactory.cpp @@ -164,7 +164,8 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig) else if (type == "philipshue") { const std::string output = deviceConfig["output"].asString(); - device = new LedDevicePhilipsHue(output); + const bool switchOffOnBlack = deviceConfig.get("switch_off_on_black", false).asBool(); + device = new LedDevicePhilipsHue(output, switchOffOnBlack); } else if (type == "test") { diff --git a/libsrc/leddevice/LedDevicePhilipsHue.cpp b/libsrc/leddevice/LedDevicePhilipsHue.cpp index fcb6b2a8..beda2d71 100755 --- a/libsrc/leddevice/LedDevicePhilipsHue.cpp +++ b/libsrc/leddevice/LedDevicePhilipsHue.cpp @@ -12,8 +12,10 @@ #include -LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output) : - host(output.c_str()), username("newdeveloper") { +const ColorPoint LedDevicePhilipsHue::BLACK = {0.0f, 0.0f, 0.0f}; + +LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output, bool switchOffOnBlack) : + host(output.c_str()), username("newdeveloper"), switchOffOnBlack(switchOffOnBlack) { http = new QHttp(host); timer.setInterval(3000); timer.setSingleShot(true); @@ -37,12 +39,19 @@ int LedDevicePhilipsHue::write(const std::vector & ledValues) { HueLamp& lamp = lamps.at(idx); // Scale colors from [0, 255] to [0, 1] and convert to xy space. ColorPoint xy = rgbToXYBrightness(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f, lamp); + // Switch lamp off if switchOffOnBlack is enabled and the lamp is currently on. + if (switchOffOnBlack && xy == BLACK && lamp.color != BLACK) { + put(getStateRoute(lamp.id), QString("{\"on\": false}")); + } // Write color if color has been changed. - if (xy != lamp.color) { - // Send adjust color command in JSON format. - put(getStateRoute(lamp.id), QString("{\"xy\": [%1, %2]}").arg(xy.x).arg(xy.y)); - // Send brightness color command in JSON format. - put(getStateRoute(lamp.id), QString("{\"bri\": %1}").arg(qRound(xy.bri * 255.0f))); + else if (xy != lamp.color) { + // Switch on if the lamp has been previously switched off. + if (switchOffOnBlack && lamp.color == BLACK) { + put(getStateRoute(lamp.id), QString("{\"on\": true}")); + } + // Send adjust color and brightness command in JSON format. + put(getStateRoute(lamp.id), QString("{\"xy\": [%1, %2], \"bri\": %1}").arg(xy.x).arg(xy.y) + .arg(qRound(xy.bri * 255.0f))); // Remember written color. lamp.color = xy; } diff --git a/libsrc/leddevice/LedDevicePhilipsHue.h b/libsrc/leddevice/LedDevicePhilipsHue.h index d60c2b32..ecbecf3a 100755 --- a/libsrc/leddevice/LedDevicePhilipsHue.h +++ b/libsrc/leddevice/LedDevicePhilipsHue.h @@ -71,7 +71,9 @@ public: /// /// @param output the ip address of the bridge /// - LedDevicePhilipsHue(const std::string& output); + /// @param switchOffOnBlack kill lights for black + /// + LedDevicePhilipsHue(const std::string& output, bool switchOffOnBlack); /// /// Destructor of this device @@ -95,6 +97,7 @@ private slots: void restoreStates(); private: + const static ColorPoint BLACK; /// Array to save the lamps. std::vector lamps; /// Ip address of the bridge @@ -105,6 +108,8 @@ private: QHttp* http; /// Use timer to reset lights when we got into "GRABBINGMODE_OFF". QTimer timer; + /// + bool switchOffOnBlack; /// /// Sends a HTTP GET request (blocking).