Skip to content

Commit aaae173

Browse files
committed
Merge branch 'feature/use_zcd_selectively' into 'master'
feat: add pause comand to zero_detect component See merge request ae_group/esp-iot-solution!1191
2 parents 1719f11 + 0f327a2 commit aaae173

File tree

5 files changed

+76
-1
lines changed

5 files changed

+76
-1
lines changed

components/zero_detection/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# ChangeLog
22

3+
## v0.0.7 - 2025-1-2
4+
5+
### Enhancements:
6+
7+
- add pause command to zero_detect component
8+
39
## v0.0.6 - 2024-9-19
410

511
### Bug Fix:

components/zero_detection/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "0.0.6"
1+
version: "0.0.7"
22
description: Zero Cross Detection Driver
33
url: https://github.com/espressif/esp-iot-solution/tree/master/components/zero_detection
44
repository: https://github.com/espressif/esp-iot-solution.git

components/zero_detection/include/zero_detection.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,20 @@ zero_detect_handle_t zero_detect_create(zero_detect_config_t *config);
166166
*/
167167
void zero_show_data(zero_detect_handle_t zcd_handle);
168168

169+
/**
170+
* @brief Pause zero detect
171+
*
172+
* @param zcd_handle A zero detect handle
173+
*/
174+
void zero_detect_pause(zero_detect_handle_t zcd_handle);
175+
176+
/**
177+
* @brief Resume zero detect
178+
*
179+
* @param zcd_handle A zero detect handle
180+
*/
181+
void zero_detect_resume(zero_detect_handle_t zcd_handle);
182+
169183
/**
170184
* @brief Delete a zero detect device
171185
*

components/zero_detection/zero_detection.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ typedef struct zero_cross {
2828

2929
bool zero_source_power_invalid; //Power loss flag when signal source is lost
3030
bool zero_singal_invaild; //Signal is in an invalid range
31+
bool is_paused; //Pause flag for zero cross detection
3132

3233
zero_signal_type_t zero_signal_type; //Zero crossing signal type
3334
zero_driver_type_t zero_driver_type; //Zero crossing driver type
@@ -56,6 +57,9 @@ typedef struct zero_cross {
5657
static void IRAM_ATTR zero_cross_handle_interrupt(void *user_data, const mcpwm_capture_event_data_t *edata)
5758
{
5859
zero_cross_dev_t *zero_cross_dev = user_data;
60+
if (zero_cross_dev->is_paused) {
61+
return;
62+
}
5963
int gpio_status = 0;
6064
if (zero_cross_dev->zero_driver_type == GPIO_TYPE) {
6165
//Retrieve the current GPIO level and determine the rising or falling edge
@@ -188,6 +192,44 @@ static void IRAM_ATTR zero_detect_gpio_cb(void *arg)
188192
zero_cross_handle_interrupt(arg, &edata);
189193
}
190194

195+
void zero_detect_pause(zero_detect_handle_t zcd_handle)
196+
{
197+
if (zcd_handle == NULL) {
198+
ESP_LOGE(TAG, "ERROR: zcd_handle is NULL");
199+
return;
200+
}
201+
zero_cross_dev_t *zcd = (zero_cross_dev_t *)zcd_handle;
202+
zcd->is_paused = true;
203+
204+
#if defined(SOC_MCPWM_SUPPORTED)
205+
if (zcd->zero_driver_type == MCPWM_TYPE) {
206+
mcpwm_capture_channel_disable(zcd->cap_chan);
207+
}
208+
#endif
209+
if (zcd->zero_driver_type == GPIO_TYPE) {
210+
gpio_isr_handler_remove(zcd->capture_pin);
211+
}
212+
}
213+
214+
void zero_detect_resume(zero_detect_handle_t zcd_handle)
215+
{
216+
if (zcd_handle == NULL) {
217+
ESP_LOGE(TAG, "ERROR: zcd_handle is NULL");
218+
return;
219+
}
220+
zero_cross_dev_t *zcd = (zero_cross_dev_t *)zcd_handle;
221+
zcd->is_paused = false;
222+
223+
#if defined(SOC_MCPWM_SUPPORTED)
224+
if (zcd->zero_driver_type == MCPWM_TYPE) {
225+
mcpwm_capture_channel_enable(zcd->cap_chan);
226+
}
227+
#endif
228+
if (zcd->zero_driver_type == GPIO_TYPE) {
229+
gpio_isr_handler_add(zcd->capture_pin, zero_detect_gpio_cb, zcd);
230+
}
231+
}
232+
191233
#if defined(CONFIG_USE_GPTIMER)
192234
static IRAM_ATTR bool zero_source_power_invalid_cb(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx)
193235
{

examples/zero_cross_detection/main/zero_detect_example_main.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,19 @@ void app_main(void)
162162
vTaskDelay(500 / portTICK_PERIOD_MS);
163163
zero_show_data(g_zcds); //Show zero cross data
164164
printf("EVENT: OUT OF RANGE COUNT:%d OFF COUNT:%d OPEN COUNT:%d\n", freq_out_of_range_count, relay_off_count, relay_open_count);
165+
166+
// Test pause and resume function
167+
if (freq_out_of_range_count > 5) {
168+
ESP_LOGI(TAG, "Pausing zero detection......");
169+
zero_detect_pause(g_zcds);
170+
}
171+
172+
if (relay_open_count > 5) {
173+
ESP_LOGI(TAG, "Resuming zero detection......");
174+
zero_detect_resume(g_zcds);
175+
relay_open_count = 0;
176+
}
177+
165178
if (zcd.relay_suspend) {
166179
ESP_LOGW(TAG, "Process suspened, please wait till relay open");
167180
}

0 commit comments

Comments
 (0)