@@ -20,21 +20,23 @@ bool operator !=(CiColor p1, CiColor p2) {
2020 return !(p1 == p2);
2121}
2222
23- PhilipsHueLamp::PhilipsHueLamp (unsigned int id, QString originalState, QString modelId) :
23+ PhilipsHueLight::PhilipsHueLight (unsigned int id, QString originalState, QString modelId) :
2424 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" ,
2828 " LLC013" , " LST001" };
29+ // Hue bulbs, spots, ...
30+ const std::set<QString> GAMUT_B_MODEL_IDS = { " LCT001" , " LCT002" , " LCT003" , " LLM001" };
2931 // 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 ()) {
3137 colorSpace.red = {0 .675f , 0 .322f };
3238 colorSpace.green = {0 .4091f , 0 .518f };
3339 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 };
3840 } else {
3941 colorSpace.red = {1 .0f , 0 .0f };
4042 colorSpace.green = {0 .0f , 1 .0f };
@@ -46,11 +48,11 @@ PhilipsHueLamp::PhilipsHueLamp(unsigned int id, QString originalState, QString m
4648 color = {black.x , black.y , black.bri };
4749}
4850
49- float PhilipsHueLamp ::crossProduct (CiColor p1, CiColor p2) {
51+ float PhilipsHueLight ::crossProduct (CiColor p1, CiColor p2) {
5052 return p1.x * p2.y - p1.y * p2.x ;
5153}
5254
53- bool PhilipsHueLamp ::isPointInLampsReach (CiColor p) {
55+ bool PhilipsHueLight ::isPointInLampsReach (CiColor p) {
5456 CiColor v1 = { colorSpace.green .x - colorSpace.red .x , colorSpace.green .y - colorSpace.red .y };
5557 CiColor v2 = { colorSpace.blue .x - colorSpace.red .x , colorSpace.blue .y - colorSpace.red .y };
5658 CiColor q = { p.x - colorSpace.red .x , p.y - colorSpace.red .y };
@@ -62,7 +64,7 @@ bool PhilipsHueLamp::isPointInLampsReach(CiColor p) {
6264 return false ;
6365}
6466
65- CiColor PhilipsHueLamp ::getClosestPointToPoint (CiColor a, CiColor b, CiColor p) {
67+ CiColor PhilipsHueLight ::getClosestPointToPoint (CiColor a, CiColor b, CiColor p) {
6668 CiColor AP = { p.x - a.x , p.y - a.y };
6769 CiColor AB = { b.x - a.x , b.y - a.y };
6870 float ab2 = AB.x * AB.x + AB.y * AB.y ;
@@ -76,7 +78,7 @@ CiColor PhilipsHueLamp::getClosestPointToPoint(CiColor a, CiColor b, CiColor p)
7678 return {a.x + AB.x * t, a.y + AB.y * t};
7779}
7880
79- float PhilipsHueLamp ::getDistanceBetweenTwoPoints (CiColor p1, CiColor p2) {
81+ float PhilipsHueLight ::getDistanceBetweenTwoPoints (CiColor p1, CiColor p2) {
8082 // Horizontal difference.
8183 float dx = p1.x - p2.x ;
8284 // Vertical difference.
@@ -85,7 +87,7 @@ float PhilipsHueLamp::getDistanceBetweenTwoPoints(CiColor p1, CiColor p2) {
8587 return sqrt (dx * dx + dy * dy);
8688}
8789
88- CiColor PhilipsHueLamp ::rgbToCiColor (float red, float green, float blue) {
90+ CiColor PhilipsHueLight ::rgbToCiColor (float red, float green, float blue) {
8991 // Apply gamma correction.
9092 float r = (red > 0 .04045f ) ? powf ((red + 0 .055f ) / (1 .0f + 0 .055f ), 2 .4f ) : (red / 12 .92f );
9193 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) {
133135}
134136
135137LedDevicePhilipsHue::LedDevicePhilipsHue (const std::string& output, const std::string& username, bool switchOffOnBlack,
136- int transitiontime) :
138+ int transitiontime, std::vector< unsigned int > lightIds ) :
137139 host(output.c_str()), username(username.c_str()), switchOffOnBlack(switchOffOnBlack), transitiontime(
138- transitiontime) {
140+ transitiontime), lightIds(lightIds) {
139141 http = new QHttp (host);
140142 timer.setInterval (3000 );
141143 timer.setSingleShot (true );
@@ -153,15 +155,15 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
153155 switchOn ((unsigned int ) ledValues.size ());
154156 }
155157 // 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 ()) {
157159 restoreStates ();
158160 return 0 ;
159161 }
160162 // Iterate through colors and set light states.
161163 unsigned int idx = 0 ;
162164 for (const ColorRgb& color : ledValues) {
163165 // Get lamp.
164- PhilipsHueLamp & lamp = lamps .at (idx);
166+ PhilipsHueLight & lamp = lights .at (idx);
165167 // Scale colors from [0, 255] to [0, 1] and convert to xy space.
166168 CiColor xy = lamp.rgbToCiColor (color.red / 255 .0f , color.green / 255 .0f , color.blue / 255 .0f );
167169 // Write color if color has been changed.
@@ -247,14 +249,21 @@ QString LedDevicePhilipsHue::getRoute(unsigned int lightId) {
247249
248250void LedDevicePhilipsHue::saveStates (unsigned int nLights) {
249251 // Clear saved lamps.
250- lamps .clear ();
252+ lights .clear ();
251253 // Use json parser to parse reponse.
252254 Json::Reader reader;
253255 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+ }
254263 // Iterate lights.
255264 for (unsigned int i = 0 ; i < nLights; i++) {
256265 // Read the response.
257- QByteArray response = get (getRoute (i + 1 ));
266+ QByteArray response = get (getRoute (lightIds. at (i) ));
258267 // Parse JSON.
259268 Json::Value json;
260269 if (!reader.parse (QString (response).toStdString (), json)) {
@@ -272,24 +281,24 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
272281 QString modelId = QString (writer.write (json[" modelid" ]).c_str ()).trimmed ().replace (" \" " , " " );
273282 QString originalState = QString (writer.write (state).c_str ()).trimmed ();
274283 // Save state object.
275- lamps .push_back (PhilipsHueLamp (i + 1 , originalState, modelId));
284+ lights .push_back (PhilipsHueLight (lightIds. at (i) , originalState, modelId));
276285 }
277286}
278287
279288void 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}" );
282291 }
283292}
284293
285294void 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 );
288297 }
289298 // Clear saved light states.
290- lamps .clear ();
299+ lights .clear ();
291300}
292301
293302bool LedDevicePhilipsHue::areStatesSaved () {
294- return !lamps .empty ();
303+ return !lights .empty ();
295304}
0 commit comments