General code and comment cleanup

This commit is contained in:
2025-09-04 16:59:00 +03:00
parent 7244b57061
commit bf5e3a4426
25 changed files with 213 additions and 218 deletions

View File

@@ -28,8 +28,8 @@ static const char *TAG = "RELAY_CHN_CORE";
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
/*
* Run limit timer callback immediately dispatches a STOP command for the
/*
* Run limit timer callback immediately dispatches a STOP command for the
* relevant channel as soon as the run limit time times out
*/
static void relay_chn_run_limit_timer_cb(void* arg)
@@ -195,17 +195,17 @@ static void relay_chn_stop_prv(relay_chn_ctl_t *chn_ctl)
/**
* @brief The command issuer function.
*
* This function is the deciding logic for issuing a command to a relay channel. It evaluates
*
* This function is the deciding logic for issuing a command to a relay channel. It evaluates
* the current state of the channel before issuing the command. Then it decides whether to run
* the command immediately or wait for the opposite inertia time.
*
*
* The STOP command is an exception, it is always run immediately since it is safe in any case.
*
*
* Another special consideration is the FLIP command. If the channel is running, the FLIP command
* is issued after the channel is stopped. If the channel is stopped, the FLIP command is issued
* immediately.
*
*
* @param chn_ctl The relay channel to issue the command to.
* @param cmd The command to issue.
*/
@@ -214,7 +214,7 @@ void relay_chn_issue_cmd(relay_chn_ctl_t* chn_ctl, relay_chn_cmd_t cmd)
if (cmd == RELAY_CHN_CMD_NONE) {
return;
}
if (cmd == RELAY_CHN_CMD_STOP) {
if (chn_ctl->state == RELAY_CHN_STATE_STOPPED) {
return; // Do nothing if already stopped
@@ -232,7 +232,7 @@ void relay_chn_issue_cmd(relay_chn_ctl_t* chn_ctl, relay_chn_cmd_t cmd)
// If the channel is idle, run the command immediately
relay_chn_dispatch_cmd(chn_ctl, cmd);
break;
case RELAY_CHN_STATE_FORWARD_PENDING:
case RELAY_CHN_STATE_REVERSE_PENDING:
// The channel is already waiting for the opposite inertia time,
@@ -241,7 +241,7 @@ void relay_chn_issue_cmd(relay_chn_ctl_t* chn_ctl, relay_chn_cmd_t cmd)
relay_chn_dispatch_cmd(chn_ctl, cmd);
}
break;
case RELAY_CHN_STATE_STOPPED:
if (last_run_cmd == cmd || last_run_cmd == RELAY_CHN_CMD_NONE) {
// Since the state is STOPPED, the inertia timer should be running and must be invalidated
@@ -254,7 +254,7 @@ void relay_chn_issue_cmd(relay_chn_ctl_t* chn_ctl, relay_chn_cmd_t cmd)
relay_chn_dispatch_cmd(chn_ctl, cmd);
}
else {
// If the last run command is different from the current command, calculate the time passed
// If the last run command is different from the current command, calculate the time passed
// since the last run command stopped and decide whether to run the command immediately or wait
uint32_t last_run_cmd_time_ms = relay_chn_run_info_get_last_run_cmd_time_ms(chn_ctl->run_info);
uint32_t current_time_ms = (uint32_t)(esp_timer_get_time() / 1000);
@@ -281,7 +281,7 @@ void relay_chn_issue_cmd(relay_chn_ctl_t* chn_ctl, relay_chn_cmd_t cmd)
}
}
break;
case RELAY_CHN_STATE_FORWARD:
case RELAY_CHN_STATE_REVERSE:
if (cmd == RELAY_CHN_CMD_FLIP) {
@@ -290,18 +290,18 @@ void relay_chn_issue_cmd(relay_chn_ctl_t* chn_ctl, relay_chn_cmd_t cmd)
relay_chn_dispatch_cmd(chn_ctl, cmd);
return;
}
if (last_run_cmd == cmd) {
// If the last run command is the same as the current command, do nothing
return;
}
// Stop the channel first before the schedule
relay_chn_stop_prv(chn_ctl);
// If the last run command is different from the current command, wait for the opposite inertia time
chn_ctl->pending_cmd = cmd;
relay_chn_state_t new_state = cmd == RELAY_CHN_CMD_FORWARD
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_timer_or_idle(chn_ctl, chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS, "inertia");
@@ -366,9 +366,6 @@ static void relay_chn_execute_stop(relay_chn_ctl_t *chn_ctl)
{
relay_chn_stop_prv(chn_ctl);
// If there is any pending command, cancel it since the STOP command is issued right after it
// chn_ctl->pending_cmd = RELAY_CHN_CMD_NONE;
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
esp_timer_stop(chn_ctl->run_limit_timer);
#endif
@@ -382,8 +379,7 @@ static void relay_chn_execute_stop(relay_chn_ctl_t *chn_ctl)
chn_ctl->pending_cmd = RELAY_CHN_CMD_IDLE;
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);
// If the channel was not running forward or reverse, issue a free command immediately
relay_chn_execute_idle(chn_ctl);
}
}

