tm1829.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "module.h"
  2. #include "lauxlib.h"
  3. #include "platform.h"
  4. #include "c_stdlib.h"
  5. #include "c_string.h"
  6. #include "user_interface.h"
  7. static inline uint32_t _getCycleCount(void) {
  8. uint32_t cycles;
  9. __asm__ __volatile__("rsr %0,ccount":"=a" (cycles));
  10. return cycles;
  11. }
  12. // This algorithm reads the cpu clock cycles to calculate the correct
  13. // pulse widths. It works in both 80 and 160 MHz mode.
  14. static void ICACHE_RAM_ATTR tm1829_write_to_pin(uint8_t pin, uint8_t *pixels, uint32_t length) {
  15. uint8_t *p, *end;
  16. p = pixels;
  17. end = p + length;
  18. const uint32_t t0l = (1000 * system_get_cpu_freq()) / 3333; // 0.390us (spec=0.35 +- 0.15)
  19. const uint32_t t1l = (1000 * system_get_cpu_freq()) / 1250; // 0.800us (spec=0.70 +- 0.15)
  20. const uint32_t ttot = (1000 * system_get_cpu_freq()) / 800; // 1.25us
  21. while (p != end) {
  22. register int i;
  23. register uint8_t pixel = *p++;
  24. ets_intr_lock();
  25. for (i = 7; i >= 0; i--) {
  26. register uint32_t pin_mask = 1 << pin;
  27. // Select low time
  28. register uint32_t tl = ((pixel >> i) & 1) ? t1l : t0l;
  29. register uint32_t t1, t2;
  30. register uint32_t t = _getCycleCount();
  31. t1 = t + tl;
  32. t2 = t + ttot;
  33. GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pin_mask); // Set pin low
  34. while (_getCycleCount() < t1);
  35. GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pin_mask); // Set pin high
  36. while (_getCycleCount() < t2);
  37. }
  38. ets_intr_unlock();
  39. }
  40. }
  41. // Lua: tm1829.write(pin, "string")
  42. // Byte triples in the string are interpreted as R G B values and sent to the hardware as G R B.
  43. // WARNING: this function scrambles the input buffer :
  44. // a = string.char(255,0,128)
  45. // tm1829.write(3,a)
  46. // =a.byte()
  47. // (0,255,128)
  48. static int ICACHE_FLASH_ATTR tm1829_write(lua_State* L)
  49. {
  50. const uint8_t pin = luaL_checkinteger(L, 1);
  51. size_t length;
  52. const char *rgb = luaL_checklstring(L, 2, &length);
  53. // dont modify lua-internal lstring - make a copy instead
  54. char *buffer = (char *)c_malloc(length);
  55. // Ignore incomplete Byte triples at the end of buffer
  56. length -= length % 3;
  57. // Copy payload and make sure first byte is < 0xFF (triggers
  58. // constant current command, instead of PWM duty command)
  59. size_t i;
  60. for (i = 0; i < length; i += 3) {
  61. buffer[i] = rgb[i];
  62. buffer[i + 1] = rgb[i + 1];
  63. buffer[i + 2] = rgb[i + 2];
  64. // Check for first byte
  65. if (buffer[i] == 0xff)
  66. buffer[i] = 0xfe;
  67. }
  68. // Initialize the output pin and wait a bit
  69. platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT);
  70. platform_gpio_write(pin, 1);
  71. // Send the buffer
  72. tm1829_write_to_pin(pin_num[pin], (uint8_t*) buffer, length);
  73. os_delay_us(500); // reset time
  74. c_free(buffer);
  75. return 0;
  76. }
  77. static const LUA_REG_TYPE tm1829_map[] =
  78. {
  79. { LSTRKEY( "write" ), LFUNCVAL( tm1829_write) },
  80. { LNILKEY, LNILVAL }
  81. };
  82. int luaopen_tm1829(lua_State *L) {
  83. // TODO: Make sure that the GPIO system is initialized
  84. return 0;
  85. }
  86. NODEMCU_MODULE(TM1829, "tm1829", tm1829_map, luaopen_tm1829);