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