2015-12-16 06:04:58 +01:00
|
|
|
#include "module.h"
|
2015-02-05 18:40:46 +01:00
|
|
|
#include "lauxlib.h"
|
2016-05-09 07:21:50 +02:00
|
|
|
#include "lmem.h"
|
2015-02-05 18:40:46 +01:00
|
|
|
#include "platform.h"
|
2015-04-05 14:19:14 +02:00
|
|
|
#include "c_stdlib.h"
|
2018-01-23 21:05:04 +01:00
|
|
|
#include "c_math.h"
|
2015-04-05 14:19:14 +02:00
|
|
|
#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"
|
2016-06-10 22:23:37 +02:00
|
|
|
#include "osapi.h"
|
2015-07-12 12:44:11 +02:00
|
|
|
|
2018-01-23 21:05:04 +01:00
|
|
|
#include "ws2812.h"
|
|
|
|
|
2015-11-24 22:40:30 +01:00
|
|
|
#define CANARY_VALUE 0x32383132
|
2016-07-20 22:28:47 +02:00
|
|
|
#define MODE_SINGLE 0
|
|
|
|
#define MODE_DUAL 1
|
2015-11-24 22:40:30 +01:00
|
|
|
|
2016-06-10 22:23:37 +02:00
|
|
|
|
|
|
|
|
2018-01-23 21:05:04 +01:00
|
|
|
|
|
|
|
|
2015-11-24 22:40:30 +01:00
|
|
|
|
2016-07-20 22:28:47 +02:00
|
|
|
// Init UART1 to be able to stream WS2812 data to GPIO2 pin
|
|
|
|
// If DUAL mode is selected, init UART0 to stream to TXD0 as well
|
|
|
|
// You HAVE to redirect LUA's output somewhere else
|
2017-02-24 21:20:09 +01:00
|
|
|
static int ws2812_init(lua_State* L) {
|
2016-07-20 22:28:47 +02:00
|
|
|
const int mode = luaL_optinteger(L, 1, MODE_SINGLE);
|
|
|
|
luaL_argcheck(L, mode == MODE_SINGLE || mode == MODE_DUAL, 1, "ws2812.SINGLE or ws2812.DUAL expected");
|
|
|
|
|
2016-01-17 22:16:35 +01:00
|
|
|
// 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));
|
|
|
|
|
2016-07-20 22:28:47 +02:00
|
|
|
if (mode == MODE_DUAL) {
|
|
|
|
// Configure UART0
|
|
|
|
// Set baudrate of UART0 to 3200000
|
|
|
|
WRITE_PERI_REG(UART_CLKDIV(0), UART_CLK_FREQ / 3200000);
|
|
|
|
// Set UART Configuration No parity / 6 DataBits / 1 StopBits / Invert TX
|
|
|
|
WRITE_PERI_REG(UART_CONF0(0), UART_TXD_INV | (1 << UART_STOP_BIT_NUM_S) | (1 << UART_BIT_NUM_S));
|
|
|
|
}
|
|
|
|
|
2016-05-09 07:21:50 +02:00
|
|
|
// Pull GPIO2 down
|
|
|
|
platform_gpio_mode(4, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT);
|
|
|
|
platform_gpio_write(4, 0);
|
|
|
|
|
|
|
|
// Waits 10us to simulate a reset
|
|
|
|
os_delay_us(10);
|
|
|
|
|
2016-01-17 22:16:35 +01:00
|
|
|
// 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);
|
2017-02-24 21:20:09 +01:00
|
|
|
|
|
|
|
return 0;
|
2016-05-09 07:21:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stream data using UART1 routed to GPIO2
|
|
|
|
// ws2812.init() should be called first
|
|
|
|
//
|
|
|
|
// NODE_DEBUG should not be activated because it also uses UART1
|
2018-01-23 21:05:04 +01:00
|
|
|
void ICACHE_RAM_ATTR ws2812_write_data(const uint8_t *pixels, uint32_t length, const uint8_t *pixels2, uint32_t length2) {
|
2016-05-09 07:21:50 +02: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
|
|
|
|
// Array declared as static const to avoid runtime generation
|
|
|
|
// But declared in ".data" section to avoid read penalty from FLASH
|
|
|
|
static const __attribute__((section(".data._uartData"))) uint8_t _uartData[4] = { 0b00110111, 0b00000111, 0b00110100, 0b00000100 };
|
2016-01-17 22:16:35 +01:00
|
|
|
|
2016-07-20 22:28:47 +02:00
|
|
|
const uint8_t *end = pixels + length;
|
|
|
|
const uint8_t *end2 = pixels2 + length2;
|
2016-05-09 07:21:50 +02:00
|
|
|
|
2016-01-17 22:16:35 +01:00
|
|
|
do {
|
2016-07-20 22:28:47 +02:00
|
|
|
// If something to send for first buffer and enough room
|
|
|
|
// in FIFO buffer (we wants to write 4 bytes, so less than
|
|
|
|
// 124 in the buffer)
|
|
|
|
if (pixels < end && (((READ_PERI_REG(UART_STATUS(1)) >> UART_TXFIFO_CNT_S) & UART_TXFIFO_CNT) <= 124)) {
|
|
|
|
uint8_t value = *pixels++;
|
|
|
|
|
|
|
|
// 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]);
|
|
|
|
}
|
|
|
|
// Same for the second buffer
|
|
|
|
if (pixels2 < end2 && (((READ_PERI_REG(UART_STATUS(0)) >> UART_TXFIFO_CNT_S) & UART_TXFIFO_CNT) <= 124)) {
|
|
|
|
uint8_t value = *pixels2++;
|
|
|
|
|
|
|
|
// Fill the buffer
|
|
|
|
WRITE_PERI_REG(UART_FIFO(0), _uartData[(value >> 6) & 3]);
|
|
|
|
WRITE_PERI_REG(UART_FIFO(0), _uartData[(value >> 4) & 3]);
|
|
|
|
WRITE_PERI_REG(UART_FIFO(0), _uartData[(value >> 2) & 3]);
|
|
|
|
WRITE_PERI_REG(UART_FIFO(0), _uartData[(value >> 0) & 3]);
|
|
|
|
}
|
|
|
|
} while(pixels < end || pixels2 < end2); // Until there is still something to send
|
2015-02-05 18:40:46 +01:00
|
|
|
}
|
|
|
|
|
2016-05-09 07:21:50 +02:00
|
|
|
// Lua: ws2812.write("string")
|
|
|
|
// Byte triples in the string are interpreted as G R B values.
|
2015-05-26 23:12:38 +02:00
|
|
|
//
|
2016-05-09 07:21:50 +02:00
|
|
|
// ws2812.init() should be called first
|
|
|
|
//
|
|
|
|
// ws2812.write(string.char(0, 255, 0)) sets the first LED red.
|
|
|
|
// ws2812.write(string.char(0, 0, 255):rep(10)) sets ten LEDs blue.
|
|
|
|
// ws2812.write(string.char(255, 0, 0, 255, 255, 255)) first LED green, second LED white.
|
2016-07-20 22:28:47 +02:00
|
|
|
//
|
|
|
|
// In DUAL mode 'ws2812.init(ws2812.DUAL)', you may pass a second string as parameter
|
|
|
|
// It will be sent through TXD0 in parallel
|
|
|
|
static int ws2812_write(lua_State* L) {
|
|
|
|
size_t length1, length2;
|
|
|
|
const char *buffer1, *buffer2;
|
|
|
|
|
|
|
|
// First mandatory parameter
|
|
|
|
int type = lua_type(L, 1);
|
|
|
|
if (type == LUA_TNIL)
|
|
|
|
{
|
|
|
|
buffer1 = 0;
|
|
|
|
length1 = 0;
|
|
|
|
}
|
|
|
|
else if(type == LUA_TSTRING)
|
|
|
|
{
|
|
|
|
buffer1 = lua_tolstring(L, 1, &length1);
|
|
|
|
}
|
|
|
|
else if (type == LUA_TUSERDATA)
|
|
|
|
{
|
2016-11-05 13:53:42 +01:00
|
|
|
ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
|
2015-05-26 23:12:38 +02:00
|
|
|
|
2016-07-20 22:28:47 +02:00
|
|
|
buffer1 = buffer->values;
|
|
|
|
length1 = buffer->colorsPerLed*buffer->size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
luaL_argerror(L, 1, "ws2812.buffer or string expected");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second optionnal parameter
|
|
|
|
type = lua_type(L, 2);
|
|
|
|
if (type == LUA_TNONE || type == LUA_TNIL)
|
|
|
|
{
|
|
|
|
buffer2 = 0;
|
|
|
|
length2 = 0;
|
|
|
|
}
|
|
|
|
else if (type == LUA_TSTRING)
|
|
|
|
{
|
|
|
|
buffer2 = lua_tolstring(L, 2, &length2);
|
|
|
|
}
|
|
|
|
else if (type == LUA_TUSERDATA)
|
|
|
|
{
|
2016-11-05 13:53:42 +01:00
|
|
|
ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 2, "ws2812.buffer");
|
2016-07-20 22:28:47 +02:00
|
|
|
|
|
|
|
buffer2 = buffer->values;
|
|
|
|
length2 = buffer->colorsPerLed*buffer->size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
luaL_argerror(L, 2, "ws2812.buffer or string expected");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the buffers
|
|
|
|
ws2812_write_data(buffer1, length1, buffer2, length2);
|
2015-11-24 22:40:30 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-05 13:53:42 +01:00
|
|
|
static ptrdiff_t posrelat (ptrdiff_t pos, size_t len) {
|
|
|
|
/* relative string position: negative means back from end */
|
|
|
|
if (pos < 0) pos += (ptrdiff_t)len + 1;
|
|
|
|
return (pos >= 0) ? pos : 0;
|
|
|
|
}
|
2015-11-24 22:40:30 +01:00
|
|
|
|
2016-11-05 13:53:42 +01:00
|
|
|
static ws2812_buffer *allocate_buffer(lua_State *L, int leds, int colorsPerLed) {
|
2015-11-24 22:40:30 +01:00
|
|
|
// Allocate memory
|
2016-05-09 07:21:50 +02:00
|
|
|
size_t size = sizeof(ws2812_buffer) + colorsPerLed*leds*sizeof(uint8_t);
|
2015-11-24 22:40:30 +01:00
|
|
|
ws2812_buffer * buffer = (ws2812_buffer*)lua_newuserdata(L, size);
|
|
|
|
|
|
|
|
// Associate its metatable
|
|
|
|
luaL_getmetatable(L, "ws2812.buffer");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
|
|
|
|
// Save led strip size
|
|
|
|
buffer->size = leds;
|
2016-05-09 07:21:50 +02:00
|
|
|
buffer->colorsPerLed = colorsPerLed;
|
2015-11-24 22:40:30 +01:00
|
|
|
|
2016-11-05 13:53:42 +01:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2018-01-23 21:05:04 +01:00
|
|
|
|
2016-11-05 13:53:42 +01:00
|
|
|
// Handle a buffer where we can store led values
|
|
|
|
static int ws2812_new_buffer(lua_State *L) {
|
|
|
|
const int leds = luaL_checkint(L, 1);
|
|
|
|
const int colorsPerLed = luaL_checkint(L, 2);
|
|
|
|
|
|
|
|
luaL_argcheck(L, leds > 0, 1, "should be a positive integer");
|
|
|
|
luaL_argcheck(L, colorsPerLed > 0, 2, "should be a positive integer");
|
|
|
|
|
|
|
|
ws2812_buffer * buffer = allocate_buffer(L, leds, colorsPerLed);
|
|
|
|
|
|
|
|
c_memset(buffer->values, 0, colorsPerLed * leds);
|
2015-11-24 22:40:30 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-01-23 21:05:04 +01:00
|
|
|
|
|
|
|
int ws2812_buffer_fill(ws2812_buffer * buffer, int * colors) {
|
2015-11-24 22:40:30 +01:00
|
|
|
|
2016-05-09 07:21:50 +02:00
|
|
|
// Grab colors
|
|
|
|
int i, j;
|
2015-11-24 22:40:30 +01:00
|
|
|
|
2016-05-09 07:21:50 +02:00
|
|
|
// Fill buffer
|
2015-11-24 22:40:30 +01:00
|
|
|
uint8_t * p = &buffer->values[0];
|
2016-05-09 07:21:50 +02:00
|
|
|
for(i = 0; i < buffer->size; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < buffer->colorsPerLed; j++)
|
|
|
|
{
|
|
|
|
*p++ = colors[j];
|
|
|
|
}
|
2015-11-24 22:40:30 +01:00
|
|
|
}
|
|
|
|
|
2018-01-23 21:05:04 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ws2812_buffer_fill_lua(lua_State* L) {
|
|
|
|
ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
|
|
|
|
|
|
|
|
// Grab colors
|
|
|
|
int i;
|
|
|
|
int * colors = luaM_malloc(L, buffer->colorsPerLed * sizeof(int));
|
|
|
|
|
|
|
|
for (i = 0; i < buffer->colorsPerLed; i++)
|
|
|
|
{
|
|
|
|
colors[i] = luaL_checkinteger(L, 2+i);
|
|
|
|
}
|
|
|
|
|
|
|
|
ws2812_buffer_fill(buffer, colors);
|
|
|
|
|
2016-05-09 07:21:50 +02:00
|
|
|
// Free memory
|
|
|
|
luaM_free(L, colors);
|
|
|
|
|
2015-11-24 22:40:30 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-09 07:21:50 +02:00
|
|
|
static int ws2812_buffer_fade(lua_State* L) {
|
2016-11-05 13:53:42 +01:00
|
|
|
ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
|
2015-11-24 22:40:30 +01:00
|
|
|
const int fade = luaL_checkinteger(L, 2);
|
2016-06-10 22:23:37 +02:00
|
|
|
unsigned direction = luaL_optinteger( L, 3, FADE_OUT );
|
2015-11-24 22:40:30 +01:00
|
|
|
|
|
|
|
luaL_argcheck(L, fade > 0, 2, "fade value should be a strict positive number");
|
|
|
|
|
|
|
|
uint8_t * p = &buffer->values[0];
|
2016-06-10 22:23:37 +02:00
|
|
|
int val = 0;
|
2015-11-24 22:40:30 +01:00
|
|
|
int i;
|
2016-05-09 07:21:50 +02:00
|
|
|
for(i = 0; i < buffer->size * buffer->colorsPerLed; i++)
|
|
|
|
{
|
2016-06-10 22:23:37 +02:00
|
|
|
if (direction == FADE_OUT)
|
|
|
|
{
|
|
|
|
*p++ /= fade;
|
|
|
|
}
|
2016-07-20 22:28:47 +02:00
|
|
|
else
|
2016-06-10 22:23:37 +02:00
|
|
|
{
|
|
|
|
// as fade in can result in value overflow, an int is used to perform the check afterwards
|
|
|
|
val = *p * fade;
|
|
|
|
if (val > 255) val = 255;
|
|
|
|
*p++ = val;
|
|
|
|
}
|
2015-11-24 22:40:30 +01:00
|
|
|
}
|
2015-05-26 23:12:38 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-10 22:23:37 +02:00
|
|
|
|
2018-01-23 21:05:04 +01:00
|
|
|
int ws2812_buffer_shift(ws2812_buffer * buffer, int shiftValue, unsigned shift_type, int pos_start, int pos_end) {
|
2016-06-10 22:23:37 +02:00
|
|
|
|
2018-01-23 21:05:04 +01:00
|
|
|
ptrdiff_t start = posrelat(pos_start, buffer->size);
|
|
|
|
ptrdiff_t end = posrelat(pos_end, buffer->size);
|
2016-11-05 13:53:42 +01:00
|
|
|
if (start < 1) start = 1;
|
|
|
|
if (end > (ptrdiff_t)buffer->size) end = (ptrdiff_t)buffer->size;
|
|
|
|
|
|
|
|
start--;
|
|
|
|
int size = end - start;
|
|
|
|
size_t offset = start * buffer->colorsPerLed;
|
|
|
|
|
2018-01-23 21:05:04 +01:00
|
|
|
//luaL_argcheck(L, shiftValue > 0-size && shiftValue < size, 2, "shifting more elements than buffer size");
|
2016-06-10 22:23:37 +02:00
|
|
|
|
|
|
|
int shift = shiftValue >= 0 ? shiftValue : -shiftValue;
|
2016-07-20 22:28:47 +02:00
|
|
|
|
2016-06-10 22:23:37 +02:00
|
|
|
// check if we want to shift at all
|
2016-11-05 13:53:42 +01:00
|
|
|
if (shift == 0 || size <= 0)
|
2016-06-10 22:23:37 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-07-20 22:28:47 +02:00
|
|
|
|
2018-01-23 21:05:04 +01:00
|
|
|
uint8_t * tmp_pixels = c_malloc(buffer->colorsPerLed * sizeof(uint8_t) * shift);
|
2016-06-10 22:23:37 +02:00
|
|
|
int i,j;
|
|
|
|
size_t shift_len, remaining_len;
|
|
|
|
// calculate length of shift section and remaining section
|
|
|
|
shift_len = shift*buffer->colorsPerLed;
|
2016-11-05 13:53:42 +01:00
|
|
|
remaining_len = (size-shift)*buffer->colorsPerLed;
|
2016-07-20 22:28:47 +02:00
|
|
|
|
|
|
|
if (shiftValue > 0)
|
2016-06-10 22:23:37 +02:00
|
|
|
{
|
|
|
|
// Store the values which are moved out of the array (last n pixels)
|
2016-11-05 13:53:42 +01:00
|
|
|
c_memcpy(tmp_pixels, &buffer->values[offset + (size-shift)*buffer->colorsPerLed], shift_len);
|
2016-06-10 22:23:37 +02:00
|
|
|
// Move pixels to end
|
2016-11-05 13:53:42 +01:00
|
|
|
os_memmove(&buffer->values[offset + shift*buffer->colorsPerLed], &buffer->values[offset], remaining_len);
|
2016-06-10 22:23:37 +02:00
|
|
|
// Fill beginning with temp data
|
|
|
|
if (shift_type == SHIFT_LOGICAL)
|
|
|
|
{
|
2016-11-05 13:53:42 +01:00
|
|
|
c_memset(&buffer->values[offset], 0, shift_len);
|
2016-06-10 22:23:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-05 13:53:42 +01:00
|
|
|
c_memcpy(&buffer->values[offset], tmp_pixels, shift_len);
|
2016-06-10 22:23:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Store the values which are moved out of the array (last n pixels)
|
2016-11-05 13:53:42 +01:00
|
|
|
c_memcpy(tmp_pixels, &buffer->values[offset], shift_len);
|
2016-06-10 22:23:37 +02:00
|
|
|
// Move pixels to end
|
2016-11-05 13:53:42 +01:00
|
|
|
os_memmove(&buffer->values[offset], &buffer->values[offset + shift*buffer->colorsPerLed], remaining_len);
|
2016-06-10 22:23:37 +02:00
|
|
|
// Fill beginning with temp data
|
|
|
|
if (shift_type == SHIFT_LOGICAL)
|
|
|
|
{
|
2016-11-05 13:53:42 +01:00
|
|
|
c_memset(&buffer->values[offset + (size-shift)*buffer->colorsPerLed], 0, shift_len);
|
2016-06-10 22:23:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-05 13:53:42 +01:00
|
|
|
c_memcpy(&buffer->values[offset + (size-shift)*buffer->colorsPerLed], tmp_pixels, shift_len);
|
2016-06-10 22:23:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Free memory
|
2018-01-23 21:05:04 +01:00
|
|
|
c_free(tmp_pixels);
|
2016-06-10 22:23:37 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-23 21:05:04 +01:00
|
|
|
|
|
|
|
static int ws2812_buffer_shift_lua(lua_State* L) {
|
|
|
|
|
|
|
|
ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
|
|
|
|
const int shiftValue = luaL_checkinteger(L, 2);
|
|
|
|
const unsigned shift_type = luaL_optinteger( L, 3, SHIFT_LOGICAL );
|
|
|
|
|
|
|
|
const int pos_start = luaL_optinteger(L, 4, 1);
|
|
|
|
const int pos_end = luaL_optinteger(L, 5, -1);
|
|
|
|
|
|
|
|
|
|
|
|
ws2812_buffer_shift(buffer, shiftValue, shift_type, pos_start, pos_end);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-05 13:53:42 +01:00
|
|
|
static int ws2812_buffer_dump(lua_State* L) {
|
|
|
|
ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
|
|
|
|
|
|
|
|
lua_pushlstring(L, buffer->values, buffer->size * buffer->colorsPerLed);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ws2812_buffer_replace(lua_State* L) {
|
|
|
|
ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
|
|
|
|
size_t l = buffer->size;
|
|
|
|
ptrdiff_t start = posrelat(luaL_optinteger(L, 3, 1), l);
|
|
|
|
|
|
|
|
uint8_t *src;
|
|
|
|
size_t srcLen;
|
|
|
|
|
|
|
|
if (lua_type(L, 2) == LUA_TSTRING) {
|
|
|
|
size_t length;
|
|
|
|
|
|
|
|
src = (uint8_t *) lua_tolstring(L, 2, &length);
|
|
|
|
srcLen = length / buffer->colorsPerLed;
|
|
|
|
} else {
|
|
|
|
ws2812_buffer * rhs = (ws2812_buffer*)luaL_checkudata(L, 2, "ws2812.buffer");
|
|
|
|
src = rhs->values;
|
|
|
|
srcLen = rhs->size;
|
|
|
|
luaL_argcheck(L, rhs->colorsPerLed == buffer->colorsPerLed, 2, "Buffers have different colors");
|
|
|
|
}
|
|
|
|
|
|
|
|
luaL_argcheck(L, srcLen + start - 1 <= buffer->size, 2, "Does not fit into destination");
|
2016-06-10 22:23:37 +02:00
|
|
|
|
2016-11-05 13:53:42 +01:00
|
|
|
c_memcpy(buffer->values + (start - 1) * buffer->colorsPerLed, src, srcLen * buffer->colorsPerLed);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// buffer:mix(factor1, buffer1, ..)
|
|
|
|
// factor is 256 for 100%
|
|
|
|
// uses saturating arithmetic (one buffer at a time)
|
|
|
|
static int ws2812_buffer_mix(lua_State* L) {
|
|
|
|
ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
|
|
|
|
|
|
|
|
int pos = 2;
|
|
|
|
size_t cells = buffer->size * buffer->colorsPerLed;
|
|
|
|
|
|
|
|
int n_sources = (lua_gettop(L) - 1) / 2;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
int factor;
|
|
|
|
const uint8_t *values;
|
|
|
|
} source[n_sources];
|
|
|
|
|
|
|
|
int src;
|
|
|
|
for (src = 0; src < n_sources; src++, pos += 2) {
|
|
|
|
int factor = luaL_checkinteger(L, pos);
|
|
|
|
ws2812_buffer *src_buffer = (ws2812_buffer*) luaL_checkudata(L, pos + 1, "ws2812.buffer");
|
|
|
|
|
|
|
|
luaL_argcheck(L, src_buffer->size == buffer->size && src_buffer->colorsPerLed == buffer->colorsPerLed, pos + 1, "Buffer not same shape");
|
2018-01-23 21:05:04 +01:00
|
|
|
|
2016-11-05 13:53:42 +01:00
|
|
|
source[src].factor = factor;
|
|
|
|
source[src].values = src_buffer->values;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < cells; i++) {
|
2017-10-22 19:20:45 +02:00
|
|
|
int32_t val = 0;
|
2016-11-05 13:53:42 +01:00
|
|
|
for (src = 0; src < n_sources; src++) {
|
2017-10-22 19:20:45 +02:00
|
|
|
val += (int32_t)(source[src].values[i] * source[src].factor);
|
2016-11-05 13:53:42 +01:00
|
|
|
}
|
|
|
|
|
2017-10-22 19:20:45 +02:00
|
|
|
val >>= 8;
|
|
|
|
|
2016-11-05 13:53:42 +01:00
|
|
|
if (val < 0) {
|
|
|
|
val = 0;
|
|
|
|
} else if (val > 255) {
|
|
|
|
val = 255;
|
|
|
|
}
|
2017-10-22 19:20:45 +02:00
|
|
|
buffer->values[i] = (uint8_t)val;
|
2016-11-05 13:53:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the total of all channels
|
|
|
|
static int ws2812_buffer_power(lua_State* L) {
|
|
|
|
ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
|
|
|
|
|
|
|
|
size_t cells = buffer->size * buffer->colorsPerLed;
|
|
|
|
|
|
|
|
size_t i;
|
|
|
|
int total = 0;
|
|
|
|
for (i = 0; i < cells; i++) {
|
|
|
|
total += buffer->values[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pushnumber(L, total);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2016-06-10 22:23:37 +02:00
|
|
|
|
2016-05-09 07:21:50 +02:00
|
|
|
static int ws2812_buffer_get(lua_State* L) {
|
2016-11-05 13:53:42 +01:00
|
|
|
ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
|
2016-05-09 07:21:50 +02:00
|
|
|
const int led = luaL_checkinteger(L, 2) - 1;
|
2015-11-24 22:40:30 +01:00
|
|
|
|
|
|
|
luaL_argcheck(L, led >= 0 && led < buffer->size, 2, "index out of range");
|
|
|
|
|
2016-05-09 07:21:50 +02:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < buffer->colorsPerLed; i++)
|
|
|
|
{
|
|
|
|
lua_pushnumber(L, buffer->values[buffer->colorsPerLed*led+i]);
|
|
|
|
}
|
2015-11-24 22:40:30 +01:00
|
|
|
|
2016-05-09 07:21:50 +02:00
|
|
|
return buffer->colorsPerLed;
|
2015-11-24 22:40:30 +01:00
|
|
|
}
|
|
|
|
|
2016-05-09 07:21:50 +02:00
|
|
|
static int ws2812_buffer_set(lua_State* L) {
|
2016-11-05 13:53:42 +01:00
|
|
|
ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
|
2016-05-09 07:21:50 +02:00
|
|
|
const int led = luaL_checkinteger(L, 2) - 1;
|
2015-11-24 22:40:30 +01:00
|
|
|
|
|
|
|
luaL_argcheck(L, led >= 0 && led < buffer->size, 2, "index out of range");
|
|
|
|
|
|
|
|
int type = lua_type(L, 3);
|
|
|
|
if(type == LUA_TTABLE)
|
|
|
|
{
|
2016-05-09 07:21:50 +02:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < buffer->colorsPerLed; i++)
|
|
|
|
{
|
|
|
|
// Get value and push it on stack
|
|
|
|
lua_rawgeti(L, 3, i+1);
|
2015-11-24 22:40:30 +01:00
|
|
|
|
2016-05-09 07:21:50 +02:00
|
|
|
// Convert it as int and store them in buffer
|
|
|
|
buffer->values[buffer->colorsPerLed*led+i] = lua_tonumber(L, -1);
|
|
|
|
}
|
2015-11-24 22:40:30 +01:00
|
|
|
|
|
|
|
// Clean up the stack
|
2016-05-09 07:21:50 +02:00
|
|
|
lua_pop(L, buffer->colorsPerLed);
|
2015-11-24 22:40:30 +01:00
|
|
|
}
|
|
|
|
else if(type == LUA_TSTRING)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
const char * buf = lua_tolstring(L, 3, &len);
|
|
|
|
|
|
|
|
// Overflow check
|
2016-05-09 07:21:50 +02:00
|
|
|
if( buffer->colorsPerLed*led + len > buffer->colorsPerLed*buffer->size )
|
2015-11-24 22:40:30 +01:00
|
|
|
{
|
|
|
|
return luaL_error(L, "string size will exceed strip length");
|
|
|
|
}
|
|
|
|
|
2016-05-09 07:21:50 +02:00
|
|
|
c_memcpy(&buffer->values[buffer->colorsPerLed*led], buf, len);
|
2015-11-24 22:40:30 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-09 07:21:50 +02:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < buffer->colorsPerLed; i++)
|
|
|
|
{
|
|
|
|
buffer->values[buffer->colorsPerLed*led+i] = luaL_checkinteger(L, 3+i);
|
|
|
|
}
|
2015-11-24 22:40:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-09 07:21:50 +02:00
|
|
|
static int ws2812_buffer_size(lua_State* L) {
|
2016-11-05 13:53:42 +01:00
|
|
|
ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
|
2015-11-24 22:40:30 +01:00
|
|
|
|
|
|
|
lua_pushnumber(L, buffer->size);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-11-05 13:53:42 +01:00
|
|
|
static int ws2812_buffer_sub(lua_State* L) {
|
|
|
|
ws2812_buffer * lhs = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
|
|
|
|
size_t l = lhs->size;
|
|
|
|
ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l);
|
|
|
|
ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l);
|
|
|
|
if (start < 1) start = 1;
|
|
|
|
if (end > (ptrdiff_t)l) end = (ptrdiff_t)l;
|
|
|
|
if (start <= end) {
|
|
|
|
ws2812_buffer *result = allocate_buffer(L, end - start + 1, lhs->colorsPerLed);
|
|
|
|
c_memcpy(result->values, lhs->values + lhs->colorsPerLed * (start - 1), lhs->colorsPerLed * (end - start + 1));
|
|
|
|
} else {
|
|
|
|
ws2812_buffer *result = allocate_buffer(L, 0, lhs->colorsPerLed);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ws2812_buffer_concat(lua_State* L) {
|
|
|
|
ws2812_buffer * lhs = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
|
|
|
|
ws2812_buffer * rhs = (ws2812_buffer*)luaL_checkudata(L, 2, "ws2812.buffer");
|
|
|
|
|
|
|
|
luaL_argcheck(L, lhs->colorsPerLed == rhs->colorsPerLed, 1, "Can only concatenate buffers with same colors");
|
|
|
|
|
|
|
|
int colorsPerLed = lhs->colorsPerLed;
|
|
|
|
int leds = lhs->size + rhs->size;
|
2018-01-23 21:05:04 +01:00
|
|
|
|
2016-11-05 13:53:42 +01:00
|
|
|
ws2812_buffer * buffer = allocate_buffer(L, leds, colorsPerLed);
|
|
|
|
|
|
|
|
c_memcpy(buffer->values, lhs->values, lhs->colorsPerLed * lhs->size);
|
|
|
|
c_memcpy(buffer->values + lhs->colorsPerLed * lhs->size, rhs->values, rhs->colorsPerLed * rhs->size);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ws2812_buffer_tostring(lua_State* L) {
|
|
|
|
ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
|
|
|
|
|
|
|
|
luaL_Buffer result;
|
|
|
|
luaL_buffinit(L, &result);
|
|
|
|
|
|
|
|
luaL_addchar(&result, '[');
|
|
|
|
int i;
|
|
|
|
int p = 0;
|
|
|
|
for (i = 0; i < buffer->size; i++) {
|
|
|
|
int j;
|
|
|
|
if (i > 0) {
|
|
|
|
luaL_addchar(&result, ',');
|
|
|
|
}
|
|
|
|
luaL_addchar(&result, '(');
|
|
|
|
for (j = 0; j < buffer->colorsPerLed; j++, p++) {
|
|
|
|
if (j > 0) {
|
|
|
|
luaL_addchar(&result, ',');
|
|
|
|
}
|
|
|
|
char numbuf[5];
|
|
|
|
c_sprintf(numbuf, "%d", buffer->values[p]);
|
|
|
|
luaL_addstring(&result, numbuf);
|
|
|
|
}
|
|
|
|
luaL_addchar(&result, ')');
|
|
|
|
}
|
|
|
|
|
|
|
|
luaL_addchar(&result, ']');
|
|
|
|
luaL_pushresult(&result);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-01-23 21:05:04 +01:00
|
|
|
|
2015-11-24 22:40:30 +01:00
|
|
|
static const LUA_REG_TYPE ws2812_buffer_map[] =
|
|
|
|
{
|
2016-11-05 13:53:42 +01:00
|
|
|
{ LSTRKEY( "dump" ), LFUNCVAL( ws2812_buffer_dump )},
|
2016-06-10 22:23:37 +02:00
|
|
|
{ LSTRKEY( "fade" ), LFUNCVAL( ws2812_buffer_fade )},
|
2018-01-23 21:05:04 +01:00
|
|
|
{ LSTRKEY( "fill" ), LFUNCVAL( ws2812_buffer_fill_lua )},
|
2016-06-10 22:23:37 +02:00
|
|
|
{ LSTRKEY( "get" ), LFUNCVAL( ws2812_buffer_get )},
|
2016-11-05 13:53:42 +01:00
|
|
|
{ LSTRKEY( "replace" ), LFUNCVAL( ws2812_buffer_replace )},
|
|
|
|
{ LSTRKEY( "mix" ), LFUNCVAL( ws2812_buffer_mix )},
|
|
|
|
{ LSTRKEY( "power" ), LFUNCVAL( ws2812_buffer_power )},
|
2016-06-10 22:23:37 +02:00
|
|
|
{ LSTRKEY( "set" ), LFUNCVAL( ws2812_buffer_set )},
|
2018-01-23 21:05:04 +01:00
|
|
|
{ LSTRKEY( "shift" ), LFUNCVAL( ws2812_buffer_shift_lua )},
|
2016-11-05 13:53:42 +01:00
|
|
|
{ LSTRKEY( "size" ), LFUNCVAL( ws2812_buffer_size )},
|
|
|
|
{ LSTRKEY( "sub" ), LFUNCVAL( ws2812_buffer_sub )},
|
|
|
|
{ LSTRKEY( "__concat" ),LFUNCVAL( ws2812_buffer_concat )},
|
2016-07-20 22:28:47 +02:00
|
|
|
{ LSTRKEY( "__index" ), LROVAL( ws2812_buffer_map )},
|
2016-11-05 13:53:42 +01:00
|
|
|
{ LSTRKEY( "__tostring" ), LFUNCVAL( ws2812_buffer_tostring )},
|
2015-11-24 22:40:30 +01:00
|
|
|
{ LNILKEY, LNILVAL}
|
|
|
|
};
|
|
|
|
|
2018-01-23 21:05:04 +01:00
|
|
|
|
2015-12-16 06:04:58 +01:00
|
|
|
static const LUA_REG_TYPE ws2812_map[] =
|
2015-02-05 18:40:46 +01:00
|
|
|
{
|
2016-07-20 22:28:47 +02:00
|
|
|
{ LSTRKEY( "init" ), LFUNCVAL( ws2812_init )},
|
|
|
|
{ LSTRKEY( "newBuffer" ), LFUNCVAL( ws2812_new_buffer )},
|
|
|
|
{ LSTRKEY( "write" ), LFUNCVAL( ws2812_write )},
|
|
|
|
{ LSTRKEY( "FADE_IN" ), LNUMVAL( FADE_IN ) },
|
|
|
|
{ LSTRKEY( "FADE_OUT" ), LNUMVAL( FADE_OUT ) },
|
|
|
|
{ LSTRKEY( "MODE_SINGLE" ), LNUMVAL( MODE_SINGLE ) },
|
|
|
|
{ LSTRKEY( "MODE_DUAL" ), LNUMVAL( MODE_DUAL ) },
|
|
|
|
{ LSTRKEY( "SHIFT_LOGICAL" ), LNUMVAL( SHIFT_LOGICAL ) },
|
|
|
|
{ LSTRKEY( "SHIFT_CIRCULAR" ), LNUMVAL( SHIFT_CIRCULAR ) },
|
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-11-24 22:40:30 +01:00
|
|
|
luaL_rometatable(L, "ws2812.buffer", (void *)ws2812_buffer_map); // create metatable for ws2812.buffer
|
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);
|