ws2812.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. #include "module.h"
  2. #include "lauxlib.h"
  3. #include "lmem.h"
  4. #include "platform.h"
  5. #include "c_stdlib.h"
  6. #include "c_string.h"
  7. #include "user_interface.h"
  8. #include "driver/uart.h"
  9. #include "osapi.h"
  10. #define CANARY_VALUE 0x32383132
  11. #define MODE_SINGLE 0
  12. #define MODE_DUAL 1
  13. #define FADE_IN 1
  14. #define FADE_OUT 0
  15. #define SHIFT_LOGICAL 0
  16. #define SHIFT_CIRCULAR 1
  17. typedef struct {
  18. int size;
  19. uint8_t colorsPerLed;
  20. uint8_t values[0];
  21. } ws2812_buffer;
  22. // Init UART1 to be able to stream WS2812 data to GPIO2 pin
  23. // If DUAL mode is selected, init UART0 to stream to TXD0 as well
  24. // You HAVE to redirect LUA's output somewhere else
  25. static void ws2812_init(lua_State* L) {
  26. const int mode = luaL_optinteger(L, 1, MODE_SINGLE);
  27. luaL_argcheck(L, mode == MODE_SINGLE || mode == MODE_DUAL, 1, "ws2812.SINGLE or ws2812.DUAL expected");
  28. // Configure UART1
  29. // Set baudrate of UART1 to 3200000
  30. WRITE_PERI_REG(UART_CLKDIV(1), UART_CLK_FREQ / 3200000);
  31. // Set UART Configuration No parity / 6 DataBits / 1 StopBits / Invert TX
  32. WRITE_PERI_REG(UART_CONF0(1), UART_TXD_INV | (1 << UART_STOP_BIT_NUM_S) | (1 << UART_BIT_NUM_S));
  33. if (mode == MODE_DUAL) {
  34. // Configure UART0
  35. // Set baudrate of UART0 to 3200000
  36. WRITE_PERI_REG(UART_CLKDIV(0), UART_CLK_FREQ / 3200000);
  37. // Set UART Configuration No parity / 6 DataBits / 1 StopBits / Invert TX
  38. WRITE_PERI_REG(UART_CONF0(0), UART_TXD_INV | (1 << UART_STOP_BIT_NUM_S) | (1 << UART_BIT_NUM_S));
  39. }
  40. // Pull GPIO2 down
  41. platform_gpio_mode(4, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT);
  42. platform_gpio_write(4, 0);
  43. // Waits 10us to simulate a reset
  44. os_delay_us(10);
  45. // Redirect UART1 to GPIO2
  46. // Disable GPIO2
  47. GPIO_REG_WRITE(GPIO_ENABLE_W1TC_ADDRESS, BIT2);
  48. // Enable Function 2 for GPIO2 (U1TXD)
  49. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);
  50. }
  51. // Stream data using UART1 routed to GPIO2
  52. // ws2812.init() should be called first
  53. //
  54. // NODE_DEBUG should not be activated because it also uses UART1
  55. static void ICACHE_RAM_ATTR ws2812_write_data(const uint8_t *pixels, uint32_t length, const uint8_t *pixels2, uint32_t length2) {
  56. // Data are sent LSB first, with a start bit at 0, an end bit at 1 and all inverted
  57. // 0b00110111 => 110111 => [0]111011[1] => 10001000 => 00
  58. // 0b00000111 => 000111 => [0]111000[1] => 10001110 => 01
  59. // 0b00110100 => 110100 => [0]001011[1] => 11101000 => 10
  60. // 0b00000100 => 000100 => [0]001000[1] => 11101110 => 11
  61. // Array declared as static const to avoid runtime generation
  62. // But declared in ".data" section to avoid read penalty from FLASH
  63. static const __attribute__((section(".data._uartData"))) uint8_t _uartData[4] = { 0b00110111, 0b00000111, 0b00110100, 0b00000100 };
  64. const uint8_t *end = pixels + length;
  65. const uint8_t *end2 = pixels2 + length2;
  66. do {
  67. // If something to send for first buffer and enough room
  68. // in FIFO buffer (we wants to write 4 bytes, so less than
  69. // 124 in the buffer)
  70. if (pixels < end && (((READ_PERI_REG(UART_STATUS(1)) >> UART_TXFIFO_CNT_S) & UART_TXFIFO_CNT) <= 124)) {
  71. uint8_t value = *pixels++;
  72. // Fill the buffer
  73. WRITE_PERI_REG(UART_FIFO(1), _uartData[(value >> 6) & 3]);
  74. WRITE_PERI_REG(UART_FIFO(1), _uartData[(value >> 4) & 3]);
  75. WRITE_PERI_REG(UART_FIFO(1), _uartData[(value >> 2) & 3]);
  76. WRITE_PERI_REG(UART_FIFO(1), _uartData[(value >> 0) & 3]);
  77. }
  78. // Same for the second buffer
  79. if (pixels2 < end2 && (((READ_PERI_REG(UART_STATUS(0)) >> UART_TXFIFO_CNT_S) & UART_TXFIFO_CNT) <= 124)) {
  80. uint8_t value = *pixels2++;
  81. // Fill the buffer
  82. WRITE_PERI_REG(UART_FIFO(0), _uartData[(value >> 6) & 3]);
  83. WRITE_PERI_REG(UART_FIFO(0), _uartData[(value >> 4) & 3]);
  84. WRITE_PERI_REG(UART_FIFO(0), _uartData[(value >> 2) & 3]);
  85. WRITE_PERI_REG(UART_FIFO(0), _uartData[(value >> 0) & 3]);
  86. }
  87. } while(pixels < end || pixels2 < end2); // Until there is still something to send
  88. }
  89. // Lua: ws2812.write("string")
  90. // Byte triples in the string are interpreted as G R B values.
  91. //
  92. // ws2812.init() should be called first
  93. //
  94. // ws2812.write(string.char(0, 255, 0)) sets the first LED red.
  95. // ws2812.write(string.char(0, 0, 255):rep(10)) sets ten LEDs blue.
  96. // ws2812.write(string.char(255, 0, 0, 255, 255, 255)) first LED green, second LED white.
  97. //
  98. // In DUAL mode 'ws2812.init(ws2812.DUAL)', you may pass a second string as parameter
  99. // It will be sent through TXD0 in parallel
  100. static int ws2812_write(lua_State* L) {
  101. size_t length1, length2;
  102. const char *buffer1, *buffer2;
  103. // First mandatory parameter
  104. int type = lua_type(L, 1);
  105. if (type == LUA_TNIL)
  106. {
  107. buffer1 = 0;
  108. length1 = 0;
  109. }
  110. else if(type == LUA_TSTRING)
  111. {
  112. buffer1 = lua_tolstring(L, 1, &length1);
  113. }
  114. else if (type == LUA_TUSERDATA)
  115. {
  116. ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
  117. buffer1 = buffer->values;
  118. length1 = buffer->colorsPerLed*buffer->size;
  119. }
  120. else
  121. {
  122. luaL_argerror(L, 1, "ws2812.buffer or string expected");
  123. }
  124. // Second optionnal parameter
  125. type = lua_type(L, 2);
  126. if (type == LUA_TNONE || type == LUA_TNIL)
  127. {
  128. buffer2 = 0;
  129. length2 = 0;
  130. }
  131. else if (type == LUA_TSTRING)
  132. {
  133. buffer2 = lua_tolstring(L, 2, &length2);
  134. }
  135. else if (type == LUA_TUSERDATA)
  136. {
  137. ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 2, "ws2812.buffer");
  138. buffer2 = buffer->values;
  139. length2 = buffer->colorsPerLed*buffer->size;
  140. }
  141. else
  142. {
  143. luaL_argerror(L, 2, "ws2812.buffer or string expected");
  144. }
  145. // Send the buffers
  146. ws2812_write_data(buffer1, length1, buffer2, length2);
  147. return 0;
  148. }
  149. static ptrdiff_t posrelat (ptrdiff_t pos, size_t len) {
  150. /* relative string position: negative means back from end */
  151. if (pos < 0) pos += (ptrdiff_t)len + 1;
  152. return (pos >= 0) ? pos : 0;
  153. }
  154. static ws2812_buffer *allocate_buffer(lua_State *L, int leds, int colorsPerLed) {
  155. // Allocate memory
  156. size_t size = sizeof(ws2812_buffer) + colorsPerLed*leds*sizeof(uint8_t);
  157. ws2812_buffer * buffer = (ws2812_buffer*)lua_newuserdata(L, size);
  158. // Associate its metatable
  159. luaL_getmetatable(L, "ws2812.buffer");
  160. lua_setmetatable(L, -2);
  161. // Save led strip size
  162. buffer->size = leds;
  163. buffer->colorsPerLed = colorsPerLed;
  164. return buffer;
  165. }
  166. // Handle a buffer where we can store led values
  167. static int ws2812_new_buffer(lua_State *L) {
  168. const int leds = luaL_checkint(L, 1);
  169. const int colorsPerLed = luaL_checkint(L, 2);
  170. luaL_argcheck(L, leds > 0, 1, "should be a positive integer");
  171. luaL_argcheck(L, colorsPerLed > 0, 2, "should be a positive integer");
  172. ws2812_buffer * buffer = allocate_buffer(L, leds, colorsPerLed);
  173. c_memset(buffer->values, 0, colorsPerLed * leds);
  174. return 1;
  175. }
  176. static int ws2812_buffer_fill(lua_State* L) {
  177. ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
  178. // Grab colors
  179. int i, j;
  180. int * colors = luaM_malloc(L, buffer->colorsPerLed * sizeof(int));
  181. for (i = 0; i < buffer->colorsPerLed; i++)
  182. {
  183. colors[i] = luaL_checkinteger(L, 2+i);
  184. }
  185. // Fill buffer
  186. uint8_t * p = &buffer->values[0];
  187. for(i = 0; i < buffer->size; i++)
  188. {
  189. for (j = 0; j < buffer->colorsPerLed; j++)
  190. {
  191. *p++ = colors[j];
  192. }
  193. }
  194. // Free memory
  195. luaM_free(L, colors);
  196. return 0;
  197. }
  198. static int ws2812_buffer_fade(lua_State* L) {
  199. ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
  200. const int fade = luaL_checkinteger(L, 2);
  201. unsigned direction = luaL_optinteger( L, 3, FADE_OUT );
  202. luaL_argcheck(L, fade > 0, 2, "fade value should be a strict positive number");
  203. uint8_t * p = &buffer->values[0];
  204. int val = 0;
  205. int i;
  206. for(i = 0; i < buffer->size * buffer->colorsPerLed; i++)
  207. {
  208. if (direction == FADE_OUT)
  209. {
  210. *p++ /= fade;
  211. }
  212. else
  213. {
  214. // as fade in can result in value overflow, an int is used to perform the check afterwards
  215. val = *p * fade;
  216. if (val > 255) val = 255;
  217. *p++ = val;
  218. }
  219. }
  220. return 0;
  221. }
  222. static int ws2812_buffer_shift(lua_State* L) {
  223. ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
  224. const int shiftValue = luaL_checkinteger(L, 2);
  225. const unsigned shift_type = luaL_optinteger( L, 3, SHIFT_LOGICAL );
  226. ptrdiff_t start = posrelat(luaL_optinteger(L, 4, 1), buffer->size);
  227. ptrdiff_t end = posrelat(luaL_optinteger(L, 5, -1), buffer->size);
  228. if (start < 1) start = 1;
  229. if (end > (ptrdiff_t)buffer->size) end = (ptrdiff_t)buffer->size;
  230. start--;
  231. int size = end - start;
  232. size_t offset = start * buffer->colorsPerLed;
  233. luaL_argcheck(L, shiftValue > 0-size && shiftValue < size, 2, "shifting more elements than buffer size");
  234. int shift = shiftValue >= 0 ? shiftValue : -shiftValue;
  235. // check if we want to shift at all
  236. if (shift == 0 || size <= 0)
  237. {
  238. return 0;
  239. }
  240. uint8_t * tmp_pixels = luaM_malloc(L, buffer->colorsPerLed * sizeof(uint8_t) * shift);
  241. int i,j;
  242. size_t shift_len, remaining_len;
  243. // calculate length of shift section and remaining section
  244. shift_len = shift*buffer->colorsPerLed;
  245. remaining_len = (size-shift)*buffer->colorsPerLed;
  246. if (shiftValue > 0)
  247. {
  248. // Store the values which are moved out of the array (last n pixels)
  249. c_memcpy(tmp_pixels, &buffer->values[offset + (size-shift)*buffer->colorsPerLed], shift_len);
  250. // Move pixels to end
  251. os_memmove(&buffer->values[offset + shift*buffer->colorsPerLed], &buffer->values[offset], remaining_len);
  252. // Fill beginning with temp data
  253. if (shift_type == SHIFT_LOGICAL)
  254. {
  255. c_memset(&buffer->values[offset], 0, shift_len);
  256. }
  257. else
  258. {
  259. c_memcpy(&buffer->values[offset], tmp_pixels, shift_len);
  260. }
  261. }
  262. else
  263. {
  264. // Store the values which are moved out of the array (last n pixels)
  265. c_memcpy(tmp_pixels, &buffer->values[offset], shift_len);
  266. // Move pixels to end
  267. os_memmove(&buffer->values[offset], &buffer->values[offset + shift*buffer->colorsPerLed], remaining_len);
  268. // Fill beginning with temp data
  269. if (shift_type == SHIFT_LOGICAL)
  270. {
  271. c_memset(&buffer->values[offset + (size-shift)*buffer->colorsPerLed], 0, shift_len);
  272. }
  273. else
  274. {
  275. c_memcpy(&buffer->values[offset + (size-shift)*buffer->colorsPerLed], tmp_pixels, shift_len);
  276. }
  277. }
  278. // Free memory
  279. luaM_free(L, tmp_pixels);
  280. return 0;
  281. }
  282. static int ws2812_buffer_dump(lua_State* L) {
  283. ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
  284. lua_pushlstring(L, buffer->values, buffer->size * buffer->colorsPerLed);
  285. return 1;
  286. }
  287. static int ws2812_buffer_replace(lua_State* L) {
  288. ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
  289. size_t l = buffer->size;
  290. ptrdiff_t start = posrelat(luaL_optinteger(L, 3, 1), l);
  291. uint8_t *src;
  292. size_t srcLen;
  293. if (lua_type(L, 2) == LUA_TSTRING) {
  294. size_t length;
  295. src = (uint8_t *) lua_tolstring(L, 2, &length);
  296. srcLen = length / buffer->colorsPerLed;
  297. } else {
  298. ws2812_buffer * rhs = (ws2812_buffer*)luaL_checkudata(L, 2, "ws2812.buffer");
  299. src = rhs->values;
  300. srcLen = rhs->size;
  301. luaL_argcheck(L, rhs->colorsPerLed == buffer->colorsPerLed, 2, "Buffers have different colors");
  302. }
  303. luaL_argcheck(L, srcLen + start - 1 <= buffer->size, 2, "Does not fit into destination");
  304. c_memcpy(buffer->values + (start - 1) * buffer->colorsPerLed, src, srcLen * buffer->colorsPerLed);
  305. return 0;
  306. }
  307. // buffer:mix(factor1, buffer1, ..)
  308. // factor is 256 for 100%
  309. // uses saturating arithmetic (one buffer at a time)
  310. static int ws2812_buffer_mix(lua_State* L) {
  311. ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
  312. int pos = 2;
  313. size_t cells = buffer->size * buffer->colorsPerLed;
  314. int n_sources = (lua_gettop(L) - 1) / 2;
  315. struct {
  316. int factor;
  317. const uint8_t *values;
  318. } source[n_sources];
  319. int src;
  320. for (src = 0; src < n_sources; src++, pos += 2) {
  321. int factor = luaL_checkinteger(L, pos);
  322. ws2812_buffer *src_buffer = (ws2812_buffer*) luaL_checkudata(L, pos + 1, "ws2812.buffer");
  323. luaL_argcheck(L, src_buffer->size == buffer->size && src_buffer->colorsPerLed == buffer->colorsPerLed, pos + 1, "Buffer not same shape");
  324. source[src].factor = factor;
  325. source[src].values = src_buffer->values;
  326. }
  327. size_t i;
  328. for (i = 0; i < cells; i++) {
  329. int val = 0;
  330. for (src = 0; src < n_sources; src++) {
  331. val += ((int)(source[src].values[i] * source[src].factor) >> 8);
  332. }
  333. if (val < 0) {
  334. val = 0;
  335. } else if (val > 255) {
  336. val = 255;
  337. }
  338. buffer->values[i] = val;
  339. }
  340. return 0;
  341. }
  342. // Returns the total of all channels
  343. static int ws2812_buffer_power(lua_State* L) {
  344. ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
  345. size_t cells = buffer->size * buffer->colorsPerLed;
  346. size_t i;
  347. int total = 0;
  348. for (i = 0; i < cells; i++) {
  349. total += buffer->values[i];
  350. }
  351. lua_pushnumber(L, total);
  352. return 1;
  353. }
  354. static int ws2812_buffer_get(lua_State* L) {
  355. ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
  356. const int led = luaL_checkinteger(L, 2) - 1;
  357. luaL_argcheck(L, led >= 0 && led < buffer->size, 2, "index out of range");
  358. int i;
  359. for (i = 0; i < buffer->colorsPerLed; i++)
  360. {
  361. lua_pushnumber(L, buffer->values[buffer->colorsPerLed*led+i]);
  362. }
  363. return buffer->colorsPerLed;
  364. }
  365. static int ws2812_buffer_set(lua_State* L) {
  366. ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
  367. const int led = luaL_checkinteger(L, 2) - 1;
  368. luaL_argcheck(L, led >= 0 && led < buffer->size, 2, "index out of range");
  369. int type = lua_type(L, 3);
  370. if(type == LUA_TTABLE)
  371. {
  372. int i;
  373. for (i = 0; i < buffer->colorsPerLed; i++)
  374. {
  375. // Get value and push it on stack
  376. lua_rawgeti(L, 3, i+1);
  377. // Convert it as int and store them in buffer
  378. buffer->values[buffer->colorsPerLed*led+i] = lua_tonumber(L, -1);
  379. }
  380. // Clean up the stack
  381. lua_pop(L, buffer->colorsPerLed);
  382. }
  383. else if(type == LUA_TSTRING)
  384. {
  385. size_t len;
  386. const char * buf = lua_tolstring(L, 3, &len);
  387. // Overflow check
  388. if( buffer->colorsPerLed*led + len > buffer->colorsPerLed*buffer->size )
  389. {
  390. return luaL_error(L, "string size will exceed strip length");
  391. }
  392. c_memcpy(&buffer->values[buffer->colorsPerLed*led], buf, len);
  393. }
  394. else
  395. {
  396. int i;
  397. for (i = 0; i < buffer->colorsPerLed; i++)
  398. {
  399. buffer->values[buffer->colorsPerLed*led+i] = luaL_checkinteger(L, 3+i);
  400. }
  401. }
  402. return 0;
  403. }
  404. static int ws2812_buffer_size(lua_State* L) {
  405. ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
  406. lua_pushnumber(L, buffer->size);
  407. return 1;
  408. }
  409. static int ws2812_buffer_sub(lua_State* L) {
  410. ws2812_buffer * lhs = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
  411. size_t l = lhs->size;
  412. ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l);
  413. ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l);
  414. if (start < 1) start = 1;
  415. if (end > (ptrdiff_t)l) end = (ptrdiff_t)l;
  416. if (start <= end) {
  417. ws2812_buffer *result = allocate_buffer(L, end - start + 1, lhs->colorsPerLed);
  418. c_memcpy(result->values, lhs->values + lhs->colorsPerLed * (start - 1), lhs->colorsPerLed * (end - start + 1));
  419. } else {
  420. ws2812_buffer *result = allocate_buffer(L, 0, lhs->colorsPerLed);
  421. }
  422. return 1;
  423. }
  424. static int ws2812_buffer_concat(lua_State* L) {
  425. ws2812_buffer * lhs = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
  426. ws2812_buffer * rhs = (ws2812_buffer*)luaL_checkudata(L, 2, "ws2812.buffer");
  427. luaL_argcheck(L, lhs->colorsPerLed == rhs->colorsPerLed, 1, "Can only concatenate buffers with same colors");
  428. int colorsPerLed = lhs->colorsPerLed;
  429. int leds = lhs->size + rhs->size;
  430. ws2812_buffer * buffer = allocate_buffer(L, leds, colorsPerLed);
  431. c_memcpy(buffer->values, lhs->values, lhs->colorsPerLed * lhs->size);
  432. c_memcpy(buffer->values + lhs->colorsPerLed * lhs->size, rhs->values, rhs->colorsPerLed * rhs->size);
  433. return 1;
  434. }
  435. static int ws2812_buffer_tostring(lua_State* L) {
  436. ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
  437. luaL_Buffer result;
  438. luaL_buffinit(L, &result);
  439. luaL_addchar(&result, '[');
  440. int i;
  441. int p = 0;
  442. for (i = 0; i < buffer->size; i++) {
  443. int j;
  444. if (i > 0) {
  445. luaL_addchar(&result, ',');
  446. }
  447. luaL_addchar(&result, '(');
  448. for (j = 0; j < buffer->colorsPerLed; j++, p++) {
  449. if (j > 0) {
  450. luaL_addchar(&result, ',');
  451. }
  452. char numbuf[5];
  453. c_sprintf(numbuf, "%d", buffer->values[p]);
  454. luaL_addstring(&result, numbuf);
  455. }
  456. luaL_addchar(&result, ')');
  457. }
  458. luaL_addchar(&result, ']');
  459. luaL_pushresult(&result);
  460. return 1;
  461. }
  462. static const LUA_REG_TYPE ws2812_buffer_map[] =
  463. {
  464. { LSTRKEY( "dump" ), LFUNCVAL( ws2812_buffer_dump )},
  465. { LSTRKEY( "fade" ), LFUNCVAL( ws2812_buffer_fade )},
  466. { LSTRKEY( "fill" ), LFUNCVAL( ws2812_buffer_fill )},
  467. { LSTRKEY( "get" ), LFUNCVAL( ws2812_buffer_get )},
  468. { LSTRKEY( "replace" ), LFUNCVAL( ws2812_buffer_replace )},
  469. { LSTRKEY( "mix" ), LFUNCVAL( ws2812_buffer_mix )},
  470. { LSTRKEY( "power" ), LFUNCVAL( ws2812_buffer_power )},
  471. { LSTRKEY( "set" ), LFUNCVAL( ws2812_buffer_set )},
  472. { LSTRKEY( "shift" ), LFUNCVAL( ws2812_buffer_shift )},
  473. { LSTRKEY( "size" ), LFUNCVAL( ws2812_buffer_size )},
  474. { LSTRKEY( "sub" ), LFUNCVAL( ws2812_buffer_sub )},
  475. { LSTRKEY( "__concat" ),LFUNCVAL( ws2812_buffer_concat )},
  476. { LSTRKEY( "__index" ), LROVAL( ws2812_buffer_map )},
  477. { LSTRKEY( "__tostring" ), LFUNCVAL( ws2812_buffer_tostring )},
  478. { LNILKEY, LNILVAL}
  479. };
  480. static const LUA_REG_TYPE ws2812_map[] =
  481. {
  482. { LSTRKEY( "init" ), LFUNCVAL( ws2812_init )},
  483. { LSTRKEY( "newBuffer" ), LFUNCVAL( ws2812_new_buffer )},
  484. { LSTRKEY( "write" ), LFUNCVAL( ws2812_write )},
  485. { LSTRKEY( "FADE_IN" ), LNUMVAL( FADE_IN ) },
  486. { LSTRKEY( "FADE_OUT" ), LNUMVAL( FADE_OUT ) },
  487. { LSTRKEY( "MODE_SINGLE" ), LNUMVAL( MODE_SINGLE ) },
  488. { LSTRKEY( "MODE_DUAL" ), LNUMVAL( MODE_DUAL ) },
  489. { LSTRKEY( "SHIFT_LOGICAL" ), LNUMVAL( SHIFT_LOGICAL ) },
  490. { LSTRKEY( "SHIFT_CIRCULAR" ), LNUMVAL( SHIFT_CIRCULAR ) },
  491. { LNILKEY, LNILVAL}
  492. };
  493. int luaopen_ws2812(lua_State *L) {
  494. // TODO: Make sure that the GPIO system is initialized
  495. luaL_rometatable(L, "ws2812.buffer", (void *)ws2812_buffer_map); // create metatable for ws2812.buffer
  496. return 0;
  497. }
  498. NODEMCU_MODULE(WS2812, "ws2812", ws2812_map, luaopen_ws2812);