diff --git a/src/relay_chn_core.c b/src/relay_chn_core.c index 4119062..a1ae5a4 100644 --- a/src/relay_chn_core.c +++ b/src/relay_chn_core.c @@ -263,6 +263,18 @@ void relay_chn_update_state(relay_chn_ctl_t *chn_ctl, relay_chn_state_t new_stat } } +static void relay_chn_execute_idle(relay_chn_ctl_t *chn_ctl); + +static void relay_chn_start_timer_or_idle(relay_chn_ctl_t *chn_ctl, esp_timer_handle_t timer, uint32_t time_ms, const char* timer_name) +{ + if (relay_chn_start_esp_timer_once(timer, time_ms) != ESP_OK) { + ESP_LOGE(TAG, "Failed to start %s timer for ch %d", timer_name, chn_ctl->id); + // Attempt to go to a safe state. + // relay_chn_execute_idle is safe to call, it stops timers and sets state. + relay_chn_execute_idle(chn_ctl); + } +} + /** * @brief The command issuer function. * @@ -374,7 +386,7 @@ void relay_chn_issue_cmd(relay_chn_ctl_t* chn_ctl, relay_chn_cmd_t cmd) relay_chn_state_t new_state = cmd == RELAY_CHN_CMD_FORWARD ? RELAY_CHN_STATE_FORWARD_PENDING : RELAY_CHN_STATE_REVERSE_PENDING; relay_chn_update_state(chn_ctl, new_state); - relay_chn_start_esp_timer_once(chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS); + relay_chn_start_timer_or_idle(chn_ctl, chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS, "inertia"); break; #if CONFIG_RELAY_CHN_ENABLE_TILTING @@ -456,12 +468,13 @@ static void relay_chn_execute_stop(relay_chn_ctl_t *chn_ctl) relay_chn_run_info_set_last_run_cmd_time_ms(chn_ctl->run_info, (uint32_t)(esp_timer_get_time() / 1000)); // Schedule a free command for the channel chn_ctl->pending_cmd = RELAY_CHN_CMD_IDLE; - relay_chn_start_esp_timer_once(chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS); + relay_chn_start_timer_or_idle(chn_ctl, chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS, "idle"); } else { // If the channel was not running one of the run or fwd, issue a free command immediately // relay_chn_dispatch_cmd(chn_ctl, RELAY_CHN_CMD_IDLE); relay_chn_execute_idle(chn_ctl); } + ESP_LOGI(TAG, "execute_stop: #%d, state: %d", chn_ctl->id, chn_ctl->state); // TODO delete } static void relay_chn_execute_forward(relay_chn_ctl_t *chn_ctl) @@ -474,7 +487,7 @@ static void relay_chn_execute_forward(relay_chn_ctl_t *chn_ctl) relay_chn_update_state(chn_ctl, RELAY_CHN_STATE_FORWARD); #if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT - relay_chn_start_esp_timer_once(chn_ctl->run_limit_timer, chn_ctl->run_limit_sec * 1000); + relay_chn_start_timer_or_idle(chn_ctl, chn_ctl->run_limit_timer, chn_ctl->run_limit_sec * 1000, "run limit"); #endif } @@ -488,7 +501,7 @@ static void relay_chn_execute_reverse(relay_chn_ctl_t *chn_ctl) relay_chn_update_state(chn_ctl, RELAY_CHN_STATE_REVERSE); #if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT - relay_chn_start_esp_timer_once(chn_ctl->run_limit_timer, chn_ctl->run_limit_sec * 1000); + relay_chn_start_timer_or_idle(chn_ctl, chn_ctl->run_limit_timer, chn_ctl->run_limit_sec * 1000, "run limit"); #endif } @@ -497,7 +510,7 @@ static void relay_chn_execute_flip(relay_chn_ctl_t *chn_ctl) relay_chn_output_flip(chn_ctl->output); // Set an inertia on the channel to prevent any immediate movement chn_ctl->pending_cmd = RELAY_CHN_CMD_IDLE; - relay_chn_start_esp_timer_once(chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS); + relay_chn_start_timer_or_idle(chn_ctl, chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS, "flip inertia"); } // Dispatch relay channel command diff --git a/src/relay_chn_tilt.c b/src/relay_chn_tilt.c index f07e36f..98c7c7c 100644 --- a/src/relay_chn_tilt.c +++ b/src/relay_chn_tilt.c @@ -91,6 +91,18 @@ static uint32_t relay_chn_tilt_get_required_timing_before_tilting(relay_chn_tilt return CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS - inertia_time_passed_ms; } +static void relay_chn_tilt_execute_stop(relay_chn_tilt_ctl_t *tilt_ctl); + +static void relay_chn_tilt_start_timer_or_stop(relay_chn_tilt_ctl_t *tilt_ctl, esp_timer_handle_t timer, uint32_t time_ms, const char* timer_name) +{ + if (relay_chn_start_esp_timer_once(timer, time_ms) != ESP_OK) { + ESP_LOGE(TAG, "Failed to start %s timer for ch %d", timer_name, tilt_ctl->chn_ctl->id); + // Attempt to go to a safe state for tilt. + // relay_chn_tilt_execute_stop is safe to call, it stops timers and sets state. + relay_chn_tilt_execute_stop(tilt_ctl); + } +} + // Issue a tilt command to a specific relay channel. static void relay_chn_tilt_issue_cmd(relay_chn_tilt_ctl_t *tilt_ctl, relay_chn_tilt_cmd_t cmd) { @@ -136,7 +148,7 @@ static void relay_chn_tilt_issue_cmd(relay_chn_tilt_ctl_t *tilt_ctl, relay_chn_t } else { // Channel needs timing before running tilting action, schedule it tilt_ctl->step = RELAY_CHN_TILT_STEP_PENDING; - relay_chn_start_esp_timer_once(tilt_ctl->tilt_timer, req_timing_ms); + relay_chn_tilt_start_timer_or_stop(tilt_ctl, tilt_ctl->tilt_timer, req_timing_ms, "pending tilt"); } break; } @@ -147,7 +159,7 @@ static void relay_chn_tilt_issue_cmd(relay_chn_tilt_ctl_t *tilt_ctl, relay_chn_t relay_chn_dispatch_cmd(tilt_ctl->chn_ctl, RELAY_CHN_CMD_STOP); // Schedule for tilting tilt_ctl->step = RELAY_CHN_TILT_STEP_PENDING; - relay_chn_start_esp_timer_once(tilt_ctl->tilt_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS); + relay_chn_tilt_start_timer_or_stop(tilt_ctl, tilt_ctl->tilt_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS, "tilt inertia"); } else if (cmd == RELAY_CHN_TILT_CMD_REVERSE) { // Stop the running channel first relay_chn_dispatch_cmd(tilt_ctl->chn_ctl, RELAY_CHN_CMD_STOP); @@ -162,7 +174,7 @@ static void relay_chn_tilt_issue_cmd(relay_chn_tilt_ctl_t *tilt_ctl, relay_chn_t relay_chn_dispatch_cmd(tilt_ctl->chn_ctl, RELAY_CHN_CMD_STOP); // Schedule for tilting tilt_ctl->step = RELAY_CHN_TILT_STEP_PENDING; - relay_chn_start_esp_timer_once(tilt_ctl->tilt_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS); + relay_chn_tilt_start_timer_or_stop(tilt_ctl, tilt_ctl->tilt_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS, "tilt inertia"); } else if (cmd == RELAY_CHN_TILT_CMD_FORWARD) { // Stop the running channel first relay_chn_dispatch_cmd(tilt_ctl->chn_ctl, RELAY_CHN_CMD_STOP); @@ -507,7 +519,10 @@ static void relay_chn_tilt_execute_stop(relay_chn_tilt_ctl_t *tilt_ctl) #if CONFIG_RELAY_CHN_ENABLE_NVS // Start the flush debounce timer - relay_chn_start_esp_timer_once(tilt_ctl->flush_timer, RELAY_CHN_TILT_FLUSH_DEBOUNCE_MS); + if (relay_chn_start_esp_timer_once(tilt_ctl->flush_timer, RELAY_CHN_TILT_FLUSH_DEBOUNCE_MS) != ESP_OK) { + ESP_LOGE(TAG, "Failed to start tilt flush timer for ch %d", tilt_ctl->chn_ctl->id); + // This is not a critical failure, just log it. The count will be saved on next stop. + } #endif } @@ -520,7 +535,7 @@ static void relay_chn_tilt_execute_forward(relay_chn_tilt_ctl_t *tilt_ctl) return; } // Set the move time timer - relay_chn_start_esp_timer_once(tilt_ctl->tilt_timer, tilt_ctl->tilt_timing.move_time_ms); + relay_chn_tilt_start_timer_or_stop(tilt_ctl, tilt_ctl->tilt_timer, tilt_ctl->tilt_timing.move_time_ms, "tilt move"); // Set to pause step tilt_ctl->step = RELAY_CHN_TILT_STEP_PAUSE; } @@ -534,7 +549,7 @@ static void relay_chn_tilt_execute_reverse(relay_chn_tilt_ctl_t *tilt_ctl) return; } // Set the move time timer - relay_chn_start_esp_timer_once(tilt_ctl->tilt_timer, tilt_ctl->tilt_timing.move_time_ms); + relay_chn_tilt_start_timer_or_stop(tilt_ctl, tilt_ctl->tilt_timer, tilt_ctl->tilt_timing.move_time_ms, "tilt move"); // Set to pause step tilt_ctl->step = RELAY_CHN_TILT_STEP_PAUSE; } @@ -558,7 +573,7 @@ static void relay_chn_tilt_execute_pause(relay_chn_tilt_ctl_t *tilt_ctl) } // Set the pause time timer - relay_chn_start_esp_timer_once(tilt_ctl->tilt_timer, tilt_ctl->tilt_timing.pause_time_ms); + relay_chn_tilt_start_timer_or_stop(tilt_ctl, tilt_ctl->tilt_timer, tilt_ctl->tilt_timing.pause_time_ms, "tilt pause"); // Set to move step tilt_ctl->step = RELAY_CHN_TILT_STEP_MOVE; }