ws2812.c 18 KB

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