2015-12-16 06:04:58 +01:00
|
|
|
#include "module.h"
|
2015-02-05 18:40:46 +01:00
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "platform.h"
|
2015-04-05 14:19:14 +02:00
|
|
|
#include "c_stdlib.h"
|
|
|
|
#include "c_string.h"
|
2015-07-12 12:44:11 +02:00
|
|
|
#include "user_interface.h"
|
2016-01-17 22:16:35 +01:00
|
|
|
#include "driver/uart.h"
|
2015-07-12 12:44:11 +02:00
|
|
|
|
2016-01-17 22:16:35 +01:00
|
|
|
// Stream data using UART1 routed to GPIO2
|
|
|
|
// NODE_DEBUG should not be activated because it also uses UART1
|
2015-07-12 12:44:11 +02:00
|
|
|
static void ICACHE_RAM_ATTR ws2812_write(uint8_t pin, uint8_t *pixels, uint32_t length) {
|
2016-01-17 22:16:35 +01:00
|
|
|
|
|
|
|
// Data are sent LSB first, with a start bit at 0, an end bit at 1 and all inverted
|
|
|
|
// 0b00110111 => 110111 => [0]111011[1] => 10001000 => 00
|
|
|
|
// 0b00000111 => 000111 => [0]111000[1] => 10001110 => 01
|
|
|
|
// 0b00110100 => 110100 => [0]001011[1] => 11101000 => 10
|
|
|
|
// 0b00000100 => 000100 => [0]001000[1] => 11101110 => 11
|
|
|
|
uint8_t _uartData[4] = { 0b00110111, 0b00000111, 0b00110100, 0b00000100 };
|
|
|
|
|
|
|
|
// Configure UART1
|
|
|
|
// Set baudrate of UART1 to 3200000
|
|
|
|
WRITE_PERI_REG(UART_CLKDIV(1), UART_CLK_FREQ / 3200000);
|
|
|
|
// Set UART Configuration No parity / 6 DataBits / 1 StopBits / Invert TX
|
|
|
|
WRITE_PERI_REG(UART_CONF0(1), UART_TXD_INV | (1 << UART_STOP_BIT_NUM_S) | (1 << UART_BIT_NUM_S));
|
|
|
|
|
|
|
|
// Redirect UART1 to GPIO2
|
|
|
|
// Disable GPIO2
|
|
|
|
GPIO_REG_WRITE(GPIO_ENABLE_W1TC_ADDRESS, BIT2);
|
|
|
|
// Enable Function 2 for GPIO2 (U1TXD)
|
|
|
|
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);
|
|
|
|
|
|
|
|
uint8_t *end = pixels + length;
|
|
|
|
do {
|
|
|
|
uint8_t value = *pixels++;
|
|
|
|
|
|
|
|
// Wait enough space in the FIFO buffer
|
|
|
|
// (Less than 124 bytes in the buffer)
|
|
|
|
while (((READ_PERI_REG(UART_STATUS(1)) >> UART_TXFIFO_CNT_S) & UART_TXFIFO_CNT) > 124);
|
|
|
|
|
|
|
|
// Fill the buffer
|
|
|
|
WRITE_PERI_REG(UART_FIFO(1), _uartData[(value >> 6) & 3]);
|
|
|
|
WRITE_PERI_REG(UART_FIFO(1), _uartData[(value >> 4) & 3]);
|
|
|
|
WRITE_PERI_REG(UART_FIFO(1), _uartData[(value >> 2) & 3]);
|
|
|
|
WRITE_PERI_REG(UART_FIFO(1), _uartData[(value >> 0) & 3]);
|
|
|
|
|
|
|
|
} while(pixels < end);
|
2015-02-05 18:40:46 +01:00
|
|
|
}
|
|
|
|
|
2015-05-26 23:12:38 +02:00
|
|
|
// Lua: ws2812.writergb(pin, "string")
|
2015-03-03 10:20:02 +01:00
|
|
|
// Byte triples in the string are interpreted as R G B values and sent to the hardware as G R B.
|
2015-05-26 23:12:38 +02:00
|
|
|
// WARNING: this function scrambles the input buffer :
|
|
|
|
// a = string.char(255,0,128)
|
|
|
|
// ws212.writergb(3,a)
|
2015-07-12 12:44:11 +02:00
|
|
|
// =a.byte()
|
2015-05-26 23:12:38 +02:00
|
|
|
// (0,255,128)
|
|
|
|
|
|
|
|
// ws2812.writergb(4, string.char(255, 0, 0)) uses GPIO2 and sets the first LED red.
|
|
|
|
// ws2812.writergb(3, string.char(0, 0, 255):rep(10)) uses GPIO0 and sets ten LEDs blue.
|
|
|
|
// ws2812.writergb(4, string.char(0, 255, 0, 255, 255, 255)) first LED green, second LED white.
|
2015-03-03 10:20:02 +01:00
|
|
|
static int ICACHE_FLASH_ATTR ws2812_writergb(lua_State* L)
|
|
|
|
{
|
2015-02-05 18:40:46 +01:00
|
|
|
const uint8_t pin = luaL_checkinteger(L, 1);
|
|
|
|
size_t length;
|
2015-04-05 14:19:14 +02:00
|
|
|
const char *rgb = luaL_checklstring(L, 2, &length);
|
2015-07-12 12:44:11 +02:00
|
|
|
|
2015-04-05 14:19:14 +02:00
|
|
|
// dont modify lua-internal lstring - make a copy instead
|
|
|
|
char *buffer = (char *)c_malloc(length);
|
|
|
|
c_memcpy(buffer, rgb, length);
|
2015-02-05 18:40:46 +01:00
|
|
|
|
2015-03-03 10:20:02 +01:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2015-07-12 12:44:11 +02:00
|
|
|
// Send the buffer
|
|
|
|
ws2812_write(pin_num[pin], (uint8_t*) buffer, length);
|
2015-02-05 18:40:46 +01:00
|
|
|
|
2015-04-05 14:19:14 +02:00
|
|
|
c_free(buffer);
|
|
|
|
|
2015-02-05 18:40:46 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-26 23:12:38 +02:00
|
|
|
// Lua: ws2812.write(pin, "string")
|
|
|
|
// Byte triples in the string are interpreted as G R B values.
|
|
|
|
// This function does not corrupt your buffer.
|
|
|
|
//
|
|
|
|
// ws2812.write(4, string.char(0, 255, 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_writegrb(lua_State* L) {
|
|
|
|
const uint8_t pin = luaL_checkinteger(L, 1);
|
|
|
|
size_t length;
|
|
|
|
const char *buffer = luaL_checklstring(L, 2, &length);
|
|
|
|
|
2015-07-12 12:44:11 +02:00
|
|
|
// Send the buffer
|
|
|
|
ws2812_write(pin_num[pin], (uint8_t*) buffer, length);
|
2015-05-26 23:12:38 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-16 06:04:58 +01:00
|
|
|
static const LUA_REG_TYPE ws2812_map[] =
|
2015-02-05 18:40:46 +01:00
|
|
|
{
|
2015-03-03 10:20:02 +01:00
|
|
|
{ LSTRKEY( "writergb" ), LFUNCVAL( ws2812_writergb )},
|
2015-05-26 23:12:38 +02:00
|
|
|
{ LSTRKEY( "write" ), LFUNCVAL( ws2812_writegrb )},
|
2015-02-07 07:05:41 +01:00
|
|
|
{ LNILKEY, LNILVAL}
|
2015-02-05 18:40:46 +01:00
|
|
|
};
|
|
|
|
|
2015-12-16 06:04:58 +01:00
|
|
|
int luaopen_ws2812(lua_State *L) {
|
2015-02-07 07:05:41 +01:00
|
|
|
// TODO: Make sure that the GPIO system is initialized
|
2015-12-12 04:27:31 +01:00
|
|
|
return 0;
|
2015-02-05 18:40:46 +01:00
|
|
|
}
|
2015-12-16 06:04:58 +01:00
|
|
|
|
|
|
|
NODEMCU_MODULE(WS2812, "ws2812", ws2812_map, luaopen_ws2812);
|