ws2812: change colors to accept RGB values

Nobody would expect GRB input, and converting from RGB to GRB via Lua is too slow,
so convert it in place before sending to the hardware.
This commit is contained in:
Florian Klink 2015-02-27 07:55:40 +01:00
parent e60e4f1f5d
commit 1ef1843e7a
1 changed files with 13 additions and 6 deletions

View File

@ -36,28 +36,35 @@ static void ICACHE_FLASH_ATTR send_ws_1(uint8_t gpio) {
}
// Lua: ws2812.write(pin, "string")
// Byte triples in the string are interpreted as G R B values.
// ws2812.write(4, string.char(0, 255, 0)) uses GPIO2 and sets the first LED red.
// Byte triples in the string are interpreted as R G B values
// and sent to the hardware as G R B.
// ws2812.write(4, string.char(255, 0, 0)) uses GPIO2 and sets the first LED red.
// ws2812.write(3, string.char(0, 0, 255):rep(10)) uses GPIO0 and sets ten LEDs blue.
// ws2812.write(4, string.char(255, 0, 0, 255, 255, 255)) first LED green, second LED white.
// ws2812.write(4, string.char(0, 255, 0, 255, 255, 255)) first LED green, second LED white.
static int ICACHE_FLASH_ATTR ws2812_write(lua_State* L) {
const uint8_t pin = luaL_checkinteger(L, 1);
size_t length;
const char *buffer = luaL_checklstring(L, 2, &length);
//ignore incomplete Byte triples at the end of buffer
length -= length % 3;
platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT);
platform_gpio_write(pin, 0);
os_delay_us(10);
os_intr_lock();
const char * const end = buffer + length;
while (buffer != end) {
size_t i = 1;
while (buffer + i <= end) {
uint8_t mask = 0x80;
while (mask) {
(*buffer & mask) ? send_ws_1(pin_num[pin]) : send_ws_0(pin_num[pin]);
(buffer[i] & mask) ? send_ws_1(pin_num[pin]) : send_ws_0(pin_num[pin]);
mask >>= 1;
}
++buffer;
i += ((i % 3) == 1) ? -1 : 2;
}
os_intr_unlock();