ChaosDMX
Open-Source DMX-Interface
Loading...
Searching...
No Matches
led.c
Go to the documentation of this file.
1
5
6#define LOG_TAG "LED"
7
8#include "led.h"
9
10#define LED_GPIO_PIN 7
11#define LED_SPEED_MODE LEDC_LOW_SPEED_MODE
12#define LED_TIMER LEDC_TIMER_0
13#define LED_CHANNEL LEDC_CHANNEL_0
14#define LED_DUTY_RES LEDC_TIMER_8_BIT
15#define LED_FREQUENCY 5000
16
17/* --- Core Control States --- */
18static uint8_t s_config_brightness =
19 128;
21 255;
24static SemaphoreHandle_t s_led_mutex =
25 NULL;
26static TaskHandle_t s_generator_task_handle =
27 NULL;
28
29/* --- Internal Generator Parameters --- */
30static uint32_t s_param_period_ms = 1000;
31static bool s_param_breathing =
32 false;
33
34#ifndef M_PI
35#define M_PI 3.14159265358979323846
36#endif
37
43static void set_pwm_duty(uint8_t duty) {
44 ledc_set_duty(LED_SPEED_MODE, LED_CHANNEL, duty);
45 ledc_update_duty(LED_SPEED_MODE, LED_CHANNEL);
46}
47
56static void led_generator_task(void *pvParameters) {
57 const uint32_t step_interval_ms = 20; // 50 Hz Refresh-Rate
58 uint32_t elapsed_time_ms = 0;
59
60 while (1) {
61 xSemaphoreTake(s_led_mutex, portMAX_DELAY);
62
65 xSemaphoreGive(s_led_mutex);
66 vTaskDelete(NULL);
67 }
68
69 uint32_t period = s_param_period_ms;
70 bool breathing = s_param_breathing;
71 uint8_t max_b = s_active_max_brightness; // Use the currently valid limit
72
73 xSemaphoreGive(s_led_mutex);
74
75 if (breathing) {
76 float target =
77 (sinf(((float)elapsed_time_ms / period) * 2.0f * (float)M_PI -
78 ((float)M_PI / 2.0f)) +
79 1.0f) /
80 2.0f;
81 set_pwm_duty((uint8_t)(target * (float)max_b));
82 } else {
83 bool upper_half = elapsed_time_ms < (period / 2);
84 set_pwm_duty(upper_half ? max_b : 0);
85 }
86
87 vTaskDelay(pdMS_TO_TICKS(step_interval_ms));
88 elapsed_time_ms = (elapsed_time_ms + step_interval_ms) % period;
89 }
90}
91
101static void update_led_generator_unsafe(uint32_t period_ms, bool breathing) {
102 s_param_period_ms = period_ms;
103 s_param_breathing = breathing;
104
105 if (s_generator_task_handle == NULL) {
106 BaseType_t res = xTaskCreate(led_generator_task, "led_gen", 2048, NULL, 2,
108 if (res != pdPASS) {
109 LOGE("Failed to create LED generator task");
111 }
112 }
113}
114
115/* --- Public API --- */
116
117esp_err_t led_init(void) {
118 s_led_mutex = xSemaphoreCreateMutex();
119 if (s_led_mutex == NULL)
120 return ESP_ERR_NO_MEM;
121
122 ledc_timer_config_t ledc_timer = {.speed_mode = LED_SPEED_MODE,
123 .timer_num = LED_TIMER,
124 .duty_resolution = LED_DUTY_RES,
125 .freq_hz = LED_FREQUENCY,
126 .clk_cfg = LEDC_AUTO_CLK};
127 ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
128
129 ledc_channel_config_t ledc_channel = {.speed_mode = LED_SPEED_MODE,
130 .channel = LED_CHANNEL,
131 .timer_sel = LED_TIMER,
132 .gpio_num = LED_GPIO_PIN,
133 .duty = 0,
134 .hpoint = 0};
135 ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
136
138 return ESP_OK;
139}
140
142 if (!s_led_mutex)
143 return;
144
145 xSemaphoreTake(s_led_mutex, portMAX_DELAY);
146 if (s_current_mode == mode) {
147 xSemaphoreGive(s_led_mutex);
148 return;
149 }
150
151 s_current_mode = mode;
152
153 switch (mode) {
156 update_led_generator_unsafe(1000, true);
157 break;
158
159 case LED_MODE_RESET:
162 break;
163
164 case LED_MODE_ERROR:
166 update_led_generator_unsafe(300, false);
167 break;
168
169 case LED_MODE_WARN:
171 update_led_generator_unsafe(1000, false);
172 break;
173
174 case LED_MODE_NORMAL:
177 break;
178
179 case LED_MODE_OFF:
182 break;
183 }
184
185 xSemaphoreGive(s_led_mutex);
186}
187
188void led_set_brightness(uint8_t brightness) {
189 if (!s_led_mutex) {
190 return;
191 }
192
193 xSemaphoreTake(s_led_mutex, portMAX_DELAY);
194
195 s_config_brightness = brightness;
196
199
202 }
203 }
204
205 xSemaphoreGive(s_led_mutex);
206}
void led_set_brightness(uint8_t brightness)
Dynamically updates the LED maximum brightness.
Definition led.c:188
static void update_led_generator_unsafe(uint32_t period_ms, bool breathing)
Configures effect parameters and ensures the generator task is running.
Definition led.c:101
static uint8_t s_config_brightness
User defined brightness (0-255) in RAM.
Definition led.c:18
#define LED_DUTY_RES
LEDC duty resolution (8-bit).
Definition led.c:14
#define LED_FREQUENCY
PWM frequency in Hz.
Definition led.c:15
static bool s_param_breathing
True for sine-fade, False for square-blink.
Definition led.c:31
#define LED_SPEED_MODE
LEDC speed mode (Low Speed).
Definition led.c:11
static void led_generator_task(void *pvParameters)
Generic asynchronous generator for LED effects.
Definition led.c:56
static uint32_t s_param_period_ms
Cycle duration for effects.
Definition led.c:30
void led_set_mode(led_mode_t mode)
Sets the current operational mode of the LED.
Definition led.c:141
#define LED_CHANNEL
LEDC channel index.
Definition led.c:13
esp_err_t led_init(void)
Initializes the LEDC peripheral for the status LED.
Definition led.c:117
static TaskHandle_t s_generator_task_handle
Handle for the effect generator task.
Definition led.c:26
static SemaphoreHandle_t s_led_mutex
Guard for thread-safe access to LED state.
Definition led.c:24
static uint8_t s_active_max_brightness
Active peak duty cycle for current mode (0-255).
Definition led.c:20
#define LED_TIMER
LEDC timer index.
Definition led.c:12
static led_mode_t s_current_mode
Current operational mode.
Definition led.c:22
#define M_PI
Mathematical constant PI.
Definition led.c:35
#define LED_GPIO_PIN
GPIO pin number for the status LED.
Definition led.c:10
static void set_pwm_duty(uint8_t duty)
Updates the physical LEDC duty cycle.
Definition led.c:43
LED control component with flexible blinking and breathing modes.
led_mode_t
Available operational modes for the status LED.
Definition led.h:24
@ LED_MODE_NORMAL
Definition led.h:28
@ LED_MODE_WARN
Definition led.h:29
@ LED_MODE_OFF
Definition led.h:25
@ LED_MODE_ERROR
Definition led.h:30
@ LED_MODE_BOOT_BREATHING
Definition led.h:26
@ LED_MODE_RESET
Definition led.h:27
#define LOGE(...)
Log a message at Error level.
Definition logger.h:38