ChaosDMX
Open-Source DMX-Interface
Loading...
Searching...
No Matches
led.c File Reference

Implementation of LEDC-driven status LED with dynamic generator. More...

#include "led.h"
Include dependency graph for led.c:

Go to the source code of this file.

Macros

#define LOG_TAG   "LED"
 Logging tag for the LED component.
#define LED_GPIO_PIN   7
 GPIO pin number for the status LED.
#define LED_SPEED_MODE   LEDC_LOW_SPEED_MODE
 LEDC speed mode (Low Speed).
#define LED_TIMER   LEDC_TIMER_0
 LEDC timer index.
#define LED_CHANNEL   LEDC_CHANNEL_0
 LEDC channel index.
#define LED_DUTY_RES   LEDC_TIMER_8_BIT
 LEDC duty resolution (8-bit).
#define LED_FREQUENCY   5000
 PWM frequency in Hz.
#define M_PI   3.14159265358979323846
 Mathematical constant PI.

Functions

static void set_pwm_duty (uint8_t duty)
 Updates the physical LEDC duty cycle.
static void led_generator_task (void *pvParameters)
 Generic asynchronous generator for LED effects.
static void update_led_generator_unsafe (uint32_t period_ms, bool breathing)
 Configures effect parameters and ensures the generator task is running.
esp_err_t led_init (void)
 Initializes the LEDC peripheral for the status LED.
void led_set_mode (led_mode_t mode)
 Sets the current operational mode of the LED.
void led_set_brightness (uint8_t brightness)
 Dynamically updates the LED maximum brightness.

Variables

static uint8_t s_config_brightness
 User defined brightness (0-255) in RAM.
static uint8_t s_active_max_brightness
 Active peak duty cycle for current mode (0-255).
static led_mode_t s_current_mode
 Current operational mode.
static SemaphoreHandle_t s_led_mutex
 Guard for thread-safe access to LED state.
static TaskHandle_t s_generator_task_handle
 Handle for the effect generator task.
static uint32_t s_param_period_ms = 1000
 Cycle duration for effects.
static bool s_param_breathing
 True for sine-fade, False for square-blink.

Detailed Description

Implementation of LEDC-driven status LED with dynamic generator.

Definition in file led.c.

Macro Definition Documentation

◆ LED_CHANNEL

#define LED_CHANNEL   LEDC_CHANNEL_0

LEDC channel index.

Definition at line 13 of file led.c.

Referenced by led_init(), and set_pwm_duty().

◆ LED_DUTY_RES

#define LED_DUTY_RES   LEDC_TIMER_8_BIT

LEDC duty resolution (8-bit).

Definition at line 14 of file led.c.

Referenced by led_init().

◆ LED_FREQUENCY

#define LED_FREQUENCY   5000

PWM frequency in Hz.

Definition at line 15 of file led.c.

Referenced by led_init().

◆ LED_GPIO_PIN

#define LED_GPIO_PIN   7

GPIO pin number for the status LED.

Definition at line 10 of file led.c.

Referenced by led_init().

◆ LED_SPEED_MODE

#define LED_SPEED_MODE   LEDC_LOW_SPEED_MODE

LEDC speed mode (Low Speed).

Definition at line 11 of file led.c.

Referenced by led_init(), and set_pwm_duty().

◆ LED_TIMER

#define LED_TIMER   LEDC_TIMER_0

LEDC timer index.

Definition at line 12 of file led.c.

Referenced by led_init().

◆ LOG_TAG

#define LOG_TAG   "LED"

Logging tag for the LED component.

Definition at line 6 of file led.c.

◆ M_PI

#define M_PI   3.14159265358979323846

Mathematical constant PI.

Definition at line 35 of file led.c.

Referenced by led_generator_task().

Function Documentation

◆ led_generator_task()

void led_generator_task ( void * pvParameters)
static

Generic asynchronous generator for LED effects.

Runs in its own task and calculates the duty cycle based on mode parameters. Deletes itself when mode is set to LED_MODE_NORMAL.

Parameters
[in]pvParametersUnused.

Definition at line 56 of file led.c.

References LED_MODE_NORMAL, M_PI, s_active_max_brightness, s_current_mode, s_generator_task_handle, s_led_mutex, s_param_breathing, s_param_period_ms, and set_pwm_duty().

