-
Notifications
You must be signed in to change notification settings - Fork 0
/
led.c
672 lines (558 loc) · 16.6 KB
/
led.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/**
******************************************************************************
* @file : led.c
* @author : Mauricio Barroso Benavides
* @date : Mar 21, 2022
* @brief : This file provides code for the configuration and control
* LEDs instances
******************************************************************************
* @attention
*
* MIT License
*
* Copyright (c) 2022 Mauricio Barroso Benavides
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "led.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "freertos/queue.h"
/* External variables --------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
#if CONFIG_IDF_TARGET_ESP32
#define LED_SPEED_MODE LEDC_HIGH_SPEED_MODE
#endif
#if !CONFIG_IDF_TARGET_ESP32
#define LED_SPEED_MODE LEDC_LOW_SPEED_MODE
#endif
#define LED_PWM_MAX_NUM SOC_LEDC_CHANNEL_NUM
#define LED_PWM_TIMER_FREQ 5000
#define LED_PWM_TIMER_NUM LEDC_TIMER_0
#define LED_PWM_TIMER_BIT LEDC_TIMER_8_BIT
#define LED_PWM_SINGLE_NUM 1
#define LED_PWM_RGB_NUM 3
/* Private function prototypes -----------------------------------------------*/
static void _led_control_task(void * arg);
static esp_err_t _led_init_common(void);
static void _led_set(led_t *const me);
static esp_err_t _led_pwm_init(led_t *const me, uint32_t *gpio_list, uint8_t len);
static led_color_delta_t _led_get_delta(led_t *const me);
static led_rgb_t _led_assign_rgb(uint8_t r, uint8_t g, uint8_t b);
/* Private variables ---------------------------------------------------------*/
static const char *TAG = "led";
static uint8_t led_pwm_num = 0;
/* LED control task variables */
static TaskHandle_t led_control_handle = NULL;
static QueueHandle_t led_control_queue = NULL;
/* Exported functions --------------------------------------------------------*/
/**
* @brief Function to initialize a single instance LED
*/
esp_err_t led_single_init(led_t *const me, uint32_t gpio) {
ESP_LOGI(TAG, "Initializing LED instance...");
/* Error code variable */
esp_err_t ret = ESP_OK;
me->type = LED_TYPE_SINGLE;
me->mode = LED_MODE_OFF;
me->state = LED_STATE_OFF;
ret = _led_pwm_init(me, &gpio, LED_PWM_SINGLE_NUM);
if (ret != ESP_OK) {
return ret;
}
ret = _led_init_common();
/* Return ESP_OK */
return ret;
}
/**
* @brief Function to initialize a RGB instance LED
*/
esp_err_t led_rgb_init(led_t *const me, uint32_t gpio_r, uint32_t gpio_g,
uint32_t gpio_b)
{
ESP_LOGI(TAG, "Initializing LED instance...");
/* Error code variable */
esp_err_t ret = ESP_OK;
/* Fill the members with the default values */
me->type = LED_TYPE_RGB;
me->mode = LED_MODE_OFF;
me->state = LED_STATE_OFF;
uint32_t gpio_num[LED_PWM_RGB_NUM] = {gpio_r, gpio_g, gpio_b};
ret = _led_pwm_init(me, gpio_num, LED_PWM_RGB_NUM);
if (ret != ESP_OK) {
return ret;
}
ret = _led_init_common();
/* Return ESP_OK */
return ret;
}
/**
* @brief Function to initialize a strip instance LED
*/
esp_err_t led_strip_init(led_t *const me, uint32_t gpio, uint16_t num)
{
/* Error code variable */
esp_err_t ret = ESP_OK;
/* Fill the members with the default values */
me->type = LED_TYPE_STRIP;
me->mode = LED_MODE_OFF;
me->state = LED_STATE_OFF;
/* Check if the GPIO number is valid */
if (gpio >= GPIO_NUM_MAX) {
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Invalid GPIO number argument");
return ESP_ERR_INVALID_ARG;
}
}
/* Configure the GPIO and the RGB LEDs number */
led_strip_config_t led_strip_config = {
.strip_gpio_num = gpio,
.max_leds = num,
.led_pixel_format = LED_PIXEL_FORMAT_GRB,
.led_model = LED_MODEL_WS2812,
.flags.invert_out = false
};
/* Configure RMT ticks resolution */
led_strip_rmt_config_t rmt_config = {
.clk_src = RMT_CLK_SRC_DEFAULT,
.resolution_hz = 10 * 1000 * 1000,
.flags.with_dma = false
};
ret = led_strip_new_rmt_device(
&led_strip_config,
&rmt_config,
&me->config.strip.handle);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to create a new RMT device");
return ret;
}
me->config.strip.num = num;
/* Clear all RGB LEDs */
ret = led_strip_clear(me->config.strip.handle);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to turn off LEDs");
return ret;
}
ret = _led_init_common();
/* Return ESP_OK */
return ret;
}
/**
* @brief Set single LED instance function mode to continuous
*/
void led_single_set_continuous(led_t *const me, uint8_t level)
{
/* Check if the new operating mode is different than the current */
if (me->mode == LED_MODE_CONTINUOUS) {
/* Check if the new color is different than the current */
if (level == me->color_init.level) {
return;
}
}
/* Update the new operating mode and color */
else {
me->mode = LED_MODE_CONTINUOUS;
}
/* Update the new color */
me->color_init.level = level;
/* Send to queue */
if(xQueueSend(led_control_queue, &me, 0) != pdPASS) {
ESP_LOGE(TAG, "Failed to send to queue");
return;
}
}
/**
* @brief Set single LED instance function mode to blink
*/
void led_single_set_blink(led_t *const me, uint8_t level, uint16_t on_time,
uint16_t off_time)
{
/* Check if the new operating mode is different than the current */
if (me->mode == LED_MODE_BLINK) {
/* Check if the new color is different than the current */
if (level == me->color_init.level) {
return;
}
}
/* Update the new operating mode and color */
else {
me->mode = LED_MODE_BLINK;
}
/* Update the new color */
me->color_init.level = level;
/* update the new times */
me->on_time = on_time;
me->off_time = off_time;
/* Reset on and off counters */
me->counter = 0;
/* Calculate the steps and delta */
me->steps = me->on_time / 10;
me->delta = _led_get_delta(me);
/**/
me->state = LED_STATE_ON;
/* Send to queue */
if (xQueueSend(led_control_queue, &me, 0) != pdPASS) {
ESP_LOGE(TAG, "Failed to send to queue");
return;
}
}
/**
* @brief Set single LED instance function mode to fade
*/
void led_single_set_fade(led_t *const me, uint8_t level, uint16_t on_time,
uint16_t off_time)
{
/* Check if the new operating mode is different than the current */
if (me->mode == LED_MODE_FADE) {
/* Check if the new color is different than the current */
if (level == me->color_init.level) {
return;
}
}
/* Update the new operating mode and color */
else {
me->mode = LED_MODE_FADE;
}
/* Update the new color */
me->color_init.level = level;
/* update the new times */
me->on_time = on_time;
me->off_time = off_time;
/* Reset the step countert */
me->counter = 0;
/* Calculate the steps and delta */
me->steps = me->on_time / 10;
me->delta.level = (float)me->color_init.level / me->steps;
/* Set LED state */
me->state = LED_STATE_ON;
/* Send to queue */
if (xQueueSend(led_control_queue, &me, 0) != pdPASS) {
ESP_LOGE(TAG, "Failed to send to queue");
return;
}
}
/**
* @brief Set RGB or strip LED instance function mode to continuous
*/
void led_rgb_set_continuous(led_t *const me, uint8_t r, uint8_t g,
uint8_t b)
{
/* Check if the new operating mode is different than the current */
if (me->mode == LED_MODE_CONTINUOUS) {
/* Check if the new color is different than the current */
if (me->color_init.rgb.r == r && me->color_init.rgb.g == g && me->color_init.rgb.b == b) {
return;
}
}
/* Update the new operating mode and color */
me->mode = LED_MODE_CONTINUOUS;
/* Update the new color */
me->color_init.rgb.r = r;
me->color_init.rgb.g = g;
me->color_init.rgb.b = b;
/* Send to queue */
if (xQueueSend(led_control_queue, &me, 0) != pdPASS) {
ESP_LOGE(TAG, "Failed to send to queue");
return;
}
}
/**
* @brief Set RGB or strip LED instance function mode to blink
*/
void led_rgb_set_blink(led_t *const me, uint8_t r, uint8_t g,
uint8_t b, uint16_t on_time, uint16_t off_time)
{
/* Check if the new operating mode is different than the current */
if (me->mode == LED_MODE_BLINK) {
/* Check if the new color is different than the current */
if (me->color_init.rgb.r == r && me->color_init.rgb.g == g && me->color_init.rgb.b == b) {
return;
}
}
/* Update the new operating mode and color */
else {
me->mode = LED_MODE_BLINK;
}
/* Update the new color */
me->color_init.rgb = _led_assign_rgb(r, g, b);
/* update the new times */
me->on_time = on_time;
me->off_time = off_time;
/* Reset the step countert */
me->counter = 0;
/* Calculate the steps and delta */
me->steps = me->on_time / 10;
me->delta = _led_get_delta(me);
/* Set LED state */
me->state = LED_STATE_ON;
me->last_time = xTaskGetTickCount();
/* Send to queue */
if (xQueueSend(led_control_queue, &me, 0) != pdPASS) {
ESP_LOGE(TAG, "Failed to send to queue");
return;
}
}
/**
* @brief Set RGB or strip LED instance function mode to fade
*/
void led_rgb_set_fade(led_t *const me, uint8_t r, uint8_t g,
uint8_t b, uint16_t on_time, uint16_t off_time) {
/* Check if the new operating mode is different than the current */
if (me->mode == LED_MODE_FADE) {
/* Check if the new color is different than the current */
if (me->color_init.rgb.r == r && me->color_init.rgb.g == g && me->color_init.rgb.b == b) {
return;
}
}
/* Update the new operating mode and color */
me->mode = LED_MODE_FADE;
/* Update the new color */
me->color_init.rgb = _led_assign_rgb(r, g, b);
/* update the new times */
me->on_time = on_time;
me->off_time = off_time;
/* Reset the step countert */
me->counter = 0;
/* Calculate the steps and delta */
me->steps = me->on_time / 10;
me->delta = _led_get_delta(me);
/* Set LED state */
me->state = LED_STATE_ON;
me->last_time = xTaskGetTickCount();
/* Send to queue */
if (xQueueSend(led_control_queue, &me, 0) != pdPASS) {
ESP_LOGE(TAG, "Failed to send to queue");
return;
}
}
/* Private functions ---------------------------------------------------------*/
static void _led_control_task(void *arg)
{
led_t *led;
/* Inifinite loop */
for (;;) {
/* Try to read the queue */
xQueueReceive(led_control_queue, &led, portMAX_DELAY);
switch (led->mode) {
case LED_MODE_CONTINUOUS:
led->color_curr = led->color_init;
_led_set(led);
break;
case LED_MODE_BLINK:
/* Change the current color and state */
if (led->state == LED_STATE_ON) {
if (++led->counter >= led->steps) {
led->steps = led->off_time / 10;
led->counter = led->steps;
led->state = LED_STATE_OFF;
/* Set the current color */
led->color_curr.level = 0;
led->color_curr.rgb = _led_assign_rgb(0, 0, 0);
_led_set(led);
}
}
else {
if (--led->counter <= 0) {
led->steps = led->on_time / 10;
led->counter = 0;
led->state = LED_STATE_ON;
/* Set the current color */
led->color_curr = led->color_init;
_led_set(led);
}
}
xQueueSend(led_control_queue, &led, 0);
vTaskDelayUntil(&led->last_time, pdMS_TO_TICKS(10));
break;
case LED_MODE_FADE:
/* Change the current color and state */
if (led->state == LED_STATE_ON) {
if (++led->counter >= led->steps) {
led->steps = led->off_time / 10;
led->counter = led->steps;
led->state = LED_STATE_OFF;
}
}
else {
if (--led->counter <= 0) {
led->steps = led->on_time / 10;
led->counter = 0;
led->state = LED_STATE_ON;
}
}
/* Set the current color */
led->color_curr.level = led->delta.level * led->counter;
led->color_curr.rgb = _led_assign_rgb(
led->delta.rgb.r * led->counter,
led->delta.rgb.g * led->counter,
led->delta.rgb.b * led->counter);
led->delta = _led_get_delta(led);
_led_set(led);
xQueueSend(led_control_queue, &led, 0);
vTaskDelayUntil(&led->last_time, pdMS_TO_TICKS(10));
break;
case LED_MODE_OFF:
/* Set the current color */
led->color_curr.rgb = _led_assign_rgb(0, 0, 0);
_led_set(led);
break;
default:
ESP_LOGW(TAG, "Unknown LED mode");
break;
}
}
}
static esp_err_t _led_init_common(void)
{
/* Return code variable */
esp_err_t ret = ESP_OK;
/* Create queue to send data to control LEDs */
if(led_control_queue == NULL) {
led_control_queue = xQueueCreate(32, sizeof(led_t *));
if(led_control_queue == NULL) {
ESP_LOGE(TAG, "Failed to create queue");
return ESP_FAIL;
}
}
/* Create task to control LEDs */
if(led_control_handle == NULL) {
xTaskCreate(_led_control_task,
"LED control task",
configMINIMAL_STACK_SIZE * 4,
NULL,
tskIDLE_PRIORITY + 1,
&led_control_handle);
if(led_control_handle == NULL) {
ESP_LOGE(TAG, "Failed to create task");
return ESP_FAIL;
}
}
/* Return ESP_OK */
return ret;
}
static void _led_set(led_t *const me) {
switch (me->type) {
case LED_TYPE_SINGLE:
ledc_set_duty(
me->config.single.speed_mode,
me->config.single.channel,
me->color_curr.level);
ledc_update_duty(
me->config.single.speed_mode,
me->config.single.channel);
break;
case LED_TYPE_RGB: {
for (uint8_t i = 0; i < LED_PWM_RGB_NUM; i++) {
ledc_set_duty(
me->config.rgb[i].speed_mode,
me->config.rgb[i].channel,
*((uint8_t *)&me->color_curr.rgb + i));
ledc_update_duty(
me->config.rgb[i].speed_mode,
me->config.rgb[i].channel);
}
break;
case LED_TYPE_STRIP:
for (uint8_t i = 0; i < me->config.strip.num; i++) {
led_strip_set_pixel(
me->config.strip.handle,
i,
me->color_curr.rgb.r,
me->color_curr.rgb.g,
me->color_curr.rgb.b);
}
led_strip_refresh(me->config.strip.handle);
break;
}
default:
break;
}
}
static esp_err_t _led_pwm_init(led_t *const me, uint32_t *gpio_list, uint8_t len)
{
esp_err_t ret = ESP_OK;
for (uint8_t i = 0; i < len; i++) {
if (gpio_list[i] >= GPIO_NUM_MAX) {
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Invalid GPIO number argument");
return ESP_ERR_INVALID_ARG;
}
}
}
if (led_pwm_num + len > LED_PWM_MAX_NUM - 1) {
ESP_LOGE(TAG, "Maximum number of PWM LEDs reached");
return ESP_FAIL;
}
/* Configure and initialize timer for the first instance */
if(!led_pwm_num) {
ledc_timer_config_t leds_timer = {
.duty_resolution = LED_PWM_TIMER_BIT,
.freq_hz = LED_PWM_TIMER_FREQ,
.speed_mode = LEDC_LOW_SPEED_MODE,
.timer_num = LED_PWM_TIMER_NUM,
.clk_cfg = LEDC_AUTO_CLK,
};
ret = ledc_timer_config(&leds_timer);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to configure LEDC timer");
return ret;
}
}
/* Set and configure LEDC channel */
for (uint8_t i = 0; i < len; i++) {
me->config.rgb[i].channel = led_pwm_num;
me->config.rgb[i].duty = 0;
me->config.rgb[i].gpio_num = gpio_list[i];
me->config.rgb[i].speed_mode = LED_SPEED_MODE;
me->config.rgb[i].hpoint = 0;
me->config.rgb[i].timer_sel = LED_PWM_TIMER_NUM;
me->config.rgb[i].flags.output_invert = false;
me->config.rgb[i].intr_type = LEDC_INTR_DISABLE;
ret = ledc_channel_config(&me->config.rgb[i]);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to configure LEDC channel");
return ret;
}
/* Increment PWM LEDs number */
led_pwm_num++;
}
return ret;
}
static led_color_delta_t _led_get_delta(led_t *const me)
{
led_color_delta_t delta;
if (me->type == LED_TYPE_SINGLE) {
delta.level = (float)me->color_init.level / me->steps;
}
else {
delta.rgb.r = (float)me->color_init.rgb.r / me->steps;
delta.rgb.g = (float)me->color_init.rgb.g / me->steps;
delta.rgb.b = (float)me->color_init.rgb.b / me->steps;
}
return delta;
}
static led_rgb_t _led_assign_rgb(uint8_t r, uint8_t g, uint8_t b)
{
return (led_rgb_t){r, g, b};
}
/***************************** END OF FILE ************************************/