View File

@@ -140,7 +140,7 @@ void relay_chn_ctl_stop_all()
void relay_chn_ctl_flip_direction(uint8_t chn_id)
{
if (relay_chn_is_channel_id_valid(chn_id))
relay_chn_issue_cmd(&chn_ctls[chn_id], RELAY_CHN_CMD_FLIP);
relay_chn_issue_cmd(&chn_ctls[chn_id], RELAY_CHN_CMD_FLIP);
}
void relay_chn_ctl_flip_direction_all()

View File

@@ -85,7 +85,7 @@ void relay_chn_ctl_stop()
void relay_chn_ctl_flip_direction()
{
relay_chn_issue_cmd(&chn_ctl, RELAY_CHN_CMD_FLIP);
relay_chn_issue_cmd(&chn_ctl, RELAY_CHN_CMD_FLIP);
}
relay_chn_direction_t relay_chn_ctl_get_direction()
@@ -106,13 +106,13 @@ void relay_chn_ctl_set_run_limit(uint16_t limit_sec)
limit_sec = CONFIG_RELAY_CHN_RUN_LIMIT_MAX_SEC;
else if (limit_sec < CONFIG_RELAY_CHN_RUN_LIMIT_MIN_SEC)
limit_sec = CONFIG_RELAY_CHN_RUN_LIMIT_MIN_SEC;
chn_ctl.run_limit_sec = limit_sec;
#if CONFIG_RELAY_CHN_ENABLE_NVS
relay_chn_nvs_set_run_limit(chn_ctl.id, limit_sec);
#endif
}
}
#endif
/* relay_chn APIs */

View File

