2013-07-26 22:38:34 +02:00
|
|
|
|
2013-08-21 16:25:27 +02:00
|
|
|
// QT includes
|
2013-08-14 17:02:09 +02:00
|
|
|
#include <QDateTime>
|
|
|
|
|
2013-07-26 22:38:34 +02:00
|
|
|
// JsonSchema include
|
|
|
|
#include <utils/jsonschema/JsonFactory.h>
|
|
|
|
|
|
|
|
// hyperion include
|
|
|
|
#include <hyperion/Hyperion.h>
|
|
|
|
#include <hyperion/LedDevice.h>
|
2013-08-15 21:11:02 +02:00
|
|
|
#include <hyperion/ImageProcessorFactory.h>
|
2013-07-26 22:38:34 +02:00
|
|
|
|
|
|
|
#include "LedDeviceWs2801.h"
|
2013-08-15 21:11:02 +02:00
|
|
|
#include "LedDeviceTest.h"
|
2013-08-13 11:10:45 +02:00
|
|
|
|
2013-08-23 18:24:10 +02:00
|
|
|
#include <utils/ColorTransform.h>
|
|
|
|
#include <utils/HsvTransform.h>
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-08-24 11:51:52 +02:00
|
|
|
LedDevice* Hyperion::constructDevice(const Json::Value& deviceConfig)
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
|
|
|
std::cout << "Device configuration: " << deviceConfig << std::endl;
|
|
|
|
LedDevice* device = nullptr;
|
|
|
|
if (deviceConfig["type"].asString() == "ws2801")
|
|
|
|
{
|
|
|
|
const std::string output = deviceConfig["output"].asString();
|
|
|
|
const unsigned rate = deviceConfig["rate"].asInt();
|
|
|
|
|
2013-09-09 17:23:44 +02:00
|
|
|
LedDeviceWs2801* deviceWs2801 = new LedDeviceWs2801(output, rate);
|
2013-07-26 22:38:34 +02:00
|
|
|
deviceWs2801->open();
|
|
|
|
|
|
|
|
device = deviceWs2801;
|
|
|
|
}
|
2013-08-15 21:11:02 +02:00
|
|
|
else if (deviceConfig["type"].asString() == "test")
|
|
|
|
{
|
|
|
|
device = new LedDeviceTest();
|
|
|
|
}
|
2013-07-26 22:38:34 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Unknown / Unimplemented device
|
|
|
|
}
|
|
|
|
return device;
|
|
|
|
}
|
|
|
|
|
2013-08-24 11:51:52 +02:00
|
|
|
HsvTransform * Hyperion::createHsvTransform(const Json::Value & hsvConfig)
|
2013-08-21 21:50:17 +02:00
|
|
|
{
|
|
|
|
return new HsvTransform(hsvConfig["saturationGain"].asDouble(), hsvConfig["valueGain"].asDouble());
|
|
|
|
}
|
|
|
|
|
2013-08-24 11:51:52 +02:00
|
|
|
ColorTransform* Hyperion::createColorTransform(const Json::Value& colorConfig)
|
2013-08-13 11:10:45 +02:00
|
|
|
{
|
|
|
|
const double threshold = colorConfig["threshold"].asDouble();
|
|
|
|
const double gamma = colorConfig["gamma"].asDouble();
|
|
|
|
const double blacklevel = colorConfig["blacklevel"].asDouble();
|
|
|
|
const double whitelevel = colorConfig["whitelevel"].asDouble();
|
|
|
|
|
|
|
|
ColorTransform* transform = new ColorTransform(threshold, gamma, blacklevel, whitelevel);
|
|
|
|
return transform;
|
|
|
|
}
|
2013-08-15 21:11:02 +02:00
|
|
|
LedString Hyperion::createLedString(const Json::Value& ledsConfig)
|
2013-08-13 11:10:45 +02:00
|
|
|
{
|
|
|
|
LedString ledString;
|
|
|
|
|
|
|
|
for (const Json::Value& ledConfig : ledsConfig)
|
|
|
|
{
|
|
|
|
Led led;
|
|
|
|
led.index = ledConfig["index"].asInt();
|
|
|
|
const Json::Value& hscanConfig = ledConfig["hscan"];
|
|
|
|
const Json::Value& vscanConfig = ledConfig["vscan"];
|
|
|
|
led.minX_frac = std::max(0.0, std::min(100.0, hscanConfig["minimum"].asDouble()))/100.0;
|
|
|
|
led.maxX_frac = std::max(0.0, std::min(100.0, hscanConfig["maximum"].asDouble()))/100.0;
|
|
|
|
led.minY_frac = 1.0 - std::max(0.0, std::min(100.0, vscanConfig["maximum"].asDouble()))/100.0;
|
|
|
|
led.maxY_frac = 1.0 - std::max(0.0, std::min(100.0, vscanConfig["minimum"].asDouble()))/100.0;
|
|
|
|
|
|
|
|
ledString.leds().push_back(led);
|
|
|
|
}
|
|
|
|
return ledString;
|
|
|
|
}
|
|
|
|
|
2013-07-26 22:38:34 +02:00
|
|
|
Hyperion::Hyperion(const Json::Value &jsonConfig) :
|
2013-08-18 13:33:56 +02:00
|
|
|
_ledString(createLedString(jsonConfig["leds"])),
|
|
|
|
_muxer(_ledString.leds().size()),
|
2013-08-21 21:50:17 +02:00
|
|
|
_hsvTransform(createHsvTransform(jsonConfig["color"]["hsv"])),
|
2013-08-18 13:33:56 +02:00
|
|
|
_redTransform(createColorTransform(jsonConfig["color"]["red"])),
|
|
|
|
_greenTransform(createColorTransform(jsonConfig["color"]["green"])),
|
|
|
|
_blueTransform(createColorTransform(jsonConfig["color"]["blue"])),
|
|
|
|
_device(constructDevice(jsonConfig["device"])),
|
2013-08-14 17:02:09 +02:00
|
|
|
_timer()
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2013-08-18 13:33:56 +02:00
|
|
|
ImageProcessorFactory::getInstance().init(_ledString);
|
2013-08-15 21:11:02 +02:00
|
|
|
|
2013-08-14 17:02:09 +02:00
|
|
|
_timer.setSingleShot(true);
|
|
|
|
QObject::connect(&_timer, SIGNAL(timeout()), this, SLOT(update()));
|
|
|
|
|
2013-08-18 13:33:56 +02:00
|
|
|
// initialize the leds
|
|
|
|
update();
|
2013-07-26 22:38:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Hyperion::~Hyperion()
|
|
|
|
{
|
|
|
|
// Delete the Led-String
|
2013-08-18 13:33:56 +02:00
|
|
|
delete _device;
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-08-21 21:50:17 +02:00
|
|
|
// delete he hsv transform
|
|
|
|
delete _hsvTransform;
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-08-13 11:10:45 +02:00
|
|
|
// Delete the color-transform
|
2013-08-18 13:33:56 +02:00
|
|
|
delete _blueTransform;
|
|
|
|
delete _greenTransform;
|
|
|
|
delete _redTransform;
|
2013-07-26 22:38:34 +02:00
|
|
|
}
|
|
|
|
|
2013-08-14 10:54:49 +02:00
|
|
|
unsigned Hyperion::getLedCount() const
|
|
|
|
{
|
2013-08-18 13:33:56 +02:00
|
|
|
return _ledString.leds().size();
|
2013-08-14 10:54:49 +02:00
|
|
|
}
|
|
|
|
|
2013-09-09 17:23:44 +02:00
|
|
|
void Hyperion::setColor(int priority, const RgbColor &color, const int timeout_ms)
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2013-08-18 13:33:56 +02:00
|
|
|
// create led output
|
|
|
|
std::vector<RgbColor> ledColors(_ledString.leds().size(), color);
|
|
|
|
|
|
|
|
// set colors
|
|
|
|
setColors(priority, ledColors, timeout_ms);
|
|
|
|
}
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-09-09 17:23:44 +02:00
|
|
|
void Hyperion::setColors(int priority, const std::vector<RgbColor>& ledColors, const int timeout_ms)
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2013-08-14 17:02:09 +02:00
|
|
|
if (timeout_ms > 0)
|
|
|
|
{
|
|
|
|
const uint64_t timeoutTime = QDateTime::currentMSecsSinceEpoch() + timeout_ms;
|
2013-08-18 13:33:56 +02:00
|
|
|
_muxer.setInput(priority, ledColors, timeoutTime);
|
2013-08-14 17:02:09 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-18 13:33:56 +02:00
|
|
|
_muxer.setInput(priority, ledColors);
|
2013-08-14 17:02:09 +02:00
|
|
|
}
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-08-18 13:33:56 +02:00
|
|
|
if (priority == _muxer.getCurrentPriority())
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2013-08-14 17:02:09 +02:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-19 19:57:35 +02:00
|
|
|
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:
|
2013-08-21 21:50:17 +02:00
|
|
|
break;
|
2013-08-19 19:57:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// set transform value
|
|
|
|
switch (transform)
|
|
|
|
{
|
2013-08-21 21:50:17 +02:00
|
|
|
case SATURATION_GAIN:
|
|
|
|
_hsvTransform->setSaturationGain(value);
|
|
|
|
break;
|
|
|
|
case VALUE_GAIN:
|
|
|
|
_hsvTransform->setValueGain(value);
|
|
|
|
break;
|
2013-08-19 19:57:35 +02:00
|
|
|
case THRESHOLD:
|
2013-08-21 21:50:17 +02:00
|
|
|
assert (t != nullptr);
|
2013-08-19 19:57:35 +02:00
|
|
|
t->setThreshold(value);
|
|
|
|
break;
|
|
|
|
case GAMMA:
|
2013-08-21 21:50:17 +02:00
|
|
|
assert (t != nullptr);
|
2013-08-19 19:57:35 +02:00
|
|
|
t->setGamma(value);
|
|
|
|
break;
|
|
|
|
case BLACKLEVEL:
|
2013-08-21 21:50:17 +02:00
|
|
|
assert (t != nullptr);
|
2013-08-19 19:57:35 +02:00
|
|
|
t->setBlacklevel(value);
|
|
|
|
break;
|
|
|
|
case WHITELEVEL:
|
2013-08-21 21:50:17 +02:00
|
|
|
assert (t != nullptr);
|
2013-08-19 19:57:35 +02:00
|
|
|
t->setWhitelevel(value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
2013-08-21 21:50:17 +02:00
|
|
|
|
|
|
|
// update the led output
|
|
|
|
update();
|
2013-08-19 19:57:35 +02:00
|
|
|
}
|
|
|
|
|
2013-08-18 13:33:56 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2013-08-19 20:33:36 +02:00
|
|
|
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:
|
2013-08-21 21:50:17 +02:00
|
|
|
break;
|
2013-08-19 20:33:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// set transform value
|
|
|
|
switch (transform)
|
|
|
|
{
|
2013-08-21 21:50:17 +02:00
|
|
|
case SATURATION_GAIN:
|
|
|
|
return _hsvTransform->getSaturationGain();
|
|
|
|
case VALUE_GAIN:
|
|
|
|
return _hsvTransform->getValueGain();
|
2013-08-19 20:33:36 +02:00
|
|
|
case THRESHOLD:
|
2013-08-21 21:50:17 +02:00
|
|
|
assert (t != nullptr);
|
2013-08-19 20:33:36 +02:00
|
|
|
return t->getThreshold();
|
|
|
|
case GAMMA:
|
2013-08-21 21:50:17 +02:00
|
|
|
assert (t != nullptr);
|
2013-08-19 20:33:36 +02:00
|
|
|
return t->getGamma();
|
|
|
|
case BLACKLEVEL:
|
2013-08-21 21:50:17 +02:00
|
|
|
assert (t != nullptr);
|
2013-08-19 20:33:36 +02:00
|
|
|
return t->getBlacklevel();
|
|
|
|
case WHITELEVEL:
|
2013-08-21 21:50:17 +02:00
|
|
|
assert (t != nullptr);
|
2013-08-19 20:33:36 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-08-14 17:02:09 +02:00
|
|
|
void Hyperion::update()
|
|
|
|
{
|
|
|
|
// Update the muxer, cleaning obsolete priorities
|
2013-08-18 13:33:56 +02:00
|
|
|
_muxer.setCurrentTime(QDateTime::currentMSecsSinceEpoch());
|
2013-08-14 17:02:09 +02:00
|
|
|
|
|
|
|
// Obtain the current priority channel
|
2013-08-18 13:33:56 +02:00
|
|
|
int priority = _muxer.getCurrentPriority();
|
|
|
|
const PriorityMuxer::InputInfo & priorityInfo = _muxer.getInputInfo(priority);
|
2013-08-14 17:02:09 +02:00
|
|
|
|
2013-08-21 21:50:17 +02:00
|
|
|
// 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);
|
|
|
|
}
|
2013-08-14 17:02:09 +02:00
|
|
|
|
|
|
|
// Write the data to the device
|
2013-08-21 21:50:17 +02:00
|
|
|
_device->write(ledColors);
|
2013-08-14 17:02:09 +02:00
|
|
|
|
|
|
|
// Start the timeout-timer
|
|
|
|
if (priorityInfo.timeoutTime_ms == -1)
|
|
|
|
{
|
|
|
|
_timer.stop();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int timeout_ms = std::max(0, int(priorityInfo.timeoutTime_ms - QDateTime::currentMSecsSinceEpoch()));
|
|
|
|
_timer.start(timeout_ms);
|
2013-07-26 22:38:34 +02:00
|
|
|
}
|
|
|
|
}
|