Arduino AudioKit HAL
periph_led.h
1 /*
2  * ESPRESSIF MIT License
3  *
4  * Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
5  *
6  * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case,
7  * it is free of charge, to any person obtaining a copy of this software and associated
8  * documentation files (the "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
11  * to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies or
14  * substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  */
24 
25 #ifndef _PERIPH_LED_H_
26 #define _PERIPH_LED_H_
27 
28 #include "driver/ledc.h"
29 #include "esp_peripherals.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /**
36  * @brief Peripheral LED event id
37  */
38 typedef enum {
39  PERIPH_LED_UNCHANGE = 0, /*!< No event */
40  PERIPH_LED_BLINK_FINISH, /*!< When LED blink is finished */
41 } periph_led_event_id_t;
42 
43 /**
44  * @brief Peripheral LED idle output level
45  */
46 typedef enum {
47  PERIPH_LED_IDLE_LEVEL_LOW, /*!< Low level output */
48  PERIPH_LED_IDLE_LEVEL_HIGH /*!< High level output */
49 } periph_led_idle_level_t;
50 
51 /**
52  * @brief The LED peripheral configuration
53  */
54 typedef struct {
55  ledc_mode_t led_speed_mode; /*!< LEDC speed speed_mode, high-speed mode or low-speed mode */
56  ledc_timer_bit_t led_duty_resolution; /*!< LEDC channel duty resolution */
57  ledc_timer_t led_timer_num; /*!< Select the timer source of channel (0 - 3) */
58  uint32_t led_freq_hz; /*!< LEDC timer frequency (Hz) */
59  int gpio_num; /*!< Optional, < 0 invalid gpio number */
61 
62 /**
63  * @brief Create the LED peripheral handle for esp_peripherals
64  *
65  * @note The handle was created by this function automatically destroy when `esp_periph_destroy` is called
66  *
67  * @param config The configuration
68  *
69  * @return The esp peripheral handle
70  */
71 esp_periph_handle_t periph_led_init(periph_led_cfg_t* config);
72 
73 /**
74  * @brief Bink LED Peripheral, this function will automatically configure the gpio_num to control the LED,
75  * with `time_on_ms` as the time (in milliseconds) switch from OFF to ON (or ON if fade is disabled),
76  * and `time_off_ms` as the time (in milliseconds) switch from ON to OFF (or OFF if fade is disabled).
77  * When switching from ON -> OFF and vice versa, the loop decreases once, and will turn off the effect when the loop is 0.
78  * With a loop value less than 0, the LED effect will loop endlessly.
79  * PERIPH_LED_BLINK_FINISH events will be sent at each end of loop
80  *
81  * @param[in] periph The LED periph
82  * @param[in] gpio_num The gpio number
83  * @param[in] time_on_ms The time on milliseconds
84  * @param[in] time_off_ms The time off milliseconds
85  * @param[in] fade Fading enabled
86  * @param[in] loop Loop
87  * @param[in] level idle level
88  *
89  * @return
90  * - ESP_OK
91  * - ESP_FAIL
92  */
93 esp_err_t periph_led_blink(esp_periph_handle_t periph, int gpio_num, int time_on_ms, int time_off_ms, bool fade, int loop, periph_led_idle_level_t level);
94 
95 /**
96  * @brief Stop Blink the LED
97  *
98  * @param[in] periph The periph
99  * @param[in] gpio_num The gpio number
100  *
101  * @return
102  * - ESP_OK
103  * - ESP_FAIL
104  */
105 esp_err_t periph_led_stop(esp_periph_handle_t periph, int gpio_num);
106 
107 #ifdef __cplusplus
108 }
109 #endif
110 
111 #endif
The LED peripheral configuration.
Definition: periph_led.h:54
uint32_t led_freq_hz
Definition: periph_led.h:58
ledc_mode_t led_speed_mode
Definition: periph_led.h:55
ledc_timer_t led_timer_num
Definition: periph_led.h:57
ledc_timer_bit_t led_duty_resolution
Definition: periph_led.h:56