Leddevice refactoring the next next part (#263)

* switch rs232 provider to completly async transfer

* start of implementing a seperate init function for leddevices

* rename setconfig to init

* more fixes

* implement missing code

* fix code style

* remove debug  code

* remove  debug stuff

* set loglevel to original state
This commit is contained in:
redPanther
2016-10-08 08:14:36 +02:00
committed by GitHub
parent afed2e68e0
commit 5aac2be702
72 changed files with 542 additions and 452 deletions

View File

@@ -2,24 +2,31 @@
#include "LedDeviceWS281x.h"
// Constructor
LedDeviceWS281x::LedDeviceWS281x(const Json::Value &deviceConfig)
: LedDevice()
, _initialized(false)
{
setConfig(deviceConfig);
Debug( _log, "whiteAlgorithm : %s", _whiteAlgorithm.c_str());
if (ws2811_init(&_led_string) < 0)
{
throw std::runtime_error("Unable to initialize ws281x library.");
}
_initialized = true;
_deviceReady = init(deviceConfig);
}
bool LedDeviceWS281x::setConfig(const Json::Value &deviceConfig)
LedDeviceWS281x::~LedDeviceWS281x()
{
_whiteAlgorithm = deviceConfig.get("white_algorithm","").asString();
if (_deviceReady)
{
ws2811_fini(&_led_string);
}
}
bool LedDeviceWS281x::init(const Json::Value &deviceConfig)
{
std::string whiteAlgorithm = deviceConfig.get("white_algorithm","white_off").asString();
_whiteAlgorithm = RGBW::stringToWhiteAlgorithm(whiteAlgorithm);
Debug( _log, "whiteAlgorithm : %s", whiteAlgorithm.c_str());
if (_whiteAlgorithm == RGBW::INVALID)
{
Error(_log, "unknown whiteAlgorithm %s", whiteAlgorithm.c_str());
return false;
}
_channel = deviceConfig.get("pwmchannel", 0).asInt();
if (_channel != 0 && _channel != 1)
{
@@ -40,6 +47,12 @@ bool LedDeviceWS281x::setConfig(const Json::Value &deviceConfig)
_led_string.channel[!_channel].brightness = 0;
_led_string.channel[!_channel].strip_type = WS2811_STRIP_RGB;
if (ws2811_init(&_led_string) < 0)
{
throw std::runtime_error("Unable to initialize ws281x library.");
}
return true;
}
@@ -48,13 +61,9 @@ LedDevice* LedDeviceWS281x::construct(const Json::Value &deviceConfig)
return new LedDeviceWS281x(deviceConfig);
}
// Send new values down the LED chain
int LedDeviceWS281x::write(const std::vector<ColorRgb> &ledValues)
{
if (!_initialized)
return -1;
int idx = 0;
for (const ColorRgb& color : ledValues)
{
@@ -85,12 +94,3 @@ int LedDeviceWS281x::write(const std::vector<ColorRgb> &ledValues)
return ws2811_render(&_led_string) ? -1 : 0;
}
// Destructor
LedDeviceWS281x::~LedDeviceWS281x()
{
if (_initialized)
{
ws2811_fini(&_led_string);
}
_initialized = false;
}