11
11
#include < QHttpRequestHeader>
12
12
#include < QEventLoop>
13
13
14
- LedDevicePhilipsHue::LedDevicePhilipsHue (const std::string & output) :
14
+ LedDevicePhilipsHue::LedDevicePhilipsHue (const std::string& output) :
15
15
host(output.c_str()), username(" newdeveloper" ) {
16
16
http = new QHttp (host);
17
- /* timer.setInterval(3000);
17
+ timer.setInterval (3000 );
18
18
timer.setSingleShot (true );
19
- connect(&timer, SIGNAL(timeout()), this, SLOT(restoreStates()));*/
19
+ connect (&timer, SIGNAL (timeout ()), this , SLOT (restoreStates ()));
20
20
}
21
21
22
22
LedDevicePhilipsHue::~LedDevicePhilipsHue () {
23
23
delete http;
24
24
}
25
25
26
- int LedDevicePhilipsHue::write (const std::vector<ColorRgb> &ledValues) {
26
+ int LedDevicePhilipsHue::write (const std::vector<ColorRgb> & ledValues) {
27
27
// Save light states if not done before.
28
28
if (!statesSaved ())
29
29
saveStates (ledValues.size ());
30
30
// Iterate through colors and set light states.
31
31
unsigned int lightId = 0 ;
32
32
for (const ColorRgb &color : ledValues) {
33
- lightId++;
34
33
// Send only request to the brigde if color changed (prevents DDOS --> 503)
35
34
if (!oldLedValues.empty ())
36
- if (!hasColorChanged (lightId, &color))
35
+ if (!hasColorChanged (lightId, &color)) {
36
+ lightId++;
37
37
continue ;
38
+ }
38
39
39
40
float r = color.red / 255 .0f ;
40
41
float g = color.green / 255 .0f ;
41
42
float b = color.blue / 255 .0f ;
42
43
43
44
// 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 ()) {
45
46
Red = {0 .675f , 0 .322f };
46
47
Green = {0 .4091f , 0 .518f };
47
48
Blue = {0 .167f , 0 .04f };
48
49
} else if (std::find (livingColors.begin (),
49
- livingColors.end (), modelIds[( lightId - 1 ) ]) != livingColors.end ()) {
50
+ livingColors.end (), modelIds[lightId]) != livingColors.end ()) {
50
51
Red = {0 .703f , 0 .296f };
51
52
Green = {0 .214f , 0 .709f };
52
53
Blue = {0 .139f , 0 .081f };
@@ -58,28 +59,30 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> &ledValues) {
58
59
// if color equal black, switch off lamp ...
59
60
if (r == 0 .0f && g == 0 .0f && b == 0 .0f ) {
60
61
switchLampOff (lightId);
62
+ lightId++;
61
63
continue ;
62
64
}
63
65
// ... and if lamp off, switch on
64
- if (!checkOnStatus (states[( lightId - 1 ) ]))
66
+ if (!checkOnStatus (states[lightId]))
65
67
switchLampOn (lightId);
66
68
67
69
float bri;
68
- CGPoint p = CGPointMake ( 0 , 0 ) ;
70
+ CGPoint p = { 0 . 0f , 0 . 0f } ;
69
71
// 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);
71
73
// Send adjust color and brightness command in JSON format.
72
74
put (getStateRoute (lightId),
73
75
QString (" {\" xy\" : [%1, %2], \" bri\" : %3}" ).arg (p.x ).arg (p.y ).arg (qRound (b * 255 .0f )));
76
+ lightId++;
74
77
}
75
78
oldLedValues = ledValues;
76
- // timer.start();
79
+ timer.start ();
77
80
return 0 ;
78
81
}
79
82
80
83
bool LedDevicePhilipsHue::hasColorChanged (unsigned int lightId, const ColorRgb *color) {
81
84
bool matchFound = true ;
82
- const ColorRgb &tmpOldColor = oldLedValues[( lightId - 1 ) ];
85
+ const ColorRgb &tmpOldColor = oldLedValues[lightId];
83
86
if ((*color).red == tmpOldColor.red )
84
87
matchFound = false ;
85
88
if (!matchFound && (*color).green == tmpOldColor.green )
@@ -95,7 +98,7 @@ bool LedDevicePhilipsHue::hasColorChanged(unsigned int lightId, const ColorRgb *
95
98
}
96
99
97
100
int LedDevicePhilipsHue::switchOff () {
98
- // timer.stop();
101
+ timer.stop ();
99
102
// If light states have been saved before, ...
100
103
if (statesSaved ()) {
101
104
// ... restore them.
@@ -122,7 +125,6 @@ void LedDevicePhilipsHue::put(QString route, QString content) {
122
125
http->request (header, content.toAscii ());
123
126
// Go into the loop until the request is finished.
124
127
loop.exec ();
125
- // std::cout << http->readAll().data() << std::endl;
126
128
}
127
129
128
130
QByteArray LedDevicePhilipsHue::get (QString route) {
@@ -140,7 +142,7 @@ QByteArray LedDevicePhilipsHue::get(QString route) {
140
142
}
141
143
142
144
QString LedDevicePhilipsHue::getStateRoute (unsigned int lightId) {
143
- return QString (" lights/%1/state" ).arg (lightId);
145
+ return QString (" lights/%1/state" ).arg (lightId + 1 );
144
146
}
145
147
146
148
QString LedDevicePhilipsHue::getRoute (unsigned int lightId) {
@@ -179,18 +181,20 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
179
181
180
182
void LedDevicePhilipsHue::switchLampOn (unsigned int lightId) {
181
183
put (getStateRoute (lightId), " {\" on\" : true}" );
182
- states[( lightId - 1 ) ].replace (" \" on\" :false" , " \" on\" :true" );
184
+ states[lightId].replace (" \" on\" :false" , " \" on\" :true" );
183
185
}
184
186
185
187
void LedDevicePhilipsHue::switchLampOff (unsigned int lightId) {
186
188
put (getStateRoute (lightId), " {\" on\" : false}" );
187
- states[( lightId - 1 ) ].replace (" \" on\" :true" , " \" on\" :false" );
189
+ states[lightId].replace (" \" on\" :true" , " \" on\" :false" );
188
190
}
189
191
190
192
void LedDevicePhilipsHue::restoreStates () {
191
- unsigned int lightId = 1 ;
193
+ unsigned int lightId = 0 ;
192
194
for (QString state : states) {
193
- put (getStateRoute (lightId), state);
195
+ if (!checkOnStatus (states[lightId]))
196
+ switchLampOn (lightId);
197
+ put (getStateRoute (lightId), states[lightId]);
194
198
lightId++;
195
199
}
196
200
// Clear saved light states.
@@ -203,23 +207,15 @@ bool LedDevicePhilipsHue::statesSaved() {
203
207
return !states.empty ();
204
208
}
205
209
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) {
215
211
return (p1.x * p2.y - p1.y * p2.x );
216
212
}
217
213
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 } ;
221
217
222
- CGPoint q = CGPointMake ( p.x - Red.x , p.y - Red.y ) ;
218
+ CGPoint q = { p.x - Red.x , p.y - Red.y } ;
223
219
224
220
float s = CrossProduct (q, v2) / CrossProduct (v1, v2);
225
221
float t = CrossProduct (v1, q) / CrossProduct (v1, v2);
@@ -229,9 +225,9 @@ bool LedDevicePhilipsHue::CheckPointInLampsReach(CGPoint p) {
229
225
return false ;
230
226
}
231
227
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 } ;
235
231
float ab2 = AB.x * AB.x + AB.y * AB.y ;
236
232
float ap_ab = AP.x * AB.x + AP.y * AB.y ;
237
233
@@ -242,18 +238,18 @@ CGPoint LedDevicePhilipsHue::GetClosestPointToPoint(CGPoint A, CGPoint B, CGPoin
242
238
else if (t > 1 .0f )
243
239
t = 1 .0f ;
244
240
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} ;
246
242
}
247
243
248
- float LedDevicePhilipsHue::GetDistanceBetweenTwoPoints (CGPoint one, CGPoint two) {
244
+ float LedDevicePhilipsHue::GetDistanceBetweenTwoPoints (CGPoint& one, CGPoint& two) {
249
245
float dx = one.x - two.x ; // horizontal difference
250
246
float dy = one.y - two.y ; // vertical difference
251
247
float dist = sqrt (dx * dx + dy * dy);
252
248
253
249
return dist;
254
250
}
255
251
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) {
257
253
// Apply gamma correction.
258
254
float r = (red > 0 .04045f ) ? powf ((red + 0 .055f ) / (1 .0f + 0 .055f ), 2 .4f ) : (red / 12 .92f );
259
255
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,
271
267
if (isnan (cy))
272
268
cy = 0 .0f ;
273
269
274
- (* xyPoint) .x = cx;
275
- (* xyPoint) .y = cy;
270
+ xyPoint.x = cx;
271
+ xyPoint.y = cy;
276
272
277
273
// Check if the given XY value is within the colourreach of our lamps.
278
- bool inReachOfLamps = CheckPointInLampsReach (* xyPoint);
274
+ bool inReachOfLamps = CheckPointInLampsReach (xyPoint);
279
275
280
276
if (!inReachOfLamps) {
281
277
// It seems the colour is out of reach
282
278
// let's find the closes colour we can produce with our lamp and send this XY value out.
283
279
284
280
// 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);
288
284
289
285
// 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);
293
289
294
290
float lowest = dAB;
295
291
CGPoint closestPoint = pAB;
@@ -304,8 +300,8 @@ void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue,
304
300
}
305
301
306
302
// 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 ;
309
305
}
310
306
311
307
// Brightness is simply Y in the XYZ space.
0 commit comments