Optimize and refactor tilt counting

- Optimized tilt counting data by reducing the tilt counter variables into one for smaller memory footprint. So the `relay_chn_tilt_counter_t` type is replaced by a single `uint16_t` variable in the `relay_chn_tilt_ctl_t` structure. Hence the `relay_chn_tilt_counter_t` type has been removed since it is not necessary anymore.
- Refactored tilt count handling in NVS: consolidate forward and reverse counts into a single tilt count parameter.
- Updated NVS test files that affected by the data type and function signature changes.

Fixes #1079
This commit is contained in:
2025-08-20 10:41:30 +03:00
parent dc2aa93d2d
commit aeeda44a2c
5 changed files with 76 additions and 110 deletions

View File

@@ -57,19 +57,13 @@ typedef struct {
uint32_t pause_time_ms; /*!< Pause time in milliseconds */
} relay_chn_tilt_timing_t;
/// @brief Tilt counter structure to manage tilt count.
typedef struct {
uint32_t tilt_forward_count; /*!< Tilt forward count */
uint32_t tilt_reverse_count; /*!< Tilt reverse count */
} relay_chn_tilt_counter_t;
/// @brief Tilt control structure to manage tilt operations.
typedef struct relay_chn_tilt_ctl {
relay_chn_ctl_t *chn_ctl; /*!< The relay channel control structure */
relay_chn_tilt_cmd_t cmd; /*!< The tilt command in process */
relay_chn_tilt_step_t step; /*!< Current tilt step */
relay_chn_tilt_timing_t tilt_timing; /*!< Tilt timing structure */
relay_chn_tilt_counter_t tilt_counter; /*!< Tilt counter structure */
uint16_t tilt_count; /*!< Tilt count to manage forward and reverse tilts */
esp_timer_handle_t tilt_timer; /*!< Tilt timer handle */
#if RELAY_CHN_ENABLE_NVS == 1
esp_timer_handle_t flush_timer; /*!< Flush timer to avoid frequent write of tilt counters */
@@ -401,8 +395,7 @@ uint8_t relay_chn_tilt_get_sensitivity()
void relay_chn_tilt_reset_count(relay_chn_tilt_ctl_t *tilt_ctl)
{
tilt_ctl->tilt_counter.tilt_forward_count = 0;
tilt_ctl->tilt_counter.tilt_reverse_count = 0;
tilt_ctl->tilt_count = 0;
#if RELAY_CHN_ENABLE_NVS == 1
esp_timer_stop(tilt_ctl->flush_timer);
@@ -415,74 +408,70 @@ void relay_chn_tilt_reset_count(relay_chn_tilt_ctl_t *tilt_ctl)
* 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 particularly. For example:
*
* 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 counter will count up on the
* relay_chn_tilt_counter_type::tilt_forward_count and the function will
* return the actual 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_counter_type::tilt_forward_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.
* 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 uint32_t The actual value of the relevant counter.
*
* @return The actual value of the relevant count.
* @return 1 if the last tilt_count was 1 and decremented to 0.
* @return 0 if:
* - related counter is already 0.
* - related count is already 0.
* - tilt command is irrelevant.
* - last run info is irrelevant.
*/
static uint32_t relay_chn_tilt_count_update(relay_chn_tilt_ctl_t *tilt_ctl)
static uint16_t relay_chn_tilt_count_update(relay_chn_tilt_ctl_t *tilt_ctl)
{
relay_chn_cmd_t last_run_cmd = relay_chn_run_info_get_last_run_cmd(tilt_ctl->chn_ctl->run_info);
if (last_run_cmd == RELAY_CHN_CMD_FORWARD) {
if (tilt_ctl->cmd == RELAY_CHN_TILT_CMD_FORWARD) {
return ++tilt_ctl->tilt_counter.tilt_forward_count;
return ++tilt_ctl->tilt_count;
}
else if (tilt_ctl->cmd == RELAY_CHN_TILT_CMD_REVERSE) {
if (tilt_ctl->tilt_counter.tilt_forward_count > 0) {
--tilt_ctl->tilt_counter.tilt_forward_count;
if (tilt_ctl->tilt_count > 0) {
--tilt_ctl->tilt_count;
// Still should do one more move, return non-zero value
return 1;
}
else
return 0;
}
else {
relay_chn_tilt_reset_count(tilt_ctl);
return 0;
}
}
else if (last_run_cmd == RELAY_CHN_CMD_REVERSE) {
if (tilt_ctl->cmd == RELAY_CHN_TILT_CMD_REVERSE) {
return ++tilt_ctl->tilt_counter.tilt_reverse_count;
return ++tilt_ctl->tilt_count;
}
else if (tilt_ctl->cmd == RELAY_CHN_TILT_CMD_FORWARD) {
if (tilt_ctl->tilt_counter.tilt_reverse_count > 0) {
--tilt_ctl->tilt_counter.tilt_reverse_count;
if (tilt_ctl->tilt_count > 0) {
--tilt_ctl->tilt_count;
// Still should do one more move, return non-zero value
return 1;
}
else
return 0;
}
else {
relay_chn_tilt_reset_count(tilt_ctl);
return 0;
}
}
// Irrelevant case -> reset
tilt_ctl->tilt_count = 0;
return 0;
}
#if RELAY_CHN_ENABLE_NVS == 1
static esp_err_t relay_chn_tilt_save_tilt_counter(relay_chn_tilt_ctl_t *tilt_ctl)
static esp_err_t relay_chn_tilt_save_tilt_count(relay_chn_tilt_ctl_t *tilt_ctl)
{
// Save the tilt count to NVS storage
esp_err_t ret = relay_chn_nvs_set_tilt_count(tilt_ctl->chn_ctl->id,
tilt_ctl->tilt_counter.tilt_forward_count,
tilt_ctl->tilt_counter.tilt_reverse_count);
esp_err_t ret = relay_chn_nvs_set_tilt_count(tilt_ctl->chn_ctl->id, tilt_ctl->tilt_count);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "relay_chn_tilt_execute_stop: Failed to save tilt count for channel #%d: %s", tilt_ctl->chn_ctl->id, esp_err_to_name(ret));
}
@@ -494,7 +483,7 @@ static void relay_chn_tilt_flush_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_flush_timer_cb: timer arg is NULL");
// Save the tilt count to storage
esp_err_t ret = relay_chn_tilt_save_tilt_counter(tilt_ctl);
esp_err_t ret = relay_chn_tilt_save_tilt_count(tilt_ctl);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "relay_chn_tilt_execute_stop: Failed to save tilt count for channel #%d: %s", tilt_ctl->chn_ctl->id, esp_err_to_name(ret));
}
@@ -558,7 +547,7 @@ static void relay_chn_tilt_execute_pause(relay_chn_tilt_ctl_t *tilt_ctl)
return;
}
// Update the tilt counter before the next move and expect the return value to be greater than 0
// Update the tilt count before the next move and expect the return value to be greater than 0
if (relay_chn_tilt_count_update(tilt_ctl) == 0) {
ESP_LOGD(TAG, "relay_chn_tilt_execute_pause: Relay channel cannot tilt anymore");
// Stop tilting since the tilting limit has been reached
@@ -640,13 +629,12 @@ static esp_err_t relay_chn_tilt_load_sensitivity(uint8_t ch, uint8_t *sensitivit
return ESP_OK;
}
static esp_err_t relay_chn_tilt_load_tilt_counter(uint8_t ch, relay_chn_tilt_counter_t *tilt_counter)
static esp_err_t relay_chn_tilt_load_tilt_count(uint8_t ch, uint16_t *tilt_count)
{
esp_err_t ret = relay_chn_nvs_get_tilt_count(ch, &tilt_counter->tilt_forward_count, &tilt_counter->tilt_reverse_count);
esp_err_t ret = relay_chn_nvs_get_tilt_count(ch, tilt_count);
if (ret == ESP_ERR_NVS_NOT_FOUND) {
ESP_LOGD(TAG, "relay_chn_tilt_load_tilt_counter: No tilt counters found in NVS for channel %d, initializing to zero", ch);
tilt_counter->tilt_forward_count = 0;
tilt_counter->tilt_reverse_count = 0;
ESP_LOGD(TAG, "relay_chn_tilt_load_tilt_count: No tilt count found in NVS for channel %d, initializing to zero", ch);
tilt_count = 0;
return ESP_OK;
}
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load tilt counters for channel %d", ch);
@@ -654,15 +642,15 @@ static esp_err_t relay_chn_tilt_load_tilt_counter(uint8_t ch, relay_chn_tilt_cou
}
#endif // RELAY_CHN_ENABLE_NVS
static esp_err_t relay_chn_tilt_ctl_init(relay_chn_tilt_ctl_t *tilt_ctl, relay_chn_ctl_t *chn_ctl,
relay_chn_tilt_counter_t *tilt_counter, uint8_t sensitivity)
static esp_err_t relay_chn_tilt_ctl_init(relay_chn_tilt_ctl_t *tilt_ctl,
relay_chn_ctl_t *chn_ctl,
uint16_t tilt_count ,
uint8_t sensitivity)
{
tilt_ctl->cmd = RELAY_CHN_TILT_CMD_NONE;
tilt_ctl->step = RELAY_CHN_TILT_STEP_NONE;
relay_chn_tilt_compute_set_sensitivity(tilt_ctl, sensitivity);
// Init tilt counters
tilt_ctl->tilt_counter.tilt_forward_count = tilt_counter->tilt_forward_count;
tilt_ctl->tilt_counter.tilt_reverse_count = tilt_counter->tilt_reverse_count;
tilt_ctl->tilt_count = tilt_count;
tilt_ctl->chn_ctl = chn_ctl;
tilt_ctl->chn_ctl->tilt_ctl = tilt_ctl;
@@ -692,33 +680,31 @@ static esp_err_t relay_chn_tilt_ctl_init(relay_chn_tilt_ctl_t *tilt_ctl, relay_c
esp_err_t relay_chn_tilt_init(relay_chn_ctl_t *chn_ctls)
{
uint8_t sensitivity;
relay_chn_tilt_counter_t tilt_counter;
uint16_t tilt_count;
#if RELAY_CHN_COUNT > 1
for (int i = 0; i < RELAY_CHN_COUNT; i++) {
#if RELAY_CHN_ENABLE_NVS == 1
esp_err_t ret = relay_chn_tilt_load_sensitivity(i, &sensitivity);
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load tilt sensitivity for channel %d", i);
ret = relay_chn_tilt_load_tilt_counter(i, &tilt_counter);
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load tilt counters for channel %d", i);
ret = relay_chn_tilt_load_tilt_count(i, &tilt_count);
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load tilt count for channel %d", i);
#else
sensitivity = RELAY_CHN_TILT_DEFAULT_SENSITIVITY;
tilt_counter.tilt_forward_count = 0;
tilt_counter.tilt_reverse_count = 0;
tilt_count = 0;
#endif // RELAY_CHN_ENABLE_NVS == 1
relay_chn_tilt_ctl_init(&tilt_ctls[i], &chn_ctls[i], &tilt_counter, sensitivity);
}
relay_chn_tilt_ctl_init(&tilt_ctls[i], &chn_ctls[i], tilt_count, sensitivity);
}
#else
sensitivity = RELAY_CHN_TILT_DEFAULT_SENSITIVITY;
tilt_counter.tilt_forward_count = 0;
tilt_counter.tilt_reverse_count = 0;
tilt_count = 0;
#if RELAY_CHN_ENABLE_NVS == 1
esp_err_t ret = relay_chn_tilt_load_sensitivity(0, &sensitivity);
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load tilt sensitivity for channel %d", 0);
ret = relay_chn_tilt_load_tilt_counter(0, &tilt_counter);
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load tilt counters for channel %d", 0);
ret = relay_chn_tilt_load_tilt_count(0, &tilt_count);
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load tilt count for channel %d", 0);
#endif // RELAY_CHN_ENABLE_NVS == 1
relay_chn_tilt_ctl_init(&tilt_ctl, chn_ctls, &tilt_counter, sensitivity);
relay_chn_tilt_ctl_init(&tilt_ctl, chn_ctls, tilt_count, sensitivity);
#endif // RELAY_CHN_COUNT > 1
return esp_event_handler_register_with(relay_chn_event_loop,