apa102.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "lualib.h"
  4. #include "lauxlib.h"
  5. #include "lrotable.h"
  6. #include "module.h"
  7. #include "platform.h"
  8. #include "user_interface.h"
  9. #define NOP asm volatile(" nop \n\t")
  10. static inline void apa102_send_byte(uint32_t data_pin, uint32_t clock_pin, uint8_t byte) {
  11. int i;
  12. for (i = 0; i < 8; i++) {
  13. if (byte & 0x80) {
  14. GPIO_OUTPUT_SET(data_pin, PLATFORM_GPIO_HIGH); // Set pin high
  15. } else {
  16. GPIO_OUTPUT_SET(data_pin, PLATFORM_GPIO_LOW); // Set pin low
  17. }
  18. GPIO_OUTPUT_SET(clock_pin, PLATFORM_GPIO_HIGH); // Set pin high
  19. byte <<= 1;
  20. NOP;
  21. NOP;
  22. GPIO_OUTPUT_SET(clock_pin, PLATFORM_GPIO_LOW); // Set pin low
  23. NOP;
  24. NOP;
  25. }
  26. }
  27. static void apa102_send_buffer(uint32_t data_pin, uint32_t clock_pin, uint32_t *buf, uint32_t nbr_frames) {
  28. int i;
  29. // Send 32-bit Start Frame that's all 0x00
  30. apa102_send_byte(data_pin, clock_pin, 0x00);
  31. apa102_send_byte(data_pin, clock_pin, 0x00);
  32. apa102_send_byte(data_pin, clock_pin, 0x00);
  33. apa102_send_byte(data_pin, clock_pin, 0x00);
  34. // Send 32-bit LED Frames
  35. for (i = 0; i < nbr_frames; i++) {
  36. uint8_t *byte = (uint8_t *) buf++;
  37. // Set the first 3 bits of that byte to 1.
  38. // This makes the lua interface easier to use since you
  39. // don't have to worry about creating invalid LED Frames.
  40. byte[0] |= 0xE0;
  41. apa102_send_byte(data_pin, clock_pin, byte[0]);
  42. apa102_send_byte(data_pin, clock_pin, byte[1]);
  43. apa102_send_byte(data_pin, clock_pin, byte[2]);
  44. apa102_send_byte(data_pin, clock_pin, byte[3]);
  45. }
  46. // Send 32-bit End Frames
  47. uint32_t required_postamble_frames = (nbr_frames + 1) / 2;
  48. for (i = 0; i < required_postamble_frames; i++) {
  49. apa102_send_byte(data_pin, clock_pin, 0xFF);
  50. apa102_send_byte(data_pin, clock_pin, 0xFF);
  51. apa102_send_byte(data_pin, clock_pin, 0xFF);
  52. apa102_send_byte(data_pin, clock_pin, 0xFF);
  53. }
  54. }
  55. // Lua: apa102.write(data_pin, clock_pin, "string")
  56. // Byte quads in the string are interpreted as (brightness, B, G, R) values.
  57. // Only the first 5 bits of the brightness value is actually used (0-31).
  58. // This function does not corrupt your buffer.
  59. //
  60. // apa102.write(1, 3, string.char(31, 0, 255, 0)) uses GPIO5 for DATA and GPIO0 for CLOCK and sets the first LED green, with the brightness 31 (out of 0-32)
  61. // apa102.write(5, 6, string.char(255, 0, 0, 255):rep(10)) uses GPIO14 for DATA and GPIO12 for CLOCK and sets ten LED to red, with the brightness 31 (out of 0-32).
  62. // Brightness values are clamped to 0-31.
  63. static int apa102_write(lua_State* L) {
  64. uint8_t data_pin = luaL_checkinteger(L, 1);
  65. MOD_CHECK_ID(gpio, data_pin);
  66. uint32_t alt_data_pin = pin_num[data_pin];
  67. uint8_t clock_pin = luaL_checkinteger(L, 2);
  68. MOD_CHECK_ID(gpio, clock_pin);
  69. uint32_t alt_clock_pin = pin_num[clock_pin];
  70. size_t buf_len;
  71. const char *buf = luaL_checklstring(L, 3, &buf_len);
  72. uint32_t nbr_frames = buf_len / 4;
  73. if (nbr_frames > 100000) {
  74. return luaL_error(L, "The supplied buffer is too long, and might cause the callback watchdog to bark.");
  75. }
  76. // Initialize the output pins
  77. platform_gpio_mode(data_pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT);
  78. GPIO_OUTPUT_SET(alt_data_pin, PLATFORM_GPIO_HIGH); // Set pin high
  79. platform_gpio_mode(clock_pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT);
  80. GPIO_OUTPUT_SET(alt_clock_pin, PLATFORM_GPIO_LOW); // Set pin low
  81. // Send the buffers
  82. apa102_send_buffer(alt_data_pin, alt_clock_pin, (uint32_t *) buf, (uint32_t) nbr_frames);
  83. return 0;
  84. }
  85. LROT_PUBLIC_BEGIN(apa102)
  86. LROT_FUNCENTRY( write, apa102_write )
  87. LROT_END( apa102, NULL, 0 )
  88. LUALIB_API int luaopen_apa102(lua_State *L) {
  89. LREGISTER(L, "apa102", apa102_map);
  90. return 0;
  91. }
  92. NODEMCU_MODULE(APA102, "apa102", apa102, luaopen_apa102);