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

Commit 20e15c8

Browse files
committed
Merge branch 'ntim-support_for_philips_hue'
2 parents 2a77b55 + f766365 commit 20e15c8

File tree

3 files changed

+50
-33
lines changed

3 files changed

+50
-33
lines changed

libsrc/leddevice/LedDeviceFactory.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,11 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
196196
const std::string username = deviceConfig.get("username", "newdeveloper").asString();
197197
const bool switchOffOnBlack = deviceConfig.get("switchOffOnBlack", true).asBool();
198198
const int transitiontime = deviceConfig.get("transitiontime", 1).asInt();
199-
device = new LedDevicePhilipsHue(output, username, switchOffOnBlack, transitiontime);
199+
std::vector<unsigned int> lightIds;
200+
for (Json::Value::ArrayIndex i = 0; i < deviceConfig["lightIds"].size(); i++) {
201+
lightIds.push_back(deviceConfig["lightIds"][i].asInt());
202+
}
203+
device = new LedDevicePhilipsHue(output, username, switchOffOnBlack, transitiontime, lightIds);
200204
}
201205
else if (type == "test")
202206
{

libsrc/leddevice/LedDevicePhilipsHue.cpp

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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

135137
LedDevicePhilipsHue::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

248250
void 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

279288
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}");
282291
}
283292
}
284293

285294
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);
288297
}
289298
// Clear saved light states.
290-
lamps.clear();
299+
lights.clear();
291300
}
292301

293302
bool LedDevicePhilipsHue::areStatesSaved() {
294-
return !lamps.empty();
303+
return !lights.empty();
295304
}

libsrc/leddevice/LedDevicePhilipsHue.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct CiColorTriangle {
3737
/**
3838
* Simple class to hold the id, the latest color, the color space and the original state.
3939
*/
40-
class PhilipsHueLamp {
40+
class PhilipsHueLight {
4141
public:
4242
unsigned int id;
4343
CiColor black;
@@ -46,15 +46,15 @@ class PhilipsHueLamp {
4646
QString originalState;
4747

4848
///
49-
/// Constructs the lamp.
49+
/// Constructs the light.
5050
///
5151
/// @param id the light id
5252
///
5353
/// @param originalState the json string of the original state
5454
///
5555
/// @param modelId the model id of the hue lamp which is used to determine the color space
5656
///
57-
PhilipsHueLamp(unsigned int id, QString originalState, QString modelId);
57+
PhilipsHueLight(unsigned int id, QString originalState, QString modelId);
5858

5959
///
6060
/// Converts an RGB color to the Hue xy color space and brightness.
@@ -131,8 +131,10 @@ Q_OBJECT
131131
///
132132
/// @param transitiontime the time duration a light change takes in multiples of 100 ms (default: 400 ms).
133133
///
134+
/// @param lightIds light ids of the lights to control if not starting at one in ascending order.
135+
///
134136
LedDevicePhilipsHue(const std::string& output, const std::string& username = "newdeveloper", bool switchOffOnBlack =
135-
false, int transitiontime = 1);
137+
false, int transitiontime = 1, std::vector<unsigned int> lightIds = {});
136138

137139
///
138140
/// Destructor of this device
@@ -157,7 +159,7 @@ private slots:
157159

158160
private:
159161
/// Array to save the lamps.
160-
std::vector<PhilipsHueLamp> lamps;
162+
std::vector<PhilipsHueLight> lights;
161163
/// Ip address of the bridge
162164
QString host;
163165
/// User name for the API ("newdeveloper")
@@ -171,6 +173,8 @@ private slots:
171173
/// Transition time in multiples of 100 ms.
172174
/// The default of the Hue lights will be 400 ms, but we want to have it snapier
173175
int transitiontime;
176+
/// Array of the light ids.
177+
std::vector<unsigned int> lightIds;
174178

175179
///
176180
/// Sends a HTTP GET request (blocking).

0 commit comments

Comments
 (0)