hyperion.ng/libsrc/hyperion/Hyperion.cpp
T. van der Zwan 39b98386dd Added simple QT-based grabber.
Moved ImageToLedsMap from include to libsrc.
Moved instantiation of objects to Hyperion (no JSON required outside this class).
2013-08-13 11:10:45 +02:00

109 lines
3.0 KiB
C++

// Syslog include
#include <syslog.h>
// JsonSchema include
#include <utils/jsonschema/JsonFactory.h>
// hyperion include
#include <hyperion/Hyperion.h>
#include <hyperion/LedDevice.h>
#include "LedDeviceWs2801.h"
#include "ColorTransform.h"
using namespace hyperion;
LedDevice* constructDevice(const Json::Value& deviceConfig)
{
std::cout << "Device configuration: " << deviceConfig << std::endl;
LedDevice* device = nullptr;
if (deviceConfig["type"].asString() == "ws2801")
{
const std::string name = "WS-2801";
const std::string output = deviceConfig["output"].asString();
const unsigned interval = deviceConfig["interval"].asInt();
const unsigned rate = deviceConfig["rate"].asInt();
LedDeviceWs2801* deviceWs2801 = new LedDeviceWs2801(name, output, interval, rate);
deviceWs2801->open();
device = deviceWs2801;
}
else
{
// Unknown / Unimplemented device
}
return device;
}
ColorTransform* createColorTransform(const Json::Value& colorConfig)
{
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;
}
LedString createLedString(const Json::Value& ledsConfig)
{
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;
}
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"]))
{
// empty
}
Hyperion::~Hyperion()
{
// Delete the Led-String
delete mDevice;
// Delete the color-transform
delete mBlueTransform;
delete mGreenTransform;
delete mRedTransform;
}
void Hyperion::setValue(int priority, std::vector<RgbColor>& ledColors)
{
// 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);
}
mMuxer.setInput(priority, ledColors);
if (priority == mMuxer.getCurrentPriority())
{
mDevice->write(ledColors);
}
}