-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiot_light.c
609 lines (500 loc) · 21.2 KB
/
iot_light.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
/*
* SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include "math.h"
#include "sys/time.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "esp_log.h"
#include "soc/ledc_reg.h"
#include "soc/timer_group_struct.h"
#include "soc/ledc_struct.h"
#include "driver/periph_ctrl.h"
#include "driver/timer.h"
#include "driver/ledc.h"
#include "iot_light.h"
static const char *TAG = "light";
#define IOT_CHECK(tag, a, ret) if(!(a)) { \
return (ret); \
}
#define ERR_ASSERT(tag, param, ret) IOT_CHECK(tag, (param) == ESP_OK, ret)
#define POINT_ASSERT(tag, param) IOT_CHECK(tag, (param) != NULL, ESP_FAIL)
#define LIGHT_NUM_MAX 4
#define LIGHTS_LED_TIMER_BIT (LEDC_TIMER_13_BIT)
#define LIGHTS_LED_FULL_DUTY ((1 << LIGHTS_LED_TIMER_BIT) - 1)
enum light_channel {
CHANNEL_ID_RED = 0,
CHANNEL_ID_GREEN = 1,
CHANNEL_ID_BLUE = 2,
};
static light_handle_t g_led_handle = NULL;
static bool g_led_blink_flag = false;
typedef enum {
LIGHT_CH_NUM_1 = 1, /*!< Light channel number */
LIGHT_CH_NUM_2 = 2, /*!< Light channel number */
LIGHT_CH_NUM_3 = 3, /*!< Light channel number */
LIGHT_CH_NUM_4 = 4, /*!< Light channel number */
LIGHT_CH_NUM_5 = 5, /*!< Light channel number */
LIGHT_CH_NUM_MAX, /*!< user shouldn't use this */
} light_channel_num_t;
typedef struct {
timer_group_t timer_group;
timer_idx_t timer_id;
} hw_timer_idx_t;
typedef struct {
gpio_num_t io_num;
ledc_mode_t mode;
ledc_channel_t channel;
int breath_period;
int blink_period;
uint32_t fade_step_num;
uint32_t *fade_duty_step;
int fade_duty_counter;
bool fade_step_up;
bool fade_once;
bool fade_start;
} light_channel_t;
typedef struct {
uint8_t channel_num;
ledc_mode_t mode;
ledc_timer_t ledc_timer;
uint32_t full_duty;
uint32_t freq_hz;
ledc_timer_bit_t timer_bit;
hw_timer_idx_t hw_timer;
light_channel_t *channel_group[0];
} light_t;
static bool g_fade_installed = false;
static bool g_hw_timer_started = false;
static light_t *g_light_group[LIGHT_NUM_MAX] = {NULL};
static esp_err_t iot_light_duty_set(light_handle_t light_handle, uint8_t channel_id, uint32_t duty);
static void iot_timer_create(hw_timer_idx_t *timer_id, bool auto_reload, double timer_interval_sec, timer_isr_handle_t *isr_handle)
{
/* Select and initialize basic parameters of the timer */
timer_config_t config = {
.divider = HW_TIMER_DIVIDER,
.counter_dir = TIMER_COUNT_UP,
.counter_en = TIMER_PAUSE,
.alarm_en = TIMER_ALARM_EN,
.intr_type = TIMER_INTR_LEVEL,
.auto_reload = auto_reload,
};
timer_init(timer_id->timer_group, timer_id->timer_id, &config);
/* Timer's counter will initially start from value below.
Also, if auto_reload is set, this value will be automatically reload on alarm */
timer_set_counter_value(timer_id->timer_group, timer_id->timer_id, 0x00000000ULL);
/* Configure the alarm value and the interrupt on alarm. */
timer_set_alarm_value(timer_id->timer_group, timer_id->timer_id, timer_interval_sec * HW_TIMER_SCALE);
timer_enable_intr(timer_id->timer_group, timer_id->timer_id);
timer_isr_register(timer_id->timer_group, timer_id->timer_id, (void *)isr_handle,
(void *) timer_id->timer_id, ESP_INTR_FLAG_IRAM, NULL);
}
static void iot_timer_start(hw_timer_idx_t *timer_id)
{
timer_start(timer_id->timer_group, timer_id->timer_id);
g_hw_timer_started = true;
}
static void iot_timer_stop(hw_timer_idx_t *timer_id)
{
timer_disable_intr(timer_id->timer_group, timer_id->timer_id);
timer_pause(timer_id->timer_group, timer_id->timer_id);
g_hw_timer_started = false;
}
static IRAM_ATTR void iot_ledc_ls_channel_update(ledc_mode_t speed_mode, ledc_channel_t channel_num)
{
if (speed_mode == LEDC_LOW_SPEED_MODE) {
LEDC.channel_group[speed_mode].channel[channel_num].conf0.low_speed_update = 1;
}
}
static IRAM_ATTR esp_err_t iot_ledc_duty_config(ledc_mode_t speed_mode, ledc_channel_t channel_num, int hpoint_val, int duty_val,
uint32_t duty_direction, uint32_t duty_num, uint32_t duty_cycle, uint32_t duty_scale)
{
if (hpoint_val >= 0) {
LEDC.channel_group[speed_mode].channel[channel_num].hpoint.hpoint = hpoint_val & LEDC_HPOINT_HSCH1_V;
}
if (duty_val >= 0) {
LEDC.channel_group[speed_mode].channel[channel_num].duty.duty = duty_val;
}
LEDC.channel_group[speed_mode].channel[channel_num].conf1.val = ((duty_direction & LEDC_DUTY_INC_HSCH0_V) << LEDC_DUTY_INC_HSCH0_S) |
((duty_num & LEDC_DUTY_NUM_HSCH0_V) << LEDC_DUTY_NUM_HSCH0_S) |
((duty_cycle & LEDC_DUTY_CYCLE_HSCH0_V) << LEDC_DUTY_CYCLE_HSCH0_S) |
((duty_scale & LEDC_DUTY_SCALE_HSCH0_V) << LEDC_DUTY_SCALE_HSCH0_S);
iot_ledc_ls_channel_update(speed_mode, channel_num);
return ESP_OK;
}
static IRAM_ATTR esp_err_t iot_ledc_set_duty(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty)
{
return iot_ledc_duty_config(speed_mode,
channel, //uint32_t chan_num,
-1,
duty << 4, //uint32_t duty_val,the least 4 bits are decimal part
1, //uint32_t increase,
1, //uint32_t duty_num,
1, //uint32_t duty_cycle,
0 //uint32_t duty_scale
);
}
static IRAM_ATTR esp_err_t iot_ledc_update_duty(ledc_mode_t speed_mode, ledc_channel_t channel)
{
LEDC.channel_group[speed_mode].channel[channel].conf0.sig_out_en = 1;
LEDC.channel_group[speed_mode].channel[channel].conf1.duty_start = 1;
iot_ledc_ls_channel_update(speed_mode, channel);
return ESP_OK;
}
static IRAM_ATTR void breath_timer_callback(void *para)
{
int timer_idx = (int) para;
/* Retrieve the interrupt status */
timer_group_get_intr_status_in_isr(HW_TIMER_GROUP);
timer_group_clr_intr_status_in_isr(HW_TIMER_GROUP, timer_idx);
/* After the alarm has been triggered
we need enable it again, so it is triggered the next time */
timer_group_enable_alarm_in_isr(HW_TIMER_GROUP, timer_idx);
for (int i = 0; i < LIGHT_NUM_MAX; i++) {
if (g_light_group[i] != NULL) {
light_t *light = g_light_group[i];
for (int j = 0; j < light->channel_num; j++) {
light_channel_t *l_chn = light->channel_group[j];
if (l_chn->fade_start == true) {
if (l_chn->fade_step_up == true) {
l_chn->fade_duty_counter++;
} else {
l_chn->fade_duty_counter--;
}
if ((l_chn->fade_duty_counter >= 0) && (l_chn->fade_duty_counter < l_chn->fade_step_num)) {
iot_ledc_set_duty(l_chn->mode, l_chn->channel, l_chn->fade_duty_step[l_chn->fade_duty_counter]);
iot_ledc_update_duty(l_chn->mode, l_chn->channel);
} else {
if (l_chn->fade_once != true) {
l_chn->fade_step_up = 1 - l_chn->fade_step_up;
} else {
l_chn->fade_start = false;
}
}
}
}
}
}
}
static light_channel_t *light_channel_create(gpio_num_t io_num, ledc_channel_t channel, ledc_mode_t mode, ledc_timer_t timer)
{
ledc_channel_config_t ledc_channel = {
.channel = channel,
.duty = 0,
.gpio_num = io_num,
.intr_type = LEDC_INTR_FADE_END,
.speed_mode = mode,
.timer_sel = timer
};
ERR_ASSERT(TAG, ledc_channel_config(&ledc_channel), NULL);
light_channel_t *pwm = (light_channel_t *)calloc(1, sizeof(light_channel_t));
pwm->io_num = io_num;
pwm->channel = channel;
pwm->mode = mode;
pwm->breath_period = 0;
pwm->blink_period = 0;
pwm->fade_step_num = 0;
pwm->fade_duty_step = NULL;
pwm->fade_duty_counter = 0;
pwm->fade_step_up = true;
pwm->fade_once = false;
pwm->fade_start = false;
return pwm;
}
static esp_err_t light_channel_delete(light_channel_t *light_channel)
{
POINT_ASSERT(TAG, light_channel);
if (light_channel->fade_duty_step != NULL) {
heap_caps_free(light_channel->fade_duty_step);
light_channel->fade_duty_step = NULL;
}
free(light_channel);
return ESP_OK;
}
static esp_err_t light_channel_fade_clear(light_channel_t *light_channel)
{
light_channel->fade_start = false;
light_channel->fade_step_num = 0;
light_channel->fade_duty_counter = 0;
light_channel->fade_step_up = true;
light_channel->fade_once = false;
if (light_channel->fade_duty_step != NULL) {
heap_caps_free(light_channel->fade_duty_step);
light_channel->fade_duty_step = NULL;
}
return ESP_OK;
}
light_handle_t iot_light_create(ledc_timer_t timer, ledc_mode_t speed_mode, uint32_t freq_hz, uint8_t channel_num, ledc_timer_bit_t timer_bit)
{
IOT_CHECK(TAG, channel_num != 0, NULL);
ledc_timer_config_t timer_conf = {
.timer_num = timer,
.speed_mode = speed_mode,
.freq_hz = freq_hz,
.bit_num = timer_bit
};
ERR_ASSERT(TAG, ledc_timer_config(&timer_conf), NULL);
light_t *light_ptr = (light_t *)calloc(1, sizeof(light_t) + sizeof(light_channel_t *) * channel_num);
light_ptr->channel_num = channel_num;
light_ptr->ledc_timer = timer;
light_ptr->full_duty = (1 << timer_bit) - 1;
light_ptr->freq_hz = freq_hz;
light_ptr->mode = speed_mode;
light_ptr->timer_bit = timer_bit;
light_ptr->hw_timer.timer_group = HW_TIMER_GROUP;
light_ptr->hw_timer.timer_id = HW_TIMER_ID;
if (g_hw_timer_started == false) {
iot_timer_create(&(light_ptr->hw_timer), 1, (double)DUTY_SET_CYCLE / 1000, (void *)breath_timer_callback);
iot_timer_start(&(light_ptr->hw_timer));
}
for (int i = 0; i < channel_num; i++) {
light_ptr->channel_group[i] = NULL;
}
for (int i = 0; i < LIGHT_NUM_MAX; i++) {
if (g_light_group[i] == NULL) {
g_light_group[i] = light_ptr;
break;
}
}
return (light_handle_t)light_ptr;
}
esp_err_t iot_light_delete(light_handle_t light_handle)
{
light_t *light = (light_t *)light_handle;
POINT_ASSERT(TAG, light_handle);
for (int i = 0; i < light->channel_num; i++) {
if (light->channel_group[i] != NULL) {
iot_light_duty_set(light, i, 0);
light_channel_delete(light->channel_group[i]);
}
}
for (int i = 0; i < LIGHT_NUM_MAX; i++) {
if (g_light_group[i] == light) {
g_light_group[i] = NULL;
break;
}
}
for (int i = 0; i < LIGHT_NUM_MAX; i++) {
if (g_light_group[i] != NULL) {
goto FREE_MEM;
}
}
ledc_fade_func_uninstall();
g_fade_installed = false;
iot_timer_stop(&(light->hw_timer));
FREE_MEM:
free(light_handle);
return ESP_OK;
}
esp_err_t iot_light_channel_regist(light_handle_t light_handle, uint8_t channel_idx, gpio_num_t io_num, ledc_channel_t channel)
{
light_t *light = (light_t *)light_handle;
POINT_ASSERT(TAG, light_handle);
IOT_CHECK(TAG, channel_idx < light->channel_num, FAIL);
if (light->channel_group[channel_idx] != NULL) {
ESP_LOGE(TAG, "this channel index has been registered");
return ESP_FAIL;
}
light->channel_group[channel_idx] = light_channel_create(io_num, channel, light->mode, light->ledc_timer);
if (g_fade_installed == false) {
ledc_fade_func_install(0);
g_fade_installed = true;
}
return ESP_OK;
}
static esp_err_t iot_light_duty_set(light_handle_t light_handle, uint8_t channel_id, uint32_t duty)
{
light_t *light = (light_t *)light_handle;
POINT_ASSERT(TAG, light_handle);
IOT_CHECK(TAG, channel_id < light->channel_num, ESP_FAIL);
POINT_ASSERT(TAG, light->channel_group[channel_id]);
light_channel_t *l_chn = light->channel_group[channel_id];
light_channel_fade_clear(l_chn);
iot_ledc_set_duty(l_chn->mode, l_chn->channel, duty);
iot_ledc_update_duty(l_chn->mode, l_chn->channel);
return ESP_OK;
}
uint32_t iot_light_duty_get(light_handle_t light_handle, uint8_t channel_id)
{
light_t *light = (light_t *)light_handle;
POINT_ASSERT(TAG, light_handle);
IOT_CHECK(TAG, channel_id < light->channel_num, ESP_FAIL);
POINT_ASSERT(TAG, light->channel_group[channel_id]);
light_channel_t *l_chn = light->channel_group[channel_id];
return ledc_get_duty(l_chn->mode, l_chn->channel);
}
esp_err_t iot_light_fade_with_time(light_handle_t light_handle, uint8_t channel_id, uint32_t duty, uint32_t fade_period_ms)
{
light_t *light = (light_t *)light_handle;
POINT_ASSERT(TAG, light_handle);
IOT_CHECK(TAG, channel_id < light->channel_num, ESP_FAIL);
POINT_ASSERT(TAG, light->channel_group[channel_id]);
light_channel_t *l_chn = light->channel_group[channel_id];
if (fade_period_ms == 0) {
return iot_light_duty_set(light, channel_id, duty);
}
light_channel_fade_clear(l_chn);
l_chn->fade_step_num = fade_period_ms / DUTY_SET_CYCLE + 1;
l_chn->fade_duty_step = (uint32_t *)heap_caps_malloc(l_chn->fade_step_num * sizeof(uint32_t), MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
uint32_t duty_cur = iot_light_duty_get(light, channel_id);
int duty_delta = duty - duty_cur;
if (duty_delta > 0) {
float coe = (duty_delta / pow((double)l_chn->fade_step_num, (double)1 / DUTY_SET_GAMMA));
for (int i = 0; i < l_chn->fade_step_num; i++) {
l_chn->fade_duty_step[i] = duty_cur + (int)(coe * pow((double)i, (double)1 / DUTY_SET_GAMMA));
}
l_chn->fade_step_up = true;
} else {
duty_delta = 0 - duty_delta;
float coe = (duty_delta / pow((double)l_chn->fade_step_num, (double)1 / DUTY_SET_GAMMA));
for (int i = 0; i < l_chn->fade_step_num; i++) {
l_chn->fade_duty_step[i] = duty + (int)(coe * pow((double)i, (double)1 / DUTY_SET_GAMMA));
}
l_chn->fade_duty_counter = l_chn->fade_step_num;
l_chn->fade_step_up = false;
}
l_chn->fade_once = true;
l_chn->fade_start = true;
return ESP_OK;
}
esp_err_t iot_light_breath_config(light_handle_t light_handle, uint8_t channel_id, uint32_t duty, uint32_t breath_period_ms)
{
light_t *light = (light_t *)light_handle;
POINT_ASSERT(TAG, light_handle);
IOT_CHECK(TAG, channel_id < light->channel_num, ESP_FAIL);
POINT_ASSERT(TAG, light->channel_group[channel_id]);
light_channel_t *l_chn = light->channel_group[channel_id];
light_channel_fade_clear(l_chn);
/**
* @brief control light with nonlinearity fade, the duty and the fade step
* conform to gamma curve. gamma curve formula: y=a*x^(1/gm) firstly,
* use the target duty and step number to calculate coefficient `a`,
* secondly, calculate the duty for every step
*/
l_chn->fade_step_num = (breath_period_ms / 2) / DUTY_SET_CYCLE + 1;
float coe = (duty / pow((double)l_chn->fade_step_num, (double)1 / DUTY_SET_GAMMA));
l_chn->fade_duty_step = (uint32_t *)heap_caps_malloc(l_chn->fade_step_num * sizeof(uint32_t), MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
for (int i = 0; i < l_chn->fade_step_num; i++) {
l_chn->fade_duty_step[i] = (int)(coe * pow((double)i, (double)1 / DUTY_SET_GAMMA));
}
return ESP_OK;
}
esp_err_t iot_light_blink_config(light_handle_t light_handle, uint8_t channel_id, uint32_t blink_period_ms)
{
light_t *light = (light_t *)light_handle;
POINT_ASSERT(TAG, light_handle);
IOT_CHECK(TAG, channel_id < light->channel_num, ESP_FAIL);
light_channel_t *l_chn = light->channel_group[channel_id];
light_channel_fade_clear(l_chn);
l_chn->fade_step_num = (blink_period_ms / 2) / DUTY_SET_CYCLE;
l_chn->fade_duty_step = (uint32_t *)heap_caps_malloc(l_chn->fade_step_num * sizeof(uint32_t), MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
int i = 0;
for (i = 0; i < (l_chn->fade_step_num / 2); i++) {
l_chn->fade_duty_step[i] = 0;
}
for (; i < l_chn->fade_step_num; i++) {
l_chn->fade_duty_step[i] = light->full_duty;
}
return ESP_OK;
}
esp_err_t iot_light_operate_start(light_handle_t light_handle, uint8_t channel_id)
{
light_t *light = (light_t *)light_handle;
POINT_ASSERT(TAG, light_handle);
IOT_CHECK(TAG, channel_id < light->channel_num, ESP_FAIL);
light->channel_group[channel_id]->fade_start = true;
return ESP_OK;
}
esp_err_t iot_light_operate_stop(light_handle_t light_handle, uint8_t channel_id)
{
light_t *light = (light_t *)light_handle;
POINT_ASSERT(TAG, light_handle);
IOT_CHECK(TAG, channel_id < light->channel_num, ESP_FAIL);
light->channel_group[channel_id]->fade_start = false;
// iot_light_duty_set(light, channel_id, 0);
return ESP_OK;
}
// user functions added
esp_err_t led_set_rgb(uint8_t red, uint8_t green, uint8_t blue)
{
esp_err_t ret = ESP_OK;
// NOTE: repair the pwd output unbalance */
red = red / 2 * 180 / 255;
green = green * 180 / 255;
blue = blue * 180 / 255;
if (g_led_blink_flag) {
led_blink_stop();
}
ret = iot_light_fade_with_time(g_led_handle, CHANNEL_ID_RED, red * LIGHTS_LED_FULL_DUTY / 255, 0);
MDF_ERROR_CHECK(ret < 0, ret, "iot_light_fade_with_time, ret: %d", ret);
ret = iot_light_fade_with_time(g_led_handle, CHANNEL_ID_GREEN, green * LIGHTS_LED_FULL_DUTY / 255, 0);
MDF_ERROR_CHECK(ret < 0, ret, "iot_light_fade_with_time, ret: %d", ret);
ret = iot_light_fade_with_time(g_led_handle, CHANNEL_ID_BLUE, blue * LIGHTS_LED_FULL_DUTY / 255, 0);
MDF_ERROR_CHECK(ret < 0, ret, "iot_light_fade_with_time, ret: %d", ret);
return ESP_OK;
}
esp_err_t led_blink_start(uint8_t red, uint8_t green, uint8_t blue, uint32_t period_ms)
{
esp_err_t ret = ESP_OK;
// NOTE: repair the pwd output unbalance */
red = red / 2 * 128 / 255;
green = green * 128 / 255;
blue = blue * 128 / 255;
if (g_led_blink_flag) {
led_blink_stop();
}
ret = iot_light_breath_config(g_led_handle, CHANNEL_ID_RED,
red * LIGHTS_LED_FULL_DUTY / 255, period_ms);
MDF_ERROR_CHECK(ret < 0, ESP_FAIL, "iot_light_breath_config, ret: %d", ret);
ret = iot_light_breath_config(g_led_handle, CHANNEL_ID_GREEN,
green * LIGHTS_LED_FULL_DUTY / 255, period_ms);
MDF_ERROR_CHECK(ret < 0, ESP_FAIL, "iot_light_breath_config, ret: %d", ret);
ret = iot_light_breath_config(g_led_handle, CHANNEL_ID_BLUE,
blue * LIGHTS_LED_FULL_DUTY / 255, period_ms);
MDF_ERROR_CHECK(ret < 0, ESP_FAIL, "iot_light_breath_config, ret: %d", ret);
ret = iot_light_operate_start(g_led_handle, CHANNEL_ID_RED);
MDF_ERROR_CHECK(ret < 0, ESP_FAIL, "iot_light_operate_start, ret: %d", ret);
ret = iot_light_operate_start(g_led_handle, CHANNEL_ID_GREEN);
MDF_ERROR_CHECK(ret < 0, ESP_FAIL, "iot_light_operate_start, ret: %d", ret);
ret = iot_light_operate_start(g_led_handle, CHANNEL_ID_BLUE);
MDF_ERROR_CHECK(ret < 0, ESP_FAIL, "iot_light_operate_start, ret: %d", ret);
g_led_blink_flag = true;
return ESP_OK;
}
esp_err_t led_blink_stop()
{
esp_err_t ret = ESP_OK;
if (g_led_blink_flag == false) {
return ESP_OK;
}
ret = iot_light_operate_stop(g_led_handle, CHANNEL_ID_RED);
MDF_ERROR_CHECK(ret < 0, ESP_FAIL, "iot_light_operate_stop, ret: %d", ret);
ret = iot_light_operate_stop(g_led_handle, CHANNEL_ID_GREEN);
MDF_ERROR_CHECK(ret < 0, ESP_FAIL, "iot_light_operate_stop, ret: %d", ret);
ret = iot_light_operate_stop(g_led_handle, CHANNEL_ID_BLUE);
MDF_ERROR_CHECK(ret < 0, ESP_FAIL, "iot_light_operate_stop, ret: %d", ret);
return ESP_OK;
}
void
lights_init( void ){
esp_err_t ret = ESP_OK;
for (int i = 0; i < LIGHTS_LED_NUM; ++i) {
g_led_handle = iot_light_create(LEDC_TIMER_0, LEDC_HIGH_SPEED_MODE, 5000, 3, LIGHTS_LED_TIMER_BIT);
iot_light_channel_regist(g_led_handle, CHANNEL_ID_RED, LIGHTS_GPIO_LED_RED, LEDC_CHANNEL_0);
iot_light_channel_regist(g_led_handle, CHANNEL_ID_GREEN, LIGHTS_GPIO_LED_GREEN, LEDC_CHANNEL_1);
iot_light_channel_regist(g_led_handle, CHANNEL_ID_BLUE, LIGHTS_GPIO_LED_BLUE, LEDC_CHANNEL_2);
}
}
/* examples
led_set_rgb(128, 128, 0); // yellow
led_set_rgb(0, 0, 0); // off
led_blink_start(128, 128, 0, 2000); // yellow blink
led_set_rgb(128, 128, 0); // yellow
led_blink_start(0, 0, 128, 3000); // blue blink
led_set_rgb(0, 0, 255); // blue
led_set_rgb(255, 0, 0); // red
*/