@@ -20,21 +20,23 @@ bool operator !=(CiColor p1, CiColor p2) {
20
20
return !(p1 == p2);
21
21
}
22
22
23
- PhilipsHueLamp::PhilipsHueLamp (unsigned int id, QString originalState, QString modelId) :
23
+ PhilipsHueLight::PhilipsHueLight (unsigned int id, QString originalState, QString modelId) :
24
24
id(id), originalState(originalState) {
25
- // Hue system model ids.
26
- const std::set<QString> HUE_BULBS_MODEL_IDS = { " LCT001 " , " LCT002 " , " LCT003 " };
27
- const std::set<QString> LIVING_COLORS_MODEL_IDS = { " LLC001" , " LLC005" , " LLC006" , " LLC007" , " LLC011" , " LLC012" ,
25
+ // Hue system model ids (http://www.developers.meethue.com/documentation/supported-lights) .
26
+ // Light strips, color iris, ...
27
+ const std::set<QString> GAMUT_A_MODEL_IDS = { " LLC001" , " LLC005" , " LLC006" , " LLC007" , " LLC010 " , " LLC011" , " LLC012" ,
28
28
" LLC013" , " LST001" };
29
+ // Hue bulbs, spots, ...
30
+ const std::set<QString> GAMUT_B_MODEL_IDS = { " LCT001" , " LCT002" , " LCT003" , " LLM001" };
29
31
// Find id in the sets and set the appropiate color space.
30
- if (HUE_BULBS_MODEL_IDS.find (modelId) != HUE_BULBS_MODEL_IDS.end ()) {
32
+ if (GAMUT_A_MODEL_IDS.find (modelId) != GAMUT_A_MODEL_IDS.end ()) {
33
+ colorSpace.red = {0 .703f , 0 .296f };
34
+ colorSpace.green = {0 .2151f , 0 .7106f };
35
+ colorSpace.blue = {0 .138f , 0 .08f };
36
+ } else if (GAMUT_B_MODEL_IDS.find (modelId) != GAMUT_B_MODEL_IDS.end ()) {
31
37
colorSpace.red = {0 .675f , 0 .322f };
32
38
colorSpace.green = {0 .4091f , 0 .518f };
33
39
colorSpace.blue = {0 .167f , 0 .04f };
34
- } else if (LIVING_COLORS_MODEL_IDS.find (modelId) != LIVING_COLORS_MODEL_IDS.end ()) {
35
- colorSpace.red = {0 .703f , 0 .296f };
36
- colorSpace.green = {0 .214f , 0 .709f };
37
- colorSpace.blue = {0 .139f , 0 .081f };
38
40
} else {
39
41
colorSpace.red = {1 .0f , 0 .0f };
40
42
colorSpace.green = {0 .0f , 1 .0f };
@@ -46,11 +48,11 @@ PhilipsHueLamp::PhilipsHueLamp(unsigned int id, QString originalState, QString m
46
48
color = {black.x , black.y , black.bri };
47
49
}
48
50
49
- float PhilipsHueLamp ::crossProduct (CiColor p1, CiColor p2) {
51
+ float PhilipsHueLight ::crossProduct (CiColor p1, CiColor p2) {
50
52
return p1.x * p2.y - p1.y * p2.x ;
51
53
}
52
54
53
- bool PhilipsHueLamp ::isPointInLampsReach (CiColor p) {
55
+ bool PhilipsHueLight ::isPointInLampsReach (CiColor p) {
54
56
CiColor v1 = { colorSpace.green .x - colorSpace.red .x , colorSpace.green .y - colorSpace.red .y };
55
57
CiColor v2 = { colorSpace.blue .x - colorSpace.red .x , colorSpace.blue .y - colorSpace.red .y };
56
58
CiColor q = { p.x - colorSpace.red .x , p.y - colorSpace.red .y };
@@ -62,7 +64,7 @@ bool PhilipsHueLamp::isPointInLampsReach(CiColor p) {
62
64
return false ;
63
65
}
64
66
65
- CiColor PhilipsHueLamp ::getClosestPointToPoint (CiColor a, CiColor b, CiColor p) {
67
+ CiColor PhilipsHueLight ::getClosestPointToPoint (CiColor a, CiColor b, CiColor p) {
66
68
CiColor AP = { p.x - a.x , p.y - a.y };
67
69
CiColor AB = { b.x - a.x , b.y - a.y };
68
70
float ab2 = AB.x * AB.x + AB.y * AB.y ;
@@ -76,7 +78,7 @@ CiColor PhilipsHueLamp::getClosestPointToPoint(CiColor a, CiColor b, CiColor p)
76
78
return {a.x + AB.x * t, a.y + AB.y * t};
77
79
}
78
80
79
- float PhilipsHueLamp ::getDistanceBetweenTwoPoints (CiColor p1, CiColor p2) {
81
+ float PhilipsHueLight ::getDistanceBetweenTwoPoints (CiColor p1, CiColor p2) {
80
82
// Horizontal difference.
81
83
float dx = p1.x - p2.x ;
82
84
// Vertical difference.
@@ -85,7 +87,7 @@ float PhilipsHueLamp::getDistanceBetweenTwoPoints(CiColor p1, CiColor p2) {
85
87
return sqrt (dx * dx + dy * dy);
86
88
}
87
89
88
- CiColor PhilipsHueLamp ::rgbToCiColor (float red, float green, float blue) {
90
+ CiColor PhilipsHueLight ::rgbToCiColor (float red, float green, float blue) {
89
91
// Apply gamma correction.
90
92
float r = (red > 0 .04045f ) ? powf ((red + 0 .055f ) / (1 .0f + 0 .055f ), 2 .4f ) : (red / 12 .92f );
91
93
float g = (green > 0 .04045f ) ? powf ((green + 0 .055f ) / (1 .0f + 0 .055f ), 2 .4f ) : (green / 12 .92f );
@@ -133,9 +135,9 @@ CiColor PhilipsHueLamp::rgbToCiColor(float red, float green, float blue) {
133
135
}
134
136
135
137
LedDevicePhilipsHue::LedDevicePhilipsHue (const std::string& output, const std::string& username, bool switchOffOnBlack,
136
- int transitiontime) :
138
+ int transitiontime, std::vector< unsigned int > lightIds ) :
137
139
host(output.c_str()), username(username.c_str()), switchOffOnBlack(switchOffOnBlack), transitiontime(
138
- transitiontime) {
140
+ transitiontime), lightIds(lightIds) {
139
141
http = new QHttp (host);
140
142
timer.setInterval (3000 );
141
143
timer.setSingleShot (true );
@@ -153,15 +155,15 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
153
155
switchOn ((unsigned int ) ledValues.size ());
154
156
}
155
157
// If there are less states saved than colors given, then maybe something went wrong before.
156
- if (lamps .size () != ledValues.size ()) {
158
+ if (lights .size () != ledValues.size ()) {
157
159
restoreStates ();
158
160
return 0 ;
159
161
}
160
162
// Iterate through colors and set light states.
161
163
unsigned int idx = 0 ;
162
164
for (const ColorRgb& color : ledValues) {
163
165
// Get lamp.
164
- PhilipsHueLamp & lamp = lamps .at (idx);
166
+ PhilipsHueLight & lamp = lights .at (idx);
165
167
// Scale colors from [0, 255] to [0, 1] and convert to xy space.
166
168
CiColor xy = lamp.rgbToCiColor (color.red / 255 .0f , color.green / 255 .0f , color.blue / 255 .0f );
167
169
// Write color if color has been changed.
@@ -247,14 +249,21 @@ QString LedDevicePhilipsHue::getRoute(unsigned int lightId) {
247
249
248
250
void LedDevicePhilipsHue::saveStates (unsigned int nLights) {
249
251
// Clear saved lamps.
250
- lamps .clear ();
252
+ lights .clear ();
251
253
// Use json parser to parse reponse.
252
254
Json::Reader reader;
253
255
Json::FastWriter writer;
256
+ // Create light ids if none supplied by the user.
257
+ if (lightIds.size () != nLights) {
258
+ lightIds.clear ();
259
+ for (unsigned int i = 0 ; i < nLights; i++) {
260
+ lightIds.push_back (i + 1 );
261
+ }
262
+ }
254
263
// Iterate lights.
255
264
for (unsigned int i = 0 ; i < nLights; i++) {
256
265
// Read the response.
257
- QByteArray response = get (getRoute (i + 1 ));
266
+ QByteArray response = get (getRoute (lightIds. at (i) ));
258
267
// Parse JSON.
259
268
Json::Value json;
260
269
if (!reader.parse (QString (response).toStdString (), json)) {
@@ -272,24 +281,24 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
272
281
QString modelId = QString (writer.write (json[" modelid" ]).c_str ()).trimmed ().replace (" \" " , " " );
273
282
QString originalState = QString (writer.write (state).c_str ()).trimmed ();
274
283
// Save state object.
275
- lamps .push_back (PhilipsHueLamp (i + 1 , originalState, modelId));
284
+ lights .push_back (PhilipsHueLight (lightIds. at (i) , originalState, modelId));
276
285
}
277
286
}
278
287
279
288
void LedDevicePhilipsHue::switchOn (unsigned int nLights) {
280
- for (PhilipsHueLamp lamp : lamps ) {
281
- put (getStateRoute (lamp .id ), " {\" on\" : true}" );
289
+ for (PhilipsHueLight light : lights ) {
290
+ put (getStateRoute (light .id ), " {\" on\" : true}" );
282
291
}
283
292
}
284
293
285
294
void LedDevicePhilipsHue::restoreStates () {
286
- for (PhilipsHueLamp lamp : lamps ) {
287
- put (getStateRoute (lamp .id ), lamp .originalState );
295
+ for (PhilipsHueLight light : lights ) {
296
+ put (getStateRoute (light .id ), light .originalState );
288
297
}
289
298
// Clear saved light states.
290
- lamps .clear ();
299
+ lights .clear ();
291
300
}
292
301
293
302
bool LedDevicePhilipsHue::areStatesSaved () {
294
- return !lamps .empty ();
303
+ return !lights .empty ();
295
304
}
0 commit comments