mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Modified the ws2812b to use a 3bit encoding speed which allows operation of the uart at slower speed.
Former-commit-id: fb89050546f85f82fb1fcc4cc2d24f95d8f78de5
This commit is contained in:
@@ -6,109 +6,91 @@
|
||||
#include "LedDeviceWs2812b.h"
|
||||
|
||||
LedDeviceWs2812b::LedDeviceWs2812b() :
|
||||
LedRs232Device("/dev/ttyAMA0", 4000000)
|
||||
LedRs232Device("/dev/ttyAMA0", 2500000)
|
||||
{
|
||||
fillTable();
|
||||
// empty
|
||||
}
|
||||
|
||||
int LedDeviceWs2812b::write(const std::vector<ColorRgb> & ledValues)
|
||||
{
|
||||
// Ensure the size of the led-buffer
|
||||
if (_ledBuffer.size() != ledValues.size()*3)
|
||||
if (_ledBuffer.size() != ledValues.size()*8)
|
||||
{
|
||||
_ledBuffer.resize(ledValues.size()*3);
|
||||
_ledBuffer.resize(ledValues.size()*8, ~0x24);
|
||||
}
|
||||
|
||||
// Translate the channel of each color to a signal
|
||||
for (unsigned iLed=0; iLed<ledValues.size(); ++iLed)
|
||||
uint8_t * signal_ptr = _ledBuffer.data();
|
||||
for (const ColorRgb & color : ledValues)
|
||||
{
|
||||
const ColorRgb & color = ledValues[iLed];
|
||||
|
||||
_ledBuffer[3*iLed] = _byte2signalTable[color.red];
|
||||
_ledBuffer[3*iLed + 1] = _byte2signalTable[color.green];
|
||||
_ledBuffer[3*iLed + 2] = _byte2signalTable[color.blue];
|
||||
signal_ptr = color2signal(color, signal_ptr);
|
||||
}
|
||||
|
||||
const int result = writeBytes(_ledBuffer.size()*sizeof(ByteSignal), reinterpret_cast<uint8_t *>(_ledBuffer.data()));
|
||||
const int result = writeBytes(_ledBuffer.size(), _ledBuffer.data());
|
||||
// Official latch time is 50us (lets give it 50us more)
|
||||
usleep(100);
|
||||
return result;
|
||||
}
|
||||
|
||||
uint8_t * LedDeviceWs2812b::color2signal(const ColorRgb & color, uint8_t * signal)
|
||||
{
|
||||
*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;
|
||||
}
|
||||
|
||||
int LedDeviceWs2812b::switchOff()
|
||||
{
|
||||
// Set all bytes in the signal buffer to zero
|
||||
for (ByteSignal & signal : _ledBuffer)
|
||||
for (uint8_t & signal : _ledBuffer)
|
||||
{
|
||||
signal = _byte2signalTable[0];
|
||||
signal = ~0x24;
|
||||
}
|
||||
|
||||
return writeBytes(_ledBuffer.size()*sizeof(ByteSignal), reinterpret_cast<uint8_t *>(_ledBuffer.data()));
|
||||
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
|
||||
}
|
||||
|
||||
void LedDeviceWs2812b::fillTable()
|
||||
{
|
||||
_byte2signalTable.clear();
|
||||
for (int byte=0; byte<256; ++byte)
|
||||
{
|
||||
const ByteSignal signal = byte2Signal(uint8_t(byte));
|
||||
_byte2signalTable.push_back(signal);
|
||||
}
|
||||
}
|
||||
|
||||
LedDeviceWs2812b::ByteSignal LedDeviceWs2812b::byte2Signal(const uint8_t byte) const
|
||||
{
|
||||
ByteSignal result;
|
||||
result.bit_12 = bits2Signal(byte & 0x80, byte & 0x40);
|
||||
result.bit_34 = bits2Signal(byte & 0x20, byte & 0x10);
|
||||
result.bit_56 = bits2Signal(byte & 0x08, byte & 0x04);
|
||||
result.bit_78 = bits2Signal(byte & 0x02, byte & 0x01);
|
||||
return result;
|
||||
}
|
||||
|
||||
uint8_t LedDeviceWs2812b::bits2Signal(const bool bit1, const bool bit2) const
|
||||
uint8_t LedDeviceWs2812b::bits2Signal(const bool bit_1, const bool bit_2, const bool bit_3) const
|
||||
{
|
||||
// See https://github.com/tvdzwan/hyperion/wiki/Ws2812b for the explanation of the given
|
||||
// translations
|
||||
|
||||
// Encoding scheme 1
|
||||
// 00 1 1000 1100 0 1 0111 0011 0 1 1100 1110 0 0xCE
|
||||
// 01 1 1000 1110 0 1 0111 0001 0 1 1000 1110 0 0x8E
|
||||
// 10 1 1100 1100 0 1 0011 0011 0 1 1100 1100 0 0xCC
|
||||
// 11 1 1100 1110 0 1 0011 0001 0 1 1000 1100 0 0x8C
|
||||
// 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;
|
||||
|
||||
// Encoding schem 2
|
||||
// 00 - 1 0000 1000 0 - 1 1111 0111 0 - 1 1110 1111 0 - 0xEF
|
||||
// 01 - 1 0000 1111 0 - 1 1111 0000 0 - 1 0000 1111 0 - 0x0F
|
||||
// 10 - 1 1110 1000 0 - 1 0001 0111 0 - 1 1110 1000 0 - 0xE8
|
||||
// 11 - 1 1110 1111 0 - 1 0001 0000 0 - 1 0000 1000 0 - 0x08
|
||||
|
||||
if (bit1)
|
||||
if(bit_1)
|
||||
{
|
||||
if (bit2)
|
||||
{
|
||||
// return 0x08;
|
||||
return 0x8C;
|
||||
}
|
||||
else
|
||||
{
|
||||
// return 0xE8;
|
||||
return 0xCC;
|
||||
}
|
||||
result |= 0x01;
|
||||
}
|
||||
else
|
||||
if (bit_2)
|
||||
{
|
||||
if (bit2)
|
||||
{
|
||||
// return 0x0F;
|
||||
return 0x8E;
|
||||
}
|
||||
else
|
||||
{
|
||||
// return 0xEF;
|
||||
return 0xCE;
|
||||
}
|
||||
result |= 0x08;
|
||||
}
|
||||
if (bit_3)
|
||||
{
|
||||
result |= 0x40;
|
||||
}
|
||||
|
||||
return 0x00;
|
||||
return ~result;
|
||||
}
|
||||
|
@@ -34,43 +34,27 @@ public:
|
||||
private:
|
||||
|
||||
///
|
||||
/// Structure holding the four output-bytes corresponding to a single input byte
|
||||
/// Translate a color to the signal bits. The resulting bits are written to the given memory.
|
||||
///
|
||||
struct ByteSignal
|
||||
{
|
||||
uint8_t bit_12;
|
||||
uint8_t bit_34;
|
||||
uint8_t bit_56;
|
||||
uint8_t bit_78;
|
||||
};
|
||||
/// Translation table from single input-byte to output-bytes
|
||||
std::vector<ByteSignal> _byte2signalTable;
|
||||
/// @param color The color to translate
|
||||
/// @param signal The pointer at the beginning of the signal to write
|
||||
/// @return The pointer at the end of the written signal
|
||||
///
|
||||
uint8_t * color2signal(const ColorRgb & color, uint8_t * signal);
|
||||
|
||||
///
|
||||
/// Fills the translation table (_byte2signalTable)
|
||||
///
|
||||
void fillTable();
|
||||
|
||||
///
|
||||
/// Computes the output bytes that belong to a given input-byte (no table lookup)
|
||||
///
|
||||
/// @param byte The input byte
|
||||
/// @return The four bytes (ByteSignal) for the output signal
|
||||
///
|
||||
ByteSignal byte2Signal(const uint8_t byte) const;
|
||||
|
||||
///
|
||||
/// Translates two bits to a single byte
|
||||
/// Translates three bits to a single byte
|
||||
///
|
||||
/// @param bit1 The value of the first bit (1=true, zero=false)
|
||||
/// @param bit1 The value of the ssecond bit (1=true, zero=false)
|
||||
/// @param bit2 The value of the second bit (1=true, zero=false)
|
||||
/// @param bit3 The value of the third bit (1=true, zero=false)
|
||||
///
|
||||
/// @return The output-byte for the given two bit
|
||||
///
|
||||
uint8_t bits2Signal(const bool bit1, const bool bit2) const;
|
||||
uint8_t bits2Signal(const bool bit1, const bool bit2, const bool bit3) const;
|
||||
|
||||
///
|
||||
/// The output buffer for writing bytes to the output
|
||||
///
|
||||
std::vector<ByteSignal> _ledBuffer;
|
||||
std::vector<uint8_t> _ledBuffer;
|
||||
};
|
||||
|
Reference in New Issue
Block a user