|
14 | 14 | #define TAG "led" |
15 | 15 |
|
16 | 16 | #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 |
29 | 29 | }; |
30 | 30 |
|
31 | 31 | static uint8_t led_mode_index = 3; |
32 | 32 |
|
33 | 33 | static void led_task(void *pvParameter) |
34 | 34 | { |
35 | | - uint16_t i = 0; |
| 35 | + bool active = false; |
36 | 36 | portTickType xLastWakeTime; |
37 | 37 |
|
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); |
39 | 52 |
|
40 | 53 | ESP_LOGI(TAG, "started."); |
41 | 54 |
|
42 | 55 | while (1) { |
43 | 56 | xLastWakeTime = xTaskGetTickCount(); |
44 | 57 |
|
45 | | - if (i++ % led_mode_table[led_mode_index][1]) { |
46 | 58 | #ifdef CONFIG_LED_ACTIVE_LOW |
| 59 | + if (!active) { |
| 60 | +#else |
| 61 | + if (active) { |
| 62 | +#endif |
47 | 63 | gpio_set_level(CONFIG_LED_PIN, 1); |
48 | 64 | } else { |
49 | 65 | gpio_set_level(CONFIG_LED_PIN, 0); |
50 | 66 | } |
51 | | -#else |
52 | | - gpio_set_level(CONFIG_LED_PIN, 0); |
53 | | - } else { |
54 | | - gpio_set_level(CONFIG_LED_PIN, 1); |
55 | | - } |
56 | | -#endif |
57 | 67 |
|
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); |
59 | 71 | } |
60 | 72 | } |
61 | 73 | #endif |
62 | 74 |
|
63 | 75 | void led_set_mode(uint8_t idx) |
64 | 76 | { |
65 | 77 | #ifdef CONFIG_ENABLE_LED |
66 | | - if (idx >= (sizeof(led_mode_table) / 2)) { |
| 78 | + if (idx >= sizeof(led_mode_table)/2) { |
67 | 79 | ESP_LOGE(TAG, "invalid mode index"); |
68 | 80 | return; |
69 | 81 | } |
|
0 commit comments