2013-12-23 22:58:54 +01:00
|
|
|
|
2013-12-28 08:34:49 +01:00
|
|
|
// Linux includes
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2013-12-23 22:58:54 +01:00
|
|
|
// Local Hyperion-Leddevice includes
|
|
|
|
#include "LedDeviceWs2812b.h"
|
|
|
|
|
|
|
|
LedDeviceWs2812b::LedDeviceWs2812b() :
|
2014-01-27 20:15:04 +01:00
|
|
|
LedRs232Device("/dev/ttyUSB0", 2000000)
|
2013-12-23 22:58:54 +01:00
|
|
|
{
|
2013-12-29 20:22:55 +01:00
|
|
|
// empty
|
2013-12-23 22:58:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int LedDeviceWs2812b::write(const std::vector<ColorRgb> & ledValues)
|
|
|
|
{
|
|
|
|
// Ensure the size of the led-buffer
|
2013-12-29 20:22:55 +01:00
|
|
|
if (_ledBuffer.size() != ledValues.size()*8)
|
2013-12-23 22:58:54 +01:00
|
|
|
{
|
2013-12-29 20:22:55 +01:00
|
|
|
_ledBuffer.resize(ledValues.size()*8, ~0x24);
|
2013-12-23 22:58:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Translate the channel of each color to a signal
|
2013-12-29 20:22:55 +01:00
|
|
|
uint8_t * signal_ptr = _ledBuffer.data();
|
|
|
|
for (const ColorRgb & color : ledValues)
|
2013-12-23 22:58:54 +01:00
|
|
|
{
|
2013-12-29 20:22:55 +01:00
|
|
|
signal_ptr = color2signal(color, signal_ptr);
|
2013-12-23 22:58:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-29 20:22:55 +01:00
|
|
|
const int result = writeBytes(_ledBuffer.size(), _ledBuffer.data());
|
2013-12-28 08:34:49 +01:00
|
|
|
// Official latch time is 50us (lets give it 50us more)
|
|
|
|
usleep(100);
|
|
|
|
return result;
|
2013-12-23 22:58:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-29 20:22:55 +01:00
|
|
|
uint8_t * LedDeviceWs2812b::color2signal(const ColorRgb & color, uint8_t * signal)
|
2013-12-23 22:58:54 +01:00
|
|
|
{
|
2013-12-29 20:22:55 +01:00
|
|
|
*signal = bits2Signal(color.red & 0x80, color.red & 0x40, color.red & 0x20);
|
|
|
|
++signal;
|
|
|
|
*signal = bits2Signal(color.red & 0x10, color.red & 0x08, color.red & 0x04);
|
|
|
|
++signal;
|
|
|
|
*signal = bits2Signal(color.red & 0x02, color.green & 0x01, color.green & 0x80);
|
|
|
|
++signal;
|
|
|
|
*signal = bits2Signal(color.green & 0x40, color.green & 0x20, color.green & 0x10);
|
|
|
|
++signal;
|
|
|
|
*signal = bits2Signal(color.green & 0x08, color.green & 0x04, color.green & 0x02);
|
|
|
|
++signal;
|
|
|
|
*signal = bits2Signal(color.green & 0x01, color.blue & 0x80, color.blue & 0x40);
|
|
|
|
++signal;
|
|
|
|
*signal = bits2Signal(color.blue & 0x20, color.blue & 0x10, color.blue & 0x08);
|
|
|
|
++signal;
|
|
|
|
*signal = bits2Signal(color.blue & 0x04, color.blue & 0x02, color.blue & 0x01);
|
|
|
|
++signal;
|
|
|
|
|
|
|
|
return signal;
|
2013-12-23 22:58:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-29 20:22:55 +01:00
|
|
|
int LedDeviceWs2812b::switchOff()
|
2013-12-23 22:58:54 +01:00
|
|
|
{
|
2013-12-29 20:22:55 +01:00
|
|
|
// Set all bytes in the signal buffer to zero
|
|
|
|
for (uint8_t & signal : _ledBuffer)
|
2013-12-23 22:58:54 +01:00
|
|
|
{
|
2013-12-29 20:22:55 +01:00
|
|
|
signal = ~0x24;
|
2013-12-23 22:58:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-29 20:22:55 +01:00
|
|
|
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
|
2013-12-23 22:58:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-29 20:22:55 +01:00
|
|
|
uint8_t LedDeviceWs2812b::bits2Signal(const bool bit_1, const bool bit_2, const bool bit_3) const
|
2013-12-23 22:58:54 +01:00
|
|
|
{
|
2013-12-25 16:37:59 +01:00
|
|
|
// See https://github.com/tvdzwan/hyperion/wiki/Ws2812b for the explanation of the given
|
|
|
|
// translations
|
2013-12-28 08:34:49 +01:00
|
|
|
|
2013-12-29 20:22:55 +01:00
|
|
|
// Bit index(default):1 2 3
|
|
|
|
// | | |
|
|
|
|
// default value (1) 00 100 10 (0)
|
|
|
|
//
|
|
|
|
// Reversed value (1) 01 001 00 (0)
|
|
|
|
// | | |
|
|
|
|
// Bit index (rev): 3 2 1
|
|
|
|
uint8_t result = 0x24;
|
2013-12-28 08:34:49 +01:00
|
|
|
|
2013-12-29 20:22:55 +01:00
|
|
|
if(bit_1)
|
|
|
|
{
|
|
|
|
result |= 0x01;
|
|
|
|
}
|
|
|
|
if (bit_2)
|
2013-12-23 22:58:54 +01:00
|
|
|
{
|
2013-12-29 20:22:55 +01:00
|
|
|
result |= 0x08;
|
2013-12-23 22:58:54 +01:00
|
|
|
}
|
2013-12-29 20:22:55 +01:00
|
|
|
if (bit_3)
|
2013-12-23 22:58:54 +01:00
|
|
|
{
|
2013-12-29 20:22:55 +01:00
|
|
|
result |= 0x40;
|
2013-12-23 22:58:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-29 20:22:55 +01:00
|
|
|
return ~result;
|
2013-12-23 22:58:54 +01:00
|
|
|
}
|