led_strip.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /* ----------------------------------------------------------------------------
  2. File: led_strip.c
  3. Author(s): Lucas Bruder <LBruder@me.com>
  4. Date Created: 11/23/2016
  5. Last modified: 11/26/2016
  6. Updated: C. Rohs - The update thread now
  7. only runs when signalled. The double buffer code was modified to copy on show
  8. instead of the ping pong buffer that destroyed the buffers contents.
  9. The current code is not thread safe, but is more performant, and the thread
  10. safety does not matter the was it is currently used.
  11. Description: LED Library for driving various led strips on ESP32.
  12. This library uses double buffering to display the LEDs.
  13. ------------------------------------------------------------------------- */
  14. #include "led_strip.h"
  15. #include "freertos/task.h"
  16. #include <string.h>
  17. #define LED_STRIP_TASK_SIZE (1024)
  18. #define LED_STRIP_TASK_PRIORITY (configMAX_PRIORITIES - 1)
  19. #define LED_STRIP_REFRESH_PERIOD_MS (30U) // TODO: add as parameter to led_strip_init
  20. #define LED_STRIP_NUM_RMT_ITEMS_PER_LED (24U) // Assumes 24 bit color for each led
  21. // RMT Clock source is @ 80 MHz. Dividing it by 8 gives us 10 MHz frequency, or 100ns period.
  22. #define LED_STRIP_RMT_CLK_DIV (8)
  23. /****************************
  24. WS2812 Timing
  25. ****************************/
  26. #define LED_STRIP_RMT_TICKS_BIT_1_HIGH_WS2812 9 // 900ns (900ns +/- 150ns per datasheet)
  27. #define LED_STRIP_RMT_TICKS_BIT_1_LOW_WS2812 3 // 300ns (350ns +/- 150ns per datasheet)
  28. #define LED_STRIP_RMT_TICKS_BIT_0_HIGH_WS2812 3 // 300ns (350ns +/- 150ns per datasheet)
  29. #define LED_STRIP_RMT_TICKS_BIT_0_LOW_WS2812 9 // 900ns (900ns +/- 150ns per datasheet)
  30. /****************************
  31. SK6812 Timing
  32. ****************************/
  33. #define LED_STRIP_RMT_TICKS_BIT_1_HIGH_SK6812 6
  34. #define LED_STRIP_RMT_TICKS_BIT_1_LOW_SK6812 6
  35. #define LED_STRIP_RMT_TICKS_BIT_0_HIGH_SK6812 3
  36. #define LED_STRIP_RMT_TICKS_BIT_0_LOW_SK6812 9
  37. /****************************
  38. APA106 Timing
  39. ****************************/
  40. #define LED_STRIP_RMT_TICKS_BIT_1_HIGH_APA106 14 // 1.36us +/- 150ns per datasheet
  41. #define LED_STRIP_RMT_TICKS_BIT_1_LOW_APA106 3 // 350ns +/- 150ns per datasheet
  42. #define LED_STRIP_RMT_TICKS_BIT_0_HIGH_APA106 3 // 350ns +/- 150ns per datasheet
  43. #define LED_STRIP_RMT_TICKS_BIT_0_LOW_APA106 14 // 1.36us +/- 150ns per datasheet
  44. // Function pointer for generating waveforms based on different LED drivers
  45. typedef void (*led_fill_rmt_items_fn)(struct led_color_t *led_strip_buf, rmt_item32_t *rmt_items, uint32_t led_strip_length);
  46. static inline void led_strip_fill_item_level(rmt_item32_t* item, int high_ticks, int low_ticks)
  47. {
  48. item->level0 = 1;
  49. item->duration0 = high_ticks;
  50. item->level1 = 0;
  51. item->duration1 = low_ticks;
  52. }
  53. static inline void led_strip_rmt_bit_1_sk6812(rmt_item32_t* item)
  54. {
  55. led_strip_fill_item_level(item, LED_STRIP_RMT_TICKS_BIT_1_HIGH_SK6812, LED_STRIP_RMT_TICKS_BIT_1_LOW_SK6812);
  56. }
  57. static inline void led_strip_rmt_bit_0_sk6812(rmt_item32_t* item)
  58. {
  59. led_strip_fill_item_level(item, LED_STRIP_RMT_TICKS_BIT_0_HIGH_SK6812, LED_STRIP_RMT_TICKS_BIT_0_LOW_SK6812);
  60. }
  61. static void led_strip_fill_rmt_items_sk6812(struct led_color_t *led_strip_buf, rmt_item32_t *rmt_items, uint32_t led_strip_length)
  62. {
  63. uint32_t rmt_items_index = 0;
  64. for (uint32_t led_index = 0; led_index < led_strip_length; led_index++) {
  65. struct led_color_t led_color = led_strip_buf[led_index];
  66. for (uint8_t bit = 8; bit != 0; bit--) {
  67. uint8_t bit_set = (led_color.green >> (bit - 1)) & 1;
  68. if(bit_set) {
  69. led_strip_rmt_bit_1_sk6812(&(rmt_items[rmt_items_index]));
  70. } else {
  71. led_strip_rmt_bit_0_sk6812(&(rmt_items[rmt_items_index]));
  72. }
  73. rmt_items_index++;
  74. }
  75. for (uint8_t bit = 8; bit != 0; bit--) {
  76. uint8_t bit_set = (led_color.red >> (bit - 1)) & 1;
  77. if(bit_set) {
  78. led_strip_rmt_bit_1_sk6812(&(rmt_items[rmt_items_index]));
  79. } else {
  80. led_strip_rmt_bit_0_sk6812(&(rmt_items[rmt_items_index]));
  81. }
  82. rmt_items_index++;
  83. }
  84. for (uint8_t bit = 8; bit != 0; bit--) {
  85. uint8_t bit_set = (led_color.blue >> (bit - 1)) & 1;
  86. if(bit_set) {
  87. led_strip_rmt_bit_1_sk6812(&(rmt_items[rmt_items_index]));
  88. } else {
  89. led_strip_rmt_bit_0_sk6812(&(rmt_items[rmt_items_index]));
  90. }
  91. rmt_items_index++;
  92. }
  93. }
  94. }
  95. static inline void led_strip_rmt_bit_1_ws2812(rmt_item32_t* item)
  96. {
  97. led_strip_fill_item_level(item, LED_STRIP_RMT_TICKS_BIT_1_HIGH_WS2812, LED_STRIP_RMT_TICKS_BIT_1_LOW_WS2812);
  98. }
  99. static inline void led_strip_rmt_bit_0_ws2812(rmt_item32_t* item)
  100. {
  101. led_strip_fill_item_level(item, LED_STRIP_RMT_TICKS_BIT_0_HIGH_WS2812, LED_STRIP_RMT_TICKS_BIT_0_LOW_WS2812);
  102. }
  103. static void led_strip_fill_rmt_items_ws2812(struct led_color_t *led_strip_buf, rmt_item32_t *rmt_items, uint32_t led_strip_length)
  104. {
  105. uint32_t rmt_items_index = 0;
  106. for (uint32_t led_index = 0; led_index < led_strip_length; led_index++) {
  107. struct led_color_t led_color = led_strip_buf[led_index];
  108. for (uint8_t bit = 8; bit != 0; bit--) {
  109. uint8_t bit_set = (led_color.green >> (bit - 1)) & 1;
  110. if(bit_set) {
  111. led_strip_rmt_bit_1_ws2812(&(rmt_items[rmt_items_index]));
  112. } else {
  113. led_strip_rmt_bit_0_ws2812(&(rmt_items[rmt_items_index]));
  114. }
  115. rmt_items_index++;
  116. }
  117. for (uint8_t bit = 8; bit != 0; bit--) {
  118. uint8_t bit_set = (led_color.red >> (bit - 1)) & 1;
  119. if(bit_set) {
  120. led_strip_rmt_bit_1_ws2812(&(rmt_items[rmt_items_index]));
  121. } else {
  122. led_strip_rmt_bit_0_ws2812(&(rmt_items[rmt_items_index]));
  123. }
  124. rmt_items_index++;
  125. }
  126. for (uint8_t bit = 8; bit != 0; bit--) {
  127. uint8_t bit_set = (led_color.blue >> (bit - 1)) & 1;
  128. if(bit_set) {
  129. led_strip_rmt_bit_1_ws2812(&(rmt_items[rmt_items_index]));
  130. } else {
  131. led_strip_rmt_bit_0_ws2812(&(rmt_items[rmt_items_index]));
  132. }
  133. rmt_items_index++;
  134. }
  135. }
  136. }
  137. static inline void led_strip_rmt_bit_1_apa106(rmt_item32_t* item)
  138. {
  139. led_strip_fill_item_level(item, LED_STRIP_RMT_TICKS_BIT_1_HIGH_APA106, LED_STRIP_RMT_TICKS_BIT_1_LOW_APA106);
  140. }
  141. static inline void led_strip_rmt_bit_0_apa106(rmt_item32_t* item)
  142. {
  143. led_strip_fill_item_level(item, LED_STRIP_RMT_TICKS_BIT_0_HIGH_APA106, LED_STRIP_RMT_TICKS_BIT_0_LOW_APA106);
  144. }
  145. static void led_strip_fill_rmt_items_apa106(struct led_color_t *led_strip_buf, rmt_item32_t *rmt_items, uint32_t led_strip_length)
  146. {
  147. uint32_t rmt_items_index = 0;
  148. for (uint32_t led_index = 0; led_index < led_strip_length; led_index++) {
  149. struct led_color_t led_color = led_strip_buf[led_index];
  150. for (uint8_t bit = 8; bit != 0; bit--) {
  151. uint8_t bit_set = (led_color.red >> (bit - 1)) & 1;
  152. if(bit_set) {
  153. led_strip_rmt_bit_1_apa106(&(rmt_items[rmt_items_index]));
  154. } else {
  155. led_strip_rmt_bit_0_apa106(&(rmt_items[rmt_items_index]));
  156. }
  157. rmt_items_index++;
  158. }
  159. for (uint8_t bit = 8; bit != 0; bit--) {
  160. uint8_t bit_set = (led_color.green >> (bit - 1)) & 1;
  161. if(bit_set) {
  162. led_strip_rmt_bit_1_apa106(&(rmt_items[rmt_items_index]));
  163. } else {
  164. led_strip_rmt_bit_0_apa106(&(rmt_items[rmt_items_index]));
  165. }
  166. rmt_items_index++;
  167. }
  168. for (uint8_t bit = 8; bit != 0; bit--) {
  169. uint8_t bit_set = (led_color.blue >> (bit - 1)) & 1;
  170. if(bit_set) {
  171. led_strip_rmt_bit_1_apa106(&(rmt_items[rmt_items_index]));
  172. } else {
  173. led_strip_rmt_bit_0_apa106(&(rmt_items[rmt_items_index]));
  174. }
  175. rmt_items_index++;
  176. }
  177. }
  178. }
  179. static void led_strip_task(void *arg)
  180. {
  181. struct led_strip_t *led_strip = (struct led_strip_t *)arg;
  182. led_fill_rmt_items_fn led_make_waveform = NULL;
  183. size_t num_items_malloc = (LED_STRIP_NUM_RMT_ITEMS_PER_LED * led_strip->led_strip_length);
  184. rmt_item32_t *rmt_items = (rmt_item32_t*) malloc(sizeof(rmt_item32_t) * num_items_malloc);
  185. if (!rmt_items) {
  186. vTaskDelete(NULL);
  187. }
  188. switch (led_strip->rgb_led_type) {
  189. case RGB_LED_TYPE_WS2812:
  190. led_make_waveform = led_strip_fill_rmt_items_ws2812;
  191. break;
  192. case RGB_LED_TYPE_SK6812:
  193. led_make_waveform = led_strip_fill_rmt_items_sk6812;
  194. break;
  195. case RGB_LED_TYPE_APA106:
  196. led_make_waveform = led_strip_fill_rmt_items_apa106;
  197. break;
  198. default:
  199. // Will avoid keeping it point to NULL
  200. led_make_waveform = led_strip_fill_rmt_items_ws2812;
  201. break;
  202. };
  203. for(;;) {
  204. rmt_wait_tx_done(led_strip->rmt_channel, portMAX_DELAY);
  205. vTaskDelay(LED_STRIP_REFRESH_PERIOD_MS / portTICK_PERIOD_MS);
  206. xSemaphoreTake(led_strip->access_semaphore, portMAX_DELAY);
  207. led_make_waveform(led_strip->led_strip_working,
  208. rmt_items,
  209. led_strip->led_strip_length);
  210. rmt_write_items(led_strip->rmt_channel,
  211. rmt_items,
  212. num_items_malloc,
  213. false);
  214. }
  215. if (rmt_items) {
  216. free(rmt_items);
  217. }
  218. vTaskDelete(NULL);
  219. }
  220. static bool led_strip_init_rmt(struct led_strip_t *led_strip)
  221. {
  222. rmt_config_t rmt_cfg = {
  223. .rmt_mode = RMT_MODE_TX,
  224. .channel = led_strip->rmt_channel,
  225. .clk_div = LED_STRIP_RMT_CLK_DIV,
  226. .gpio_num = led_strip->gpio,
  227. .mem_block_num = 1,
  228. .tx_config = {
  229. .loop_en = false,
  230. .carrier_freq_hz = 100, // Not used, but has to be set to avoid divide by 0 err
  231. .carrier_duty_percent = 50,
  232. .carrier_level = RMT_CARRIER_LEVEL_LOW,
  233. .carrier_en = false,
  234. .idle_level = RMT_IDLE_LEVEL_LOW,
  235. .idle_output_en = true,
  236. }
  237. };
  238. esp_err_t cfg_ok = rmt_config(&rmt_cfg);
  239. if (cfg_ok != ESP_OK) {
  240. return false;
  241. }
  242. esp_err_t install_ok = rmt_driver_install(rmt_cfg.channel, 0, 0);
  243. if (install_ok != ESP_OK) {
  244. return false;
  245. }
  246. return true;
  247. }
  248. bool led_strip_init(struct led_strip_t *led_strip)
  249. {
  250. static EXT_RAM_ATTR TaskHandle_t task_created;
  251. StaticTask_t* xTaskBuffer = (StaticTask_t*) heap_caps_malloc(sizeof(StaticTask_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  252. static EXT_RAM_ATTR StackType_t xStack[LED_STRIP_TASK_SIZE] __attribute__ ((aligned (4)));
  253. if ((led_strip == NULL) ||
  254. (led_strip->rmt_channel >= RMT_CHANNEL_MAX) ||
  255. (led_strip->gpio > GPIO_NUM_33) ||
  256. (led_strip->led_strip_working == NULL) ||
  257. (led_strip->led_strip_showing == NULL) ||
  258. (led_strip->led_strip_length == 0) ||
  259. (led_strip->access_semaphore == NULL)) {
  260. return false;
  261. }
  262. if(led_strip->led_strip_working == led_strip->led_strip_showing) {
  263. return false;
  264. }
  265. memset(led_strip->led_strip_working, 0, sizeof(struct led_color_t) * led_strip->led_strip_length);
  266. memset(led_strip->led_strip_showing, 0, sizeof(struct led_color_t) * led_strip->led_strip_length);
  267. bool init_rmt = led_strip_init_rmt(led_strip);
  268. if (!init_rmt) {
  269. return false;
  270. }
  271. xSemaphoreGive(led_strip->access_semaphore);
  272. task_created = xTaskCreateStatic(led_strip_task,
  273. "led_strip_task",
  274. LED_STRIP_TASK_SIZE,
  275. led_strip,
  276. LED_STRIP_TASK_PRIORITY,
  277. xStack, xTaskBuffer);
  278. if (!task_created) {
  279. return false;
  280. }
  281. return true;
  282. }
  283. bool led_strip_set_pixel_color(struct led_strip_t *led_strip, uint32_t pixel_num, struct led_color_t *color)
  284. {
  285. bool set_led_success = true;
  286. if ((!led_strip) || (!color) || (pixel_num > led_strip->led_strip_length)) {
  287. return false;
  288. }
  289. led_strip->led_strip_working[pixel_num] = *color;
  290. return set_led_success;
  291. }
  292. bool led_strip_set_pixel_rgb(struct led_strip_t *led_strip, uint32_t pixel_num, uint8_t red, uint8_t green, uint8_t blue)
  293. {
  294. bool set_led_success = true;
  295. if ((!led_strip) || (pixel_num > led_strip->led_strip_length)) {
  296. return false;
  297. }
  298. led_strip->led_strip_working[pixel_num].red = red;
  299. led_strip->led_strip_working[pixel_num].green = green;
  300. led_strip->led_strip_working[pixel_num].blue = blue;
  301. return set_led_success;
  302. }
  303. bool led_strip_get_pixel_color(struct led_strip_t *led_strip, uint32_t pixel_num, struct led_color_t *color)
  304. {
  305. bool get_success = true;
  306. if ((!led_strip) ||
  307. (pixel_num > led_strip->led_strip_length) ||
  308. (!color)) {
  309. color = NULL;
  310. return false;
  311. }
  312. *color = led_strip->led_strip_working[pixel_num];
  313. return get_success;
  314. }
  315. /**
  316. * Updates the led buffer to be shown
  317. */
  318. bool led_strip_show(struct led_strip_t *led_strip)
  319. {
  320. bool success = true;
  321. if (!led_strip) {
  322. return false;
  323. }
  324. /* copy the current buffer for display */
  325. memcpy(led_strip->led_strip_showing,led_strip->led_strip_working, sizeof(struct led_color_t) * led_strip->led_strip_length);
  326. xSemaphoreGive(led_strip->access_semaphore);
  327. return success;
  328. }
  329. /**
  330. * Clears the LED strip
  331. */
  332. bool led_strip_clear(struct led_strip_t *led_strip)
  333. {
  334. bool success = true;
  335. if (!led_strip) {
  336. return false;
  337. }
  338. memset(led_strip->led_strip_working,
  339. 0,
  340. sizeof(struct led_color_t) * led_strip->led_strip_length);
  341. return success;
  342. }