led_strip.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* ---------------------------------------------------------------------------
  2. File: led_strip.h
  3. Author(s): Lucas Bruder <LBruder@me.com>
  4. Date Created: 11/23/2016
  5. Last modified: 11/26/2016
  6. Description:
  7. This library can drive led strips through the RMT module on the ESP32.
  8. ------------------------------------------------------------------------ */
  9. #ifndef LED_STRIP_H
  10. #define LED_STRIP_H
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. #include <driver/rmt.h>
  15. #include <driver/gpio.h>
  16. #include "freertos/FreeRTOS.h"
  17. #include <stddef.h>
  18. enum rgb_led_type_t {
  19. RGB_LED_TYPE_WS2812 = 0,
  20. RGB_LED_TYPE_SK6812 = 1,
  21. RGB_LED_TYPE_APA106 = 2,
  22. RGB_LED_TYPE_MAX,
  23. };
  24. /**
  25. * RGB LED colors
  26. */
  27. struct led_color_t {
  28. uint8_t red;
  29. uint8_t green;
  30. uint8_t blue;
  31. };
  32. struct led_strip_t {
  33. const enum rgb_led_type_t rgb_led_type;
  34. uint32_t led_strip_length;
  35. // RMT peripheral settings
  36. rmt_channel_t rmt_channel;
  37. /*
  38. * Interrupt table is located in soc.h
  39. * As of 11/27/16, reccomended interrupts are:
  40. * 9, 12, 13, 17, 18, 19, 20, 21 or 23
  41. * Ensure that the same interrupt number isn't used twice
  42. * across all libraries
  43. */
  44. int rmt_interrupt_num;
  45. gpio_num_t gpio; // Must be less than GPIO_NUM_33
  46. struct led_color_t *led_strip_working;
  47. struct led_color_t *led_strip_showing;
  48. SemaphoreHandle_t access_semaphore;
  49. };
  50. bool led_strip_init(struct led_strip_t *led_strip);
  51. /**
  52. * Sets the pixel at pixel_num to color.
  53. */
  54. bool led_strip_set_pixel_color(struct led_strip_t *led_strip, uint32_t pixel_num, struct led_color_t *color);
  55. 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);
  56. /**
  57. * Get the pixel color at pixel_num for the led strip that is currently being shown!
  58. * NOTE: If you call set_pixel_color then get_pixel_color for the same pixel_num, you will not
  59. * get back the same pixel value. This gets you the color of the pixel currently being shown, not the one
  60. * being updated
  61. *
  62. * If there is an invalid argument, color will point to NULL and this function will return false.
  63. */
  64. bool led_strip_get_pixel_color(struct led_strip_t *led_strip, uint32_t pixel_num, struct led_color_t *color);
  65. /**
  66. * Updates the led buffer to be shown using double buffering.
  67. */
  68. bool led_strip_show(struct led_strip_t *led_strip);
  69. /**
  70. * Clears the LED strip.
  71. */
  72. bool led_strip_clear(struct led_strip_t *led_strip);
  73. #ifdef __cplusplus
  74. }
  75. #endif
  76. #endif // LED_STRIP_H