led_strip_example_main.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <string.h>
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "esp_log.h"
  10. #include "driver/rmt_tx.h"
  11. #include "led_strip_encoder.h"
  12. #define RMT_LED_STRIP_RESOLUTION_HZ 10000000 // 10MHz resolution, 1 tick = 0.1us (led strip needs a high resolution)
  13. #define RMT_LED_STRIP_GPIO_NUM 0
  14. #define EXAMPLE_LED_NUMBERS 24
  15. #define EXAMPLE_CHASE_SPEED_MS 10
  16. static const char *TAG = "example";
  17. static uint8_t led_strip_pixels[EXAMPLE_LED_NUMBERS * 3];
  18. /**
  19. * @brief Simple helper function, converting HSV color space to RGB color space
  20. *
  21. * Wiki: https://en.wikipedia.org/wiki/HSL_and_HSV
  22. *
  23. */
  24. void led_strip_hsv2rgb(uint32_t h, uint32_t s, uint32_t v, uint32_t *r, uint32_t *g, uint32_t *b)
  25. {
  26. h %= 360; // h -> [0,360]
  27. uint32_t rgb_max = v * 2.55f;
  28. uint32_t rgb_min = rgb_max * (100 - s) / 100.0f;
  29. uint32_t i = h / 60;
  30. uint32_t diff = h % 60;
  31. // RGB adjustment amount by hue
  32. uint32_t rgb_adj = (rgb_max - rgb_min) * diff / 60;
  33. switch (i) {
  34. case 0:
  35. *r = rgb_max;
  36. *g = rgb_min + rgb_adj;
  37. *b = rgb_min;
  38. break;
  39. case 1:
  40. *r = rgb_max - rgb_adj;
  41. *g = rgb_max;
  42. *b = rgb_min;
  43. break;
  44. case 2:
  45. *r = rgb_min;
  46. *g = rgb_max;
  47. *b = rgb_min + rgb_adj;
  48. break;
  49. case 3:
  50. *r = rgb_min;
  51. *g = rgb_max - rgb_adj;
  52. *b = rgb_max;
  53. break;
  54. case 4:
  55. *r = rgb_min + rgb_adj;
  56. *g = rgb_min;
  57. *b = rgb_max;
  58. break;
  59. default:
  60. *r = rgb_max;
  61. *g = rgb_min;
  62. *b = rgb_max - rgb_adj;
  63. break;
  64. }
  65. }
  66. void app_main(void)
  67. {
  68. uint32_t red = 0;
  69. uint32_t green = 0;
  70. uint32_t blue = 0;
  71. uint16_t hue = 0;
  72. uint16_t start_rgb = 0;
  73. ESP_LOGI(TAG, "Create RMT TX channel");
  74. rmt_channel_handle_t led_chan = NULL;
  75. rmt_tx_channel_config_t tx_chan_config = {
  76. .clk_src = RMT_CLK_SRC_DEFAULT, // select source clock
  77. .gpio_num = RMT_LED_STRIP_GPIO_NUM,
  78. .mem_block_symbols = 64, // increase the block size can make the LED less flickering
  79. .resolution_hz = RMT_LED_STRIP_RESOLUTION_HZ,
  80. .trans_queue_depth = 4, // set the number of transactions that can be pending in the background
  81. };
  82. ESP_ERROR_CHECK(rmt_new_tx_channel(&tx_chan_config, &led_chan));
  83. ESP_LOGI(TAG, "Install led strip encoder");
  84. rmt_encoder_handle_t led_encoder = NULL;
  85. led_strip_encoder_config_t encoder_config = {
  86. .resolution = RMT_LED_STRIP_RESOLUTION_HZ,
  87. };
  88. ESP_ERROR_CHECK(rmt_new_led_strip_encoder(&encoder_config, &led_encoder));
  89. ESP_LOGI(TAG, "Enable RMT TX channel");
  90. ESP_ERROR_CHECK(rmt_enable(led_chan));
  91. ESP_LOGI(TAG, "Start LED rainbow chase");
  92. rmt_transmit_config_t tx_config = {
  93. .loop_count = 0, // no transfer loop
  94. };
  95. while (1) {
  96. for (int i = 0; i < 3; i++) {
  97. for (int j = i; j < EXAMPLE_LED_NUMBERS; j += 3) {
  98. // Build RGB pixels
  99. hue = j * 360 / EXAMPLE_LED_NUMBERS + start_rgb;
  100. led_strip_hsv2rgb(hue, 100, 100, &red, &green, &blue);
  101. led_strip_pixels[j * 3 + 0] = green;
  102. led_strip_pixels[j * 3 + 1] = blue;
  103. led_strip_pixels[j * 3 + 2] = red;
  104. }
  105. // Flush RGB values to LEDs
  106. ESP_ERROR_CHECK(rmt_transmit(led_chan, led_encoder, led_strip_pixels, sizeof(led_strip_pixels), &tx_config));
  107. ESP_ERROR_CHECK(rmt_tx_wait_all_done(led_chan, portMAX_DELAY));
  108. vTaskDelay(pdMS_TO_TICKS(EXAMPLE_CHASE_SPEED_MS));
  109. memset(led_strip_pixels, 0, sizeof(led_strip_pixels));
  110. ESP_ERROR_CHECK(rmt_transmit(led_chan, led_encoder, led_strip_pixels, sizeof(led_strip_pixels), &tx_config));
  111. ESP_ERROR_CHECK(rmt_tx_wait_all_done(led_chan, portMAX_DELAY));
  112. vTaskDelay(pdMS_TO_TICKS(EXAMPLE_CHASE_SPEED_MS));
  113. }
  114. start_rgb += 60;
  115. }
  116. }