52 lines
1.4 KiB
C++
52 lines
1.4 KiB
C++
#include "ws2812b.h"
|
|
|
|
enum WS2812B_SPI : uint32_t {
|
|
BIT_LOW = 1u, BIT_HIGH = 3u,
|
|
};
|
|
|
|
OneColor::OneColor(const uint8_t color) noexcept {
|
|
b0 = (color & 0x80u) ? BIT_HIGH : BIT_LOW;
|
|
b1 = (color & 0x40u) ? BIT_HIGH : BIT_LOW;
|
|
b2 = (color & 0x20u) ? BIT_HIGH : BIT_LOW;
|
|
b3 = (color & 0x10u) ? BIT_HIGH : BIT_LOW;
|
|
b4 = (color & 0x08u) ? BIT_HIGH : BIT_LOW;
|
|
b5 = (color & 0x04u) ? BIT_HIGH : BIT_LOW;
|
|
b6 = (color & 0x02u) ? BIT_HIGH : BIT_LOW;
|
|
b7 = (color & 0x01u) ? BIT_HIGH : BIT_LOW;
|
|
}
|
|
unsigned OneColor::to_string(char * ptr, const int m) {
|
|
auto f = [=](const uint32_t x, char * buf) -> unsigned {
|
|
unsigned i = 0;
|
|
for (int k=0; k<m; k++) {
|
|
buf [i++] = x & (1u << k) ? '1' : '0';
|
|
}
|
|
return i;
|
|
};
|
|
unsigned n = 0;
|
|
n += f (b0, ptr + n);
|
|
n += f (b1, ptr + n);
|
|
n += f (b2, ptr + n);
|
|
n += f (b3, ptr + n);
|
|
n += f (b4, ptr + n);
|
|
n += f (b5, ptr + n);
|
|
n += f (b6, ptr + n);
|
|
n += f (b7, ptr + n);
|
|
ptr [n] = '\0';
|
|
return n;
|
|
}
|
|
|
|
ws2812b::ws2812b (FIFO<uint32_t,8> & r) noexcept : OneWay (), ring(r) {
|
|
}
|
|
unsigned int ws2812b::Send (uint8_t * const ptr, const unsigned int len) {
|
|
uint32_t cmd;
|
|
while (ring.Read(cmd)) {
|
|
const Entry ne (cmd);
|
|
if (ne.ws.order < NUMLEDS) {
|
|
Color * cptr = reinterpret_cast<Color*>(ptr);
|
|
Color & c = cptr [ne.ws.order];
|
|
const OneColor cb (ne.ws.b), cg (ne.ws.g), cr (ne.ws.r);
|
|
c.b = cb; c.g = cg; c.r = cr;
|
|
}
|
|
}
|
|
return len;
|
|
}
|