LED Controller API

API Component: LED Controller API_COMP_LED_CONTROLLER
status: implemented
header_file: main/components/led_controller/led_controller.h

Brief Description

Hardware abstraction layer for WS2812 addressable LED strips using ESP32 RMT peripheral. Provides low-level control with RAM buffer management for efficient updates.

Key features:

  • Individual pixel control (set/get/clear)

  • RAM buffer maintains current LED state

  • Manual update trigger for performance optimization

  • Configurable LED count and GPIO pin

  • Color utility functions

Architecture:

Uses ESP32 RMT peripheral for precise WS2812 timing. Maintains a RAM buffer for current LED state. Only one controller instance supported. Configuration is loaded from NVS via config_manager.

Data Types

API Data Structure: led_color_t API_STRUCT_LED_COLOR
status: implemented
component: API_COMP_LED_CONTROLLER
links outgoing: REQ_LED_2, REQ_LED_4

RGB color structure for LED pixel color.

Definition:

typedef struct {
    uint8_t red;
    uint8_t green;
    uint8_t blue;
} led_color_t;

Fields:

  • red - Red component (0-255)

  • green - Green component (0-255)

  • blue - Blue component (0-255)

Lifecycle Functions

API Function: led_controller_init API_FUNC_LED_CONTROLLER_INIT
status: implemented
api_signature: esp_err_t led_controller_init(gpio_num_t data_pin)
returns: ESP_OK on success, ESP_ERR_INVALID_STATE, ESP_ERR_INVALID_ARG, ESP_ERR_*
parameters: data_pin (GPIO pin for WS2812 data line)
component: API_COMP_LED_CONTROLLER

Initialize the LED controller. Loads configuration from config_manager and allocates RAM buffer. Only one instance supported. Uses RMT channel 0.

Signature:

esp_err_t led_controller_init(gpio_num_t data_pin);

Parameters:

  • data_pin - GPIO pin for WS2812 data line

Returns:

  • ESP_OK - Initialization successful

  • ESP_ERR_INVALID_STATE - Config manager not initialized

  • ESP_ERR_INVALID_ARG - Invalid pin

  • ESP_ERR_* - Configuration or hardware error

Note

Allocates memory for LED buffer (3 bytes per LED). Only one instance supported. RMT channel 0 is used.

API Function: led_controller_deinit API_FUNC_LED_CONTROLLER_DEINIT
status: implemented
api_signature: esp_err_t led_controller_deinit(void)
returns: ESP_OK on success, ESP_ERR_*
parameters: None
component: API_COMP_LED_CONTROLLER
links outgoing: REQ_LED_1

Deinitialize the LED controller. Frees memory, deinitializes RMT, and resets GPIO.

Signature:

esp_err_t led_controller_deinit(void);

Parameters:

  • None

Returns:

  • ESP_OK - Deinitialization successful

  • ESP_ERR_* - Failure during deinit

Note

Frees all allocated resources and resets hardware state.

Pixel Control Functions

API Function: led_set_pixel API_FUNC_LED_SET_PIXEL
status: implemented
api_signature: esp_err_t led_set_pixel(uint16_t index, led_color_t color)
returns: ESP_OK on success, ESP_ERR_INVALID_ARG
parameters: index (LED index), color (RGB color)
component: API_COMP_LED_CONTROLLER
links outgoing: REQ_LED_2, REQ_LED_4

Set color of a specific LED pixel in RAM buffer. Call led_show() to update physical LEDs.

Signature:

esp_err_t led_set_pixel(uint16_t index, led_color_t color);

Parameters:

  • index - LED index (0 to led_count-1)

  • color - RGB color to set

Returns:

  • ESP_OK - Pixel color updated in buffer

  • ESP_ERR_INVALID_ARG - Index out of range

Note

Does not update physical LEDs until led_show() is called.

API Function: led_clear_pixel API_FUNC_LED_CLEAR_PIXEL
status: implemented
api_signature: esp_err_t led_clear_pixel(uint16_t index)
returns: ESP_OK on success, ESP_ERR_INVALID_ARG
parameters: index (LED index)
component: API_COMP_LED_CONTROLLER
links outgoing: REQ_LED_2

Clear a specific LED pixel (turn off). Equivalent to led_set_pixel(index, LED_COLOR_OFF).

Signature:

esp_err_t led_clear_pixel(uint16_t index);

Parameters:

  • index - LED index (0 to led_count-1)

Returns:

  • ESP_OK - Pixel cleared

  • ESP_ERR_INVALID_ARG - Index out of range

Note

Does not update physical LEDs until led_show() is called.

API Function: led_get_pixel API_FUNC_LED_GET_PIXEL
status: implemented
api_signature: led_color_t led_get_pixel(uint16_t index)
returns: Current color or LED_COLOR_OFF
parameters: index (LED index)
component: API_COMP_LED_CONTROLLER
links outgoing: REQ_LED_2, REQ_LED_4

