2014-04-27 12:59:44 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// STL includes
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
// Qt includes
|
2014-05-03 10:12:41 +02:00
|
|
|
#include <QObject>
|
2014-04-27 12:59:44 +02:00
|
|
|
#include <QString>
|
2016-01-10 22:17:59 +01:00
|
|
|
#include <QNetworkAccessManager>
|
2014-05-03 10:12:41 +02:00
|
|
|
#include <QTimer>
|
2014-04-27 12:59:44 +02:00
|
|
|
// Leddevice includes
|
|
|
|
#include <leddevice/LedDevice.h>
|
2014-04-28 14:32:37 +02:00
|
|
|
|
2014-07-15 12:06:26 +02:00
|
|
|
/**
|
|
|
|
* A color point in the color space of the hue system.
|
|
|
|
*/
|
2014-07-16 20:22:37 +02:00
|
|
|
struct CiColor {
|
|
|
|
/// X component.
|
2014-07-15 08:51:07 +02:00
|
|
|
float x;
|
2014-07-16 20:22:37 +02:00
|
|
|
/// Y component.
|
2014-07-15 08:51:07 +02:00
|
|
|
float y;
|
2014-07-16 20:22:37 +02:00
|
|
|
/// The brightness.
|
2014-07-15 09:55:58 +02:00
|
|
|
float bri;
|
2014-07-15 08:51:07 +02:00
|
|
|
};
|
|
|
|
|
2014-07-16 20:22:37 +02:00
|
|
|
bool operator==(CiColor p1, CiColor p2);
|
|
|
|
bool operator!=(CiColor p1, CiColor p2);
|
2014-07-15 09:55:58 +02:00
|
|
|
|
2014-07-15 12:06:26 +02:00
|
|
|
/**
|
|
|
|
* Color triangle to define an available color space for the hue lamps.
|
|
|
|
*/
|
2014-07-16 20:22:37 +02:00
|
|
|
struct CiColorTriangle {
|
|
|
|
CiColor red, green, blue;
|
2014-07-15 09:55:58 +02:00
|
|
|
};
|
|
|
|
|
2014-07-15 12:06:26 +02:00
|
|
|
/**
|
|
|
|
* Simple class to hold the id, the latest color, the color space and the original state.
|
|
|
|
*/
|
2015-03-03 15:46:53 +01:00
|
|
|
class PhilipsHueLight {
|
2014-07-15 09:55:58 +02:00
|
|
|
public:
|
|
|
|
unsigned int id;
|
2014-07-16 20:22:37 +02:00
|
|
|
CiColor black;
|
|
|
|
CiColor color;
|
|
|
|
CiColorTriangle colorSpace;
|
2014-07-15 09:55:58 +02:00
|
|
|
QString originalState;
|
|
|
|
|
2014-07-15 12:06:26 +02:00
|
|
|
///
|
2015-03-03 15:46:53 +01:00
|
|
|
/// Constructs the light.
|
2014-07-15 12:06:26 +02:00
|
|
|
///
|
|
|
|
/// @param id the light id
|
|
|
|
///
|
|
|
|
/// @param originalState the json string of the original state
|
|
|
|
///
|
|
|
|
/// @param modelId the model id of the hue lamp which is used to determine the color space
|
|
|
|
///
|
2015-03-03 15:46:53 +01:00
|
|
|
PhilipsHueLight(unsigned int id, QString originalState, QString modelId);
|
2014-07-16 20:22:37 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// Converts an RGB color to the Hue xy color space and brightness.
|
|
|
|
/// https://github.com/PhilipsHue/PhilipsHueSDK-iOS-OSX/blob/master/ApplicationDesignNotes/RGB%20to%20xy%20Color%20conversion.md
|
|
|
|
///
|
|
|
|
/// @param red the red component in [0, 1]
|
|
|
|
///
|
|
|
|
/// @param green the green component in [0, 1]
|
|
|
|
///
|
|
|
|
/// @param blue the blue component in [0, 1]
|
|
|
|
///
|
|
|
|
/// @return color point
|
|
|
|
///
|
|
|
|
CiColor rgbToCiColor(float red, float green, float blue);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// @param p the color point to check
|
|
|
|
///
|
|
|
|
/// @return true if the color point is covered by the lamp color space
|
|
|
|
///
|
|
|
|
bool isPointInLampsReach(CiColor p);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// @param p1 point one
|
|
|
|
///
|
|
|
|
/// @param p2 point tow
|
|
|
|
///
|
|
|
|
/// @return the cross product between p1 and p2
|
|
|
|
///
|
|
|
|
float crossProduct(CiColor p1, CiColor p2);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// @param a reference point one
|
|
|
|
///
|
|
|
|
/// @param b reference point two
|
|
|
|
///
|
|
|
|
/// @param p the point to which the closest point is to be found
|
|
|
|
///
|
|
|
|
/// @return the closest color point of p to a and b
|
|
|
|
///
|
|
|
|
CiColor getClosestPointToPoint(CiColor a, CiColor b, CiColor p);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// @param p1 point one
|
|
|
|
///
|
|
|
|
/// @param p2 point tow
|
|
|
|
///
|
|
|
|
/// @return the distance between the two points
|
|
|
|
///
|
|
|
|
float getDistanceBetweenTwoPoints(CiColor p1, CiColor p2);
|
2014-07-15 08:51:07 +02:00
|
|
|
};
|
|
|
|
|
2014-04-27 12:59:44 +02:00
|
|
|
/**
|
|
|
|
* Implementation for the Philips Hue system.
|
2014-04-30 22:38:59 +02:00
|
|
|
*
|
2014-04-27 12:59:44 +02:00
|
|
|
* To use set the device to "philipshue".
|
|
|
|
* Uses the official Philips Hue API (http://developers.meethue.com).
|
2014-04-27 18:42:26 +02:00
|
|
|
* Framegrabber must be limited to 10 Hz / numer of lights to avoid rate limitation by the hue bridge.
|
2014-04-27 12:59:44 +02:00
|
|
|
* Create a new API user name "newdeveloper" on the bridge (http://developers.meethue.com/gettingstarted.html)
|
2014-04-30 22:38:59 +02:00
|
|
|
*
|
2014-07-16 16:21:11 +02:00
|
|
|
* @author ntim (github), bimsarck (github)
|
2014-04-27 12:59:44 +02:00
|
|
|
*/
|
2014-05-03 10:12:41 +02:00
|
|
|
class LedDevicePhilipsHue: public QObject, public LedDevice {
|
|
|
|
Q_OBJECT
|
2014-04-27 12:59:44 +02:00
|
|
|
public:
|
|
|
|
///
|
|
|
|
/// Constructs the device.
|
|
|
|
///
|
|
|
|
/// @param output the ip address of the bridge
|
|
|
|
///
|
2015-02-01 15:29:40 +01:00
|
|
|
/// @param username username of the hue bridge (default: newdeveloper)
|
2014-07-16 11:49:34 +02:00
|
|
|
///
|
2015-02-01 15:29:40 +01:00
|
|
|
/// @param switchOffOnBlack kill lights for black (default: false)
|
|
|
|
///
|
|
|
|
/// @param transitiontime the time duration a light change takes in multiples of 100 ms (default: 400 ms).
|
|
|
|
///
|
2015-03-03 15:45:09 +01:00
|
|
|
/// @param lightIds light ids of the lights to control if not starting at one in ascending order.
|
|
|
|
///
|
2015-02-01 15:29:40 +01:00
|
|
|
LedDevicePhilipsHue(const std::string& output, const std::string& username = "newdeveloper", bool switchOffOnBlack =
|
2015-08-11 12:47:44 +02:00
|
|
|
false, int transitiontime = 1, std::vector<unsigned int> lightIds = std::vector<unsigned int>());
|
2014-04-27 12:59:44 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// Destructor of this device
|
|
|
|
///
|
|
|
|
virtual ~LedDevicePhilipsHue();
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Sends the given led-color values via put request to the hue system
|
|
|
|
///
|
|
|
|
/// @param ledValues The color-value per led
|
|
|
|
///
|
|
|
|
/// @return Zero on success else negative
|
|
|
|
///
|
|
|
|
virtual int write(const std::vector<ColorRgb> & ledValues);
|
|
|
|
|
2014-05-07 15:22:13 +02:00
|
|
|
/// Restores the original state of the leds.
|
2014-04-27 12:59:44 +02:00
|
|
|
virtual int switchOff();
|
|
|
|
|
2014-05-03 10:12:41 +02:00
|
|
|
private slots:
|
|
|
|
/// Restores the status of all lights.
|
|
|
|
void restoreStates();
|
|
|
|
|
2014-04-27 12:59:44 +02:00
|
|
|
private:
|
2014-07-15 09:55:58 +02:00
|
|
|
/// Array to save the lamps.
|
2015-03-03 15:46:53 +01:00
|
|
|
std::vector<PhilipsHueLight> lights;
|
2014-04-27 12:59:44 +02:00
|
|
|
/// Ip address of the bridge
|
|
|
|
QString host;
|
|
|
|
/// User name for the API ("newdeveloper")
|
|
|
|
QString username;
|
2016-01-10 22:17:59 +01:00
|
|
|
/// QNetworkAccessManager object for sending requests.
|
2016-01-18 10:36:20 +01:00
|
|
|
QNetworkAccessManager* manager;
|
2014-05-03 10:12:41 +02:00
|
|
|
/// Use timer to reset lights when we got into "GRABBINGMODE_OFF".
|
|
|
|
QTimer timer;
|
2014-07-16 11:49:34 +02:00
|
|
|
///
|
|
|
|
bool switchOffOnBlack;
|
2015-02-01 15:29:40 +01:00
|
|
|
/// Transition time in multiples of 100 ms.
|
|
|
|
/// The default of the Hue lights will be 400 ms, but we want to have it snapier
|
|
|
|
int transitiontime;
|
2015-03-03 15:45:09 +01:00
|
|
|
/// Array of the light ids.
|
|
|
|
std::vector<unsigned int> lightIds;
|
2014-04-27 12:59:44 +02:00
|
|
|
|
|
|
|
///
|
2014-04-28 14:32:37 +02:00
|
|
|
/// Sends a HTTP GET request (blocking).
|
|
|
|
///
|
|
|
|
/// @param route the URI of the request
|
|
|
|
///
|
|
|
|
/// @return response of the request
|
|
|
|
///
|
|
|
|
QByteArray get(QString route);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Sends a HTTP PUT request (non-blocking).
|
2014-04-27 12:59:44 +02:00
|
|
|
///
|
|
|
|
/// @param route the URI of the request
|
|
|
|
///
|
|
|
|
/// @param content content of the request
|
|
|
|
///
|
|
|
|
void put(QString route, QString content);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// @param lightId the id of the hue light (starting from 1)
|
|
|
|
///
|
2014-04-28 14:32:37 +02:00
|
|
|
/// @return the URI of the light state for PUT requests.
|
|
|
|
///
|
|
|
|
QString getStateRoute(unsigned int lightId);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// @param lightId the id of the hue light (starting from 1)
|
|
|
|
///
|
|
|
|
/// @return the URI of the light for GET requests.
|
2014-04-27 12:59:44 +02:00
|
|
|
///
|
|
|
|
QString getRoute(unsigned int lightId);
|
|
|
|
|
2016-03-19 12:36:55 +01:00
|
|
|
///
|
|
|
|
/// @param route
|
|
|
|
///
|
|
|
|
/// @return the full URL of the request.
|
|
|
|
///
|
|
|
|
QString getUrl(QString route);
|
|
|
|
|
2014-04-28 14:32:37 +02:00
|
|
|
///
|
|
|
|
/// Queries the status of all lights and saves it.
|
|
|
|
///
|
|
|
|
/// @param nLights the number of lights
|
|
|
|
///
|
|
|
|
void saveStates(unsigned int nLights);
|
|
|
|
|
2014-05-07 15:22:13 +02:00
|
|
|
///
|
|
|
|
/// Switches the leds on.
|
|
|
|
///
|
|
|
|
/// @param nLights the number of lights
|
|
|
|
///
|
|
|
|
void switchOn(unsigned int nLights);
|
|
|
|
|
2014-04-28 14:32:37 +02:00
|
|
|
///
|
|
|
|
/// @return true if light states have been saved.
|
|
|
|
///
|
2014-07-15 08:51:07 +02:00
|
|
|
bool areStatesSaved();
|
2014-04-28 14:32:37 +02:00
|
|
|
|
2014-04-27 12:59:44 +02:00
|
|
|
};
|