mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
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:
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user