Referenced by update_led_generator_unsafe().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ led_init()

esp_err_t led_init ( void )

Initializes the LEDC peripheral for the status LED.

Sets up the timer and channel configuration for the LED GPIO.

Returns
ESP_OK on success, or an error code from the LEDC driver.

Definition at line 117 of file led.c.

References LED_CHANNEL, LED_DUTY_RES, LED_FREQUENCY, LED_GPIO_PIN, LED_SPEED_MODE, LED_TIMER, s_active_max_brightness, s_led_mutex, and set_pwm_duty().

Referenced by app_main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ led_set_brightness()

void led_set_brightness ( uint8_t brightness)

Dynamically updates the LED maximum brightness.

Parameters
[in]brightnessValue from 0 (off) to 255 (max).

Definition at line 188 of file led.c.

References LED_MODE_NORMAL, LED_MODE_WARN, s_active_max_brightness, s_config_brightness, s_current_mode, s_led_mutex, and set_pwm_duty().

Referenced by app_main(), and config_set_led_brightness().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ led_set_mode()

void led_set_mode ( led_mode_t mode)

Sets the current operational mode of the LED.

Modes include static, blinking, and breathing effects.

Parameters
[in]modeThe desired led_mode_t to activate.

Definition at line 141 of file led.c.

References LED_MODE_BOOT_BREATHING, LED_MODE_ERROR, LED_MODE_NORMAL, LED_MODE_OFF, LED_MODE_RESET, LED_MODE_WARN, s_active_max_brightness, s_config_brightness, s_current_mode, s_led_mutex, set_pwm_duty(), and update_led_generator_unsafe().

Referenced by app_main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_pwm_duty()

void set_pwm_duty ( uint8_t duty)
static

Updates the physical LEDC duty cycle.

Parameters
[in]duty8-bit duty cycle value (0-255).

Definition at line 43 of file led.c.

References LED_CHANNEL, and LED_SPEED_MODE.

Referenced by led_generator_task(), led_init(), led_set_brightness(), and led_set_mode().

Here is the caller graph for this function:

◆ update_led_generator_unsafe()

void update_led_generator_unsafe ( uint32_t period_ms,
bool breathing )
static

Configures effect parameters and ensures the generator task is running.

Note
MUST be called while holding s_led_mutex.
Parameters
[in]period_msEffect cycle time.
[in]breathingTrue for sine-pulse, false for blink.

Definition at line 101 of file led.c.

References led_generator_task(), LOGE, s_generator_task_handle, s_param_breathing, and s_param_period_ms.

Referenced by led_set_mode().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ s_active_max_brightness

uint8_t s_active_max_brightness
static
Initial value:
=
255

Active peak duty cycle for current mode (0-255).

Definition at line 20 of file led.c.

Referenced by led_generator_task(), led_init(), led_set_brightness(), and led_set_mode().

◆ s_config_brightness

uint8_t s_config_brightness
static
Initial value:
=
128

User defined brightness (0-255) in RAM.

Definition at line 18 of file led.c.

Referenced by led_set_brightness(), and led_set_mode().

◆ s_current_mode

led_mode_t s_current_mode
static
Initial value:
=
@ LED_MODE_NORMAL
Definition led.h:28

Current operational mode.

Definition at line 22 of file led.c.

Referenced by led_generator_task(), led_set_brightness(), and led_set_mode().

◆ s_generator_task_handle

TaskHandle_t s_generator_task_handle
static
Initial value:
=
NULL

Handle for the effect generator task.

Definition at line 26 of file led.c.

Referenced by led_generator_task(), and update_led_generator_unsafe().

◆ s_led_mutex

SemaphoreHandle_t s_led_mutex
static
Initial value:
=
NULL

Guard for thread-safe access to LED state.

Definition at line 24 of file led.c.

Referenced by led_generator_task(), led_init(), led_set_brightness(), and led_set_mode().

◆ s_param_breathing

bool s_param_breathing
static
Initial value:
=
false

True for sine-fade, False for square-blink.

Definition at line 31 of file led.c.

Referenced by led_generator_task(), and update_led_generator_unsafe().

◆ s_param_period_ms

uint32_t s_param_period_ms = 1000
static

Cycle duration for effects.

Definition at line 30 of file led.c.

Referenced by led_generator_task(), and update_led_generator_unsafe().