-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiot_light.h
233 lines (202 loc) · 7.19 KB
/
iot_light.h
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
// Copyright 2020 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __IOT_LIGHT_H__
#define __IOT_LIGHT_H__
#ifdef __cplusplus
extern "C" {
#endif
#include "driver/ledc.h"
/********************************** NOTE *********************************/
/* When we create a light object, a hardware timer will be enabled, this */
/* timer is used to realize fade and blink operation. The default timer */
/* occupied is timer 0 of timer group 0, user can change this config in */
/* menuconfig. */
/*************************************************************************/
#define LIGHTS_LED_NUM (3)
#define LIGHTS_GPIO_LED_RED GPIO_NUM_25
#define LIGHTS_GPIO_LED_GREEN GPIO_NUM_19
#define LIGHTS_GPIO_LED_BLUE GPIO_NUM_5
typedef void *light_handle_t;
#define HW_TIMER_GROUP (0) /**< Hardware timer group */
#define HW_TIMER_ID (0) /**< Hardware timer number */
#define HW_TIMER_DIVIDER (16) /**< Hardware timer clock divider */
#define HW_TIMER_SCALE (TIMER_BASE_CLK / HW_TIMER_DIVIDER) /**< Convert counter value to seconds */
#define DUTY_SET_CYCLE (20) /**< Set duty cycle */
#define DUTY_SET_GAMMA (0.6) /**< Set the Gamma value for the fade curve, default value is 0.6 */
#define LIGHT_MAX_CHANNEL_NUM (5)
#define MDF_LOG_LEVEL ESP_LOG_DEBUG
#define MDF_LOG_FORMAT(letter, format) LOG_COLOR_ ## letter #letter " (%u) [%s, %d]: " format LOG_RESET_COLOR "\n"
#define MDF_LOGW( format, ... ) do { \
if (MDF_LOG_LEVEL >= ESP_LOG_WARN) { \
esp_log_write(ESP_LOG_WARN, TAG, MDF_LOG_FORMAT(W, format), esp_log_timestamp(), TAG, __LINE__, ##__VA_ARGS__); \
} \
} while(0)
/**
* Macro which can be used to check the error code,
* and terminate the program in case the code is not MDF_OK.
* Prints the error code, error location, and the failed statement to serial output.
*
* Disabled if assertions are disabled.
*/
#define MDF_ERROR_CHECK(con, err, format, ...) do { \
if (con) { \
if(*format != '\0') \
MDF_LOGW("<%s> " format, esp_err_to_name(err), ##__VA_ARGS__); \
return err; \
} \
} while(0)
/**
* @brief light initialize
*
* @param timer the LEDC timer used by light
* @param speed_mode speed mode of LEDC timer
* @param freq_hz frequency of LEDC timer
* @param channel_num decide how many channels the light contains
* @param timer_bit LEDC PWM duty resolution
*
* @return the handle of light
*/
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);
/**
* @brief add an output channel to light
*
* @param light_handle light handle
* @param channel_idx the id of channel (0 ~ channel_num-1)
* @param io_num the IO number use to output LEDC PWM
* @param channel the ledc channel you want to use
*
* @return
* - ESP_OK: succeed
* - others: fail
*/
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);
/**
* @brief free the memory of light
*
* @param light_handle light handle
*
* @return
* - ESP_OK: succeed
* - others: fail
*/
esp_err_t iot_light_delete(light_handle_t light_handle);
/**
* @brief get channel duty
*
* @param light_handle light handle
* @param channel_id the id of channel (0 ~ channel_num-1)
*
* @return
* - LEDC_ERR_DUTY if parameter error
* - Others Current LEDC duty
*/
uint32_t iot_light_duty_get(light_handle_t light_handle, uint8_t channel_id);
/**
* @brief set light fade with time. if set fade_period_ms as 0, set the duty directly.
*
* @param light_handle light handle
* @param channel_id the id of channel (0 ~ channel_num-1)
* @param duty target duty
* @param fade_period_ms fade time (uint: ms)
*
* @return
* - ESP_OK: succeed
* - others: fail
*/
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);
/**
* @brief set breath config of a light channel, call `iot_light_operate_start` to start breath operation
*
* @param light_handle light handle
* @param channel_id the id of channel (0 ~ channel_num-1)
* @param duty the maximum duty when breath
* @param breath_period_ms breath period (uint: ms)
*
* @return
* - ESP_OK: succeed
* - others: fail
*/
esp_err_t iot_light_breath_config(light_handle_t light_handle, uint8_t channel_id, uint32_t duty, uint32_t breath_period_ms);
/**
* @brief set blink config of a light channel, call `iot_light_operate_start` to start blink operation
*
* @param light_handle light handle
* @param channel_id the id of channel (0 ~ channel_num-1)
* @param blink_period_ms blink period (uint: ms)
*
* @return
* - ESP_OK: succeed
* - others: fail
*/
esp_err_t iot_light_blink_config(light_handle_t light_handle, uint8_t channel_id, uint32_t blink_period_ms);
/**
* @brief start breath or blink operation, user need to set breath or blink config before call this API
*
* @param light_handle light handle
* @param channel_id the id of channel (0 ~ channel_num-1)
*
* @return
* - ESP_OK: succeed
* - others: fail
*/
esp_err_t iot_light_operate_start(light_handle_t light_handle, uint8_t channel_id);
/**
* @brief stop breath or blink operation
*
* @param light_handle light handle
* @param channel_id the id of channel (0 ~ channel_num-1)
*
* @return
* - ESP_OK: succeed
* - others: fail
*/
esp_err_t iot_light_operate_stop(light_handle_t light_handle, uint8_t channel_id);
// user functions
/**
* @brief Set the color of the light
*
* @param red Red led brightness, the range is: 0 ~ 255
* @param green Green led brightness, the range is: 0 ~ 255
* @param blue Blue led brightness, the range is: 0 ~ 255
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t led_set_rgb(uint8_t red, uint8_t green, uint8_t blue);
/**
* [led_blink_start description]
*
* @param red Red led brightness, the range is: 0 ~ 255
* @param green Green led brightness, the range is: 0 ~ 255
* @param blue Blue led brightness, the range is: 0 ~ 255
* @param period_ms breath period (uint: ms)
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t led_blink_start(uint8_t red, uint8_t green, uint8_t blue, uint32_t period_ms);
/**
* @brief Stop the blink of led
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t led_blink_stop();
void lights_init( void );
#ifdef __cplusplus
}
#endif
#endif /**< __IOT_LIGHT_H__ */