mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
leddevice refactoring. code style and extension of baseclass to avoid dups (#174)
This commit is contained in:
@@ -1,75 +1,75 @@
|
||||
#include <iostream>
|
||||
#include <exception>
|
||||
|
||||
#include "LedDeviceWS281x.h"
|
||||
|
||||
// Constructor
|
||||
LedDeviceWS281x::LedDeviceWS281x(const int gpio, const int leds, const uint32_t freq, const int dmanum, const int pwmchannel, const int invert, const int rgbw, const std::string& whiteAlgorithm)
|
||||
: LedDevice()
|
||||
, _channel(pwmchannel)
|
||||
, _initialized(false)
|
||||
, _whiteAlgorithm(whiteAlgorithm)
|
||||
{
|
||||
_whiteAlgorithm = whiteAlgorithm;
|
||||
Debug( _log, "whiteAlgorithm : %s", whiteAlgorithm.c_str());
|
||||
|
||||
initialized = false;
|
||||
led_string.freq = freq;
|
||||
led_string.dmanum = dmanum;
|
||||
if (pwmchannel != 0 && pwmchannel != 1) {
|
||||
Error( _log, "WS281x: invalid PWM channel; must be 0 or 1.");
|
||||
throw -1;
|
||||
}
|
||||
chan = pwmchannel;
|
||||
led_string.channel[chan].gpionum = gpio;
|
||||
led_string.channel[chan].invert = invert;
|
||||
led_string.channel[chan].count = leds;
|
||||
led_string.channel[chan].brightness = 255;
|
||||
if (rgbw == 1) {
|
||||
led_string.channel[chan].strip_type = SK6812_STRIP_GRBW;
|
||||
} else {
|
||||
led_string.channel[chan].strip_type = WS2811_STRIP_RGB;
|
||||
_led_string.freq = freq;
|
||||
_led_string.dmanum = dmanum;
|
||||
if (pwmchannel != 0 && pwmchannel != 1)
|
||||
{
|
||||
throw std::runtime_error("WS281x: invalid PWM channel; must be 0 or 1.");
|
||||
}
|
||||
_led_string.channel[_channel].gpionum = gpio;
|
||||
_led_string.channel[_channel].invert = invert;
|
||||
_led_string.channel[_channel].count = leds;
|
||||
_led_string.channel[_channel].brightness = 255;
|
||||
_led_string.channel[_channel].strip_type = ((rgbw == 1) ? SK6812_STRIP_GRBW : WS2811_STRIP_RGB);
|
||||
|
||||
led_string.channel[!chan].gpionum = 0;
|
||||
led_string.channel[!chan].invert = invert;
|
||||
led_string.channel[!chan].count = 0;
|
||||
led_string.channel[!chan].brightness = 0;
|
||||
led_string.channel[!chan].strip_type = WS2811_STRIP_RGB;
|
||||
if (ws2811_init(&led_string) < 0) {
|
||||
Error( _log, "Unable to initialize ws281x library.");
|
||||
throw -1;
|
||||
_led_string.channel[!_channel].gpionum = 0;
|
||||
_led_string.channel[!_channel].invert = invert;
|
||||
_led_string.channel[!_channel].count = 0;
|
||||
_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.");
|
||||
}
|
||||
initialized = true;
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
// Send new values down the LED chain
|
||||
int LedDeviceWS281x::write(const std::vector<ColorRgb> &ledValues)
|
||||
{
|
||||
if (!initialized)
|
||||
if (!_initialized)
|
||||
return -1;
|
||||
|
||||
int idx = 0;
|
||||
for (const ColorRgb& color : ledValues)
|
||||
{
|
||||
if (idx >= led_string.channel[chan].count)
|
||||
if (idx >= _led_string.channel[_channel].count)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
_temp_rgbw.red = color.red;
|
||||
_temp_rgbw.green = color.green;
|
||||
_temp_rgbw.blue = color.blue;
|
||||
_temp_rgbw.white = 0;
|
||||
|
||||
if (led_string.channel[chan].strip_type == SK6812_STRIP_GRBW) {
|
||||
if (_led_string.channel[_channel].strip_type == SK6812_STRIP_GRBW)
|
||||
{
|
||||
Rgb_to_Rgbw(color, &_temp_rgbw, _whiteAlgorithm);
|
||||
}
|
||||
|
||||
led_string.channel[chan].leds[idx++] =
|
||||
_led_string.channel[_channel].leds[idx++] =
|
||||
((uint32_t)_temp_rgbw.white << 24) + ((uint32_t)_temp_rgbw.red << 16) + ((uint32_t)_temp_rgbw.green << 8) + _temp_rgbw.blue;
|
||||
|
||||
}
|
||||
while (idx < led_string.channel[chan].count)
|
||||
led_string.channel[chan].leds[idx++] = 0;
|
||||
|
||||
if (ws2811_render(&led_string))
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
while (idx < _led_string.channel[_channel].count)
|
||||
{
|
||||
_led_string.channel[_channel].leds[idx++] = 0;
|
||||
}
|
||||
|
||||
return ws2811_render(&_led_string) ? -1 : 0;
|
||||
}
|
||||
|
||||
// Turn off the LEDs by sending 000000's
|
||||
@@ -77,25 +77,24 @@ int LedDeviceWS281x::write(const std::vector<ColorRgb> &ledValues)
|
||||
// make it more likely we don't accidentally drive data into an off strip
|
||||
int LedDeviceWS281x::switchOff()
|
||||
{
|
||||
if (!initialized)
|
||||
if (!_initialized)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int idx = 0;
|
||||
while (idx < led_string.channel[chan].count)
|
||||
led_string.channel[chan].leds[idx++] = 0;
|
||||
while (idx < _led_string.channel[_channel].count)
|
||||
_led_string.channel[_channel].leds[idx++] = 0;
|
||||
|
||||
if (ws2811_render(&led_string))
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
return ws2811_render(&_led_string) ? -1 : 0;
|
||||
}
|
||||
|
||||
// Destructor
|
||||
LedDeviceWS281x::~LedDeviceWS281x()
|
||||
{
|
||||
if (initialized)
|
||||
if (_initialized)
|
||||
{
|
||||
ws2811_fini(&led_string);
|
||||
ws2811_fini(&_led_string);
|
||||
}
|
||||
initialized = false;
|
||||
_initialized = false;
|
||||
}
|
||||
|
Reference in New Issue
Block a user