14
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 ());
@@ -65,15 +65,15 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> &ledValues) {
65
65
switchLampOn (lightId);
66
66
67
67
float bri;
68
- CGPoint p = CGPointMake ( 0 , 0 ) ;
68
+ CGPoint p = { 0 . 0f , 0 . 0f } ;
69
69
// Scale colors from [0, 255] to [0, 1] and convert to xy space.
70
- rgbToXYBrightness (r, g, b, & p, bri);
70
+ rgbToXYBrightness (r, g, b, p, bri);
71
71
// Send adjust color and brightness command in JSON format.
72
72
put (getStateRoute (lightId),
73
73
QString (" {\" xy\" : [%1, %2], \" bri\" : %3}" ).arg (p.x ).arg (p.y ).arg (qRound (b * 255 .0f )));
74
74
}
75
75
oldLedValues = ledValues;
76
- // timer.start();
76
+ timer.start ();
77
77
return 0 ;
78
78
}
79
79
@@ -95,7 +95,7 @@ bool LedDevicePhilipsHue::hasColorChanged(unsigned int lightId, const ColorRgb *
95
95
}
96
96
97
97
int LedDevicePhilipsHue::switchOff () {
98
- // timer.stop();
98
+ timer.stop ();
99
99
// If light states have been saved before, ...
100
100
if (statesSaved ()) {
101
101
// ... restore them.
@@ -122,7 +122,6 @@ void LedDevicePhilipsHue::put(QString route, QString content) {
122
122
http->request (header, content.toAscii ());
123
123
// Go into the loop until the request is finished.
124
124
loop.exec ();
125
- // std::cout << http->readAll().data() << std::endl;
126
125
}
127
126
128
127
QByteArray LedDevicePhilipsHue::get (QString route) {
@@ -203,23 +202,15 @@ bool LedDevicePhilipsHue::statesSaved() {
203
202
return !states.empty ();
204
203
}
205
204
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) {
205
+ float LedDevicePhilipsHue::CrossProduct (CGPoint& p1, CGPoint& p2) {
215
206
return (p1.x * p2.y - p1.y * p2.x );
216
207
}
217
208
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 ) ;
209
+ bool LedDevicePhilipsHue::CheckPointInLampsReach (CGPoint& p) {
210
+ CGPoint v1 = { Green.x - Red.x , Green.y - Red.y } ;
211
+ CGPoint v2 = { Blue.x - Red.x , Blue.y - Red.y } ;
221
212
222
- CGPoint q = CGPointMake ( p.x - Red.x , p.y - Red.y ) ;
213
+ CGPoint q = { p.x - Red.x , p.y - Red.y } ;
223
214
224
215
float s = CrossProduct (q, v2) / CrossProduct (v1, v2);
225
216
float t = CrossProduct (v1, q) / CrossProduct (v1, v2);
@@ -229,9 +220,9 @@ bool LedDevicePhilipsHue::CheckPointInLampsReach(CGPoint p) {
229
220
return false ;
230
221
}
231
222
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 ) ;
223
+ CGPoint LedDevicePhilipsHue::GetClosestPointToPoint (CGPoint& A, CGPoint& B, CGPoint& P) {
224
+ CGPoint AP = { P.x - A.x , P.y - A.y } ;
225
+ CGPoint AB = { B.x - A.x , B.y - A.y } ;
235
226
float ab2 = AB.x * AB.x + AB.y * AB.y ;
236
227
float ap_ab = AP.x * AB.x + AP.y * AB.y ;
237
228
@@ -242,18 +233,18 @@ CGPoint LedDevicePhilipsHue::GetClosestPointToPoint(CGPoint A, CGPoint B, CGPoin
242
233
else if (t > 1 .0f )
243
234
t = 1 .0f ;
244
235
245
- return CGPointMake ( A.x + AB.x * t, A.y + AB.y * t) ;
236
+ return { A.x + AB.x * t, A.y + AB.y * t} ;
246
237
}
247
238
248
- float LedDevicePhilipsHue::GetDistanceBetweenTwoPoints (CGPoint one, CGPoint two) {
239
+ float LedDevicePhilipsHue::GetDistanceBetweenTwoPoints (CGPoint& one, CGPoint& two) {
249
240
float dx = one.x - two.x ; // horizontal difference
250
241
float dy = one.y - two.y ; // vertical difference
251
242
float dist = sqrt (dx * dx + dy * dy);
252
243
253
244
return dist;
254
245
}
255
246
256
- void LedDevicePhilipsHue::rgbToXYBrightness (float red, float green, float blue, CGPoint * xyPoint, float & brightness) {
247
+ void LedDevicePhilipsHue::rgbToXYBrightness (float red, float green, float blue, CGPoint& xyPoint, float & brightness) {
257
248
// Apply gamma correction.
258
249
float r = (red > 0 .04045f ) ? powf ((red + 0 .055f ) / (1 .0f + 0 .055f ), 2 .4f ) : (red / 12 .92f );
259
250
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,
271
262
if (isnan (cy))
272
263
cy = 0 .0f ;
273
264
274
- (* xyPoint) .x = cx;
275
- (* xyPoint) .y = cy;
265
+ xyPoint.x = cx;
266
+ xyPoint.y = cy;
276
267
277
268
// Check if the given XY value is within the colourreach of our lamps.
278
- bool inReachOfLamps = CheckPointInLampsReach (* xyPoint);
269
+ bool inReachOfLamps = CheckPointInLampsReach (xyPoint);
279
270
280
271
if (!inReachOfLamps) {
281
272
// It seems the colour is out of reach
282
273
// let's find the closes colour we can produce with our lamp and send this XY value out.
283
274
284
275
// 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);
276
+ CGPoint pAB = GetClosestPointToPoint (Red, Green, xyPoint);
277
+ CGPoint pAC = GetClosestPointToPoint (Blue, Red, xyPoint);
278
+ CGPoint pBC = GetClosestPointToPoint (Green, Blue, xyPoint);
288
279
289
280
// 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);
281
+ float dAB = GetDistanceBetweenTwoPoints (xyPoint, pAB);
282
+ float dAC = GetDistanceBetweenTwoPoints (xyPoint, pAC);
283
+ float dBC = GetDistanceBetweenTwoPoints (xyPoint, pBC);
293
284
294
285
float lowest = dAB;
295
286
CGPoint closestPoint = pAB;
@@ -304,8 +295,8 @@ void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue,
304
295
}
305
296
306
297
// 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 ;
298
+ xyPoint.x = closestPoint.x ;
299
+ xyPoint.y = closestPoint.y ;
309
300
}
310
301
311
302
// Brightness is simply Y in the XYZ space.
0 commit comments