mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2023-10-10 13:36:59 +02:00
Merge pull request #130 from bimsarck/master
Remove unnecessary code Former-commit-id: 265b481bc708fdcf4294b9fb92bbc1fcee05844c
This commit is contained in:
commit
769d550167
@ -11,42 +11,43 @@
|
|||||||
#include <QHttpRequestHeader>
|
#include <QHttpRequestHeader>
|
||||||
#include <QEventLoop>
|
#include <QEventLoop>
|
||||||
|
|
||||||
LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string &output) :
|
LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output) :
|
||||||
host(output.c_str()), username("newdeveloper") {
|
host(output.c_str()), username("newdeveloper") {
|
||||||
http = new QHttp(host);
|
http = new QHttp(host);
|
||||||
/* timer.setInterval(3000);
|
timer.setInterval(3000);
|
||||||
timer.setSingleShot(true);
|
timer.setSingleShot(true);
|
||||||
connect(&timer, SIGNAL(timeout()), this, SLOT(restoreStates()));*/
|
connect(&timer, SIGNAL(timeout()), this, SLOT(restoreStates()));
|
||||||
}
|
}
|
||||||
|
|
||||||
LedDevicePhilipsHue::~LedDevicePhilipsHue() {
|
LedDevicePhilipsHue::~LedDevicePhilipsHue() {
|
||||||
delete http;
|
delete http;
|
||||||
}
|
}
|
||||||
|
|
||||||
int LedDevicePhilipsHue::write(const std::vector<ColorRgb> &ledValues) {
|
int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
|
||||||
// Save light states if not done before.
|
// Save light states if not done before.
|
||||||
if (!statesSaved())
|
if (!statesSaved())
|
||||||
saveStates(ledValues.size());
|
saveStates(ledValues.size());
|
||||||
// Iterate through colors and set light states.
|
// Iterate through colors and set light states.
|
||||||
unsigned int lightId = 0;
|
unsigned int lightId = 0;
|
||||||
for (const ColorRgb &color : ledValues) {
|
for (const ColorRgb &color : ledValues) {
|
||||||
lightId++;
|
|
||||||
// Send only request to the brigde if color changed (prevents DDOS --> 503)
|
// Send only request to the brigde if color changed (prevents DDOS --> 503)
|
||||||
if (!oldLedValues.empty())
|
if (!oldLedValues.empty())
|
||||||
if(!hasColorChanged(lightId, &color))
|
if(!hasColorChanged(lightId, &color)) {
|
||||||
|
lightId++;
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
float r = color.red / 255.0f;
|
float r = color.red / 255.0f;
|
||||||
float g = color.green / 255.0f;
|
float g = color.green / 255.0f;
|
||||||
float b = color.blue / 255.0f;
|
float b = color.blue / 255.0f;
|
||||||
|
|
||||||
//set color gamut triangle
|
//set color gamut triangle
|
||||||
if(std::find(hueBulbs.begin(), hueBulbs.end(), modelIds[(lightId - 1)]) != hueBulbs.end()) {
|
if(std::find(hueBulbs.begin(), hueBulbs.end(), modelIds[lightId]) != hueBulbs.end()) {
|
||||||
Red = {0.675f, 0.322f};
|
Red = {0.675f, 0.322f};
|
||||||
Green = {0.4091f, 0.518f};
|
Green = {0.4091f, 0.518f};
|
||||||
Blue = {0.167f, 0.04f};
|
Blue = {0.167f, 0.04f};
|
||||||
} else if (std::find(livingColors.begin(),
|
} else if (std::find(livingColors.begin(),
|
||||||
livingColors.end(), modelIds[(lightId - 1)]) != livingColors.end()) {
|
livingColors.end(), modelIds[lightId]) != livingColors.end()) {
|
||||||
Red = {0.703f, 0.296f};
|
Red = {0.703f, 0.296f};
|
||||||
Green = {0.214f, 0.709f};
|
Green = {0.214f, 0.709f};
|
||||||
Blue = {0.139f, 0.081f};
|
Blue = {0.139f, 0.081f};
|
||||||
@ -58,28 +59,30 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> &ledValues) {
|
|||||||
// if color equal black, switch off lamp ...
|
// if color equal black, switch off lamp ...
|
||||||
if (r == 0.0f && g == 0.0f && b == 0.0f) {
|
if (r == 0.0f && g == 0.0f && b == 0.0f) {
|
||||||
switchLampOff(lightId);
|
switchLampOff(lightId);
|
||||||
|
lightId++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// ... and if lamp off, switch on
|
// ... and if lamp off, switch on
|
||||||
if (!checkOnStatus(states[(lightId - 1)]))
|
if (!checkOnStatus(states[lightId]))
|
||||||
switchLampOn(lightId);
|
switchLampOn(lightId);
|
||||||
|
|
||||||
float bri;
|
float bri;
|
||||||
CGPoint p = CGPointMake(0, 0);
|
CGPoint p = {0.0f, 0.0f};
|
||||||
// Scale colors from [0, 255] to [0, 1] and convert to xy space.
|
// Scale colors from [0, 255] to [0, 1] and convert to xy space.
|
||||||
rgbToXYBrightness(r, g, b, &p, bri);
|
rgbToXYBrightness(r, g, b, p, bri);
|
||||||
// Send adjust color and brightness command in JSON format.
|
// Send adjust color and brightness command in JSON format.
|
||||||
put(getStateRoute(lightId),
|
put(getStateRoute(lightId),
|
||||||
QString("{\"xy\": [%1, %2], \"bri\": %3}").arg(p.x).arg(p.y).arg(qRound(b * 255.0f)));
|
QString("{\"xy\": [%1, %2], \"bri\": %3}").arg(p.x).arg(p.y).arg(qRound(b * 255.0f)));
|
||||||
|
lightId++;
|
||||||
}
|
}
|
||||||
oldLedValues = ledValues;
|
oldLedValues = ledValues;
|
||||||
//timer.start();
|
timer.start();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LedDevicePhilipsHue::hasColorChanged(unsigned int lightId, const ColorRgb *color) {
|
bool LedDevicePhilipsHue::hasColorChanged(unsigned int lightId, const ColorRgb *color) {
|
||||||
bool matchFound = true;
|
bool matchFound = true;
|
||||||
const ColorRgb &tmpOldColor = oldLedValues[(lightId - 1)];
|
const ColorRgb &tmpOldColor = oldLedValues[lightId];
|
||||||
if ((*color).red == tmpOldColor.red)
|
if ((*color).red == tmpOldColor.red)
|
||||||
matchFound = false;
|
matchFound = false;
|
||||||
if (!matchFound && (*color).green == tmpOldColor.green)
|
if (!matchFound && (*color).green == tmpOldColor.green)
|
||||||
@ -95,7 +98,7 @@ bool LedDevicePhilipsHue::hasColorChanged(unsigned int lightId, const ColorRgb *
|
|||||||
}
|
}
|
||||||
|
|
||||||
int LedDevicePhilipsHue::switchOff() {
|
int LedDevicePhilipsHue::switchOff() {
|
||||||
//timer.stop();
|
timer.stop();
|
||||||
// If light states have been saved before, ...
|
// If light states have been saved before, ...
|
||||||
if (statesSaved()) {
|
if (statesSaved()) {
|
||||||
// ... restore them.
|
// ... restore them.
|
||||||
@ -122,7 +125,6 @@ void LedDevicePhilipsHue::put(QString route, QString content) {
|
|||||||
http->request(header, content.toAscii());
|
http->request(header, content.toAscii());
|
||||||
// Go into the loop until the request is finished.
|
// Go into the loop until the request is finished.
|
||||||
loop.exec();
|
loop.exec();
|
||||||
//std::cout << http->readAll().data() << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray LedDevicePhilipsHue::get(QString route) {
|
QByteArray LedDevicePhilipsHue::get(QString route) {
|
||||||
@ -140,7 +142,7 @@ QByteArray LedDevicePhilipsHue::get(QString route) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
QString LedDevicePhilipsHue::getStateRoute(unsigned int lightId) {
|
QString LedDevicePhilipsHue::getStateRoute(unsigned int lightId) {
|
||||||
return QString("lights/%1/state").arg(lightId);
|
return QString("lights/%1/state").arg(lightId + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString LedDevicePhilipsHue::getRoute(unsigned int lightId) {
|
QString LedDevicePhilipsHue::getRoute(unsigned int lightId) {
|
||||||
@ -179,18 +181,20 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
|
|||||||
|
|
||||||
void LedDevicePhilipsHue::switchLampOn(unsigned int lightId) {
|
void LedDevicePhilipsHue::switchLampOn(unsigned int lightId) {
|
||||||
put(getStateRoute(lightId), "{\"on\": true}");
|
put(getStateRoute(lightId), "{\"on\": true}");
|
||||||
states[(lightId - 1)].replace("\"on\":false", "\"on\":true");
|
states[lightId].replace("\"on\":false", "\"on\":true");
|
||||||
}
|
}
|
||||||
|
|
||||||
void LedDevicePhilipsHue::switchLampOff(unsigned int lightId) {
|
void LedDevicePhilipsHue::switchLampOff(unsigned int lightId) {
|
||||||
put(getStateRoute(lightId), "{\"on\": false}");
|
put(getStateRoute(lightId), "{\"on\": false}");
|
||||||
states[(lightId - 1)].replace("\"on\":true", "\"on\":false");
|
states[lightId].replace("\"on\":true", "\"on\":false");
|
||||||
}
|
}
|
||||||
|
|
||||||
void LedDevicePhilipsHue::restoreStates() {
|
void LedDevicePhilipsHue::restoreStates() {
|
||||||
unsigned int lightId = 1;
|
unsigned int lightId = 0;
|
||||||
for (QString state : states) {
|
for (QString state : states) {
|
||||||
put(getStateRoute(lightId), state);
|
if (!checkOnStatus(states[lightId]))
|
||||||
|
switchLampOn(lightId);
|
||||||
|
put(getStateRoute(lightId), states[lightId]);
|
||||||
lightId++;
|
lightId++;
|
||||||
}
|
}
|
||||||
// Clear saved light states.
|
// Clear saved light states.
|
||||||
@ -203,23 +207,15 @@ bool LedDevicePhilipsHue::statesSaved() {
|
|||||||
return !states.empty();
|
return !states.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
CGPoint LedDevicePhilipsHue::CGPointMake(float x, float y) {
|
float LedDevicePhilipsHue::CrossProduct(CGPoint& p1, CGPoint& p2) {
|
||||||
CGPoint p;
|
|
||||||
p.x = x;
|
|
||||||
p.y = y;
|
|
||||||
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
|
|
||||||
float LedDevicePhilipsHue::CrossProduct(CGPoint p1, CGPoint p2) {
|
|
||||||
return (p1.x * p2.y - p1.y * p2.x);
|
return (p1.x * p2.y - p1.y * p2.x);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LedDevicePhilipsHue::CheckPointInLampsReach(CGPoint p) {
|
bool LedDevicePhilipsHue::CheckPointInLampsReach(CGPoint& p) {
|
||||||
CGPoint v1 = CGPointMake(Green.x - Red.x, Green.y - Red.y);
|
CGPoint v1 = {Green.x - Red.x, Green.y - Red.y};
|
||||||
CGPoint v2 = CGPointMake(Blue.x - Red.x, Blue.y - Red.y);
|
CGPoint v2 = {Blue.x - Red.x, Blue.y - Red.y};
|
||||||
|
|
||||||
CGPoint q = CGPointMake(p.x - Red.x, p.y - Red.y);
|
CGPoint q = {p.x - Red.x, p.y - Red.y};
|
||||||
|
|
||||||
float s = CrossProduct(q, v2) / CrossProduct(v1, v2);
|
float s = CrossProduct(q, v2) / CrossProduct(v1, v2);
|
||||||
float t = CrossProduct(v1, q) / CrossProduct(v1, v2);
|
float t = CrossProduct(v1, q) / CrossProduct(v1, v2);
|
||||||
@ -229,9 +225,9 @@ bool LedDevicePhilipsHue::CheckPointInLampsReach(CGPoint p) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
CGPoint LedDevicePhilipsHue::GetClosestPointToPoint(CGPoint A, CGPoint B, CGPoint P) {
|
CGPoint LedDevicePhilipsHue::GetClosestPointToPoint(CGPoint& A, CGPoint& B, CGPoint& P) {
|
||||||
CGPoint AP = CGPointMake(P.x - A.x, P.y - A.y);
|
CGPoint AP = {P.x - A.x, P.y - A.y};
|
||||||
CGPoint AB = CGPointMake(B.x - A.x, B.y - A.y);
|
CGPoint AB = {B.x - A.x, B.y - A.y};
|
||||||
float ab2 = AB.x * AB.x + AB.y * AB.y;
|
float ab2 = AB.x * AB.x + AB.y * AB.y;
|
||||||
float ap_ab = AP.x * AB.x + AP.y * AB.y;
|
float ap_ab = AP.x * AB.x + AP.y * AB.y;
|
||||||
|
|
||||||
@ -242,10 +238,10 @@ CGPoint LedDevicePhilipsHue::GetClosestPointToPoint(CGPoint A, CGPoint B, CGPoin
|
|||||||
else if (t > 1.0f)
|
else if (t > 1.0f)
|
||||||
t = 1.0f;
|
t = 1.0f;
|
||||||
|
|
||||||
return CGPointMake(A.x + AB.x * t, A.y + AB.y * t);
|
return {A.x + AB.x * t, A.y + AB.y * t};
|
||||||
}
|
}
|
||||||
|
|
||||||
float LedDevicePhilipsHue::GetDistanceBetweenTwoPoints(CGPoint one, CGPoint two) {
|
float LedDevicePhilipsHue::GetDistanceBetweenTwoPoints(CGPoint& one, CGPoint& two) {
|
||||||
float dx = one.x - two.x; // horizontal difference
|
float dx = one.x - two.x; // horizontal difference
|
||||||
float dy = one.y - two.y; // vertical difference
|
float dy = one.y - two.y; // vertical difference
|
||||||
float dist = sqrt(dx * dx + dy * dy);
|
float dist = sqrt(dx * dx + dy * dy);
|
||||||
@ -253,7 +249,7 @@ float LedDevicePhilipsHue::GetDistanceBetweenTwoPoints(CGPoint one, CGPoint two)
|
|||||||
return dist;
|
return dist;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue, CGPoint *xyPoint, float &brightness) {
|
void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue, CGPoint& xyPoint, float& brightness) {
|
||||||
//Apply gamma correction.
|
//Apply gamma correction.
|
||||||
float r = (red > 0.04045f) ? powf((red + 0.055f) / (1.0f + 0.055f), 2.4f) : (red / 12.92f);
|
float r = (red > 0.04045f) ? powf((red + 0.055f) / (1.0f + 0.055f), 2.4f) : (red / 12.92f);
|
||||||
float g = (green > 0.04045f) ? powf((green + 0.055f) / (1.0f + 0.055f), 2.4f) : (green / 12.92f);
|
float g = (green > 0.04045f) ? powf((green + 0.055f) / (1.0f + 0.055f), 2.4f) : (green / 12.92f);
|
||||||
@ -271,25 +267,25 @@ void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue,
|
|||||||
if (isnan(cy))
|
if (isnan(cy))
|
||||||
cy = 0.0f;
|
cy = 0.0f;
|
||||||
|
|
||||||
(*xyPoint).x = cx;
|
xyPoint.x = cx;
|
||||||
(*xyPoint).y = cy;
|
xyPoint.y = cy;
|
||||||
|
|
||||||
//Check if the given XY value is within the colourreach of our lamps.
|
//Check if the given XY value is within the colourreach of our lamps.
|
||||||
bool inReachOfLamps = CheckPointInLampsReach(*xyPoint);
|
bool inReachOfLamps = CheckPointInLampsReach(xyPoint);
|
||||||
|
|
||||||
if (!inReachOfLamps) {
|
if (!inReachOfLamps) {
|
||||||
//It seems the colour is out of reach
|
//It seems the colour is out of reach
|
||||||
//let's find the closes colour we can produce with our lamp and send this XY value out.
|
//let's find the closes colour we can produce with our lamp and send this XY value out.
|
||||||
|
|
||||||
//Find the closest point on each line in the triangle.
|
//Find the closest point on each line in the triangle.
|
||||||
CGPoint pAB = GetClosestPointToPoint(Red, Green, *xyPoint);
|
CGPoint pAB = GetClosestPointToPoint(Red, Green, xyPoint);
|
||||||
CGPoint pAC = GetClosestPointToPoint(Blue, Red, *xyPoint);
|
CGPoint pAC = GetClosestPointToPoint(Blue, Red, xyPoint);
|
||||||
CGPoint pBC = GetClosestPointToPoint(Green, Blue, *xyPoint);
|
CGPoint pBC = GetClosestPointToPoint(Green, Blue, xyPoint);
|
||||||
|
|
||||||
//Get the distances per point and see which point is closer to our Point.
|
//Get the distances per point and see which point is closer to our Point.
|
||||||
float dAB = GetDistanceBetweenTwoPoints(*xyPoint, pAB);
|
float dAB = GetDistanceBetweenTwoPoints(xyPoint, pAB);
|
||||||
float dAC = GetDistanceBetweenTwoPoints(*xyPoint, pAC);
|
float dAC = GetDistanceBetweenTwoPoints(xyPoint, pAC);
|
||||||
float dBC = GetDistanceBetweenTwoPoints(*xyPoint, pBC);
|
float dBC = GetDistanceBetweenTwoPoints(xyPoint, pBC);
|
||||||
|
|
||||||
float lowest = dAB;
|
float lowest = dAB;
|
||||||
CGPoint closestPoint = pAB;
|
CGPoint closestPoint = pAB;
|
||||||
@ -304,8 +300,8 @@ void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue,
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Change the xy value to a value which is within the reach of the lamp.
|
//Change the xy value to a value which is within the reach of the lamp.
|
||||||
(*xyPoint).x = closestPoint.x;
|
xyPoint.x = closestPoint.x;
|
||||||
(*xyPoint).y = closestPoint.y;
|
xyPoint.y = closestPoint.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Brightness is simply Y in the XYZ space.
|
// Brightness is simply Y in the XYZ space.
|
||||||
|
@ -49,7 +49,7 @@ public:
|
|||||||
///
|
///
|
||||||
/// @return Zero on success else negative
|
/// @return Zero on success else negative
|
||||||
///
|
///
|
||||||
virtual int write(const std::vector<ColorRgb> &ledValues);
|
virtual int write(const std::vector<ColorRgb> & ledValues);
|
||||||
|
|
||||||
/// Restores the original state of the leds.
|
/// Restores the original state of the leds.
|
||||||
virtual int switchOff();
|
virtual int switchOff();
|
||||||
@ -59,18 +59,17 @@ private slots:
|
|||||||
void restoreStates();
|
void restoreStates();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// ModelIds
|
/// Available modelIds
|
||||||
const std::vector<QString> hueBulbs = {"LCT001", "LCT002", "LCT003"};
|
const std::vector<QString> hueBulbs = {"LCT001", "LCT002", "LCT003"};
|
||||||
const std::vector<QString> livingColors = {"LLC001", "LLC005", "LLC006", "LLC007",
|
const std::vector<QString> livingColors = {"LLC001", "LLC005", "LLC006", "LLC007",
|
||||||
"LLC011", "LLC012", "LLC013", "LST001"};
|
"LLC011", "LLC012", "LLC013", "LST001"};
|
||||||
/// LivingColors color gamut triangle
|
/// Color gamut triangle
|
||||||
CGPoint Red , Green, Blue;
|
CGPoint Red , Green, Blue;
|
||||||
|
|
||||||
CGPoint CGPointMake(float x, float y);
|
float CrossProduct(CGPoint& p1, CGPoint& p2);
|
||||||
float CrossProduct(CGPoint p1, CGPoint p2);
|
bool CheckPointInLampsReach(CGPoint& p);
|
||||||
bool CheckPointInLampsReach(CGPoint p);
|
CGPoint GetClosestPointToPoint(CGPoint& A, CGPoint& B, CGPoint& P);
|
||||||
CGPoint GetClosestPointToPoint(CGPoint A, CGPoint B, CGPoint P);
|
float GetDistanceBetweenTwoPoints(CGPoint& one, CGPoint& two);
|
||||||
float GetDistanceBetweenTwoPoints(CGPoint one, CGPoint two);
|
|
||||||
|
|
||||||
/// Array to save the light states.
|
/// Array to save the light states.
|
||||||
std::vector<QString> states;
|
std::vector<QString> states;
|
||||||
@ -159,6 +158,6 @@ private:
|
|||||||
///
|
///
|
||||||
/// @param brightness converted brightness component
|
/// @param brightness converted brightness component
|
||||||
///
|
///
|
||||||
void rgbToXYBrightness(float red, float green, float blue, CGPoint *xyPoint, float &brightness);
|
void rgbToXYBrightness(float red, float green, float blue, CGPoint& xyPoint, float& brightness);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user