Hyperion-remote changed to support multiple colors in the setColor command

Former-commit-id: f0acda054056d6fa937e9cbd2b22686979911bc9
This commit is contained in:
johan 2013-12-13 00:01:48 +01:00
parent cf7f66bc40
commit c11808878f
4 changed files with 37 additions and 20 deletions

View File

@ -11,7 +11,7 @@
#include "ColorTransformValues.h"
/// Data parameter for a color
typedef vlofgren::PODParameter<QColor> ColorParameter;
typedef vlofgren::PODParameter<std::vector<QColor>> ColorParameter;
/// Data parameter for an image
typedef vlofgren::PODParameter<QImage> ImageParameter;
@ -21,40 +21,54 @@ typedef vlofgren::PODParameter<ColorTransformValues> TransformParameter;
namespace vlofgren {
///
/// Translates a string (as passed on the commandline) to a color
/// Translates a string (as passed on the commandline) to a vector of colors
///
/// @param[in] s The string (as passed on the commandline)
///
/// @return The translated color
/// @return The translated colors
///
/// @throws Parameter::ParameterRejected If the string did not result in a color
///
template<>
QColor ColorParameter::validate(const std::string& s) throw (Parameter::ParameterRejected)
std::vector<QColor> ColorParameter::validate(const std::string& s) throw (Parameter::ParameterRejected)
{
// Check if we can create the color by name
QColor color(s.c_str());
if (color.isValid())
{
return color;
return std::vector<QColor>{color};
}
// check if we can create the color by hex RRGGBB value
if (s.length() == 6 && isxdigit(s[0]) && isxdigit(s[1]) && isxdigit(s[2]) && isxdigit(s[3]) && isxdigit(s[4]) && isxdigit(s[5]))
if (s.length() >= 6u && (s.length()%6) == 0u && std::count_if(s.begin(), s.end(), isxdigit) == s.length())
{
bool ok = true;
int rgb[3];
for (int i = 0; i < 3 && ok; ++i)
std::vector<QColor> colors;
for (size_t j = 0; j < s.length()/6; ++j)
{
QString colorComponent(s.substr(2*i, 2).c_str());
rgb[i] = colorComponent.toInt(&ok, 16);
int rgb[3];
for (int i = 0; i < 3 && ok; ++i)
{
QString colorComponent(s.substr(6*j+2*i, 2).c_str());
rgb[i] = colorComponent.toInt(&ok, 16);
}
if (ok)
{
color.setRgb(rgb[0], rgb[1], rgb[2]);
colors.push_back(color);
}
else
{
break;
}
}
// check if all components parsed succesfully
if (ok)
{
color.setRgb(rgb[0], rgb[1], rgb[2]);
return color;
return colors;
}
}
@ -65,7 +79,7 @@ namespace vlofgren {
}
throw Parameter::ParameterRejected(errorMessage.str());
return color;
return std::vector<QColor>{color};
}
template<>

View File

@ -39,18 +39,21 @@ JsonConnection::~JsonConnection()
_socket.close();
}
void JsonConnection::setColor(QColor color, int priority, int duration)
void JsonConnection::setColor(std::vector<QColor> colors, int priority, int duration)
{
std::cout << "Set color to " << color.red() << " " << color.green() << " " << color.blue() << std::endl;
std::cout << "Set color to " << colors[0].red() << " " << colors[0].green() << " " << colors[0].blue() << (colors.size() > 1 ? " + ..." : "") << std::endl;
// create command
Json::Value command;
command["command"] = "color";
command["priority"] = priority;
Json::Value & rgbValue = command["color"];
rgbValue[0] = color.red();
rgbValue[1] = color.green();
rgbValue[2] = color.blue();
for (const QColor & color : colors)
{
rgbValue.append(color.red());
rgbValue.append(color.green());
rgbValue.append(color.blue());
}
if (duration > 0)
{
command["duration"] = duration;

View File

@ -41,7 +41,7 @@ public:
/// @param priority The priority
/// @param duration The duration in milliseconds
///
void setColor(QColor color, int priority, int duration);
void setColor(std::vector<QColor> color, int priority, int duration);
///
/// Set the leds according to the given image (assume the image is stretched to the display size)

View File

@ -40,7 +40,7 @@ int main(int argc, char * argv[])
StringParameter & argAddress = parameters.add<StringParameter> ('a', "address" , QString("Set the address of the hyperion server [default: %1]").arg(defaultServerAddress).toAscii().constData());
IntParameter & argPriority = parameters.add<IntParameter> ('p', "priority" , QString("Use to the provided priority channel (the lower the number, the higher the priority) [default: %1]").arg(defaultPriority).toAscii().constData());
IntParameter & argDuration = parameters.add<IntParameter> ('d', "duration" , "Specify how long the leds should be switched on in millseconds [default: infinity]");
ColorParameter & argColor = parameters.add<ColorParameter> ('c', "color" , "Set all leds to a constant color (either RRGGBB hex value or a color name)");
ColorParameter & argColor = parameters.add<ColorParameter> ('c', "color" , "Set all leds to a constant color (either RRGGBB hex value or a color name. The color may be repeated multiple time like: RRGGBBRRGGBB)");
ImageParameter & argImage = parameters.add<ImageParameter> ('i', "image" , "Set the leds to the colors according to the given image file");
StringParameter & argEffect = parameters.add<StringParameter> ('e', "effect" , "Enable the effect with the given name");
StringParameter & argEffectArgs = parameters.add<StringParameter> (0x0, "effectArgs", "Arguments to use in combination with the specified effect. Should be a Json object string.");