@@ -127,7 +127,7 @@ static relay_chn_listener_entry_t* find_listener_entry(relay_chn_state_listener_
for (ListItem_t *pxListItem = listGET_HEAD_ENTRY(&listeners);
pxListItem != listGET_END_MARKER(&listeners);
pxListItem = listGET_NEXT(pxListItem)) {
relay_chn_listener_entry_t *entry = (relay_chn_listener_entry_t *) listGET_LIST_ITEM_OWNER(pxListItem);
if (entry->listener == listener) {
// Found the listener, return the entry

View File

@@ -81,7 +81,7 @@ esp_err_t relay_chn_nvs_init()
ESP_LOGE(TAG, "Failed to create deinit semaphore");
return ESP_ERR_NO_MEM;
}
nvs_queue_handle = xQueueCreate(RELAY_CHN_NVS_QUEUE_LEN, sizeof(relay_chn_nvs_msg_t));
if (!nvs_queue_handle) {
ESP_LOGE(TAG, "Failed to create NVS queue");
@@ -165,7 +165,7 @@ static esp_err_t relay_chn_nvs_task_set_direction(uint8_t ch, uint8_t direction)
esp_err_t relay_chn_nvs_get_direction(uint8_t ch, relay_chn_direction_t *direction, relay_chn_direction_t default_val)
{
ESP_RETURN_ON_FALSE(direction != NULL, ESP_ERR_INVALID_ARG, TAG, "Direction pointer is NULL");
uint8_t direction_val;
esp_err_t ret = nvs_get_u8(relay_chn_nvs, RELAY_CHN_KEY_DIR, &direction_val);
if (ret == ESP_ERR_NVS_NOT_FOUND) {
@@ -295,7 +295,7 @@ static esp_err_t relay_chn_nvs_task_set_tilt_count(uint8_t ch, uint16_t tilt_cou
esp_err_t relay_chn_nvs_get_tilt_count(uint8_t ch, uint16_t *tilt_count, uint16_t default_val)
{
ESP_RETURN_ON_FALSE(tilt_count != NULL, ESP_ERR_INVALID_ARG, TAG, "Counter pointers are NULL");
esp_err_t ret;
#if CONFIG_RELAY_CHN_COUNT > 1
char key[NVS_KEY_NAME_MAX_SIZE];

View File

@@ -50,7 +50,7 @@ static esp_err_t relay_chn_output_ctl_init(relay_chn_output_t *output,
"Invalid GPIO pin number for forward_pin: %d", forward_pin);
ESP_RETURN_ON_FALSE(GPIO_IS_VALID_OUTPUT_GPIO(reverse_pin), ESP_ERR_INVALID_ARG, TAG,
"Invalid GPIO pin number for reverse_pin: %d", reverse_pin);
// Check if the GPIOs are valid
esp_err_t ret;
// Initialize the GPIOs
@@ -58,7 +58,7 @@ static esp_err_t relay_chn_output_ctl_init(relay_chn_output_t *output,
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to reset GPIO forward pin: %d", forward_pin);
ret = gpio_set_direction(forward_pin, GPIO_MODE_OUTPUT);
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to set GPIO direction for forward pin: %d", forward_pin);
ret = gpio_reset_pin(reverse_pin);
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to reset GPIO reverse pin: %d", reverse_pin);
ret = gpio_set_direction(reverse_pin, GPIO_MODE_OUTPUT);
@@ -75,8 +75,8 @@ static esp_err_t relay_chn_output_ctl_init(relay_chn_output_t *output,
#if CONFIG_RELAY_CHN_ENABLE_NVS
static esp_err_t relay_chn_output_load_direction(uint8_t ch, relay_chn_direction_t *direction)
{
// relay_chn_nvs_get_direction handles the NOT_FOUND case and returns the provided default value.
esp_err_t ret = relay_chn_nvs_get_direction(ch, direction, RELAY_CHN_DIRECTION_DEFAULT);
// relay_chn_nvs_get_direction now handles the NOT_FOUND case and returns a default value.
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to get direction from storage for channel %d: %s", ch, esp_err_to_name(ret));
return ESP_OK;
}
@@ -87,7 +87,7 @@ esp_err_t relay_chn_output_init(const uint8_t* gpio_map, uint8_t gpio_count)
esp_err_t ret;
ret = relay_chn_output_check_gpio_capabilities(gpio_count);
ESP_RETURN_ON_ERROR(ret, TAG, "Device does not support the provided GPIOs");
#if CONFIG_RELAY_CHN_COUNT > 1
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
relay_chn_output_t* output = &outputs[i];
@@ -185,8 +185,8 @@ void relay_chn_output_flip(relay_chn_output_t *output)
output->forward_pin = output->reverse_pin;
output->reverse_pin = temp;
// Flip the direction
output->direction = (output->direction == RELAY_CHN_DIRECTION_DEFAULT)
? RELAY_CHN_DIRECTION_FLIPPED
output->direction = (output->direction == RELAY_CHN_DIRECTION_DEFAULT)
? RELAY_CHN_DIRECTION_FLIPPED
: RELAY_CHN_DIRECTION_DEFAULT;
#if CONFIG_RELAY_CHN_ENABLE_NVS

View File

@@ -22,7 +22,7 @@ static const char *TAG = "RELAY_CHN_TILT";
/**@{*/
/*
* Tilt Pattern Timing Definitions
*
*
* The min and max timing definitions as well as the default timing definitions.
* These definitions are used to define and adjust the tilt sensitivity.
*/
@@ -85,7 +85,7 @@ static uint32_t relay_chn_tilt_get_required_timing_before_tilting(relay_chn_tilt
return 0;
else if (cmd == RELAY_CHN_TILT_CMD_REVERSE && last_run_cmd == RELAY_CHN_CMD_FORWARD)
return 0;
uint32_t last_run_cmd_time_ms = relay_chn_run_info_get_last_run_cmd_time_ms(tilt_ctl->chn_ctl->run_info);
uint32_t inertia_time_passed_ms = (uint32_t) (esp_timer_get_time() / 1000) - last_run_cmd_time_ms;
return CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS - inertia_time_passed_ms;
@@ -115,13 +115,13 @@ static void relay_chn_tilt_issue_cmd(relay_chn_tilt_ctl_t *tilt_ctl, relay_chn_t
relay_chn_tilt_dispatch_cmd(tilt_ctl, cmd);
return;
}
if (relay_chn_run_info_get_last_run_cmd(tilt_ctl->chn_ctl->run_info) == RELAY_CHN_CMD_NONE) {
// Do not tilt if the channel hasn't been run before
ESP_LOGD(TAG, "relay_chn_tilt_issue_cmd: Tilt will not be executed since the channel hasn't been run yet");
return;
}
if (tilt_ctl->cmd == cmd) {
ESP_LOGD(TAG, "relay_chn_tilt_issue_cmd: There is already a tilt command in progress!");
return;
@@ -134,7 +134,7 @@ static void relay_chn_tilt_issue_cmd(relay_chn_tilt_ctl_t *tilt_ctl, relay_chn_t
// Relay channel is free, tilt can be issued immediately
relay_chn_tilt_dispatch_cmd(tilt_ctl, cmd);
break;
case RELAY_CHN_STATE_FORWARD_PENDING:
case RELAY_CHN_STATE_REVERSE_PENDING:
// Issue a stop command first so that the timer and pending cmd get cleared
@@ -322,7 +322,7 @@ static void relay_chn_tilt_compute_set_sensitivity(relay_chn_tilt_ctl_t *tilt_ct
uint32_t tilt_run_time_ms = 0, tilt_pause_time_ms = 0;
tilt_run_time_ms = RELAY_CHN_TILT_RUN_MIN_MS + (sensitivity * (RELAY_CHN_TILT_RUN_MAX_MS - RELAY_CHN_TILT_RUN_MIN_MS) / 100);
tilt_pause_time_ms = RELAY_CHN_TILT_PAUSE_MIN_MS + (sensitivity * (RELAY_CHN_TILT_PAUSE_MAX_MS - RELAY_CHN_TILT_PAUSE_MIN_MS) / 100);
relay_chn_tilt_set_timing_values(&tilt_ctl->tilt_timing,
sensitivity,
tilt_run_time_ms,
@@ -336,7 +336,7 @@ void relay_chn_tilt_set_sensitivity(uint8_t chn_id, uint8_t sensitivity)
if (relay_chn_is_channel_id_valid(chn_id)) {
ADJUST_TILT_SENS_BOUNDARIES(sensitivity);
relay_chn_tilt_compute_set_sensitivity(&tilt_ctls[chn_id], sensitivity);
#if CONFIG_RELAY_CHN_ENABLE_NVS
relay_chn_nvs_set_tilt_sensitivity(chn_id, sensitivity);
#endif // CONFIG_RELAY_CHN_ENABLE_NVS
@@ -346,7 +346,7 @@ void relay_chn_tilt_set_sensitivity(uint8_t chn_id, uint8_t sensitivity)
esp_err_t relay_chn_tilt_set_sensitivity_all(uint8_t *sensitivities)
{
ESP_RETURN_ON_FALSE(sensitivities != NULL, ESP_ERR_INVALID_ARG, TAG, "set_sensitivity_all: sensitivities is NULL");
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
uint8_t *src_sensitivity = &sensitivities[i];
if (src_sensitivity == NULL) {
@@ -382,7 +382,7 @@ uint8_t relay_chn_tilt_get_sensitivity(uint8_t chn_id)
esp_err_t relay_chn_tilt_get_sensitivity_all(uint8_t *sensitivities)
{
ESP_RETURN_ON_FALSE(sensitivities != NULL, ESP_ERR_INVALID_ARG, TAG, "get_sensitivity_all: sensitivities is NULL");
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
uint8_t *dest_sensitivity = &sensitivities[i];
if (dest_sensitivity == NULL) {
@@ -400,7 +400,7 @@ void relay_chn_tilt_set_sensitivity(uint8_t sensitivity)
{
ADJUST_TILT_SENS_BOUNDARIES(sensitivity);
relay_chn_tilt_compute_set_sensitivity(&tilt_ctl, sensitivity);
#if CONFIG_RELAY_CHN_ENABLE_NVS
relay_chn_nvs_set_tilt_sensitivity(0, sensitivity);
#endif // CONFIG_RELAY_CHN_ENABLE_NVS
@@ -423,25 +423,25 @@ void relay_chn_tilt_reset_count(relay_chn_tilt_ctl_t *tilt_ctl)
/**
* @brief Update tilt count automatically and return the current value.
*
*
* This helper function updates the relevant tilt count depending on the
* last run info and helps the tilt module in deciding whether the requested
* tilt should execute or not.
*
*
* This is useful to control reverse tilting for the same direction particularly.
* For example:
* - If the channel's last run was FORWARD and a TILT_FORWARD is requested,
* then the tilt count will count up on the relay_chn_tilt_ctl_t::tilt_count
* then the tilt count will count up on the relay_chn_tilt_ctl_t::tilt_count
* and the function will return the actual count.
* - If the channel's last run was FORWARD and a TILT_REVERSE is requested,
* then the relay_chn_tilt_ctl_t::tilt_count will be checked against zero first,
* and then it will count down and return the actual count if it is greater
* then the relay_chn_tilt_ctl_t::tilt_count will be checked against zero first,
* and then it will count down and return the actual count if it is greater
* than 0, else the function will return 0.
* - If the tilt command is irrelevant then the function will return 0.
* - If the last run is irrelevant then the function will return 0.
*
*
* @param tilt_ctl The relay channel handle.
*
*
* @return The actual value of the relevant count.
* @return 1 if the last tilt_count was 1 and decremented to 0.
* @return 0 if:
@@ -612,7 +612,7 @@ static void relay_chn_tilt_timer_cb(void *arg)
{
relay_chn_tilt_ctl_t* tilt_ctl = (relay_chn_tilt_ctl_t*) arg;
ESP_RETURN_VOID_ON_FALSE(tilt_ctl != NULL, TAG, "relay_chn_tilt_timer_cb: timer arg is NULL");
switch (tilt_ctl->step)
{
case RELAY_CHN_TILT_STEP_MOVE:
@@ -632,7 +632,7 @@ static void relay_chn_tilt_timer_cb(void *arg)
// Just dispatch the pending tilt command
relay_chn_tilt_dispatch_cmd(tilt_ctl, tilt_ctl->cmd);
break;
default:
break;
}
@@ -667,7 +667,7 @@ static esp_err_t relay_chn_tilt_ctl_init(relay_chn_tilt_ctl_t *tilt_ctl,
tilt_ctl->chn_ctl = chn_ctl;
tilt_ctl->chn_ctl->tilt_ctl = tilt_ctl;
// Create tilt timer for the channel
char timer_name[32];
snprintf(timer_name, sizeof(timer_name), "relay_chn_%2d_tilt_timer", chn_ctl->id);
@@ -694,7 +694,7 @@ esp_err_t relay_chn_tilt_init(relay_chn_ctl_t *chn_ctls)
{
uint8_t sensitivity;
uint16_t tilt_count;
#if CONFIG_RELAY_CHN_COUNT > 1
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
esp_err_t ret;