ws2812.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "module.h"
  2. #include "lauxlib.h"
  3. #include "lmem.h"
  4. #include "platform.h"
  5. #include <stdlib.h>
  6. #include <math.h>
  7. #include <string.h>
  8. #include "user_interface.h"
  9. #include "driver/uart.h"
  10. #include "osapi.h"
  11. #include "pixbuf.h"
  12. #define MODE_SINGLE 0
  13. #define MODE_DUAL 1
  14. // Init UART1 to be able to stream WS2812 data to GPIO2 pin
  15. // If DUAL mode is selected, init UART0 to stream to TXD0 as well
  16. // You HAVE to redirect LUA's output somewhere else
  17. static int ws2812_init(lua_State* L) {
  18. const int mode = luaL_optinteger(L, 1, MODE_SINGLE);
  19. luaL_argcheck(L, mode == MODE_SINGLE || mode == MODE_DUAL, 1, "ws2812.SINGLE or ws2812.DUAL expected");
  20. // Configure UART1
  21. // Set baudrate of UART1 to 3200000
  22. WRITE_PERI_REG(UART_CLKDIV(1), UART_CLK_FREQ / 3200000);
  23. // Set UART Configuration No parity / 6 DataBits / 1 StopBits / Invert TX
  24. WRITE_PERI_REG(UART_CONF0(1), UART_TXD_INV | (1 << UART_STOP_BIT_NUM_S) | (1 << UART_BIT_NUM_S));
  25. if (mode == MODE_DUAL) {
  26. // Configure UART0
  27. // Set baudrate of UART0 to 3200000
  28. WRITE_PERI_REG(UART_CLKDIV(0), UART_CLK_FREQ / 3200000);
  29. // Set UART Configuration No parity / 6 DataBits / 1 StopBits / Invert TX
  30. WRITE_PERI_REG(UART_CONF0(0), UART_TXD_INV | (1 << UART_STOP_BIT_NUM_S) | (1 << UART_BIT_NUM_S));
  31. }
  32. // Pull GPIO2 down
  33. platform_gpio_mode(4, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT);
  34. platform_gpio_write(4, 0);
  35. // Waits 10us to simulate a reset
  36. os_delay_us(10);
  37. // Redirect UART1 to GPIO2
  38. // Disable GPIO2
  39. GPIO_REG_WRITE(GPIO_ENABLE_W1TC_ADDRESS, BIT2);
  40. // Enable Function 2 for GPIO2 (U1TXD)
  41. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);
  42. return 0;
  43. }
  44. // Stream data using UART1 routed to GPIO2
  45. // ws2812.init() should be called first
  46. //
  47. // NODE_DEBUG should not be activated because it also uses UART1
  48. void ICACHE_RAM_ATTR ws2812_write_data(const uint8_t *pixels, uint32_t length, const uint8_t *pixels2, uint32_t length2) {
  49. // Data are sent LSB first, with a start bit at 0, an end bit at 1 and all inverted
  50. // 0b00110111 => 110111 => [0]111011[1] => 10001000 => 00
  51. // 0b00000111 => 000111 => [0]111000[1] => 10001110 => 01
  52. // 0b00110100 => 110100 => [0]001011[1] => 11101000 => 10
  53. // 0b00000100 => 000100 => [0]001000[1] => 11101110 => 11
  54. // Array declared as static const to avoid runtime generation
  55. // But declared in ".data" section to avoid read penalty from FLASH
  56. static const __attribute__((section(".data._uartData"))) uint8_t _uartData[4] = { 0b00110111, 0b00000111, 0b00110100, 0b00000100 };
  57. const uint8_t *end = pixels + length;
  58. const uint8_t *end2 = pixels2 + length2;
  59. do {
  60. // If something to send for first buffer and enough room
  61. // in FIFO buffer (we wants to write 4 bytes, so less than
  62. // 124 in the buffer)
  63. if (pixels < end && (((READ_PERI_REG(UART_STATUS(1)) >> UART_TXFIFO_CNT_S) & UART_TXFIFO_CNT) <= 124)) {
  64. uint8_t value = *pixels++;
  65. // Fill the buffer
  66. WRITE_PERI_REG(UART_FIFO(1), _uartData[(value >> 6) & 3]);
  67. WRITE_PERI_REG(UART_FIFO(1), _uartData[(value >> 4) & 3]);
  68. WRITE_PERI_REG(UART_FIFO(1), _uartData[(value >> 2) & 3]);
  69. WRITE_PERI_REG(UART_FIFO(1), _uartData[(value >> 0) & 3]);
  70. }
  71. // Same for the second buffer
  72. if (pixels2 < end2 && (((READ_PERI_REG(UART_STATUS(0)) >> UART_TXFIFO_CNT_S) & UART_TXFIFO_CNT) <= 124)) {
  73. uint8_t value = *pixels2++;
  74. // Fill the buffer
  75. WRITE_PERI_REG(UART_FIFO(0), _uartData[(value >> 6) & 3]);
  76. WRITE_PERI_REG(UART_FIFO(0), _uartData[(value >> 4) & 3]);
  77. WRITE_PERI_REG(UART_FIFO(0), _uartData[(value >> 2) & 3]);
  78. WRITE_PERI_REG(UART_FIFO(0), _uartData[(value >> 0) & 3]);
  79. }
  80. } while(pixels < end || pixels2 < end2); // Until there is still something to send
  81. }
  82. // Lua: ws2812.write("string")
  83. // Byte triples in the string are interpreted as G R B values.
  84. //
  85. // ws2812.init() should be called first
  86. //
  87. // ws2812.write(string.char(0, 255, 0)) sets the first LED red.
  88. // ws2812.write(string.char(0, 0, 255):rep(10)) sets ten LEDs blue.
  89. // ws2812.write(string.char(255, 0, 0, 255, 255, 255)) first LED green, second LED white.
  90. //
  91. // In DUAL mode 'ws2812.init(ws2812.DUAL)', you may pass a second string as parameter
  92. // It will be sent through TXD0 in parallel
  93. static int ws2812_write(lua_State* L) {
  94. size_t length1, length2;
  95. const char *buffer1, *buffer2;
  96. // First mandatory parameter
  97. int type = lua_type(L, 1);
  98. if (type == LUA_TNIL)
  99. {
  100. buffer1 = 0;
  101. length1 = 0;
  102. }
  103. else if(type == LUA_TSTRING)
  104. {
  105. buffer1 = lua_tolstring(L, 1, &length1);
  106. }
  107. else if (type == LUA_TUSERDATA)
  108. {
  109. pixbuf *buffer = pixbuf_from_lua_arg(L, 1);
  110. luaL_argcheck(L, pixbuf_channels(buffer) == 3, 1, "Bad pixbuf format");
  111. buffer1 = buffer->values;
  112. length1 = pixbuf_size(buffer);
  113. }
  114. else
  115. {
  116. luaL_argerror(L, 1, "pixbuf 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. pixbuf *buffer = pixbuf_from_lua_arg(L, 2);
  132. luaL_argcheck(L, pixbuf_channels(buffer) == 3, 2, "Bad pixbuf format");
  133. buffer2 = buffer->values;
  134. length2 = pixbuf_size(buffer);
  135. }
  136. else
  137. {
  138. luaL_argerror(L, 2, "pixbuf or string expected");
  139. }
  140. // Send the buffers
  141. ws2812_write_data(buffer1, length1, buffer2, length2);
  142. return 0;
  143. }
  144. LROT_BEGIN(ws2812, NULL, 0)
  145. LROT_FUNCENTRY( init, ws2812_init )
  146. LROT_FUNCENTRY( newBuffer, pixbuf_new_lua ) // backwards compatibility
  147. LROT_FUNCENTRY( write, ws2812_write )
  148. LROT_NUMENTRY( FADE_IN, PIXBUF_FADE_IN ) // BC
  149. LROT_NUMENTRY( FADE_OUT, PIXBUF_FADE_OUT ) // BC
  150. LROT_NUMENTRY( MODE_SINGLE, MODE_SINGLE )
  151. LROT_NUMENTRY( MODE_DUAL, MODE_DUAL )
  152. LROT_NUMENTRY( SHIFT_LOGICAL, PIXBUF_SHIFT_LOGICAL ) // BC
  153. LROT_NUMENTRY( SHIFT_CIRCULAR, PIXBUF_SHIFT_CIRCULAR ) // BC
  154. LROT_END(ws2812, NULL, 0)
  155. static int luaopen_ws2812(lua_State *L) {
  156. // TODO: Make sure that the GPIO system is initialized
  157. return 0;
  158. }
  159. NODEMCU_MODULE(WS2812, "ws2812", ws2812, luaopen_ws2812);