2013-12-10 19:59:49 +01:00
|
|
|
|
|
|
|
// Local hyperion includes
|
|
|
|
#include "LedDeviceWs2811.h"
|
|
|
|
|
2013-12-16 21:36:54 +01:00
|
|
|
LedDeviceWs2811::LedDeviceWs2811(const std::string & deviceName) :
|
|
|
|
LedRs232Device(deviceName, ws2811::getBaudrate(ws2811::option_1))
|
2013-12-10 19:59:49 +01:00
|
|
|
{
|
2013-12-16 21:36:54 +01:00
|
|
|
fillEncodeTable(ws2811::option_1);
|
2013-12-10 19:59:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int LedDeviceWs2811::write(const std::vector<ColorRgb> & ledValues)
|
|
|
|
{
|
|
|
|
if (_ledBuffer.size() != ledValues.size() * 3)
|
|
|
|
{
|
|
|
|
_ledBuffer.resize(ledValues.size() * 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto bufIt = _ledBuffer.begin();
|
|
|
|
for (const ColorRgb & color : ledValues)
|
|
|
|
{
|
|
|
|
*bufIt = _byteToSignalTable[color.red ];
|
|
|
|
++bufIt;
|
|
|
|
*bufIt = _byteToSignalTable[color.green];
|
|
|
|
++bufIt;
|
|
|
|
*bufIt = _byteToSignalTable[color.blue ];
|
|
|
|
++bufIt;
|
|
|
|
}
|
|
|
|
|
|
|
|
writeBytes(_ledBuffer.size() * 3, reinterpret_cast<uint8_t *>(_ledBuffer.data()));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LedDeviceWs2811::switchOff()
|
|
|
|
{
|
|
|
|
write(std::vector<ColorRgb>(_ledBuffer.size()/3, ColorRgb::BLACK));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-16 21:36:54 +01:00
|
|
|
void LedDeviceWs2811::fillEncodeTable(const ws2811::SignalTiming ledOption)
|
2013-12-10 19:59:49 +01:00
|
|
|
{
|
2013-12-16 21:36:54 +01:00
|
|
|
_byteToSignalTable.resize(256);
|
2013-12-10 19:59:49 +01:00
|
|
|
for (unsigned byteValue=0; byteValue<256; ++byteValue)
|
|
|
|
{
|
2013-12-16 21:36:54 +01:00
|
|
|
const uint8_t byteVal = uint8_t(byteValue);
|
|
|
|
_byteToSignalTable[byteValue] = ws2811::translate(ledOption, byteVal);
|
2013-12-10 19:59:49 +01:00
|
|
|
}
|
|
|
|
}
|