Skip to content

Commit e6c4608

Browse files
committed
led: update gpio driver
1 parent 03188f7 commit e6c4608

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

main/src/user/led.c

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,56 +14,68 @@
1414
#define TAG "led"
1515

1616
#ifdef CONFIG_ENABLE_LED
17-
static const uint16_t led_mode_table[][2] = {
18-
/* { delay, count} */
19-
{ 0, 2}, // 0, Keep off
20-
{ 1000, 2}, // 1,
21-
{ 500, 2}, // 2,
22-
{ 250, 2}, // 3,
23-
{ 100, 2}, // 4,
24-
{ 25, 2}, // 5,
25-
{ 25, 25}, // 6,
26-
{ 25, 50}, // 7,
27-
{ 25, 100}, // 8,
28-
{ 25, 0} // 9, Keep on
17+
static const TickType_t led_mode_table[][2] = {
18+
/* { active, inactive } */
19+
{ 2000, 2000 }, // 0
20+
{ 1000, 1000 }, // 1
21+
{ 500, 500 }, // 2
22+
{ 250, 250 }, // 3
23+
{ 100, 100 }, // 4
24+
{ 25, 25 }, // 5
25+
{ 625, 25 }, // 6
26+
{ 1250, 25 }, // 7
27+
{ 1875, 25 }, // 8
28+
{ 2500, 25 }, // 9
2929
};
3030

3131
static uint8_t led_mode_index = 3;
3232

3333
static void led_task(void *pvParameter)
3434
{
35-
uint16_t i = 0;
35+
bool active = false;
3636
portTickType xLastWakeTime;
3737

38-
gpio_set_direction(CONFIG_LED_PIN, GPIO_MODE_OUTPUT);
38+
#ifdef CONFIG_LED_ACTIVE_LOW
39+
gpio_set_level(CONFIG_LED_PIN, 1);
40+
#else
41+
gpio_set_level(CONFIG_LED_PIN, 0);
42+
#endif
43+
44+
gpio_config_t io_conf = {
45+
.pin_bit_mask = BIT64(CONFIG_LED_PIN),
46+
.mode = GPIO_MODE_OUTPUT,
47+
.pull_up_en = false,
48+
.pull_down_en = false,
49+
.intr_type = GPIO_INTR_DISABLE,
50+
};
51+
gpio_config(&io_conf);
3952

4053
ESP_LOGI(TAG, "started.");
4154

4255
while (1) {
4356
xLastWakeTime = xTaskGetTickCount();
4457

45-
if (i++ % led_mode_table[led_mode_index][1]) {
4658
#ifdef CONFIG_LED_ACTIVE_LOW
59+
if (!active) {
60+
#else
61+
if (active) {
62+
#endif
4763
gpio_set_level(CONFIG_LED_PIN, 1);
4864
} else {
4965
gpio_set_level(CONFIG_LED_PIN, 0);
5066
}
51-
#else
52-
gpio_set_level(CONFIG_LED_PIN, 0);
53-
} else {
54-
gpio_set_level(CONFIG_LED_PIN, 1);
55-
}
56-
#endif
5767

58-
vTaskDelayUntil(&xLastWakeTime, led_mode_table[led_mode_index][0] / portTICK_RATE_MS);
68+
active = !active;
69+
70+
vTaskDelayUntil(&xLastWakeTime, led_mode_table[led_mode_index][active] / portTICK_RATE_MS);
5971
}
6072
}
6173
#endif
6274

6375
void led_set_mode(uint8_t idx)
6476
{
6577
#ifdef CONFIG_ENABLE_LED
66-
if (idx >= (sizeof(led_mode_table) / 2)) {
78+
if (idx >= sizeof(led_mode_table)/2) {
6779
ESP_LOGE(TAG, "invalid mode index");
6880
return;
6981
}

0 commit comments

Comments
 (0)