Merge pull request #260 from markusgritsch/dev
Changed the ws2812 byte order from GRB to RGB
This commit is contained in:
commit
fae802dae5
10
README.md
10
README.md
|
@ -382,10 +382,10 @@ They'll be available as `u8g.<font_name>` in Lua.
|
|||
|
||||
####Control a WS2812 based light strip
|
||||
```lua
|
||||
-- set the color of one LED on GPIO 2 to red
|
||||
ws2812.write(4, string.char(0, 255, 0))
|
||||
-- set the color of 10 LEDs on GPIO 0 to blue
|
||||
ws2812.write(3, string.char(0, 0, 255):rep(10))
|
||||
-- set the color of one LED on GPIO2 to red
|
||||
ws2812.writergb(4, string.char(255, 0, 0))
|
||||
-- set the color of 10 LEDs on GPIO0 to blue
|
||||
ws2812.writergb(3, string.char(0, 0, 255):rep(10))
|
||||
-- first LED green, second LED white
|
||||
ws2812.write(4, string.char(255, 0, 0, 255, 255, 255))
|
||||
ws2812.writergb(4, string.char(0, 255, 0, 255, 255, 255))
|
||||
```
|
||||
|
|
|
@ -69,7 +69,7 @@
|
|||
#define LUA_USE_MODULES_BIT
|
||||
#define LUA_USE_MODULES_U8G
|
||||
#define LUA_USE_MODULES_MQTT
|
||||
// #define LUA_USE_MODULES_WS2812 // TODO: put this device specific module to device driver section.
|
||||
#define LUA_USE_MODULES_WS2812 // TODO: put this device specific module to device driver section.
|
||||
#endif /* LUA_USE_MODULES */
|
||||
|
||||
// TODO: put device specific module to device driver section.
|
||||
|
|
|
@ -139,7 +139,6 @@
|
|||
ROM_MODULES_PWM \
|
||||
ROM_MODULES_WIFI \
|
||||
ROM_MODULES_MQTT \
|
||||
ROM_MODULES_U8G \
|
||||
ROM_MODULES_I2C \
|
||||
ROM_MODULES_SPI \
|
||||
ROM_MODULES_TMR \
|
||||
|
|
|
@ -8,47 +8,56 @@
|
|||
* from user Markus Gritsch.
|
||||
* I just put this code into its own module and pushed into a forked repo,
|
||||
* to easily create a pull request. Thanks to Markus Gritsch for the code.
|
||||
*
|
||||
*/
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// -- This WS2812 code must be compiled with -O2 to get the timing right.
|
||||
// -- This WS2812 code must be compiled with -O2 to get the timing right. Read this:
|
||||
// -- http://wp.josh.com/2014/05/13/ws2812-neopixels-are-not-so-finicky-once-you-get-to-know-them/
|
||||
// The ICACHE_FLASH_ATTR is there to trick the compiler and get the very first pulse width correct.
|
||||
// -- The ICACHE_FLASH_ATTR is there to trick the compiler and get the very first pulse width correct.
|
||||
static void ICACHE_FLASH_ATTR send_ws_0(uint8_t gpio) {
|
||||
uint8_t i;
|
||||
i = 4;
|
||||
while (i--)
|
||||
GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio);
|
||||
i = 9;
|
||||
while (i--)
|
||||
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << gpio);
|
||||
i = 4; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio);
|
||||
i = 9; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << gpio);
|
||||
}
|
||||
|
||||
static void ICACHE_FLASH_ATTR send_ws_1(uint8_t gpio) {
|
||||
uint8_t i;
|
||||
i = 8;
|
||||
while (i--)
|
||||
GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio);
|
||||
i = 6;
|
||||
while (i--)
|
||||
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << gpio);
|
||||
i = 8; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio);
|
||||
i = 6; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << 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.
|
||||
static int ICACHE_FLASH_ATTR ws2812_write(lua_State* L) {
|
||||
// ws2812.write(4, string.char(0, 255, 0, 255, 255, 255)) first LED green, second LED white.
|
||||
static int ICACHE_FLASH_ATTR ws2812_writergb(lua_State* L)
|
||||
{
|
||||
const uint8_t pin = luaL_checkinteger(L, 1);
|
||||
size_t length;
|
||||
const char *buffer = luaL_checklstring(L, 2, &length);
|
||||
char *buffer = (char *)luaL_checklstring(L, 2, &length); // Cast away the constness.
|
||||
|
||||
// Initialize the output pin:
|
||||
platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT);
|
||||
platform_gpio_write(pin, 0);
|
||||
os_delay_us(10);
|
||||
|
||||
// Ignore incomplete Byte triples at the end of buffer:
|
||||
length -= length % 3;
|
||||
|
||||
// Rearrange R G B values to G R B order needed by WS2812 LEDs:
|
||||
size_t i;
|
||||
for (i = 0; i < length; i += 3) {
|
||||
const char r = buffer[i];
|
||||
const char g = buffer[i + 1];
|
||||
buffer[i] = g;
|
||||
buffer[i + 1] = r;
|
||||
}
|
||||
|
||||
// Do not remove these:
|
||||
os_delay_us(1);
|
||||
os_delay_us(1);
|
||||
|
||||
// Send the buffer:
|
||||
os_intr_lock();
|
||||
const char * const end = buffer + length;
|
||||
while (buffer != end) {
|
||||
|
@ -68,7 +77,7 @@ static int ICACHE_FLASH_ATTR ws2812_write(lua_State* L) {
|
|||
#include "lrodefs.h"
|
||||
const LUA_REG_TYPE ws2812_map[] =
|
||||
{
|
||||
{ LSTRKEY( "write" ), LFUNCVAL( ws2812_write )},
|
||||
{ LSTRKEY( "writergb" ), LFUNCVAL( ws2812_writergb )},
|
||||
{ LNILKEY, LNILVAL}
|
||||
};
|
||||
|
||||
|
@ -77,6 +86,3 @@ LUALIB_API int luaopen_ws2812(lua_State *L) {
|
|||
LREGISTER(L, "ws2812", ws2812_map);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
|
|
Loading…
Reference in New Issue