Get current color of a specific LED pixel from RAM buffer.

Signature:

led_color_t led_get_pixel(uint16_t index);

Parameters:

  • index - LED index (0 to led_count-1)

Returns:

  • led_color_t - Current color, or LED_COLOR_OFF if index out of range

Note

Reads from RAM buffer, not physical LED.

API Function: led_clear_all API_FUNC_LED_CLEAR_ALL
status: implemented
api_signature: esp_err_t led_clear_all(void)
returns: ESP_OK on success, ESP_ERR_*
parameters: None
component: API_COMP_LED_CONTROLLER
links outgoing: REQ_LED_2

Clear all LED pixels (turn off all LEDs) in RAM buffer. Call led_show() to update physical LEDs.

Signature:

esp_err_t led_clear_all(void);

Parameters:

  • None

Returns:

  • ESP_OK - All pixels cleared

  • ESP_ERR_* - Failure

Note

Does not update physical LEDs until led_show() is called.

API Function: led_show API_FUNC_LED_SHOW
status: implemented
api_signature: esp_err_t led_show(void)
returns: ESP_OK on success, ESP_ERR_*
parameters: None
component: API_COMP_LED_CONTROLLER
links outgoing: REQ_LED_4

Update physical LED strip with current RAM buffer using RMT peripheral. Blocks until transmission is complete.

Signature:

esp_err_t led_show(void);

Parameters:

  • None

Returns:

  • ESP_OK - LEDs updated successfully

  • ESP_ERR_* - Failure

Note

Blocks for ~1-2ms for 40 LEDs. Only function that updates visible LEDs.

Utility Functions

API Function: led_color_rgb API_FUNC_LED_COLOR_RGB
status: implemented
api_signature: led_color_t led_color_rgb(uint8_t r, uint8_t g, uint8_t b)
returns: RGB color structure
parameters: r (red), g (green), b (blue)
component: API_COMP_LED_CONTROLLER
links outgoing: REQ_LED_4

Create RGB color from individual components.

Signature:

led_color_t led_color_rgb(uint8_t r, uint8_t g, uint8_t b);

Parameters:

  • r - Red component (0-255)

  • g - Green component (0-255)

  • b - Blue component (0-255)

Returns:

  • led_color_t - RGB color structure

Note

Utility for constructing custom colors.

API Function: led_color_brightness API_FUNC_LED_COLOR_BRIGHTNESS
status: implemented
api_signature: led_color_t led_color_brightness(led_color_t color, uint8_t brightness)
returns: Color with applied brightness
parameters: color (original color), brightness (0-255)
component: API_COMP_LED_CONTROLLER
links outgoing: REQ_LED_4

Apply brightness scaling to a color. Scales all RGB components by brightness factor.

Signature:

led_color_t led_color_brightness(led_color_t color, uint8_t brightness);

Parameters:

  • color - Original color

  • brightness - Brightness factor (0=off, 255=full)

Returns:

  • led_color_t - Color with brightness applied

Note

Uses integer math for efficiency.

Query and State Functions

API Function: led_get_count API_FUNC_LED_GET_COUNT
status: implemented
api_signature: uint16_t led_get_count(void)
returns: Number of LEDs or 0
parameters: None
component: API_COMP_LED_CONTROLLER
links outgoing: REQ_LED_3

Get configured LED count.

Signature:

uint16_t led_get_count(void);

Parameters:

  • None

Returns:

  • uint16_t - Number of LEDs, or 0 if not initialized

API Function: led_is_initialized API_FUNC_LED_IS_INITIALIZED
status: implemented
api_signature: bool led_is_initialized(void)
returns: true if initialized, false otherwise
parameters: None
component: API_COMP_LED_CONTROLLER
links outgoing: REQ_LED_1

Check if LED controller is initialized.

Signature:

bool led_is_initialized(void);

Parameters:

  • None

Returns:

  • true - Controller initialized

  • false - Not initialized

API Function: led_get_all_colors API_FUNC_LED_GET_ALL_COLORS
status: implemented
api_signature: uint16_t led_get_all_colors(led_color_t* output_buffer, uint16_t max_count)
returns: Number of LEDs copied or 0
parameters: output_buffer (buffer), max_count (buffer size)
component: API_COMP_LED_CONTROLLER
links outgoing: REQ_LED_5

Get snapshot of all LED colors (thread-safe). Returns stable snapshot matching last physical LED update.

Signature:

uint16_t led_get_all_colors(led_color_t* output_buffer, uint16_t max_count);

Parameters:

  • output_buffer - Pointer to buffer to receive LED colors

  • max_count - Maximum number of LEDs to copy (buffer size)

Returns:

  • uint16_t - Number of LEDs actually copied, or 0 on error

Note

Caller must allocate output_buffer with size >= led_count * sizeof(led_color_t). Thread-safe, may block briefly. Returns snapshot matching physical LED state.