This commit is contained in:
T. van der Zwan 2013-08-21 20:46:43 +00:00
commit decc41965d
27 changed files with 913 additions and 164 deletions

View File

@ -12,11 +12,14 @@
},
"color" :
{
"hsv" : {
"saturationGain" : 1.0,
"valueGain" : 1.0
},
"red" :
{
"threshold" : 0.0,
"gamma" : 1.0,
"adjust" : 1.0,
"blacklevel" : 0.0,
"whitelevel" : 1.0
},
@ -24,7 +27,6 @@
{
"threshold" : 0.0,
"gamma" : 1.0,
"adjust" : 1.0,
"blacklevel" : 0.0,
"whitelevel" : 1.0
},
@ -32,7 +34,6 @@
{
"threshold" : 0.0,
"gamma" : 1.0,
"adjust" : 1.0,
"blacklevel" : 0.0,
"whitelevel" : 1.0
}

View File

@ -39,7 +39,7 @@ public slots:
private:
const int _updateInterval_ms;
const int _timeout_ms;
const int _priority;
QTimer _timer;

View File

@ -1,6 +1,8 @@
#pragma once
// stl includes
#include <list>
// QT includes
#include <QObject>
#include <QTimer>
@ -14,13 +16,27 @@
#include <hyperion/PriorityMuxer.h>
// Forward class declaration
namespace hyperion { class ColorTransform; }
namespace hyperion {
class HsvTransform;
class ColorTransform;
}
class Hyperion : public QObject
{
Q_OBJECT
public:
typedef PriorityMuxer::InputInfo InputInfo;
enum Color
{
RED, GREEN, BLUE, INVALID
};
enum Transform
{
SATURATION_GAIN, VALUE_GAIN, THRESHOLD, GAMMA, BLACKLEVEL, WHITELEVEL
};
static LedString createLedString(const Json::Value& ledsConfig);
static Json::Value loadConfig(const std::string& configFile);
@ -32,7 +48,21 @@ public:
unsigned getLedCount() const;
void setValue(int priority, std::vector<RgbColor> &ledColors, const int timeout_ms);
void setColor(int priority, RgbColor &ledColor, const int timeout_ms);
void setColors(int priority, std::vector<RgbColor> &ledColors, const int timeout_ms);
void setTransform(Transform transform, Color color, double value);
void clear(int priority);
void clearall();
double getTransform(Transform transform, Color color) const;
QList<int> getActivePriorities() const;
const InputInfo& getPriorityInfo(const int priority) const;
private slots:
void update();
@ -40,15 +70,16 @@ private slots:
private:
void applyTransform(std::vector<RgbColor>& colors) const;
LedString mLedString;
LedString _ledString;
PriorityMuxer mMuxer;
PriorityMuxer _muxer;
hyperion::ColorTransform* mRedTransform;
hyperion::ColorTransform* mGreenTransform;
hyperion::ColorTransform* mBlueTransform;
hyperion::HsvTransform * _hsvTransform;
hyperion::ColorTransform * _redTransform;
hyperion::ColorTransform * _greenTransform;
hyperion::ColorTransform * _blueTransform;
LedDevice* mDevice;
LedDevice* _device;
QTimer _timer;
};

View File

@ -8,8 +8,7 @@
#include <hyperion/ImageProcessorFactory.h>
// Forward class declaration
namespace hyperion { class ImageToLedsMap;
class ColorTransform; }
namespace hyperion { class ImageToLedsMap; }
/**
* The ImageProcessor translates an RGB-image to RGB-values for the leds. The processing is

View File

@ -26,7 +26,7 @@ public:
std::vector<RgbColor> ledColors;
};
PriorityMuxer();
PriorityMuxer(int ledCount);
~PriorityMuxer();
@ -47,9 +47,11 @@ public:
void setCurrentTime(const int64_t& now);
private:
int mCurrentPriority;
int _currentPriority;
QMap<int, InputInfo> mActiveInputs;
QMap<int, InputInfo> _activeInputs;
const static int MAX_PRIORITY = std::numeric_limits<int>::max();
InputInfo _lowestPriorityInfo;
const static int LOWEST_PRIORITY = std::numeric_limits<int>::max();
};

View File

@ -15,6 +15,7 @@
DispmanxWrapper::DispmanxWrapper(const unsigned grabWidth, const unsigned grabHeight, const unsigned updateRate_Hz, Hyperion * hyperion) :
_updateInterval_ms(1000/updateRate_Hz),
_timeout_ms(2 * _updateInterval_ms),
_priority(128),
_timer(),
_image(grabWidth, grabHeight),
_frameGrabber(new DispmanxFrameGrabber(grabWidth, grabHeight)),
@ -52,8 +53,7 @@ void DispmanxWrapper::action()
_processor->process(_image, _ledColors);
const int _priority = 100;
_hyperion->setValue(_priority, _ledColors, _timeout_ms);
_hyperion->setColors(_priority, _ledColors, _timeout_ms);
}
void DispmanxWrapper::stop()
{

View File

@ -21,6 +21,7 @@ SET(Hyperion_HEADERS
${CURRENT_SOURCE_DIR}/BlackBorderProcessor.h
${CURRENT_SOURCE_DIR}/BlackBorderDetector.h
${CURRENT_SOURCE_DIR}/ColorTransform.h
${CURRENT_SOURCE_DIR}/HsvTransform.h
)
SET(Hyperion_SOURCES
@ -36,6 +37,7 @@ SET(Hyperion_SOURCES
${CURRENT_SOURCE_DIR}/BlackBorderProcessor.cpp
${CURRENT_SOURCE_DIR}/BlackBorderDetector.cpp
${CURRENT_SOURCE_DIR}/ColorTransform.cpp
${CURRENT_SOURCE_DIR}/HsvTransform.cpp
)
set(Hyperion_RESOURCES

View File

@ -0,0 +1,134 @@
#include "HsvTransform.h"
using namespace hyperion;
HsvTransform::HsvTransform() :
_saturationGain(1.0),
_valueGain(1.0)
{
}
HsvTransform::HsvTransform(double saturationGain, double valueGain) :
_saturationGain(saturationGain),
_valueGain(valueGain)
{
}
HsvTransform::~HsvTransform()
{
}
void HsvTransform::setSaturationGain(double saturationGain)
{
_saturationGain = saturationGain;
}
double HsvTransform::getSaturationGain() const
{
return _saturationGain;
}
void HsvTransform::setValueGain(double valueGain)
{
_valueGain = valueGain;
}
double HsvTransform::getValueGain() const
{
return _valueGain;
}
void HsvTransform::transform(uint8_t & red, uint8_t & green, uint8_t & blue) const
{
if (_saturationGain != 1.0 || _valueGain != 1.0)
{
uint8_t hue, saturation, value;
rgb2hsv(red, green, blue, hue, saturation, value);
int s = saturation * _saturationGain;
if (s > 255)
saturation = 255;
else
saturation = s;
int v = value * _valueGain;
if (v > 255)
value = 255;
else
value = v;
hsv2rgb(hue, saturation, value, red, green, blue);
}
}
void HsvTransform::rgb2hsv(uint8_t red, uint8_t green, uint8_t blue, uint8_t & hue, uint8_t & saturation, uint8_t & value)
{
uint8_t rgbMin, rgbMax;
rgbMin = red < green ? (red < blue ? red : blue) : (green < blue ? green : blue);
rgbMax = red > green ? (red > blue ? red : blue) : (green > blue ? green : blue);
value = rgbMax;
if (value == 0)
{
hue = 0;
saturation = 0;
return;
}
saturation = 255 * long(rgbMax - rgbMin) / value;
if (saturation == 0)
{
hue = 0;
return;
}
if (rgbMax == red)
hue = 0 + 43 * (green - blue) / (rgbMax - rgbMin);
else if (rgbMax == green)
hue = 85 + 43 * (blue - red) / (rgbMax - rgbMin);
else
hue = 171 + 43 * (red - green) / (rgbMax - rgbMin);
}
void HsvTransform::hsv2rgb(uint8_t hue, uint8_t saturation, uint8_t value, uint8_t & red, uint8_t & green, uint8_t & blue)
{
uint8_t region, remainder, p, q, t;
if (saturation == 0)
{
red = value;
green = value;
blue = value;
return;
}
region = hue / 43;
remainder = (hue - (region * 43)) * 6;
p = (value * (255 - saturation)) >> 8;
q = (value * (255 - ((saturation * remainder) >> 8))) >> 8;
t = (value * (255 - ((saturation * (255 - remainder)) >> 8))) >> 8;
switch (region)
{
case 0:
red = value; green = t; blue = p;
break;
case 1:
red = q; green = value; blue = p;
break;
case 2:
red = p; green = value; blue = t;
break;
case 3:
red = p; green = q; blue = value;
break;
case 4:
red = t; green = p; blue = value;
break;
default:
red = value; green = p; blue = q;
break;
}
}

View File

@ -0,0 +1,33 @@
#pragma once
#include <cstdint>
namespace hyperion
{
class HsvTransform
{
public:
HsvTransform();
HsvTransform(double saturationGain, double valueGain);
~HsvTransform();
void setSaturationGain(double saturationGain);
double getSaturationGain() const;
void setValueGain(double valueGain);
double getValueGain() const;
void transform(uint8_t & red, uint8_t & green, uint8_t & blue) const;
private:
// integer version of the conversion are faster, but a little less accurate
static void rgb2hsv(uint8_t red, uint8_t green, uint8_t blue, uint8_t & hue, uint8_t & saturation, uint8_t & value);
static void hsv2rgb(uint8_t hue, uint8_t saturation, uint8_t value, uint8_t & red, uint8_t & green, uint8_t & blue);
private:
double _saturationGain;
double _valueGain;
};
} // namespace hyperion

View File

@ -14,6 +14,7 @@
#include "LedDeviceWs2801.h"
#include "LedDeviceTest.h"
#include "ColorTransform.h"
#include "HsvTransform.h"
using namespace hyperion;
@ -44,6 +45,11 @@ LedDevice* constructDevice(const Json::Value& deviceConfig)
return device;
}
HsvTransform * createHsvTransform(const Json::Value & hsvConfig)
{
return new HsvTransform(hsvConfig["saturationGain"].asDouble(), hsvConfig["valueGain"].asDouble());
}
ColorTransform* createColorTransform(const Json::Value& colorConfig)
{
const double threshold = colorConfig["threshold"].asDouble();
@ -102,74 +108,221 @@ Hyperion::Hyperion(const std::string& configFile) :
}
Hyperion::Hyperion(const Json::Value &jsonConfig) :
mLedString(createLedString(jsonConfig["leds"])),
mRedTransform( createColorTransform(jsonConfig["color"]["red"])),
mGreenTransform(createColorTransform(jsonConfig["color"]["green"])),
mBlueTransform( createColorTransform(jsonConfig["color"]["blue"])),
mDevice(constructDevice(jsonConfig["device"])),
_ledString(createLedString(jsonConfig["leds"])),
_muxer(_ledString.leds().size()),
_hsvTransform(createHsvTransform(jsonConfig["color"]["hsv"])),
_redTransform(createColorTransform(jsonConfig["color"]["red"])),
_greenTransform(createColorTransform(jsonConfig["color"]["green"])),
_blueTransform(createColorTransform(jsonConfig["color"]["blue"])),
_device(constructDevice(jsonConfig["device"])),
_timer()
{
ImageProcessorFactory::getInstance().init(mLedString);
ImageProcessorFactory::getInstance().init(_ledString);
_timer.setSingleShot(true);
QObject::connect(&_timer, SIGNAL(timeout()), this, SLOT(update()));
// initialize the leds
update();
}
Hyperion::~Hyperion()
{
// Delete the Led-String
delete mDevice;
delete _device;
// delete he hsv transform
delete _hsvTransform;
// Delete the color-transform
delete mBlueTransform;
delete mGreenTransform;
delete mRedTransform;
delete _blueTransform;
delete _greenTransform;
delete _redTransform;
}
unsigned Hyperion::getLedCount() const
{
return mLedString.leds().size();
return _ledString.leds().size();
}
void Hyperion::setValue(int priority, std::vector<RgbColor>& ledColors, const int timeout_ms)
void Hyperion::setColor(int priority, RgbColor & color, const int timeout_ms)
{
// Apply the transform to each led and color-channel
for (RgbColor& color : ledColors)
{
color.red = mRedTransform->transform(color.red);
color.green = mGreenTransform->transform(color.green);
color.blue = mBlueTransform->transform(color.blue);
}
// create led output
std::vector<RgbColor> ledColors(_ledString.leds().size(), color);
// set colors
setColors(priority, ledColors, timeout_ms);
}
void Hyperion::setColors(int priority, std::vector<RgbColor>& ledColors, const int timeout_ms)
{
if (timeout_ms > 0)
{
const uint64_t timeoutTime = QDateTime::currentMSecsSinceEpoch() + timeout_ms;
mMuxer.setInput(priority, ledColors, timeoutTime);
_muxer.setInput(priority, ledColors, timeoutTime);
}
else
{
mMuxer.setInput(priority, ledColors);
_muxer.setInput(priority, ledColors);
}
if (priority == mMuxer.getCurrentPriority())
if (priority == _muxer.getCurrentPriority())
{
update();
}
}
void Hyperion::setTransform(Hyperion::Transform transform, Hyperion::Color color, double value)
{
// select the transform of the requested color
ColorTransform * t = nullptr;
switch (color)
{
case RED:
t = _redTransform;
break;
case GREEN:
t = _greenTransform;
break;
case BLUE:
t = _blueTransform;
break;
default:
break;
}
// set transform value
switch (transform)
{
case SATURATION_GAIN:
_hsvTransform->setSaturationGain(value);
break;
case VALUE_GAIN:
_hsvTransform->setValueGain(value);
break;
case THRESHOLD:
assert (t != nullptr);
t->setThreshold(value);
break;
case GAMMA:
assert (t != nullptr);
t->setGamma(value);
break;
case BLACKLEVEL:
assert (t != nullptr);
t->setBlacklevel(value);
break;
case WHITELEVEL:
assert (t != nullptr);
t->setWhitelevel(value);
break;
default:
assert(false);
}
// update the led output
update();
}
void Hyperion::clear(int priority)
{
if (_muxer.hasPriority(priority))
{
_muxer.clearInput(priority);
// update leds if necessary
if (priority < _muxer.getCurrentPriority());
{
update();
}
}
}
void Hyperion::clearall()
{
_muxer.clearAll();
// update leds
update();
}
double Hyperion::getTransform(Hyperion::Transform transform, Hyperion::Color color) const
{
// select the transform of the requested color
ColorTransform * t = nullptr;
switch (color)
{
case RED:
t = _redTransform;
break;
case GREEN:
t = _greenTransform;
break;
case BLUE:
t = _blueTransform;
break;
default:
break;
}
// set transform value
switch (transform)
{
case SATURATION_GAIN:
return _hsvTransform->getSaturationGain();
case VALUE_GAIN:
return _hsvTransform->getValueGain();
case THRESHOLD:
assert (t != nullptr);
return t->getThreshold();
case GAMMA:
assert (t != nullptr);
return t->getGamma();
case BLACKLEVEL:
assert (t != nullptr);
return t->getBlacklevel();
case WHITELEVEL:
assert (t != nullptr);
return t->getWhitelevel();
default:
assert(false);
}
return 999.0;
}
QList<int> Hyperion::getActivePriorities() const
{
return _muxer.getPriorities();
}
const Hyperion::InputInfo &Hyperion::getPriorityInfo(const int priority) const
{
return _muxer.getInputInfo(priority);
}
void Hyperion::update()
{
// Update the muxer, cleaning obsolete priorities
mMuxer.setCurrentTime(QDateTime::currentMSecsSinceEpoch());
_muxer.setCurrentTime(QDateTime::currentMSecsSinceEpoch());
// Obtain the current priority channel
int priority = mMuxer.getCurrentPriority();
const PriorityMuxer::InputInfo & priorityInfo = mMuxer.getInputInfo(priority);
int priority = _muxer.getCurrentPriority();
const PriorityMuxer::InputInfo & priorityInfo = _muxer.getInputInfo(priority);
// Apply the transform to each led and color-channel
std::vector<RgbColor> ledColors(priorityInfo.ledColors);
for (RgbColor& color : ledColors)
{
_hsvTransform->transform(color.red, color.green, color.blue);
color.red = _redTransform->transform(color.red);
color.green = _greenTransform->transform(color.green);
color.blue = _blueTransform->transform(color.blue);
}
// Write the data to the device
mDevice->write(priorityInfo.ledColors);
_device->write(ledColors);
// Start the timeout-timer
if (priorityInfo.timeoutTime_ms == -1)

View File

@ -6,10 +6,14 @@
// Hyperion includes
#include <hyperion/PriorityMuxer.h>
PriorityMuxer::PriorityMuxer() :
mCurrentPriority(MAX_PRIORITY)
PriorityMuxer::PriorityMuxer(int ledCount) :
_currentPriority(LOWEST_PRIORITY),
_activeInputs(),
_lowestPriorityInfo()
{
// empty
_lowestPriorityInfo.priority = LOWEST_PRIORITY;
_lowestPriorityInfo.timeoutTime_ms = -1;
_lowestPriorityInfo.ledColors = std::vector<RgbColor>(ledCount, {0, 0, 0});
}
PriorityMuxer::~PriorityMuxer()
@ -19,23 +23,28 @@ PriorityMuxer::~PriorityMuxer()
int PriorityMuxer::getCurrentPriority() const
{
return mCurrentPriority;
return _currentPriority;
}
QList<int> PriorityMuxer::getPriorities() const
{
return mActiveInputs.keys();
return _activeInputs.keys();
}
bool PriorityMuxer::hasPriority(const int priority) const
{
return mActiveInputs.contains(priority);
return _activeInputs.contains(priority);
}
const PriorityMuxer::InputInfo& PriorityMuxer::getInputInfo(const int priority) const
{
auto elemIt = mActiveInputs.find(priority);
if (elemIt == mActiveInputs.end())
if (priority == LOWEST_PRIORITY)
{
return _lowestPriorityInfo;
}
auto elemIt = _activeInputs.find(priority);
if (elemIt == _activeInputs.end())
{
throw std::runtime_error("no such priority");
}
@ -44,50 +53,50 @@ const PriorityMuxer::InputInfo& PriorityMuxer::getInputInfo(const int priority)
void PriorityMuxer::setInput(const int priority, const std::vector<RgbColor>& ledColors, const int64_t timeoutTime_ms)
{
InputInfo& input = mActiveInputs[priority];
InputInfo& input = _activeInputs[priority];
input.priority = priority;
input.timeoutTime_ms = timeoutTime_ms;
input.ledColors = ledColors;
mCurrentPriority = std::min(mCurrentPriority, priority);
_currentPriority = std::min(_currentPriority, priority);
}
void PriorityMuxer::clearInput(const int priority)
{
mActiveInputs.remove(priority);
if (mCurrentPriority == priority)
_activeInputs.remove(priority);
if (_currentPriority == priority)
{
if (mActiveInputs.empty())
if (_activeInputs.empty())
{
mCurrentPriority = MAX_PRIORITY;
_currentPriority = LOWEST_PRIORITY;
}
else
{
QList<int> keys = mActiveInputs.keys();
mCurrentPriority = *std::min_element(keys.begin(), keys.end());
QList<int> keys = _activeInputs.keys();
_currentPriority = *std::min_element(keys.begin(), keys.end());
}
}
}
void PriorityMuxer::clearAll()
{
mActiveInputs.clear();
mCurrentPriority = MAX_PRIORITY;
_activeInputs.clear();
_currentPriority = LOWEST_PRIORITY;
}
void PriorityMuxer::setCurrentTime(const int64_t& now)
{
mCurrentPriority = MAX_PRIORITY;
_currentPriority = LOWEST_PRIORITY;
for (auto infoIt = mActiveInputs.begin(); infoIt != mActiveInputs.end();)
for (auto infoIt = _activeInputs.begin(); infoIt != _activeInputs.end();)
{
if (infoIt->timeoutTime_ms != -1 && infoIt->timeoutTime_ms <= now)
{
infoIt = mActiveInputs.erase(infoIt);
infoIt = _activeInputs.erase(infoIt);
}
else
{
mCurrentPriority = std::min(mCurrentPriority, infoIt->priority);
_currentPriority = std::min(_currentPriority, infoIt->priority);
++infoIt;
}
}

View File

@ -33,6 +33,23 @@
"type":"object",
"required":true,
"properties": {
"hsv" : {
"type" : "object",
"required" : true,
"properties" : {
"saturationGain" : {
"type" : "number",
"required" : true,
"minimum" : 0.0
},
"valueGain" : {
"type" : "number",
"required" : true,
"minimum" : 0.0
}
},
"additionalProperties" : false
},
"red": {
"type":"object",
"required":true,
@ -41,10 +58,6 @@
"type":"number",
"required":true
},
"adjust": {
"type":"number",
"required":true
},
"blacklevel": {
"type":"number",
"required":true
@ -55,7 +68,9 @@
},
"threshold": {
"type":"number",
"required":true
"required":true,
"minimum" : 0.0,
"maximum" : 1.0
}
}
},
@ -67,13 +82,19 @@
"type":"number",
"required":true
},
"adjust": {
"type":"number",
"required":true
},
"blacklevel": {
"type":"number",
"required":true
},
"whitelevel": {
"type":"number",
"required":true
},
"threshold": {
"type":"number",
"required":true,
"minimum" : 0.0,
"maximum" : 1.0
}
}
},
@ -85,13 +106,19 @@
"type":"number",
"required":true
},
"adjust": {
"whitelevel": {
"type":"number",
"required":true
},
"blacklevel": {
"type":"number",
"required":true
},
"threshold": {
"type":"number",
"required":true,
"minimum" : 0.0,
"maximum" : 1.0
}
}
}

View File

@ -9,27 +9,23 @@
// Qt includes
#include <QResource>
#include <QDateTime>
// hyperion util includes
#include "hyperion/ImageProcessorFactory.h"
#include "hyperion/ImageProcessor.h"
#include "utils/RgbColor.h"
// project includes
#include "JsonClientConnection.h"
JsonClientConnection::JsonClientConnection(QTcpSocket *socket) :
JsonClientConnection::JsonClientConnection(QTcpSocket *socket, Hyperion * hyperion) :
QObject(),
_socket(socket),
_schemaChecker(),
_imageProcessor(ImageProcessorFactory::getInstance().newImageProcessor()),
_hyperion(hyperion),
_receiveBuffer()
{
// read the json schema from the resource
QResource schemaData(":/schema.json");
assert(schemaData.isValid());
Json::Reader jsonReader;
Json::Value schemaJson;
if (!jsonReader.parse(reinterpret_cast<const char *>(schemaData.data()), reinterpret_cast<const char *>(schemaData.data()) + schemaData.size(), schemaJson, false))
{
throw std::runtime_error("Schema error: " + jsonReader.getFormattedErrorMessages()) ;
}
_schemaChecker.setSchema(schemaJson);
// connect internal signals and slots
connect(_socket, SIGNAL(disconnected()), this, SLOT(socketClosed()));
connect(_socket, SIGNAL(readyRead()), this, SLOT(readData()));
@ -64,33 +60,211 @@ void JsonClientConnection::socketClosed()
emit connectionClosed(this);
}
void JsonClientConnection::handleMessage(const std::string &message)
void JsonClientConnection::handleMessage(const std::string &messageString)
{
Json::Reader reader;
Json::Value messageRoot;
if (!reader.parse(message, messageRoot, false))
Json::Value message;
if (!reader.parse(messageString, message, false))
{
sendErrorReply("Error while parsing json: " + reader.getFormattedErrorMessages());
return;
}
if (!_schemaChecker.validate(messageRoot))
// check basic message
std::string errors;
if (!checkJson(message, ":schema", errors))
{
const std::list<std::string> & errors = _schemaChecker.getMessages();
std::stringstream ss;
ss << "Error while validating json: {";
foreach (const std::string & error, errors) {
ss << error << ", ";
}
ss << "}";
sendErrorReply(ss.str());
sendErrorReply("Error while validating json: " + errors);
return;
}
handleNotImplemented(messageRoot);
// check specific message
const std::string command = message["command"].asString();
if (!checkJson(message, QString(":schema-%1").arg(QString::fromStdString(command)), errors))
{
sendErrorReply("Error while validating json: " + errors);
return;
}
// switch over all possible commands and handle them
if (command == "color")
handleColorCommand(message);
else if (command == "image")
handleImageCommand(message);
else if (command == "serverinfo")
handleServerInfoCommand(message);
else if (command == "clear")
handleClearCommand(message);
else if (command == "clearall")
handleClearallCommand(message);
else if (command == "transform")
handleTransformCommand(message);
else
handleNotImplemented();
}
void JsonClientConnection::handleNotImplemented(const Json::Value & message)
void JsonClientConnection::handleColorCommand(const Json::Value &message)
{
// extract parameters
int priority = message["priority"].asInt();
int duration = message.get("duration", -1).asInt();
RgbColor color = {uint8_t(message["color"][0u].asInt()), uint8_t(message["color"][1u].asInt()), uint8_t(message["color"][2u].asInt())};
// set output
_hyperion->setColor(priority, color, duration);
// send reply
sendSuccessReply();
}
void JsonClientConnection::handleImageCommand(const Json::Value &message)
{
// extract parameters
int priority = message["priority"].asInt();
int duration = message.get("duration", -1).asInt();
int width = message["imagewidth"].asInt();
int height = message["imageheight"].asInt();
QByteArray data = QByteArray::fromBase64(QByteArray(message["imagedata"].asCString()));
// check consistency of the size of the received data
if (data.size() != width*height*3)
{
sendErrorReply("Size of image data does not match with the width and height");
return;
}
// set width and height of the image processor
_imageProcessor->setSize(width, height);
// create RgbImage
RgbImage image(width, height);
memcpy(image.memptr(), data.data(), data.size());
// process the image
std::vector<RgbColor> ledColors = _imageProcessor->process(image);
_hyperion->setColors(priority, ledColors, duration);
// send reply
sendSuccessReply();
}
void JsonClientConnection::handleServerInfoCommand(const Json::Value &message)
{
// create result
Json::Value result;
result["success"] = true;
Json::Value & info = result["info"];
// collect priority information
Json::Value & priorities = info["priorities"];
uint64_t now = QDateTime::currentMSecsSinceEpoch();
QList<int> activePriorities = _hyperion->getActivePriorities();
foreach (int priority, activePriorities) {
const Hyperion::InputInfo & priorityInfo = _hyperion->getPriorityInfo(priority);
Json::Value & item = priorities[priorities.size()];
item["priority"] = priority;
if (priorityInfo.timeoutTime_ms != -1)
{
item["duration_ms"] = priorityInfo.timeoutTime_ms - now;
}
}
// collect transform information
Json::Value & transform = info["transform"];
transform["saturationGain"] = _hyperion->getTransform(Hyperion::SATURATION_GAIN, Hyperion::INVALID);
transform["valueGain"] = _hyperion->getTransform(Hyperion::VALUE_GAIN, Hyperion::INVALID);
Json::Value & threshold = transform["threshold"];
threshold.append(_hyperion->getTransform(Hyperion::THRESHOLD, Hyperion::RED));
threshold.append(_hyperion->getTransform(Hyperion::THRESHOLD, Hyperion::GREEN));
threshold.append(_hyperion->getTransform(Hyperion::THRESHOLD, Hyperion::BLUE));
Json::Value & gamma = transform["gamma"];
gamma.append(_hyperion->getTransform(Hyperion::GAMMA, Hyperion::RED));
gamma.append(_hyperion->getTransform(Hyperion::GAMMA, Hyperion::GREEN));
gamma.append(_hyperion->getTransform(Hyperion::GAMMA, Hyperion::BLUE));
Json::Value & blacklevel = transform["blacklevel"];
blacklevel.append(_hyperion->getTransform(Hyperion::BLACKLEVEL, Hyperion::RED));
blacklevel.append(_hyperion->getTransform(Hyperion::BLACKLEVEL, Hyperion::GREEN));
blacklevel.append(_hyperion->getTransform(Hyperion::BLACKLEVEL, Hyperion::BLUE));
Json::Value & whitelevel = transform["whitelevel"];
whitelevel.append(_hyperion->getTransform(Hyperion::WHITELEVEL, Hyperion::RED));
whitelevel.append(_hyperion->getTransform(Hyperion::WHITELEVEL, Hyperion::GREEN));
whitelevel.append(_hyperion->getTransform(Hyperion::WHITELEVEL, Hyperion::BLUE));
// send the result
sendMessage(result);
}
void JsonClientConnection::handleClearCommand(const Json::Value &message)
{
// extract parameters
int priority = message["priority"].asInt();
// clear priority
_hyperion->clear(priority);
// send reply
sendSuccessReply();
}
void JsonClientConnection::handleClearallCommand(const Json::Value &)
{
// clear priority
_hyperion->clearall();
// send reply
sendSuccessReply();
}
void JsonClientConnection::handleTransformCommand(const Json::Value &message)
{
const Json::Value & transform = message["transform"];
if (transform.isMember("saturationGain"))
{
_hyperion->setTransform(Hyperion::SATURATION_GAIN, Hyperion::INVALID, transform["saturationGain"].asDouble());
}
if (transform.isMember("valueGain"))
{
_hyperion->setTransform(Hyperion::VALUE_GAIN, Hyperion::INVALID, transform["valueGain"].asDouble());
}
if (transform.isMember("threshold"))
{
const Json::Value & threshold = transform["threshold"];
_hyperion->setTransform(Hyperion::THRESHOLD, Hyperion::RED, threshold[0u].asDouble());
_hyperion->setTransform(Hyperion::THRESHOLD, Hyperion::GREEN, threshold[1u].asDouble());
_hyperion->setTransform(Hyperion::THRESHOLD, Hyperion::BLUE, threshold[2u].asDouble());
}
if (transform.isMember("gamma"))
{
const Json::Value & threshold = transform["gamma"];
_hyperion->setTransform(Hyperion::GAMMA, Hyperion::RED, threshold[0u].asDouble());
_hyperion->setTransform(Hyperion::GAMMA, Hyperion::GREEN, threshold[1u].asDouble());
_hyperion->setTransform(Hyperion::GAMMA, Hyperion::BLUE, threshold[2u].asDouble());
}
if (transform.isMember("blacklevel"))
{
const Json::Value & threshold = transform["blacklevel"];
_hyperion->setTransform(Hyperion::BLACKLEVEL, Hyperion::RED, threshold[0u].asDouble());
_hyperion->setTransform(Hyperion::BLACKLEVEL, Hyperion::GREEN, threshold[1u].asDouble());
_hyperion->setTransform(Hyperion::BLACKLEVEL, Hyperion::BLUE, threshold[2u].asDouble());
}
if (transform.isMember("whitelevel"))
{
const Json::Value & threshold = transform["whitelevel"];
_hyperion->setTransform(Hyperion::WHITELEVEL, Hyperion::RED, threshold[0u].asDouble());
_hyperion->setTransform(Hyperion::WHITELEVEL, Hyperion::GREEN, threshold[1u].asDouble());
_hyperion->setTransform(Hyperion::WHITELEVEL, Hyperion::BLUE, threshold[2u].asDouble());
}
sendSuccessReply();
}
void JsonClientConnection::handleNotImplemented()
{
sendErrorReply("Command not implemented");
}
@ -102,6 +276,16 @@ void JsonClientConnection::sendMessage(const Json::Value &message)
_socket->write(serializedReply.data(), serializedReply.length());
}
void JsonClientConnection::sendSuccessReply()
{
// create reply
Json::Value reply;
reply["success"] = true;
// send reply
sendMessage(reply);
}
void JsonClientConnection::sendErrorReply(const std::string &error)
{
// create reply
@ -112,3 +296,36 @@ void JsonClientConnection::sendErrorReply(const std::string &error)
// send reply
sendMessage(reply);
}
bool JsonClientConnection::checkJson(const Json::Value & message, const QString & schemaResource, std::string & errorMessage)
{
// read the json schema from the resource
QResource schemaData(schemaResource);
assert(schemaData.isValid());
Json::Reader jsonReader;
Json::Value schemaJson;
if (!jsonReader.parse(reinterpret_cast<const char *>(schemaData.data()), reinterpret_cast<const char *>(schemaData.data()) + schemaData.size(), schemaJson, false))
{
throw std::runtime_error("Schema error: " + jsonReader.getFormattedErrorMessages()) ;
}
// create schema checker
JsonSchemaChecker schema;
schema.setSchema(schemaJson);
// check the message
if (!schema.validate(message))
{
const std::list<std::string> & errors = schema.getMessages();
std::stringstream ss;
ss << "{";
foreach (const std::string & error, errors) {
ss << error << " ";
}
ss << "}";
errorMessage = ss.str();
return false;
}
return true;
}

View File

@ -10,15 +10,20 @@
// jsoncpp includes
#include <json/json.h>
// Hyperion includes
#include <hyperion/Hyperion.h>
// util includes
#include <utils/jsonschema/JsonSchemaChecker.h>
class ImageProcessor;
class JsonClientConnection : public QObject
{
Q_OBJECT
public:
JsonClientConnection(QTcpSocket * socket);
JsonClientConnection(QTcpSocket * socket, Hyperion * hyperion);
~JsonClientConnection();
signals:
@ -30,15 +35,27 @@ private slots:
private:
void handleMessage(const std::string & message);
void handleNotImplemented(const Json::Value & message);
void handleColorCommand(const Json::Value & message);
void handleImageCommand(const Json::Value & message);
void handleServerInfoCommand(const Json::Value & message);
void handleClearCommand(const Json::Value & message);
void handleClearallCommand(const Json::Value & message);
void handleTransformCommand(const Json::Value & message);
void handleNotImplemented();
void sendMessage(const Json::Value & message);
void sendSuccessReply();
void sendErrorReply(const std::string & error);
private:
bool checkJson(const Json::Value & message, const QString &schemaResource, std::string & errors);
private:
QTcpSocket * _socket;
JsonSchemaChecker _schemaChecker;
ImageProcessor * _imageProcessor;
Hyperion * _hyperion;
QByteArray _receiveBuffer;
};

View File

@ -37,12 +37,12 @@ uint16_t JsonServer::getPort() const
void JsonServer::newConnection()
{
std::cout << "New incoming json connection" << std::endl;
QTcpSocket * socket = _server.nextPendingConnection();
if (socket != nullptr)
{
JsonClientConnection * connection = new JsonClientConnection(socket);
std::cout << "New json connection" << std::endl;
JsonClientConnection * connection = new JsonClientConnection(socket, _hyperion);
_openConnections.insert(connection);
// register slot for cleaning up after the connection closed

View File

@ -1,5 +1,11 @@
<RCC>
<qresource prefix="/">
<file>schema.json</file>
</qresource>
<qresource prefix="/">
<file alias="schema">schema/schema.json</file>
<file alias="schema-color">schema/schema-color.json</file>
<file alias="schema-image">schema/schema-image.json</file>
<file alias="schema-serverinfo">schema/schema-serverinfo.json</file>
<file alias="schema-clear">schema/schema-clear.json</file>
<file alias="schema-clearall">schema/schema-clearall.json</file>
<file alias="schema-transform">schema/schema-transform.json</file>
</qresource>
</RCC>

View File

@ -0,0 +1,16 @@
{
"type":"object",
"required":true,
"properties":{
"command": {
"type" : "string",
"required" : true,
"enum" : ["clear"]
},
"priority": {
"type": "integer",
"required": true
}
},
"additionalProperties": false
}

View File

@ -0,0 +1,12 @@
{
"type":"object",
"required":true,
"properties":{
"command": {
"type" : "string",
"required" : true,
"enum" : ["clearall"]
}
},
"additionalProperties": false
}

View File

@ -0,0 +1,29 @@
{
"type":"object",
"required":true,
"properties":{
"command": {
"type" : "string",
"required" : true,
"enum" : ["color"]
},
"priority": {
"type": "integer",
"required": true
},
"duration": {
"type": "integer",
"required": false
},
"color": {
"type": "array",
"required": true,
"items" :{
"type" : "integer"
},
"minItems": 3,
"maxItems": 3
}
},
"additionalProperties": false
}

View File

@ -0,0 +1,34 @@
{
"type":"object",
"required":true,
"properties":{
"command": {
"type" : "string",
"required" : true,
"enum" : ["image"]
},
"priority": {
"type": "integer",
"required": true
},
"duration": {
"type": "integer",
"required": false
},
"imagewidth": {
"type" : "integer",
"required": true,
"minimum": 0
},
"imageheight": {
"type" : "integer",
"required": true,
"minimum": 0
},
"imagedata": {
"type": "string",
"required": true
}
},
"additionalProperties": false
}

View File

@ -0,0 +1,12 @@
{
"type":"object",
"required":true,
"properties":{
"command": {
"type" : "string",
"required" : true,
"enum" : ["serverinfo"]
}
},
"additionalProperties": false
}

View File

@ -5,43 +5,22 @@
"command": {
"type" : "string",
"required" : true,
"enum" : ["color", "image", "serverinfo", "clear", "clearall", "transform"]
},
"priority": {
"type": "integer",
"required": false
},
"duration": {
"type": "integer",
"required": false
},
"color": {
"type": "array",
"required": false,
"items" :{
"type" : "integer"
},
"minItems": 3,
"maxItems": 3
},
"imagewidth": {
"type" : "integer",
"required": false,
"minimum": 0
},
"imageheight": {
"type" : "integer",
"required": false,
"minimum": 0
},
"imagedata": {
"type": "string",
"required": false
"enum" : ["transform"]
},
"transform": {
"type": "object",
"required": false,
"required": true,
"properties": {
"saturationGain" : {
"type" : "double",
"required" : false,
"minimum" : 0.0
},
"valueGain" : {
"type" : "double",
"required" : false,
"minimum" : 0.0
},
"threshold": {
"type": "array",
"required": false,

View File

@ -0,0 +1,11 @@
{
"type":"object",
"required":true,
"properties":{
"command": {
"type" : "string",
"required" : true,
"enum" : ["color", "image", "serverinfo", "clear", "clearall", "transform"]
}
}
}

View File

@ -30,5 +30,5 @@ qt4_use_modules(hyperion-remote
Network)
target_link_libraries(hyperion-remote
hyperion-utils
jsoncpp
getoptPlusPlus)

View File

@ -160,7 +160,7 @@ void JsonConnection::clearAll()
parseReply(reply);
}
void JsonConnection::setTransform(ColorTransformValues *threshold, ColorTransformValues *gamma, ColorTransformValues *blacklevel, ColorTransformValues *whitelevel)
void JsonConnection::setTransform(double * saturation, double * value, ColorTransformValues *threshold, ColorTransformValues *gamma, ColorTransformValues *blacklevel, ColorTransformValues *whitelevel)
{
std::cout << "Set color transforms" << std::endl;
@ -169,6 +169,16 @@ void JsonConnection::setTransform(ColorTransformValues *threshold, ColorTransfor
command["command"] = "transform";
Json::Value & transform = command["transform"];
if (saturation != nullptr)
{
transform["saturationGain"] = *saturation;
}
if (value != nullptr)
{
transform["valueGain"] = *value;
}
if (threshold != nullptr)
{
Json::Value & v = transform["threshold"];
@ -180,25 +190,25 @@ void JsonConnection::setTransform(ColorTransformValues *threshold, ColorTransfor
if (gamma != nullptr)
{
Json::Value & v = transform["gamma"];
v.append(threshold->valueRed);
v.append(threshold->valueGreen);
v.append(threshold->valueBlue);
v.append(gamma->valueRed);
v.append(gamma->valueGreen);
v.append(gamma->valueBlue);
}
if (blacklevel != nullptr)
{
Json::Value & v = transform["blacklevel"];
v.append(threshold->valueRed);
v.append(threshold->valueGreen);
v.append(threshold->valueBlue);
v.append(blacklevel->valueRed);
v.append(blacklevel->valueGreen);
v.append(blacklevel->valueBlue);
}
if (whitelevel != nullptr)
{
Json::Value & v = transform["whitelevel"];
v.append(threshold->valueRed);
v.append(threshold->valueGreen);
v.append(threshold->valueBlue);
v.append(whitelevel->valueRed);
v.append(whitelevel->valueGreen);
v.append(whitelevel->valueBlue);
}
// send command message
@ -275,7 +285,7 @@ bool JsonConnection::parseReply(const Json::Value &reply)
if (!success)
{
throw std::runtime_error("Error while executing command: " + reason);
throw std::runtime_error("Error: " + reason);
}
return success;

View File

@ -39,7 +39,13 @@ public:
/// Set the color transform of the leds
/// Note that providing a NULL will leave the settings on the server unchanged
void setTransform(ColorTransformValues * threshold, ColorTransformValues * gamma, ColorTransformValues * blacklevel, ColorTransformValues * whitelevel);
void setTransform(
double * saturation,
double * value,
ColorTransformValues * threshold,
ColorTransformValues * gamma,
ColorTransformValues * blacklevel,
ColorTransformValues * whitelevel);
private:
/// Send a json command message and receive its reply

View File

@ -42,9 +42,11 @@ int main(int argc, char * argv[])
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)");
ImageParameter & argImage = parameters.add<ImageParameter> ('i', "image" , "Set the leds to the colors according to the given image file");
SwitchParameter<> & argServerInfo = parameters.add<SwitchParameter<> >('s', "info" , "List server info");
SwitchParameter<> & argServerInfo = parameters.add<SwitchParameter<> >('l', "list" , "List server info");
SwitchParameter<> & argClear = parameters.add<SwitchParameter<> >('x', "clear" , "Clear data for the priority channel provided by the -p option");
SwitchParameter<> & argClearAll = parameters.add<SwitchParameter<> >(0x0, "clear-all" , "Clear data for all priority channels");
SwitchParameter<> & argClearAll = parameters.add<SwitchParameter<> >(0x0, "clearall" , "Clear data for all active priority channels");
DoubleParameter & argSaturation = parameters.add<DoubleParameter> ('s', "saturation", "Set the HSV saturation gain of the leds");
DoubleParameter & argValue = parameters.add<DoubleParameter> ('v', "value ", "Set the HSV value gain of the leds");
TransformParameter & argGamma = parameters.add<TransformParameter>('g', "gamma" , "Set the gamma of the leds (requires 3 space seperated values)");
TransformParameter & argThreshold = parameters.add<TransformParameter>('t', "threshold" , "Set the threshold of the leds (requires 3 space seperated values between 0.0 and 1.0)");
TransformParameter & argBlacklevel = parameters.add<TransformParameter>('b', "blacklevel", "Set the blacklevel of the leds (requires 3 space seperated values which are normally between 0.0 and 1.0)");
@ -68,7 +70,7 @@ int main(int argc, char * argv[])
}
// check if at least one of the available color transforms is set
bool colorTransform = argThreshold.isSet() || argGamma.isSet() || argBlacklevel.isSet() || argWhitelevel.isSet();
bool colorTransform = argSaturation.isSet() || argValue.isSet() || argThreshold.isSet() || argGamma.isSet() || argBlacklevel.isSet() || argWhitelevel.isSet();
// check that exactly one command was given
int commandCount = count({argColor.isSet(), argImage.isSet(), argServerInfo.isSet(), argClear.isSet(), argClearAll.isSet(), colorTransform});
@ -81,6 +83,8 @@ int main(int argc, char * argv[])
std::cerr << " " << argClear.usageLine() << std::endl;
std::cerr << " " << argClearAll.usageLine() << std::endl;
std::cerr << "or one or more of the available color transformations:" << std::endl;
std::cerr << " " << argSaturation.usageLine() << std::endl;
std::cerr << " " << argValue.usageLine() << std::endl;
std::cerr << " " << argThreshold.usageLine() << std::endl;
std::cerr << " " << argGamma.usageLine() << std::endl;
std::cerr << " " << argBlacklevel.usageLine() << std::endl;
@ -115,14 +119,19 @@ int main(int argc, char * argv[])
}
else if (colorTransform)
{
double saturation, value;
ColorTransformValues threshold, gamma, blacklevel, whitelevel;
if (argSaturation.isSet()) saturation = argSaturation.getValue();
if (argValue.isSet()) value = argValue.getValue();
if (argThreshold.isSet()) threshold = argThreshold.getValue();
if (argGamma.isSet()) gamma = argGamma.getValue();
if (argBlacklevel.isSet()) blacklevel = argBlacklevel.getValue();
if (argWhitelevel.isSet()) whitelevel = argWhitelevel.getValue();
connection.setTransform(
argSaturation.isSet() ? &saturation : nullptr,
argValue.isSet() ? &value : nullptr,
argThreshold.isSet() ? &threshold : nullptr,
argGamma.isSet() ? &gamma : nullptr,
argBlacklevel.isSet() ? &blacklevel : nullptr,