Merge pull request #559 from markwal/pwmchannel

Add ws281x driver parameter for PWM channel

Former-commit-id: ae027699662ae74991275940fc7b63538386c745
This commit is contained in:
brindosch 2016-03-21 23:37:49 +01:00
commit 36304f9067
3 changed files with 26 additions and 19 deletions

View File

@ -327,8 +327,9 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
const int leds = deviceConfig.get("leds", 12).asInt();
const uint32_t freq = deviceConfig.get("freq", (Json::UInt)800000ul).asInt();
const int dmanum = deviceConfig.get("dmanum", 5).asInt();
const int pwmchannel = deviceConfig.get("pwmchannel", 0).asInt();
LedDeviceWS281x * ledDeviceWS281x = new LedDeviceWS281x(gpio, leds, freq, dmanum);
LedDeviceWS281x * ledDeviceWS281x = new LedDeviceWS281x(gpio, leds, freq, dmanum, pwmchannel);
device = ledDeviceWS281x;
}
#endif

View File

@ -3,22 +3,27 @@
#include "LedDeviceWS281x.h"
// Constructor
LedDeviceWS281x::LedDeviceWS281x(const int gpio, const int leds, const uint32_t freq, const int dmanum)
LedDeviceWS281x::LedDeviceWS281x(const int gpio, const int leds, const uint32_t freq, const int dmanum, const int pwmchannel)
{
initialized = false;
led_string.freq = freq;
led_string.dmanum = dmanum;
led_string.channel[0].gpionum = gpio;
led_string.channel[0].invert = 0;
led_string.channel[0].count = leds;
led_string.channel[0].brightness = 255;
led_string.channel[0].strip_type = WS2811_STRIP_RGB;
if (pwmchannel != 0 && pwmchannel != 1) {
std::cout << "WS281x: invalid PWM channel; must be 0 or 1." << std::endl;
throw -1;
}
chan = pwmchannel;
led_string.channel[chan].gpionum = gpio;
led_string.channel[chan].invert = 0;
led_string.channel[chan].count = leds;
led_string.channel[chan].brightness = 255;
led_string.channel[chan].strip_type = WS2811_STRIP_RGB;
led_string.channel[1].gpionum = 0;
led_string.channel[1].invert = 0;
led_string.channel[1].count = 0;
led_string.channel[1].brightness = 0;
led_string.channel[0].strip_type = WS2811_STRIP_RGB;
led_string.channel[!chan].gpionum = 0;
led_string.channel[!chan].invert = 0;
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) {
std::cout << "Unable to initialize ws281x library." << std::endl;
throw -1;
@ -35,12 +40,12 @@ int LedDeviceWS281x::write(const std::vector<ColorRgb> &ledValues)
int idx = 0;
for (const ColorRgb& color : ledValues)
{
if (idx >= led_string.channel[0].count)
if (idx >= led_string.channel[chan].count)
break;
led_string.channel[0].leds[idx++] = ((uint32_t)color.red << 16) + ((uint32_t)color.green << 8) + color.blue;
led_string.channel[chan].leds[idx++] = ((uint32_t)color.red << 16) + ((uint32_t)color.green << 8) + color.blue;
}
while (idx < led_string.channel[0].count)
led_string.channel[0].leds[idx++] = 0;
while (idx < led_string.channel[chan].count)
led_string.channel[chan].leds[idx++] = 0;
if (ws2811_render(&led_string))
return -1;
@ -57,8 +62,8 @@ int LedDeviceWS281x::switchOff()
return -1;
int idx = 0;
while (idx < led_string.channel[0].count)
led_string.channel[0].leds[idx++] = 0;
while (idx < led_string.channel[chan].count)
led_string.channel[chan].leds[idx++] = 0;
if (ws2811_render(&led_string))
return -1;

View File

@ -17,7 +17,7 @@ public:
/// @param freq The target frequency for the data line, default is 800000
/// @param dmanum The DMA channel to use, default is 5
///
LedDeviceWS281x(const int gpio, const int leds, const uint32_t freq, int dmanum);
LedDeviceWS281x(const int gpio, const int leds, const uint32_t freq, int dmanum, int pwmchannel);
///
/// Destructor of the LedDevice, waits for DMA to complete and then cleans up
@ -37,6 +37,7 @@ public:
private:
ws2811_t led_string;
int chan;
bool initialized;
};