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

Commit bb4f4bc

Browse files
author
Tim Niggemann
committed
Added config switch for turing off the lamps if the color black is written.
1 parent e10705d commit bb4f4bc

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

libsrc/leddevice/LedDeviceFactory.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
164164
else if (type == "philipshue")
165165
{
166166
const std::string output = deviceConfig["output"].asString();
167-
device = new LedDevicePhilipsHue(output);
167+
const bool switchOffOnBlack = deviceConfig.get("switch_off_on_black", false).asBool();
168+
device = new LedDevicePhilipsHue(output, switchOffOnBlack);
168169
}
169170
else if (type == "test")
170171
{

libsrc/leddevice/LedDevicePhilipsHue.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212

1313
#include <set>
1414

15-
LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output) :
16-
host(output.c_str()), username("newdeveloper") {
15+
const ColorPoint LedDevicePhilipsHue::BLACK = {0.0f, 0.0f, 0.0f};
16+
17+
LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output, bool switchOffOnBlack) :
18+
host(output.c_str()), username("newdeveloper"), switchOffOnBlack(switchOffOnBlack) {
1719
http = new QHttp(host);
1820
timer.setInterval(3000);
1921
timer.setSingleShot(true);
@@ -37,12 +39,19 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
3739
HueLamp& lamp = lamps.at(idx);
3840
// Scale colors from [0, 255] to [0, 1] and convert to xy space.
3941
ColorPoint xy = rgbToXYBrightness(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f, lamp);
42+
// Switch lamp off if switchOffOnBlack is enabled and the lamp is currently on.
43+
if (switchOffOnBlack && xy == BLACK && lamp.color != BLACK) {
44+
put(getStateRoute(lamp.id), QString("{\"on\": false}"));
45+
}
4046
// Write color if color has been changed.
41-
if (xy != lamp.color) {
42-
// Send adjust color command in JSON format.
43-
put(getStateRoute(lamp.id), QString("{\"xy\": [%1, %2]}").arg(xy.x).arg(xy.y));
44-
// Send brightness color command in JSON format.
45-
put(getStateRoute(lamp.id), QString("{\"bri\": %1}").arg(qRound(xy.bri * 255.0f)));
47+
else if (xy != lamp.color) {
48+
// Switch on if the lamp has been previously switched off.
49+
if (switchOffOnBlack && lamp.color == BLACK) {
50+
put(getStateRoute(lamp.id), QString("{\"on\": true}"));
51+
}
52+
// Send adjust color and brightness command in JSON format.
53+
put(getStateRoute(lamp.id), QString("{\"xy\": [%1, %2], \"bri\": %1}").arg(xy.x).arg(xy.y)
54+
.arg(qRound(xy.bri * 255.0f)));
4655
// Remember written color.
4756
lamp.color = xy;
4857
}

libsrc/leddevice/LedDevicePhilipsHue.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ Q_OBJECT
7171
///
7272
/// @param output the ip address of the bridge
7373
///
74-
LedDevicePhilipsHue(const std::string& output);
74+
/// @param switchOffOnBlack kill lights for black
75+
///
76+
LedDevicePhilipsHue(const std::string& output, bool switchOffOnBlack);
7577

7678
///
7779
/// Destructor of this device
@@ -95,6 +97,7 @@ private slots:
9597
void restoreStates();
9698

9799
private:
100+
const static ColorPoint BLACK;
98101
/// Array to save the lamps.
99102
std::vector<HueLamp> lamps;
100103
/// Ip address of the bridge
@@ -105,6 +108,8 @@ private slots:
105108
QHttp* http;
106109
/// Use timer to reset lights when we got into "GRABBINGMODE_OFF".
107110
QTimer timer;
111+
///
112+
bool switchOffOnBlack;
108113

109114
///
110115
/// Sends a HTTP GET request (blocking).

0 commit comments

Comments
 (0)