hyperion.ng/libsrc/leddevice/LedDeviceFactory.cpp
redPanther 9ddbf81810 merge adalight and adalightapa102 (#303)
if you want to use adalightapa102 driver, select adalight and set
lightberry_apa102_mode : true
in device config. Default is standard adalight
2016-12-01 16:17:14 +01:00

150 lines
3.4 KiB
C++
Executable File

// Stl includes
#include <string>
#include <sstream>
#include <algorithm>
#include <exception>
#include <map>
// Build configuration
#include <HyperionConfig.h>
// Leddevice includes
#include <leddevice/LedDeviceFactory.h>
#include <utils/Logger.h>
#include <leddevice/LedDevice.h>
// Local Leddevice includes
#ifdef ENABLE_SPIDEV
#include "LedDeviceLpd6803.h"
#include "LedDeviceLpd8806.h"
#include "LedDeviceP9813.h"
#include "LedDeviceWs2801.h"
#include "LedDeviceWs2812SPI.h"
#include "LedDeviceSk6812SPI.h"
#include "LedDeviceAPA102.h"
#endif
#ifdef ENABLE_TINKERFORGE
#include "LedDeviceTinkerforge.h"
#endif
#include "LedDeviceAdalight.h"
#include "LedDeviceRawHID.h"
#include "LedDeviceLightpack.h"
#include "LedDeviceMultiLightpack.h"
#include "LedDevicePaintpack.h"
#include "LedDevicePiBlaster.h"
#include "LedDeviceSedu.h"
#include "LedDeviceDMX.h"
#include "LedDeviceFile.h"
#include "LedDeviceFadeCandy.h"
#include "LedDeviceTpm2net.h"
#include "LedDeviceUdpRaw.h"
#include "LedDeviceUdpE131.h"
#include "LedDeviceHyperionUsbasp.h"
#include "LedDevicePhilipsHue.h"
#include "LedDeviceTpm2.h"
#include "LedDeviceAtmo.h"
#include "LedDeviceAtmoOrb.h"
#include "LedDeviceUdpH801.h"
#ifdef ENABLE_WS281XPWM
#include "LedDeviceWS281x.h"
#endif
LedDevice * LedDeviceFactory::construct(const QJsonObject & deviceConfig, const int ledCount)
{
Logger * log = Logger::getInstance("LedDevice");
QJsonDocument config(deviceConfig);
QString ss(config.toJson(QJsonDocument::Indented));
Info(log, "configuration: %s ", ss.toUtf8().constData());
std::string type = deviceConfig["type"].toString("UNSPECIFIED").toStdString();
std::transform(type.begin(), type.end(), type.begin(), ::tolower);
// set amount of led to leddevice
LedDevice::setLedCount(ledCount);
#define REGISTER(className) LedDevice::addToDeviceMap(QString(#className).toLower().toStdString(), LedDevice##className::construct);
// rs232 devices
REGISTER(Adalight);
REGISTER(Sedu);
REGISTER(DMX);
REGISTER(Tpm2);
REGISTER(Atmo);
// spi devices
#ifdef ENABLE_SPIDEV
REGISTER(APA102);
REGISTER(Lpd6803);
REGISTER(Lpd8806);
REGISTER(P9813);
REGISTER(Ws2801);
REGISTER(Ws2812SPI);
REGISTER(Sk6812SPI);
#endif
// pwm devices
#ifdef ENABLE_WS281XPWM
REGISTER(WS281x);
#endif
// network lights
REGISTER(FadeCandy);
REGISTER(Tpm2net);
REGISTER(UdpRaw);
REGISTER(UdpE131);
REGISTER(UdpH801);
REGISTER(PhilipsHue);
REGISTER(AtmoOrb);
#ifdef ENABLE_TINKERFORGE
REGISTER(Tinkerforge);
#endif
// direct usb
REGISTER(HyperionUsbasp);
REGISTER(RawHID);
REGISTER(Paintpack);
REGISTER(Lightpack);
REGISTER(MultiLightpack);
// other
REGISTER(File);
REGISTER(PiBlaster);
#undef REGISTER
const LedDeviceRegistry& devList = LedDevice::getDeviceMap();
LedDevice* device = nullptr;
try
{
for ( auto dev: devList)
{
if (dev.first == type)
{
device = dev.second(deviceConfig);
LedDevice::setActiveDevice(dev.first);
Info(log,"LedDevice '%s' configured.", dev.first.c_str());
break;
}
}
if (device == nullptr)
{
Error(log, "Dummy device used, because configured device '%s' is unknown", type.c_str() );
throw std::runtime_error("unknown device");
}
}
catch(std::exception& e)
{
Error(log, "Dummy device used, because configured device '%s' throws error '%s'", type.c_str(), e.what());
const QJsonObject dummyDeviceConfig;
device = LedDeviceFile::construct(QJsonObject());
}
device->open();
return device;
}