mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
First working version with some test executables
This commit is contained in:
101
libsrc/hyperion/Hyperion.cpp
Normal file
101
libsrc/hyperion/Hyperion.cpp
Normal file
@@ -0,0 +1,101 @@
|
||||
|
||||
// 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"
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Hyperion::Hyperion(const Json::Value &jsonConfig) :
|
||||
mLedString(LedString::construct(jsonConfig["leds"], jsonConfig["color"])),
|
||||
mImage(nullptr),
|
||||
mDevice(constructDevice(jsonConfig["device"]))
|
||||
{
|
||||
// empty
|
||||
}
|
||||
|
||||
|
||||
Hyperion::~Hyperion()
|
||||
{
|
||||
// Delete the existing image (or delete nullptr)
|
||||
delete mImage;
|
||||
|
||||
// Delete the Led-String
|
||||
delete mDevice;
|
||||
}
|
||||
|
||||
void Hyperion::setInputSize(const unsigned width, const unsigned height)
|
||||
{
|
||||
// Delete the existing image (or delete nullptr)
|
||||
delete mImage;
|
||||
|
||||
// Create the new image with the mapping to the leds
|
||||
mImage = new RgbImage(width, height);
|
||||
mLedsMap.createMapping(*mImage, mLedString.leds());
|
||||
}
|
||||
|
||||
void Hyperion::commit()
|
||||
{
|
||||
// Derive the color per led
|
||||
const std::vector<RgbColor> ledColors = mLedsMap.getMedianLedColor();
|
||||
// Write the Led colors to the led-string
|
||||
mDevice->write(ledColors);
|
||||
}
|
||||
|
||||
void Hyperion::operator() (const RgbImage& inputImage)
|
||||
{
|
||||
std::cout << "Cached image size: [" << mImage->width() << "x" << mImage->height() << "]. Input image size: [" << inputImage.width() << "x" << inputImage.height() << "]" << std::endl;
|
||||
// Copy the input-image into the buffer
|
||||
mImage->copy(inputImage);
|
||||
|
||||
// Derive the color per led
|
||||
// std::vector<RgbColor> ledColors = mLedsMap.getMeanLedColor();
|
||||
std::vector<RgbColor> ledColors = mLedsMap.getMedianLedColor();
|
||||
applyTransform(ledColors);
|
||||
|
||||
// Write the Led colors to the led-string
|
||||
mDevice->write(ledColors);
|
||||
}
|
||||
|
||||
void Hyperion::setColor(const RgbColor& color)
|
||||
{
|
||||
mDevice->write(std::vector<RgbColor>(mLedString.leds().size(), color));
|
||||
}
|
||||
|
||||
void Hyperion::applyTransform(std::vector<RgbColor>& colors) const
|
||||
{
|
||||
for (RgbColor& color : colors)
|
||||
{
|
||||
color.red = (color.red < mLedString.red.blacklevel)? 0 : mLedString.red.adjust + mLedString.red.gamma * color.red;
|
||||
color.green = (color.green < mLedString.green.blacklevel)? 0 : mLedString.green.adjust + mLedString.green.gamma * color.green;
|
||||
color.blue = (color.blue < mLedString.blue.blacklevel)? 0 : mLedString.blue.adjust + mLedString.blue.gamma * color.blue;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user