led_strip.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/semphr.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. enum rgb_led_type_t rgb_led_type; // should be const, but workaround needed for initialization
  34. uint32_t led_strip_length;
  35. // RMT peripheral settings
  36. rmt_channel_t rmt_channel;
  37. gpio_num_t gpio; // Must be less than GPIO_NUM_33
  38. struct led_color_t *led_strip_working;
  39. struct led_color_t *led_strip_showing;
  40. SemaphoreHandle_t access_semaphore;
  41. };
  42. bool led_strip_init(struct led_strip_t *led_strip);
  43. /**
  44. * Sets the pixel at pixel_num to color.
  45. */
  46. bool led_strip_set_pixel_color(struct led_strip_t *led_strip, uint32_t pixel_num, struct led_color_t *color);
  47. 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);
  48. /**
  49. * Get the pixel color at pixel_num for the led strip that is currently being shown!
  50. * NOTE: If you call set_pixel_color then get_pixel_color for the same pixel_num, you will not
  51. * get back the same pixel value. This gets you the color of the pixel currently being shown, not the one
  52. * being updated
  53. *
  54. * If there is an invalid argument, color will point to NULL and this function will return false.
  55. */
  56. bool led_strip_get_pixel_color(struct led_strip_t *led_strip, uint32_t pixel_num, struct led_color_t *color);
  57. /**
  58. * Updates the led buffer to be shown using double buffering.
  59. */
  60. bool led_strip_show(struct led_strip_t *led_strip);
  61. /**
  62. * Clears the LED strip.
  63. */
  64. bool led_strip_clear(struct led_strip_t *led_strip);
  65. #ifdef __cplusplus
  66. }
  67. #endif
  68. #endif // LED_STRIP_H