Merge pull request #339 from wladurner/issue_321
fixes issue #321 "ws2812.writergb scrambles buffer" reported by makefu.
This commit is contained in:
commit
e61ad9d7ba
|
@ -3,6 +3,8 @@
|
||||||
#include "platform.h"
|
#include "platform.h"
|
||||||
#include "auxmods.h"
|
#include "auxmods.h"
|
||||||
#include "lrotable.h"
|
#include "lrotable.h"
|
||||||
|
#include "c_stdlib.h"
|
||||||
|
#include "c_string.h"
|
||||||
/**
|
/**
|
||||||
* All this code is mostly from http://www.esp8266.com/viewtopic.php?f=21&t=1143&sid=a620a377672cfe9f666d672398415fcb
|
* All this code is mostly from http://www.esp8266.com/viewtopic.php?f=21&t=1143&sid=a620a377672cfe9f666d672398415fcb
|
||||||
* from user Markus Gritsch.
|
* from user Markus Gritsch.
|
||||||
|
@ -35,7 +37,10 @@ static int ICACHE_FLASH_ATTR ws2812_writergb(lua_State* L)
|
||||||
{
|
{
|
||||||
const uint8_t pin = luaL_checkinteger(L, 1);
|
const uint8_t pin = luaL_checkinteger(L, 1);
|
||||||
size_t length;
|
size_t length;
|
||||||
char *buffer = (char *)luaL_checklstring(L, 2, &length); // Cast away the constness.
|
const char *rgb = luaL_checklstring(L, 2, &length);
|
||||||
|
// dont modify lua-internal lstring - make a copy instead
|
||||||
|
char *buffer = (char *)c_malloc(length);
|
||||||
|
c_memcpy(buffer, rgb, length);
|
||||||
|
|
||||||
// Initialize the output pin:
|
// Initialize the output pin:
|
||||||
platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT);
|
platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT);
|
||||||
|
@ -59,17 +64,17 @@ static int ICACHE_FLASH_ATTR ws2812_writergb(lua_State* L)
|
||||||
|
|
||||||
// Send the buffer:
|
// Send the buffer:
|
||||||
os_intr_lock();
|
os_intr_lock();
|
||||||
const char * const end = buffer + length;
|
for (i = 0; i < length; i++) {
|
||||||
while (buffer != end) {
|
|
||||||
uint8_t mask = 0x80;
|
uint8_t mask = 0x80;
|
||||||
while (mask) {
|
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;
|
mask >>= 1;
|
||||||
}
|
}
|
||||||
++buffer;
|
|
||||||
}
|
}
|
||||||
os_intr_unlock();
|
os_intr_unlock();
|
||||||
|
|
||||||
|
c_free(buffer);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue