From 4f7524d8deb0be4b130857bb324498ef60d83b7e Mon Sep 17 00:00:00 2001 From: ntim Date: Thu, 5 Feb 2015 09:50:05 +0100 Subject: [PATCH 1/6] Small performance improvement if off on black is true. Former-commit-id: dc69a21681432bef540c396847d64b07fa0bbb09 --- libsrc/leddevice/LedDevicePhilipsHue.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/libsrc/leddevice/LedDevicePhilipsHue.cpp b/libsrc/leddevice/LedDevicePhilipsHue.cpp index c8cfa339..10437a02 100755 --- a/libsrc/leddevice/LedDevicePhilipsHue.cpp +++ b/libsrc/leddevice/LedDevicePhilipsHue.cpp @@ -166,10 +166,6 @@ int LedDevicePhilipsHue::write(const std::vector & ledValues) { CiColor xy = lamp.rgbToCiColor(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f); // Write color if color has been changed. if (xy != lamp.color) { - // Switch on if the lamp has been previously switched off. - if (switchOffOnBlack && lamp.color == lamp.black) { - put(getStateRoute(lamp.id), QString("{\"on\": true}")); - } // Send adjust color and brightness command in JSON format. // We have to set the transition time each time. put(getStateRoute(lamp.id), From acc0f61e60172af07e7f93ada57230fb30a47d8b Mon Sep 17 00:00:00 2001 From: ntim Date: Mon, 2 Mar 2015 12:51:50 +0000 Subject: [PATCH 2/6] Reverted last commit due to new api version. Former-commit-id: 1b6a204183d43865d42d4ee9f3ecf28408c86a9c --- libsrc/leddevice/LedDevicePhilipsHue.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libsrc/leddevice/LedDevicePhilipsHue.cpp b/libsrc/leddevice/LedDevicePhilipsHue.cpp index 10437a02..c8cfa339 100755 --- a/libsrc/leddevice/LedDevicePhilipsHue.cpp +++ b/libsrc/leddevice/LedDevicePhilipsHue.cpp @@ -166,6 +166,10 @@ int LedDevicePhilipsHue::write(const std::vector & ledValues) { CiColor xy = lamp.rgbToCiColor(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f); // Write color if color has been changed. if (xy != lamp.color) { + // Switch on if the lamp has been previously switched off. + if (switchOffOnBlack && lamp.color == lamp.black) { + put(getStateRoute(lamp.id), QString("{\"on\": true}")); + } // Send adjust color and brightness command in JSON format. // We have to set the transition time each time. put(getStateRoute(lamp.id), From ecea6e55eab36ab91b4c01593a98db1ed82beb61 Mon Sep 17 00:00:00 2001 From: Tim Niggemann Date: Tue, 3 Mar 2015 15:45:09 +0100 Subject: [PATCH 3/6] Added light ids to device config. Former-commit-id: 601616b0d7509b0e8ebb5226f00af5028fa905b5 --- libsrc/leddevice/LedDeviceFactory.cpp | 6 +++++- libsrc/leddevice/LedDevicePhilipsHue.cpp | 15 +++++++++++---- libsrc/leddevice/LedDevicePhilipsHue.h | 6 +++++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/libsrc/leddevice/LedDeviceFactory.cpp b/libsrc/leddevice/LedDeviceFactory.cpp index a3e49a4a..fca6bc5e 100755 --- a/libsrc/leddevice/LedDeviceFactory.cpp +++ b/libsrc/leddevice/LedDeviceFactory.cpp @@ -196,7 +196,11 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig) const std::string username = deviceConfig.get("username", "newdeveloper").asString(); const bool switchOffOnBlack = deviceConfig.get("switchOffOnBlack", true).asBool(); const int transitiontime = deviceConfig.get("transitiontime", 1).asInt(); - device = new LedDevicePhilipsHue(output, username, switchOffOnBlack, transitiontime); + std::vector lightIds; + for (size_t i = 0; i < deviceConfig["lightIds"].size(); i++) { + lightIds.push_back(deviceConfig["lightIds"][i].asInt()); + } + device = new LedDevicePhilipsHue(output, username, switchOffOnBlack, transitiontime, lightIds); } else if (type == "test") { diff --git a/libsrc/leddevice/LedDevicePhilipsHue.cpp b/libsrc/leddevice/LedDevicePhilipsHue.cpp index c8cfa339..226d5b48 100755 --- a/libsrc/leddevice/LedDevicePhilipsHue.cpp +++ b/libsrc/leddevice/LedDevicePhilipsHue.cpp @@ -133,9 +133,9 @@ CiColor PhilipsHueLamp::rgbToCiColor(float red, float green, float blue) { } LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output, const std::string& username, bool switchOffOnBlack, - int transitiontime) : + int transitiontime, std::vector lightIds) : host(output.c_str()), username(username.c_str()), switchOffOnBlack(switchOffOnBlack), transitiontime( - transitiontime) { + transitiontime), lightIds(lightIds) { http = new QHttp(host); timer.setInterval(3000); timer.setSingleShot(true); @@ -251,10 +251,17 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) { // Use json parser to parse reponse. Json::Reader reader; Json::FastWriter writer; + // Create light ids if none supplied by the user. + if (lightIds.size() != nLights) { + lightIds.clear(); + for (unsigned int i = 0; i < nLights; i++) { + lightIds.push_back(i + 1); + } + } // Iterate lights. for (unsigned int i = 0; i < nLights; i++) { // Read the response. - QByteArray response = get(getRoute(i + 1)); + QByteArray response = get(getRoute(lightIds.at(i))); // Parse JSON. Json::Value json; if (!reader.parse(QString(response).toStdString(), json)) { @@ -272,7 +279,7 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) { QString modelId = QString(writer.write(json["modelid"]).c_str()).trimmed().replace("\"", ""); QString originalState = QString(writer.write(state).c_str()).trimmed(); // Save state object. - lamps.push_back(PhilipsHueLamp(i + 1, originalState, modelId)); + lamps.push_back(PhilipsHueLamp(lightIds.at(i), originalState, modelId)); } } diff --git a/libsrc/leddevice/LedDevicePhilipsHue.h b/libsrc/leddevice/LedDevicePhilipsHue.h index 8a678505..aa95e4e0 100755 --- a/libsrc/leddevice/LedDevicePhilipsHue.h +++ b/libsrc/leddevice/LedDevicePhilipsHue.h @@ -131,8 +131,10 @@ public: /// /// @param transitiontime the time duration a light change takes in multiples of 100 ms (default: 400 ms). /// + /// @param lightIds light ids of the lights to control if not starting at one in ascending order. + /// LedDevicePhilipsHue(const std::string& output, const std::string& username = "newdeveloper", bool switchOffOnBlack = - false, int transitiontime = 1); + false, int transitiontime = 1, std::vector lightIds = {}); /// /// Destructor of this device @@ -171,6 +173,8 @@ private: /// Transition time in multiples of 100 ms. /// The default of the Hue lights will be 400 ms, but we want to have it snapier int transitiontime; + /// Array of the light ids. + std::vector lightIds; /// /// Sends a HTTP GET request (blocking). From b2b4e17bb820fa722bf5f397511143b296dfb53e Mon Sep 17 00:00:00 2001 From: Tim Niggemann Date: Tue, 3 Mar 2015 15:46:53 +0100 Subject: [PATCH 4/6] Renamed lamp to light to match HUE api convention. Former-commit-id: f9b37989291a32162a41a7549fe4b050b79daba2 --- libsrc/leddevice/LedDevicePhilipsHue.cpp | 32 ++++++++++++------------ libsrc/leddevice/LedDevicePhilipsHue.h | 8 +++--- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/libsrc/leddevice/LedDevicePhilipsHue.cpp b/libsrc/leddevice/LedDevicePhilipsHue.cpp index 226d5b48..a066f7c7 100755 --- a/libsrc/leddevice/LedDevicePhilipsHue.cpp +++ b/libsrc/leddevice/LedDevicePhilipsHue.cpp @@ -20,7 +20,7 @@ bool operator !=(CiColor p1, CiColor p2) { return !(p1 == p2); } -PhilipsHueLamp::PhilipsHueLamp(unsigned int id, QString originalState, QString modelId) : +PhilipsHueLight::PhilipsHueLight(unsigned int id, QString originalState, QString modelId) : id(id), originalState(originalState) { // Hue system model ids. const std::set HUE_BULBS_MODEL_IDS = { "LCT001", "LCT002", "LCT003" }; @@ -46,11 +46,11 @@ PhilipsHueLamp::PhilipsHueLamp(unsigned int id, QString originalState, QString m color = {black.x, black.y, black.bri}; } -float PhilipsHueLamp::crossProduct(CiColor p1, CiColor p2) { +float PhilipsHueLight::crossProduct(CiColor p1, CiColor p2) { return p1.x * p2.y - p1.y * p2.x; } -bool PhilipsHueLamp::isPointInLampsReach(CiColor p) { +bool PhilipsHueLight::isPointInLampsReach(CiColor p) { CiColor v1 = { colorSpace.green.x - colorSpace.red.x, colorSpace.green.y - colorSpace.red.y }; CiColor v2 = { colorSpace.blue.x - colorSpace.red.x, colorSpace.blue.y - colorSpace.red.y }; CiColor q = { p.x - colorSpace.red.x, p.y - colorSpace.red.y }; @@ -62,7 +62,7 @@ bool PhilipsHueLamp::isPointInLampsReach(CiColor p) { return false; } -CiColor PhilipsHueLamp::getClosestPointToPoint(CiColor a, CiColor b, CiColor p) { +CiColor PhilipsHueLight::getClosestPointToPoint(CiColor a, CiColor b, CiColor p) { CiColor AP = { p.x - a.x, p.y - a.y }; CiColor AB = { b.x - a.x, b.y - a.y }; float ab2 = AB.x * AB.x + AB.y * AB.y; @@ -76,7 +76,7 @@ CiColor PhilipsHueLamp::getClosestPointToPoint(CiColor a, CiColor b, CiColor p) return {a.x + AB.x * t, a.y + AB.y * t}; } -float PhilipsHueLamp::getDistanceBetweenTwoPoints(CiColor p1, CiColor p2) { +float PhilipsHueLight::getDistanceBetweenTwoPoints(CiColor p1, CiColor p2) { // Horizontal difference. float dx = p1.x - p2.x; // Vertical difference. @@ -85,7 +85,7 @@ float PhilipsHueLamp::getDistanceBetweenTwoPoints(CiColor p1, CiColor p2) { return sqrt(dx * dx + dy * dy); } -CiColor PhilipsHueLamp::rgbToCiColor(float red, float green, float blue) { +CiColor PhilipsHueLight::rgbToCiColor(float red, float green, float blue) { // Apply gamma correction. float r = (red > 0.04045f) ? powf((red + 0.055f) / (1.0f + 0.055f), 2.4f) : (red / 12.92f); float g = (green > 0.04045f) ? powf((green + 0.055f) / (1.0f + 0.055f), 2.4f) : (green / 12.92f); @@ -153,7 +153,7 @@ int LedDevicePhilipsHue::write(const std::vector & ledValues) { switchOn((unsigned int) ledValues.size()); } // If there are less states saved than colors given, then maybe something went wrong before. - if (lamps.size() != ledValues.size()) { + if (lights.size() != ledValues.size()) { restoreStates(); return 0; } @@ -161,7 +161,7 @@ int LedDevicePhilipsHue::write(const std::vector & ledValues) { unsigned int idx = 0; for (const ColorRgb& color : ledValues) { // Get lamp. - PhilipsHueLamp& lamp = lamps.at(idx); + PhilipsHueLight& lamp = lights.at(idx); // Scale colors from [0, 255] to [0, 1] and convert to xy space. CiColor xy = lamp.rgbToCiColor(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f); // Write color if color has been changed. @@ -247,7 +247,7 @@ QString LedDevicePhilipsHue::getRoute(unsigned int lightId) { void LedDevicePhilipsHue::saveStates(unsigned int nLights) { // Clear saved lamps. - lamps.clear(); + lights.clear(); // Use json parser to parse reponse. Json::Reader reader; Json::FastWriter writer; @@ -279,24 +279,24 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) { QString modelId = QString(writer.write(json["modelid"]).c_str()).trimmed().replace("\"", ""); QString originalState = QString(writer.write(state).c_str()).trimmed(); // Save state object. - lamps.push_back(PhilipsHueLamp(lightIds.at(i), originalState, modelId)); + lights.push_back(PhilipsHueLight(lightIds.at(i), originalState, modelId)); } } void LedDevicePhilipsHue::switchOn(unsigned int nLights) { - for (PhilipsHueLamp lamp : lamps) { - put(getStateRoute(lamp.id), "{\"on\": true}"); + for (PhilipsHueLight light : lights) { + put(getStateRoute(light.id), "{\"on\": true}"); } } void LedDevicePhilipsHue::restoreStates() { - for (PhilipsHueLamp lamp : lamps) { - put(getStateRoute(lamp.id), lamp.originalState); + for (PhilipsHueLight light : lights) { + put(getStateRoute(light.id), light.originalState); } // Clear saved light states. - lamps.clear(); + lights.clear(); } bool LedDevicePhilipsHue::areStatesSaved() { - return !lamps.empty(); + return !lights.empty(); } diff --git a/libsrc/leddevice/LedDevicePhilipsHue.h b/libsrc/leddevice/LedDevicePhilipsHue.h index aa95e4e0..17d78618 100755 --- a/libsrc/leddevice/LedDevicePhilipsHue.h +++ b/libsrc/leddevice/LedDevicePhilipsHue.h @@ -37,7 +37,7 @@ struct CiColorTriangle { /** * Simple class to hold the id, the latest color, the color space and the original state. */ -class PhilipsHueLamp { +class PhilipsHueLight { public: unsigned int id; CiColor black; @@ -46,7 +46,7 @@ public: QString originalState; /// - /// Constructs the lamp. + /// Constructs the light. /// /// @param id the light id /// @@ -54,7 +54,7 @@ public: /// /// @param modelId the model id of the hue lamp which is used to determine the color space /// - PhilipsHueLamp(unsigned int id, QString originalState, QString modelId); + PhilipsHueLight(unsigned int id, QString originalState, QString modelId); /// /// Converts an RGB color to the Hue xy color space and brightness. @@ -159,7 +159,7 @@ private slots: private: /// Array to save the lamps. - std::vector lamps; + std::vector lights; /// Ip address of the bridge QString host; /// User name for the API ("newdeveloper") From 72929223094a1ee8cf0c7bae158378112f9f137d Mon Sep 17 00:00:00 2001 From: Tim Niggemann Date: Tue, 3 Mar 2015 15:54:50 +0100 Subject: [PATCH 5/6] Improved color space checks by adding latest model ids. Former-commit-id: db3b067eabeaf36a2a9ab329614892e79d5089c4 --- libsrc/leddevice/LedDevicePhilipsHue.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/libsrc/leddevice/LedDevicePhilipsHue.cpp b/libsrc/leddevice/LedDevicePhilipsHue.cpp index a066f7c7..c65391f6 100755 --- a/libsrc/leddevice/LedDevicePhilipsHue.cpp +++ b/libsrc/leddevice/LedDevicePhilipsHue.cpp @@ -22,19 +22,21 @@ bool operator !=(CiColor p1, CiColor p2) { PhilipsHueLight::PhilipsHueLight(unsigned int id, QString originalState, QString modelId) : id(id), originalState(originalState) { - // Hue system model ids. - const std::set HUE_BULBS_MODEL_IDS = { "LCT001", "LCT002", "LCT003" }; - const std::set LIVING_COLORS_MODEL_IDS = { "LLC001", "LLC005", "LLC006", "LLC007", "LLC011", "LLC012", + // Hue system model ids (http://www.developers.meethue.com/documentation/supported-lights). + // Light strips, color iris, ... + const std::set GAMUT_A_MODEL_IDS = { "LLC001", "LLC005", "LLC006", "LLC007", "LLC010", "LLC011", "LLC012", "LLC013", "LST001" }; + // Hue bulbs, spots, ... + const std::set GAMUT_B_MODEL_IDS = { "LCT001", "LCT002", "LCT003", "LLM001" }; // Find id in the sets and set the appropiate color space. - if (HUE_BULBS_MODEL_IDS.find(modelId) != HUE_BULBS_MODEL_IDS.end()) { + if (GAMUT_A_MODEL_IDS.find(modelId) != GAMUT_A_MODEL_IDS.end()) { + colorSpace.red = {0.703f, 0.296f}; + colorSpace.green = {0.2151f, 0.7106f}; + colorSpace.blue = {0.138f, 0.08f}; + } else if (GAMUT_B_MODEL_IDS.find(modelId) != GAMUT_B_MODEL_IDS.end()) { colorSpace.red = {0.675f, 0.322f}; colorSpace.green = {0.4091f, 0.518f}; colorSpace.blue = {0.167f, 0.04f}; - } else if (LIVING_COLORS_MODEL_IDS.find(modelId) != LIVING_COLORS_MODEL_IDS.end()) { - colorSpace.red = {0.703f, 0.296f}; - colorSpace.green = {0.214f, 0.709f}; - colorSpace.blue = {0.139f, 0.081f}; } else { colorSpace.red = {1.0f, 0.0f}; colorSpace.green = {0.0f, 1.0f}; From f83802f2b181307574b1a00e439e1505d155f711 Mon Sep 17 00:00:00 2001 From: ntim Date: Sat, 7 Mar 2015 09:58:48 +0100 Subject: [PATCH 6/6] Corrected type of json array value index Former-commit-id: f766365fc3be4d1b62c36ce05e8a6d818f73dede --- libsrc/leddevice/LedDeviceFactory.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libsrc/leddevice/LedDeviceFactory.cpp b/libsrc/leddevice/LedDeviceFactory.cpp index fca6bc5e..55028fde 100755 --- a/libsrc/leddevice/LedDeviceFactory.cpp +++ b/libsrc/leddevice/LedDeviceFactory.cpp @@ -197,7 +197,7 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig) const bool switchOffOnBlack = deviceConfig.get("switchOffOnBlack", true).asBool(); const int transitiontime = deviceConfig.get("transitiontime", 1).asInt(); std::vector lightIds; - for (size_t i = 0; i < deviceConfig["lightIds"].size(); i++) { + for (Json::Value::ArrayIndex i = 0; i < deviceConfig["lightIds"].size(); i++) { lightIds.push_back(deviceConfig["lightIds"][i].asInt()); } device = new LedDevicePhilipsHue(output, username, switchOffOnBlack, transitiontime, lightIds);