From e6d39b047cc6bff6c3b526f3ae48a935f1c58f6a Mon Sep 17 00:00:00 2001 From: bimsarck Date: Sat, 12 Jul 2014 14:56:39 +0200 Subject: [PATCH 1/2] Remove unnecessary code - reenable timer Former-commit-id: b1a175a1f1e5c3a7da753240030815252b1b22bb --- libsrc/leddevice/LedDevicePhilipsHue.cpp | 67 ++++++++++-------------- libsrc/leddevice/LedDevicePhilipsHue.h | 17 +++--- 2 files changed, 37 insertions(+), 47 deletions(-) diff --git a/libsrc/leddevice/LedDevicePhilipsHue.cpp b/libsrc/leddevice/LedDevicePhilipsHue.cpp index ed549b35..1fb89e6e 100755 --- a/libsrc/leddevice/LedDevicePhilipsHue.cpp +++ b/libsrc/leddevice/LedDevicePhilipsHue.cpp @@ -14,16 +14,16 @@ LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string &output) : host(output.c_str()), username("newdeveloper") { http = new QHttp(host); -/* timer.setInterval(3000); + timer.setInterval(3000); timer.setSingleShot(true); - connect(&timer, SIGNAL(timeout()), this, SLOT(restoreStates()));*/ + connect(&timer, SIGNAL(timeout()), this, SLOT(restoreStates())); } LedDevicePhilipsHue::~LedDevicePhilipsHue() { delete http; } -int LedDevicePhilipsHue::write(const std::vector &ledValues) { +int LedDevicePhilipsHue::write(const std::vector & ledValues) { // Save light states if not done before. if (!statesSaved()) saveStates(ledValues.size()); @@ -65,15 +65,15 @@ int LedDevicePhilipsHue::write(const std::vector &ledValues) { switchLampOn(lightId); float bri; - CGPoint p = CGPointMake(0, 0); + CGPoint p = {0.0f, 0.0f}; // Scale colors from [0, 255] to [0, 1] and convert to xy space. - rgbToXYBrightness(r, g, b, &p, bri); + rgbToXYBrightness(r, g, b, p, bri); // Send adjust color and brightness command in JSON format. put(getStateRoute(lightId), QString("{\"xy\": [%1, %2], \"bri\": %3}").arg(p.x).arg(p.y).arg(qRound(b * 255.0f))); } oldLedValues = ledValues; - //timer.start(); + timer.start(); return 0; } @@ -95,7 +95,7 @@ bool LedDevicePhilipsHue::hasColorChanged(unsigned int lightId, const ColorRgb * } int LedDevicePhilipsHue::switchOff() { - //timer.stop(); + timer.stop(); // If light states have been saved before, ... if (statesSaved()) { // ... restore them. @@ -122,7 +122,6 @@ void LedDevicePhilipsHue::put(QString route, QString content) { http->request(header, content.toAscii()); // Go into the loop until the request is finished. loop.exec(); - //std::cout << http->readAll().data() << std::endl; } QByteArray LedDevicePhilipsHue::get(QString route) { @@ -203,23 +202,15 @@ bool LedDevicePhilipsHue::statesSaved() { return !states.empty(); } -CGPoint LedDevicePhilipsHue::CGPointMake(float x, float y) { - CGPoint p; - p.x = x; - p.y = y; - - return p; -} - -float LedDevicePhilipsHue::CrossProduct(CGPoint p1, CGPoint p2) { +float LedDevicePhilipsHue::CrossProduct(CGPoint& p1, CGPoint& p2) { return (p1.x * p2.y - p1.y * p2.x); } -bool LedDevicePhilipsHue::CheckPointInLampsReach(CGPoint p) { - CGPoint v1 = CGPointMake(Green.x - Red.x, Green.y - Red.y); - CGPoint v2 = CGPointMake(Blue.x - Red.x, Blue.y - Red.y); +bool LedDevicePhilipsHue::CheckPointInLampsReach(CGPoint& p) { + CGPoint v1 = {Green.x - Red.x, Green.y - Red.y}; + CGPoint v2 = {Blue.x - Red.x, Blue.y - Red.y}; - CGPoint q = CGPointMake(p.x - Red.x, p.y - Red.y); + CGPoint q = {p.x - Red.x, p.y - Red.y}; float s = CrossProduct(q, v2) / CrossProduct(v1, v2); float t = CrossProduct(v1, q) / CrossProduct(v1, v2); @@ -229,9 +220,9 @@ bool LedDevicePhilipsHue::CheckPointInLampsReach(CGPoint p) { return false; } -CGPoint LedDevicePhilipsHue::GetClosestPointToPoint(CGPoint A, CGPoint B, CGPoint P) { - CGPoint AP = CGPointMake(P.x - A.x, P.y - A.y); - CGPoint AB = CGPointMake(B.x - A.x, B.y - A.y); +CGPoint LedDevicePhilipsHue::GetClosestPointToPoint(CGPoint& A, CGPoint& B, CGPoint& P) { + CGPoint AP = {P.x - A.x, P.y - A.y}; + CGPoint AB = {B.x - A.x, B.y - A.y}; float ab2 = AB.x * AB.x + AB.y * AB.y; float ap_ab = AP.x * AB.x + AP.y * AB.y; @@ -242,10 +233,10 @@ CGPoint LedDevicePhilipsHue::GetClosestPointToPoint(CGPoint A, CGPoint B, CGPoin else if (t > 1.0f) t = 1.0f; - return CGPointMake(A.x + AB.x * t, A.y + AB.y * t); + return {A.x + AB.x * t, A.y + AB.y * t}; } -float LedDevicePhilipsHue::GetDistanceBetweenTwoPoints(CGPoint one, CGPoint two) { +float LedDevicePhilipsHue::GetDistanceBetweenTwoPoints(CGPoint& one, CGPoint& two) { float dx = one.x - two.x; // horizontal difference float dy = one.y - two.y; // vertical difference float dist = sqrt(dx * dx + dy * dy); @@ -253,7 +244,7 @@ float LedDevicePhilipsHue::GetDistanceBetweenTwoPoints(CGPoint one, CGPoint two) return dist; } -void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue, CGPoint *xyPoint, float &brightness) { +void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue, CGPoint& xyPoint, float& brightness) { //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); @@ -271,25 +262,25 @@ void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue, if (isnan(cy)) cy = 0.0f; - (*xyPoint).x = cx; - (*xyPoint).y = cy; + xyPoint.x = cx; + xyPoint.y = cy; //Check if the given XY value is within the colourreach of our lamps. - bool inReachOfLamps = CheckPointInLampsReach(*xyPoint); + bool inReachOfLamps = CheckPointInLampsReach(xyPoint); if (!inReachOfLamps) { //It seems the colour is out of reach //let's find the closes colour we can produce with our lamp and send this XY value out. //Find the closest point on each line in the triangle. - CGPoint pAB = GetClosestPointToPoint(Red, Green, *xyPoint); - CGPoint pAC = GetClosestPointToPoint(Blue, Red, *xyPoint); - CGPoint pBC = GetClosestPointToPoint(Green, Blue, *xyPoint); + CGPoint pAB = GetClosestPointToPoint(Red, Green, xyPoint); + CGPoint pAC = GetClosestPointToPoint(Blue, Red, xyPoint); + CGPoint pBC = GetClosestPointToPoint(Green, Blue, xyPoint); //Get the distances per point and see which point is closer to our Point. - float dAB = GetDistanceBetweenTwoPoints(*xyPoint, pAB); - float dAC = GetDistanceBetweenTwoPoints(*xyPoint, pAC); - float dBC = GetDistanceBetweenTwoPoints(*xyPoint, pBC); + float dAB = GetDistanceBetweenTwoPoints(xyPoint, pAB); + float dAC = GetDistanceBetweenTwoPoints(xyPoint, pAC); + float dBC = GetDistanceBetweenTwoPoints(xyPoint, pBC); float lowest = dAB; CGPoint closestPoint = pAB; @@ -304,8 +295,8 @@ void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue, } //Change the xy value to a value which is within the reach of the lamp. - (*xyPoint).x = closestPoint.x; - (*xyPoint).y = closestPoint.y; + xyPoint.x = closestPoint.x; + xyPoint.y = closestPoint.y; } // Brightness is simply Y in the XYZ space. diff --git a/libsrc/leddevice/LedDevicePhilipsHue.h b/libsrc/leddevice/LedDevicePhilipsHue.h index 9760b7ad..11ec4abd 100755 --- a/libsrc/leddevice/LedDevicePhilipsHue.h +++ b/libsrc/leddevice/LedDevicePhilipsHue.h @@ -49,7 +49,7 @@ public: /// /// @return Zero on success else negative /// - virtual int write(const std::vector &ledValues); + virtual int write(const std::vector & ledValues); /// Restores the original state of the leds. virtual int switchOff(); @@ -59,18 +59,17 @@ private slots: void restoreStates(); private: - // ModelIds + /// Available modelIds const std::vector hueBulbs = {"LCT001", "LCT002", "LCT003"}; const std::vector livingColors = {"LLC001", "LLC005", "LLC006", "LLC007", "LLC011", "LLC012", "LLC013", "LST001"}; - /// LivingColors color gamut triangle + /// Color gamut triangle CGPoint Red , Green, Blue; - CGPoint CGPointMake(float x, float y); - float CrossProduct(CGPoint p1, CGPoint p2); - bool CheckPointInLampsReach(CGPoint p); - CGPoint GetClosestPointToPoint(CGPoint A, CGPoint B, CGPoint P); - float GetDistanceBetweenTwoPoints(CGPoint one, CGPoint two); + float CrossProduct(CGPoint& p1, CGPoint& p2); + bool CheckPointInLampsReach(CGPoint& p); + CGPoint GetClosestPointToPoint(CGPoint& A, CGPoint& B, CGPoint& P); + float GetDistanceBetweenTwoPoints(CGPoint& one, CGPoint& two); /// Array to save the light states. std::vector states; @@ -159,6 +158,6 @@ private: /// /// @param brightness converted brightness component /// - void rgbToXYBrightness(float red, float green, float blue, CGPoint *xyPoint, float &brightness); + void rgbToXYBrightness(float red, float green, float blue, CGPoint& xyPoint, float& brightness); }; From c1998da2ec91515c328d024649751c0e97083dee Mon Sep 17 00:00:00 2001 From: bimsarck Date: Sun, 13 Jul 2014 13:48:49 +0200 Subject: [PATCH 2/2] Fixes saveState feature Former-commit-id: 8158c77521727bf74875a5998c803212aece0645 --- libsrc/leddevice/LedDevicePhilipsHue.cpp | 29 ++++++++++++++---------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/libsrc/leddevice/LedDevicePhilipsHue.cpp b/libsrc/leddevice/LedDevicePhilipsHue.cpp index 1fb89e6e..6b837eb0 100755 --- a/libsrc/leddevice/LedDevicePhilipsHue.cpp +++ b/libsrc/leddevice/LedDevicePhilipsHue.cpp @@ -11,7 +11,7 @@ #include #include -LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string &output) : +LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output) : host(output.c_str()), username("newdeveloper") { http = new QHttp(host); timer.setInterval(3000); @@ -30,23 +30,24 @@ int LedDevicePhilipsHue::write(const std::vector & ledValues) { // Iterate through colors and set light states. unsigned int lightId = 0; for (const ColorRgb &color : ledValues) { - lightId++; // Send only request to the brigde if color changed (prevents DDOS --> 503) if (!oldLedValues.empty()) - if(!hasColorChanged(lightId, &color)) + if(!hasColorChanged(lightId, &color)) { + lightId++; continue; + } float r = color.red / 255.0f; float g = color.green / 255.0f; float b = color.blue / 255.0f; //set color gamut triangle - if(std::find(hueBulbs.begin(), hueBulbs.end(), modelIds[(lightId - 1)]) != hueBulbs.end()) { + if(std::find(hueBulbs.begin(), hueBulbs.end(), modelIds[lightId]) != hueBulbs.end()) { Red = {0.675f, 0.322f}; Green = {0.4091f, 0.518f}; Blue = {0.167f, 0.04f}; } else if (std::find(livingColors.begin(), - livingColors.end(), modelIds[(lightId - 1)]) != livingColors.end()) { + livingColors.end(), modelIds[lightId]) != livingColors.end()) { Red = {0.703f, 0.296f}; Green = {0.214f, 0.709f}; Blue = {0.139f, 0.081f}; @@ -58,10 +59,11 @@ int LedDevicePhilipsHue::write(const std::vector & ledValues) { // if color equal black, switch off lamp ... if (r == 0.0f && g == 0.0f && b == 0.0f) { switchLampOff(lightId); + lightId++; continue; } // ... and if lamp off, switch on - if (!checkOnStatus(states[(lightId - 1)])) + if (!checkOnStatus(states[lightId])) switchLampOn(lightId); float bri; @@ -71,6 +73,7 @@ int LedDevicePhilipsHue::write(const std::vector & ledValues) { // Send adjust color and brightness command in JSON format. put(getStateRoute(lightId), QString("{\"xy\": [%1, %2], \"bri\": %3}").arg(p.x).arg(p.y).arg(qRound(b * 255.0f))); + lightId++; } oldLedValues = ledValues; timer.start(); @@ -79,7 +82,7 @@ int LedDevicePhilipsHue::write(const std::vector & ledValues) { bool LedDevicePhilipsHue::hasColorChanged(unsigned int lightId, const ColorRgb *color) { bool matchFound = true; - const ColorRgb &tmpOldColor = oldLedValues[(lightId - 1)]; + const ColorRgb &tmpOldColor = oldLedValues[lightId]; if ((*color).red == tmpOldColor.red) matchFound = false; if (!matchFound && (*color).green == tmpOldColor.green) @@ -139,7 +142,7 @@ QByteArray LedDevicePhilipsHue::get(QString route) { } QString LedDevicePhilipsHue::getStateRoute(unsigned int lightId) { - return QString("lights/%1/state").arg(lightId); + return QString("lights/%1/state").arg(lightId + 1); } QString LedDevicePhilipsHue::getRoute(unsigned int lightId) { @@ -178,18 +181,20 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) { void LedDevicePhilipsHue::switchLampOn(unsigned int lightId) { put(getStateRoute(lightId), "{\"on\": true}"); - states[(lightId - 1)].replace("\"on\":false", "\"on\":true"); + states[lightId].replace("\"on\":false", "\"on\":true"); } void LedDevicePhilipsHue::switchLampOff(unsigned int lightId) { put(getStateRoute(lightId), "{\"on\": false}"); - states[(lightId - 1)].replace("\"on\":true", "\"on\":false"); + states[lightId].replace("\"on\":true", "\"on\":false"); } void LedDevicePhilipsHue::restoreStates() { - unsigned int lightId = 1; + unsigned int lightId = 0; for (QString state : states) { - put(getStateRoute(lightId), state); + if (!checkOnStatus(states[lightId])) + switchLampOn(lightId); + put(getStateRoute(lightId), states[lightId]); lightId++; } // Clear saved light states.