Clean up code

Use tabs for indenting to make someone happy...
This commit is contained in:
johan 2013-08-13 20:10:19 +02:00
parent 463e7626b9
commit a04af1242c
6 changed files with 297 additions and 270 deletions

View File

@ -1,5 +1,6 @@
#pragma once
/// Simple structure to contain the values of a color transformation
struct ColorTransformValues
{
double valueRed;

View File

@ -1,5 +1,7 @@
// stl includes
#include <stdexcept>
// hyperion-remote includes
#include "Connection.h"
Connection::Connection(const std::string & a, bool printJson) :
@ -69,9 +71,9 @@ bool Connection::setTransform(ColorTransformValues *threshold, ColorTransformVal
return false;
}
bool Connection::sendMessage(const Json::Value & message)
Json::Value Connection::sendMessage(const Json::Value & message)
{
// rpint if requested
// print command if requested
if (_printJson)
{
std::cout << "Command: " << message << std::endl;
@ -103,11 +105,12 @@ bool Connection::sendMessage(const Json::Value & message)
throw std::runtime_error("Error while parsing reply:" + serializedReply);
}
// print reply if requested
if (_printJson)
{
std::cout << "Reply:" << reply << std::endl;
}
return true;
return reply;
}

View File

@ -1,30 +1,48 @@
#pragma once
// stl includes
#include <string>
// Qt includes
#include <QColor>
#include <QImage>
#include <QTcpSocket>
// jsoncpp includes
#include <json/json.h>
// hyperion-remote includes
#include "ColorTransformValues.h"
/// Connection class to setup an connection to the hyperion server and execute commands
class Connection
{
public:
Connection(const std::string & address, bool printJson);
~Connection();
/// Set all leds to the specified color
bool setColor(QColor color, int priority, int duration);
/// Set the leds according to the given image (assume the image is stretched to the display size)
bool setImage(QImage image, int priority, int duration);
/// Retrieve a list of all occupied priority channels
bool listPriorities();
/// Clear the given priority channel
bool clear(int priority);
/// Clear all priority channels
bool clearAll();
/// Set the color transform of the leds
/// Note that providing a NULL will leave the settings on the server unchanged
bool setTransform(ColorTransformValues * threshold, ColorTransformValues * gamma, ColorTransformValues * blacklevel, ColorTransformValues * whitelevel);
private:
bool sendMessage(const Json::Value & message);
/// Send a json command message and receive its reply
Json::Value sendMessage(const Json::Value & message);
private:
bool _printJson;

View File

@ -1,16 +1,21 @@
#pragma once
// Qt includes
#include <QColor>
#include <QImage>
// getoptPlusPLus includes
#include <getoptPlusPlus/getoptpp.h>
// hyperion-remote includes
#include "ColorTransformValues.h"
typedef vlofgren::PODParameter<QColor> ColorParameter;
typedef vlofgren::PODParameter<QImage> ImageParameter;
typedef vlofgren::PODParameter<ColorTransformValues> TransformParameter;
namespace vlofgren {
template<>
QColor ColorParameter::validate(const std::string& s) throw (Parameter::ParameterRejected)

View File

@ -1,19 +1,23 @@
// stl includes
#include <initializer_list>
// Qt includes
#include <QCoreApplication>
// getoptPlusPLus includes
#include <getoptPlusPlus/getoptpp.h>
// hyperion-remote include
#include "CustomParameter.h"
#include "Connection.h"
using namespace vlofgren;
/// Count the number of true values in a list of booleans
int count(std::initializer_list<bool> values)
{
int count = 0;
for (auto& value : values) {
for (bool value : values) {
if (value)
count++;
}
@ -26,10 +30,11 @@ int main(int argc, char * argv[])
try
{
// some settings
// some default settings
QString defaultServerAddress = "localhost:19444";
int defaultPriority = 100;
// create the option parser and initialize all parameters
OptionsParser optionParser("Simple application to send a command to hyperion using the Json interface");
ParameterSet & parameters = optionParser.getParameters();
StringParameter & argAddress = parameters.add<StringParameter> ('a', "address" , QString("Set the address of the hyperion server [default: %1]").arg(defaultServerAddress).toAscii().constData());
@ -47,41 +52,35 @@ int main(int argc, char * argv[])
SwitchParameter<> & argPrint = parameters.add<SwitchParameter<> >(0x0, "print" , "Print the json input and output messages on stdout");
SwitchParameter<> & argHelp = parameters.add<SwitchParameter<> >('h', "help" , "Show this help message and exit");
// set the default values
argAddress.setDefault(defaultServerAddress.toStdString());
argPriority.setDefault(defaultPriority);
argDuration.setDefault(-1);
// parse the options
// parse all options
optionParser.parse(argc, const_cast<const char **>(argv));
// check if we need to display the usage
// check if we need to display the usage. exit if we do.
if (argHelp.isSet())
{
optionParser.usage();
return 0;
}
// check if a color transform is set
// check if at least one of the available color transforms is set
bool colorTransform = argThreshold.isSet() || argGamma.isSet() || argBlacklevel.isSet() || argWhitelevel.isSet();
// check if exactly one command was given
// check that exactly one command was given
int commandCount = count({argColor.isSet(), argImage.isSet(), argList.isSet(), argClear.isSet(), argClearAll.isSet(), colorTransform});
if (commandCount != 1)
{
if (commandCount == 0)
{
std::cerr << "No command found. Provide one of the following options:" << std::endl;
}
else
{
std::cerr << "Multiple commands found. Provide one of the following options:" << std::endl;
}
std::cerr << (commandCount == 0 ? "No command found." : "Multiple commands found.") << " Provide exactly one of the following options:" << std::endl;
std::cerr << " " << argColor.usageLine() << std::endl;
std::cerr << " " << argImage.usageLine() << std::endl;
std::cerr << " " << argList.usageLine() << std::endl;
std::cerr << " " << argClear.usageLine() << std::endl;
std::cerr << " " << argClearAll.usageLine() << std::endl;
std::cerr << "or one or more of the color transformations:" << std::endl;
std::cerr << "or one or more of the available color transformations:" << std::endl;
std::cerr << " " << argThreshold.usageLine() << std::endl;
std::cerr << " " << argGamma.usageLine() << std::endl;
std::cerr << " " << argBlacklevel.usageLine() << std::endl;
@ -89,7 +88,7 @@ int main(int argc, char * argv[])
return 1;
}
// create the connection
// create the connection to the hyperion server
Connection connection(argAddress.getValue(), argPrint.isSet());
// now execute the given command
@ -129,6 +128,7 @@ int main(int argc, char * argv[])
}
catch (const std::runtime_error & e)
{
// An error occured. Display error and quit
std::cerr << e.what() << std::endl;
return 1;
}