-
-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Performance for multiple attributes (brightness, colour loop & colour) for multiple lamps is a bit slow #49
Comments
Hi, very nice that you find the library helpful. The delay between messages is by design, because there are limitations in the ZigBee communication. We have something similar to the way you described already implemented on the development branch. Thank you very much |
I tried to paste my code, but there was an error message I didn't notice because you can only paste picture files. #pragma once
#include "HueLight.h"
class HueLightHelper : HueLight
{
private:
nlohmann::json requestJson;
void setUpForColorXY(float x, float y);
protected:
public:
HueLightHelper(HueLight& huelight);
void setupForColorLoop(bool colourLoop);
void RemoveColorLoop();
void setupForColorRGB(int red, int green, int blue);
void setupForBrightness(int brightness);
void setupForOffOn(bool on);
bool SendPutRequests(const int transition = 4);
}; CPP code file is:
#include <string>
#include "include/HueLightHelper.h"
#include "include/HueExceptionMacro.h"
#include "include/HueLight.h"
#include "include/json/json.hpp"
#include "include/Utils.h"
HueLightHelper::HueLightHelper(HueLight& hueLight) : HueLight(hueLight)
{
refreshState();
requestJson = nlohmann::json::object();
}
void HueLightHelper::setupForColorLoop(bool colourLoop)
{
std::string effect = colourLoop ? "colorloop" : "none";
if (effect != requestJson["effect"])
{
requestJson["effect"] = effect;
}
}
void HueLightHelper::RemoveColorLoop()
{
if (requestJson.count("effect") > 0)
{
requestJson.erase("effect");
assert(requestJson.count("effect") == 0);
}
}
void HueLightHelper::setupForColorRGB(int red, int green, int blue)
{
if ((red == 0) && (green == 0) && (blue == 0))
{
setupForOffOn(false);
}
else
{
const float redf = float(red) / 255;
const float greenf = float(green) / 255;
const float bluef = float(blue) / 255;
// gamma correction
const float redCorrected = (redf > 0.04045f) ? pow((redf + 0.055f) / (1.0f + 0.055f), 2.4f) : (redf / 12.92f);
const float greenCorrected = (greenf > 0.04045f) ? pow((greenf + 0.055f) / (1.0f + 0.055f), 2.4f) : (greenf / 12.92f);
const float blueCorrected = (bluef > 0.04045f) ? pow((bluef + 0.055f) / (1.0f + 0.055f), 2.4f) : (bluef / 12.92f);
const float X = redCorrected * 0.664511f + greenCorrected * 0.154324f + blueCorrected * 0.162028f;
const float Y = redCorrected * 0.283881f + greenCorrected * 0.668433f + blueCorrected * 0.047685f;
const float Z = redCorrected * 0.000088f + greenCorrected * 0.072310f + blueCorrected * 0.986039f;
const float x = X / (X + Y + Z);
const float y = Y / (X + Y + Z);
setUpForColorXY(x, y);
}
}
void HueLightHelper::setUpForColorXY(float x, float y)
{
requestJson["xy"][0] = x;
requestJson["xy"][1] = y;
//if (std::abs(state["state"]["xy"][0].get<float>() - x) > 1E-4f
// || std::abs(state["state"]["xy"][1].get<float>() - y) > 1E-4f
// || state["state"]["colormode"] != "xy")
//{
// requestJson["xy"][0] = x;
// requestJson["xy"][1] = y;
//}
}
void HueLightHelper::setupForBrightness(int brightness)
{
assert(brightness >= 0);
if (brightness > 254)
{
brightness = 254;
}
if (requestJson["bri"] != brightness)
{
requestJson["bri"] = brightness;
}
}
void HueLightHelper::setupForOffOn(bool on)
{
if (requestJson["on"] != on)
{
requestJson["on"] = on;
}
}
bool HueLightHelper::SendPutRequests(const int transition)
{
if (requestJson.count("on") == 0 &&
requestJson.count("effect") == 0 &&
requestJson.count("xy") == 0 &&
requestJson.count("bri") == 0)
{
// Nothing needs to be changed
return true;
}
else
{
if (transition != 4)
{
// ReSharper disable StringLiteralTypo
requestJson["transitiontime"] = transition;
// ReSharper restore StringLiteralTypo
}
//if we are switching off, the do not supply a colour loop command.
if (requestJson.count("on") > 0 &&
requestJson["on"] == false &&
requestJson.count("effect") > 0)
{
RemoveColorLoop();
}
//if we are switching on and no colour loop supplier, then switch that off.
if (requestJson.count("on") > 0 &&
requestJson["on"] == true &&
requestJson.count("effect") == 0)
{
setupForColorLoop(false);
}
nlohmann::json reply = SendPutRequest(requestJson, "/state", CURRENT_FILE_INFO);
// Check whether request was successful
return utils::validateReplyForLight(requestJson, reply, id);
}
} |
Sorry, although I have used git commerically (via a GUI), I am note sure with Github how to find the development branch to get your intermediate code. |
Did you clone the repo via git or downloaded it as a zip file? When you download a zip, you can select the branch on github and then download that branch. |
Hi Jan,
I have succeeded in using the clone feature to download the development
branch as a zip file.
I noticed that many of the #include files use "<" & ">" around include file
paths which my VS2019 compiler does not like as this form is reserved for
C, C++ provided files such as #include <stdio.h>.
User include files should be in the form:
#include "../include/WinHttpHandler.h"
(my paths aren't identical to yours yet).
Do you want me to raise an official "Issue" for this, or would you prefer
this email?
regards
Richard
…On Fri, 26 Jun 2020 at 11:58, Jan ***@***.***> wrote:
Did you clone the repo via git or downloaded it as a zip file?
If you cloned it, you can use a git command line
git stash (to save your changes, because they conflict)
git checkout development
When you download a zip, you can select the branch on github and then
download that branch.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#49 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AEAPOITMW6B4J6HMZJPMUGTRYR5LJANCNFSM4OJGFJZQ>
.
|
The only difference between < and " is that " also searches the current file path. Because the include directory is always added by cmake, there is no difference between the two when used to access the header files from the cpp files. The rest is just personal preference. |
Hi Jan,
I checked in K&R ANSI ‘C’ version on page 88 and found indeed, exactly as you say, the “ delimited causes the search for the include file to start with the folder where the .cpp file was found.
However in my Visual Studio 2019 environment, I am not using “cmake”, so for me the code will not compile.
So it would be more consistent to use “ for user includes and <> for system files. The code would be more portable across more environments.
I might be able to work around it in my Raspberry Pi environment with compiler options in the make file, but it would certainly be easier for my compilations to work there with consistent use of “” and <>.
regards
Richard Croxall
Barn End, Stockbridge Road,
Timsbury,
Romsey,
Hampshire,
SO51 0NF, UK
Tel: 01794 368853
Mobile: 07973 208001
From: Jan <notifications@github.com>
Sent: 26 June 2020 14:56
To: enwi/hueplusplus <hueplusplus@noreply.github.com>
Cc: RichardCroxall <github@croxalls.co.uk>; Author <author@noreply.github.com>
Subject: Re: [enwi/hueplusplus] Performance for multiple attributes (brightness, colour loop & colour) for multiple lamps is a bit slow (#49)
The only difference between < and " is that " also searches the current file path. Because the include directory is always added by cmake, there is no difference between the two when used to access the header files from the cpp files. The rest is just personal preference.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub <#49 (comment)> , or unsubscribe <https://github.com/notifications/unsubscribe-auth/AEAPOIUQ6ITYVNZULL4MMLTRYSSHFANCNFSM4OJGFJZQ> . <https://github.com/notifications/beacon/AEAPOITROWAEIOENI2A3RTDRYSSHFA5CNFSM4OJGFJZ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOE3ASTDY.gif>
|
When you do not use cmake, you will have to add the include directory to the include path, which you should do either way when using the library so you don't have to specify relative paths everywhere. |
Hi Jan, The reason for this requirement is that I have a rules language in which you can say: |
Thank you for the feedback. I did not think of this use case. This can be made to work, however it then requires additional care by the user. |
It is for my requirement. For other people, I don't know. |
Maybe I'm biased because I am thinking only about my own application but where a person wants to directly operate a lamp, I think he will be using a phone app to directly control the lamp. |
Hi Jan, you will be pleased to hear that although I am probably not using any of your new features that after a fight with the makefile to add new files and add the "src/" folder, the code compiles and switches my 6 lights off an on again with brightness, colours and color looping. |
The above is on the target environment of a raspberry pi rather than Windows. |
Hey Richard we actually created this library for our home automation system Home++, because we did not like the other available libraries. |
Hi Jan, |
Very nice, I'm closing this issue then. |
I am really pleased that I found this library. It has exactly the functionality I needed for my home automation system.
However when I switch on the the television, I also want to switch on 6 lamps dimmed with different colours to colour loop.
This takes a couple of seconds on my raspberry pi.
To make it go a bit faster I have written a new HueLightHelper class which inherits from HueLight.
This sends a single JSON message to each lamp with brightness, colour and colour loop in the same message.
This roughly halves the time to complete.
Rather than a single call to transfer information, multiple calls are required to initialise, set up each attribute and finally send each message. It's harder to use than the original information, but I thought the performance improvement worthwhile.
My C++ is a bit rusty as I haven't done any commercially for 20 years, so you might feel the need to polish the code, but I thing the idea is sound!
The text was updated successfully, but these errors were encountered: