Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Commit 265b481

Browse files
committed
Merge pull request #130 from bimsarck/master
Remove unnecessary code
2 parents da26ec8 + 8158c77 commit 265b481

File tree

2 files changed

+54
-59
lines changed

2 files changed

+54
-59
lines changed

libsrc/leddevice/LedDevicePhilipsHue.cpp

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,43 @@
1111
#include <QHttpRequestHeader>
1212
#include <QEventLoop>
1313

14-
LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string &output) :
14+
LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output) :
1515
host(output.c_str()), username("newdeveloper") {
1616
http = new QHttp(host);
17-
/* timer.setInterval(3000);
17+
timer.setInterval(3000);
1818
timer.setSingleShot(true);
19-
connect(&timer, SIGNAL(timeout()), this, SLOT(restoreStates()));*/
19+
connect(&timer, SIGNAL(timeout()), this, SLOT(restoreStates()));
2020
}
2121

2222
LedDevicePhilipsHue::~LedDevicePhilipsHue() {
2323
delete http;
2424
}
2525

26-
int LedDevicePhilipsHue::write(const std::vector<ColorRgb> &ledValues) {
26+
int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
2727
// Save light states if not done before.
2828
if (!statesSaved())
2929
saveStates(ledValues.size());
3030
// Iterate through colors and set light states.
3131
unsigned int lightId = 0;
3232
for (const ColorRgb &color : ledValues) {
33-
lightId++;
3433
// Send only request to the brigde if color changed (prevents DDOS --> 503)
3534
if (!oldLedValues.empty())
36-
if(!hasColorChanged(lightId, &color))
35+
if(!hasColorChanged(lightId, &color)) {
36+
lightId++;
3737
continue;
38+
}
3839

3940
float r = color.red / 255.0f;
4041
float g = color.green / 255.0f;
4142
float b = color.blue / 255.0f;
4243

4344
//set color gamut triangle
44-
if(std::find(hueBulbs.begin(), hueBulbs.end(), modelIds[(lightId - 1)]) != hueBulbs.end()) {
45+
if(std::find(hueBulbs.begin(), hueBulbs.end(), modelIds[lightId]) != hueBulbs.end()) {
4546
Red = {0.675f, 0.322f};
4647
Green = {0.4091f, 0.518f};
4748
Blue = {0.167f, 0.04f};
4849
} else if (std::find(livingColors.begin(),
49-
livingColors.end(), modelIds[(lightId - 1)]) != livingColors.end()) {
50+
livingColors.end(), modelIds[lightId]) != livingColors.end()) {
5051
Red = {0.703f, 0.296f};
5152
Green = {0.214f, 0.709f};
5253
Blue = {0.139f, 0.081f};
@@ -58,28 +59,30 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> &ledValues) {
5859
// if color equal black, switch off lamp ...
5960
if (r == 0.0f && g == 0.0f && b == 0.0f) {
6061
switchLampOff(lightId);
62+
lightId++;
6163
continue;
6264
}
6365
// ... and if lamp off, switch on
64-
if (!checkOnStatus(states[(lightId - 1)]))
66+
if (!checkOnStatus(states[lightId]))
6567
switchLampOn(lightId);
6668

6769
float bri;
68-
CGPoint p = CGPointMake(0, 0);
70+
CGPoint p = {0.0f, 0.0f};
6971
// Scale colors from [0, 255] to [0, 1] and convert to xy space.
70-
rgbToXYBrightness(r, g, b, &p, bri);
72+
rgbToXYBrightness(r, g, b, p, bri);
7173
// Send adjust color and brightness command in JSON format.
7274
put(getStateRoute(lightId),
7375
QString("{\"xy\": [%1, %2], \"bri\": %3}").arg(p.x).arg(p.y).arg(qRound(b * 255.0f)));
76+
lightId++;
7477
}
7578
oldLedValues = ledValues;
76-
//timer.start();
79+
timer.start();
7780
return 0;
7881
}
7982

8083
bool LedDevicePhilipsHue::hasColorChanged(unsigned int lightId, const ColorRgb *color) {
8184
bool matchFound = true;
82-
const ColorRgb &tmpOldColor = oldLedValues[(lightId - 1)];
85+
const ColorRgb &tmpOldColor = oldLedValues[lightId];
8386
if ((*color).red == tmpOldColor.red)
8487
matchFound = false;
8588
if (!matchFound && (*color).green == tmpOldColor.green)
@@ -95,7 +98,7 @@ bool LedDevicePhilipsHue::hasColorChanged(unsigned int lightId, const ColorRgb *
9598
}
9699

97100
int LedDevicePhilipsHue::switchOff() {
98-
//timer.stop();
101+
timer.stop();
99102
// If light states have been saved before, ...
100103
if (statesSaved()) {
101104
// ... restore them.
@@ -122,7 +125,6 @@ void LedDevicePhilipsHue::put(QString route, QString content) {
122125
http->request(header, content.toAscii());
123126
// Go into the loop until the request is finished.
124127
loop.exec();
125-
//std::cout << http->readAll().data() << std::endl;
126128
}
127129

128130
QByteArray LedDevicePhilipsHue::get(QString route) {
@@ -140,7 +142,7 @@ QByteArray LedDevicePhilipsHue::get(QString route) {
140142
}
141143

142144
QString LedDevicePhilipsHue::getStateRoute(unsigned int lightId) {
143-
return QString("lights/%1/state").arg(lightId);
145+
return QString("lights/%1/state").arg(lightId + 1);
144146
}
145147

146148
QString LedDevicePhilipsHue::getRoute(unsigned int lightId) {
@@ -179,18 +181,20 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
179181

180182
void LedDevicePhilipsHue::switchLampOn(unsigned int lightId) {
181183
put(getStateRoute(lightId), "{\"on\": true}");
182-
states[(lightId - 1)].replace("\"on\":false", "\"on\":true");
184+
states[lightId].replace("\"on\":false", "\"on\":true");
183185
}
184186

185187
void LedDevicePhilipsHue::switchLampOff(unsigned int lightId) {
186188
put(getStateRoute(lightId), "{\"on\": false}");
187-
states[(lightId - 1)].replace("\"on\":true", "\"on\":false");
189+
states[lightId].replace("\"on\":true", "\"on\":false");
188190
}
189191

190192
void LedDevicePhilipsHue::restoreStates() {
191-
unsigned int lightId = 1;
193+
unsigned int lightId = 0;
192194
for (QString state : states) {
193-
put(getStateRoute(lightId), state);
195+
if (!checkOnStatus(states[lightId]))
196+
switchLampOn(lightId);
197+
put(getStateRoute(lightId), states[lightId]);
194198
lightId++;
195199
}
196200
// Clear saved light states.
@@ -203,23 +207,15 @@ bool LedDevicePhilipsHue::statesSaved() {
203207
return !states.empty();
204208
}
205209

206-
CGPoint LedDevicePhilipsHue::CGPointMake(float x, float y) {
207-
CGPoint p;
208-
p.x = x;
209-
p.y = y;
210-
211-
return p;
212-
}
213-
214-
float LedDevicePhilipsHue::CrossProduct(CGPoint p1, CGPoint p2) {
210+
float LedDevicePhilipsHue::CrossProduct(CGPoint& p1, CGPoint& p2) {
215211
return (p1.x * p2.y - p1.y * p2.x);
216212
}
217213

218-
bool LedDevicePhilipsHue::CheckPointInLampsReach(CGPoint p) {
219-
CGPoint v1 = CGPointMake(Green.x - Red.x, Green.y - Red.y);
220-
CGPoint v2 = CGPointMake(Blue.x - Red.x, Blue.y - Red.y);
214+
bool LedDevicePhilipsHue::CheckPointInLampsReach(CGPoint& p) {
215+
CGPoint v1 = {Green.x - Red.x, Green.y - Red.y};
216+
CGPoint v2 = {Blue.x - Red.x, Blue.y - Red.y};
221217

222-
CGPoint q = CGPointMake(p.x - Red.x, p.y - Red.y);
218+
CGPoint q = {p.x - Red.x, p.y - Red.y};
223219

224220
float s = CrossProduct(q, v2) / CrossProduct(v1, v2);
225221
float t = CrossProduct(v1, q) / CrossProduct(v1, v2);
@@ -229,9 +225,9 @@ bool LedDevicePhilipsHue::CheckPointInLampsReach(CGPoint p) {
229225
return false;
230226
}
231227

232-
CGPoint LedDevicePhilipsHue::GetClosestPointToPoint(CGPoint A, CGPoint B, CGPoint P) {
233-
CGPoint AP = CGPointMake(P.x - A.x, P.y - A.y);
234-
CGPoint AB = CGPointMake(B.x - A.x, B.y - A.y);
228+
CGPoint LedDevicePhilipsHue::GetClosestPointToPoint(CGPoint& A, CGPoint& B, CGPoint& P) {
229+
CGPoint AP = {P.x - A.x, P.y - A.y};
230+
CGPoint AB = {B.x - A.x, B.y - A.y};
235231
float ab2 = AB.x * AB.x + AB.y * AB.y;
236232
float ap_ab = AP.x * AB.x + AP.y * AB.y;
237233

@@ -242,18 +238,18 @@ CGPoint LedDevicePhilipsHue::GetClosestPointToPoint(CGPoint A, CGPoint B, CGPoin
242238
else if (t > 1.0f)
243239
t = 1.0f;
244240

245-
return CGPointMake(A.x + AB.x * t, A.y + AB.y * t);
241+
return {A.x + AB.x * t, A.y + AB.y * t};
246242
}
247243

248-
float LedDevicePhilipsHue::GetDistanceBetweenTwoPoints(CGPoint one, CGPoint two) {
244+
float LedDevicePhilipsHue::GetDistanceBetweenTwoPoints(CGPoint& one, CGPoint& two) {
249245
float dx = one.x - two.x; // horizontal difference
250246
float dy = one.y - two.y; // vertical difference
251247
float dist = sqrt(dx * dx + dy * dy);
252248

253249
return dist;
254250
}
255251

256-
void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue, CGPoint *xyPoint, float &brightness) {
252+
void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue, CGPoint& xyPoint, float& brightness) {
257253
//Apply gamma correction.
258254
float r = (red > 0.04045f) ? powf((red + 0.055f) / (1.0f + 0.055f), 2.4f) : (red / 12.92f);
259255
float g = (green > 0.04045f) ? powf((green + 0.055f) / (1.0f + 0.055f), 2.4f) : (green / 12.92f);
@@ -271,25 +267,25 @@ void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue,
271267
if (isnan(cy))
272268
cy = 0.0f;
273269

274-
(*xyPoint).x = cx;
275-
(*xyPoint).y = cy;
270+
xyPoint.x = cx;
271+
xyPoint.y = cy;
276272

277273
//Check if the given XY value is within the colourreach of our lamps.
278-
bool inReachOfLamps = CheckPointInLampsReach(*xyPoint);
274+
bool inReachOfLamps = CheckPointInLampsReach(xyPoint);
279275

280276
if (!inReachOfLamps) {
281277
//It seems the colour is out of reach
282278
//let's find the closes colour we can produce with our lamp and send this XY value out.
283279

284280
//Find the closest point on each line in the triangle.
285-
CGPoint pAB = GetClosestPointToPoint(Red, Green, *xyPoint);
286-
CGPoint pAC = GetClosestPointToPoint(Blue, Red, *xyPoint);
287-
CGPoint pBC = GetClosestPointToPoint(Green, Blue, *xyPoint);
281+
CGPoint pAB = GetClosestPointToPoint(Red, Green, xyPoint);
282+
CGPoint pAC = GetClosestPointToPoint(Blue, Red, xyPoint);
283+
CGPoint pBC = GetClosestPointToPoint(Green, Blue, xyPoint);
288284

289285
//Get the distances per point and see which point is closer to our Point.
290-
float dAB = GetDistanceBetweenTwoPoints(*xyPoint, pAB);
291-
float dAC = GetDistanceBetweenTwoPoints(*xyPoint, pAC);
292-
float dBC = GetDistanceBetweenTwoPoints(*xyPoint, pBC);
286+
float dAB = GetDistanceBetweenTwoPoints(xyPoint, pAB);
287+
float dAC = GetDistanceBetweenTwoPoints(xyPoint, pAC);
288+
float dBC = GetDistanceBetweenTwoPoints(xyPoint, pBC);
293289

294290
float lowest = dAB;
295291
CGPoint closestPoint = pAB;
@@ -304,8 +300,8 @@ void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue,
304300
}
305301

306302
//Change the xy value to a value which is within the reach of the lamp.
307-
(*xyPoint).x = closestPoint.x;
308-
(*xyPoint).y = closestPoint.y;
303+
xyPoint.x = closestPoint.x;
304+
xyPoint.y = closestPoint.y;
309305
}
310306

311307
// Brightness is simply Y in the XYZ space.

libsrc/leddevice/LedDevicePhilipsHue.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Q_OBJECT
4949
///
5050
/// @return Zero on success else negative
5151
///
52-
virtual int write(const std::vector<ColorRgb> &ledValues);
52+
virtual int write(const std::vector<ColorRgb> & ledValues);
5353

5454
/// Restores the original state of the leds.
5555
virtual int switchOff();
@@ -59,18 +59,17 @@ private slots:
5959
void restoreStates();
6060

6161
private:
62-
// ModelIds
62+
/// Available modelIds
6363
const std::vector<QString> hueBulbs = {"LCT001", "LCT002", "LCT003"};
6464
const std::vector<QString> livingColors = {"LLC001", "LLC005", "LLC006", "LLC007",
6565
"LLC011", "LLC012", "LLC013", "LST001"};
66-
/// LivingColors color gamut triangle
66+
/// Color gamut triangle
6767
CGPoint Red , Green, Blue;
6868

69-
CGPoint CGPointMake(float x, float y);
70-
float CrossProduct(CGPoint p1, CGPoint p2);
71-
bool CheckPointInLampsReach(CGPoint p);
72-
CGPoint GetClosestPointToPoint(CGPoint A, CGPoint B, CGPoint P);
73-
float GetDistanceBetweenTwoPoints(CGPoint one, CGPoint two);
69+
float CrossProduct(CGPoint& p1, CGPoint& p2);
70+
bool CheckPointInLampsReach(CGPoint& p);
71+
CGPoint GetClosestPointToPoint(CGPoint& A, CGPoint& B, CGPoint& P);
72+
float GetDistanceBetweenTwoPoints(CGPoint& one, CGPoint& two);
7473

7574
/// Array to save the light states.
7675
std::vector<QString> states;
@@ -159,6 +158,6 @@ private slots:
159158
///
160159
/// @param brightness converted brightness component
161160
///
162-
void rgbToXYBrightness(float red, float green, float blue, CGPoint *xyPoint, float &brightness);
161+
void rgbToXYBrightness(float red, float green, float blue, CGPoint& xyPoint, float& brightness);
163162

164163
};

0 commit comments

Comments
 (0)