led_strip.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include "esp_err.h"
  19. /**
  20. * @brief LED Strip Type
  21. *
  22. */
  23. typedef struct led_strip_s led_strip_t;
  24. /**
  25. * @brief LED Strip Device Type
  26. *
  27. */
  28. typedef void *led_strip_dev_t;
  29. /**
  30. * @brief Declare of LED Strip Type
  31. *
  32. */
  33. struct led_strip_s {
  34. /**
  35. * @brief Set RGB for a specific pixel
  36. *
  37. * @param strip: LED strip
  38. * @param index: index of pixel to set
  39. * @param red: red part of color
  40. * @param green: green part of color
  41. * @param blue: blue part of color
  42. *
  43. * @return
  44. * - ESP_OK: Set RGB for a specific pixel successfully
  45. * - ESP_ERR_INVALID_ARG: Set RGB for a specific pixel failed because of invalid parameters
  46. * - ESP_FAIL: Set RGB for a specific pixel failed because other error occurred
  47. */
  48. esp_err_t (*set_pixel)(led_strip_t *strip, uint32_t index, uint32_t red, uint32_t green, uint32_t blue);
  49. /**
  50. * @brief Refresh memory colors to LEDs
  51. *
  52. * @param strip: LED strip
  53. * @param timeout_ms: timeout value for refreshing task
  54. *
  55. * @return
  56. * - ESP_OK: Refresh successfully
  57. * - ESP_ERR_TIMEOUT: Refresh failed because of timeout
  58. * - ESP_FAIL: Refresh failed because some other error occurred
  59. *
  60. * @note:
  61. * After updating the LED colors in the memory, a following invocation of this API is needed to flush colors to strip.
  62. */
  63. esp_err_t (*refresh)(led_strip_t *strip, uint32_t timeout_ms);
  64. /**
  65. * @brief Clear LED strip (turn off all LEDs)
  66. *
  67. * @param strip: LED strip
  68. * @param timeout_ms: timeout value for clearing task
  69. *
  70. * @return
  71. * - ESP_OK: Clear LEDs successfully
  72. * - ESP_ERR_TIMEOUT: Clear LEDs failed because of timeout
  73. * - ESP_FAIL: Clear LEDs failed because some other error occurred
  74. */
  75. esp_err_t (*clear)(led_strip_t *strip, uint32_t timeout_ms);
  76. /**
  77. * @brief Free LED strip resources
  78. *
  79. * @param strip: LED strip
  80. *
  81. * @return
  82. * - ESP_OK: Free resources successfully
  83. * - ESP_FAIL: Free resources failed because error occurred
  84. */
  85. esp_err_t (*del)(led_strip_t *strip);
  86. };
  87. /**
  88. * @brief LED Strip Configuration Type
  89. *
  90. */
  91. typedef struct {
  92. uint32_t max_leds; /*!< Maximum LEDs in a single strip */
  93. led_strip_dev_t dev; /*!< LED strip device (e.g. RMT channel, PWM channel, etc) */
  94. } led_strip_config_t;
  95. /**
  96. * @brief Default configuration for LED strip
  97. *
  98. */
  99. #define LED_STRIP_DEFAULT_CONFIG(number, dev_hdl) \
  100. { \
  101. .max_leds = number, \
  102. .dev = dev_hdl, \
  103. }
  104. /**
  105. * @brief Install a new ws2812 driver (based on RMT peripheral)
  106. *
  107. * @param config: LED strip configuration
  108. * @return
  109. * LED strip instance or NULL
  110. */
  111. led_strip_t *led_strip_new_rmt_ws2812(const led_strip_config_t *config);
  112. /**
  113. * @brief Init the RMT peripheral and LED strip configuration.
  114. *
  115. * @param[in] channel: RMT peripheral channel number.
  116. * @param[in] gpio: GPIO number for the RMT data output.
  117. * @param[in] led_num: number of addressable LEDs.
  118. * @return
  119. * LED strip instance or NULL
  120. */
  121. led_strip_t * led_strip_init(uint8_t channel, uint8_t gpio, uint16_t led_num);
  122. /**
  123. * @brief Denit the RMT peripheral.
  124. *
  125. * @param[in] strip: LED strip
  126. * @return
  127. * - ESP_OK
  128. * - ESP_FAIL
  129. */
  130. esp_err_t led_strip_denit(led_strip_t *strip);
  131. #ifdef __cplusplus
  132. }
  133. #endif