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

@@ -10,8 +10,7 @@
#define RELAY_CHN_KEY_DIR "dir"
#ifdef RELAY_CHN_ENABLE_TILTING
#define RELAY_CHN_KEY_SENS(ch) "sens_%d"
#define RELAY_CHN_KEY_TFWD(ch) "tfwd_%d"
#define RELAY_CHN_KEY_TREV(ch) "trev_%d"
#define RELAY_CHN_KEY_TCNT(ch) "tcnt_%d" /*!< Tilt count key */
#endif
static const char *TAG = "RELAY_CHN_STORAGE_NVS";
@@ -85,26 +84,19 @@ esp_err_t relay_chn_nvs_get_tilt_sensitivity(uint8_t ch, uint8_t *sensitivity)
return nvs_get_u8(relay_chn_nvs, RELAY_CHN_KEY_SENS(ch), sensitivity);
}
esp_err_t relay_chn_nvs_set_tilt_count(uint8_t ch, uint32_t forward_count, uint32_t reverse_count)
esp_err_t relay_chn_nvs_set_tilt_count(uint8_t ch, uint16_t tilt_count)
{
esp_err_t ret;
ret = nvs_set_u32(relay_chn_nvs, RELAY_CHN_KEY_TFWD(ch), forward_count);
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to save forward_count tilt counter");
ret = nvs_set_u32(relay_chn_nvs, RELAY_CHN_KEY_TREV(ch), reverse_count);
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to save reverse_count tilt counter");
ret = nvs_set_u16(relay_chn_nvs, RELAY_CHN_KEY_TCNT(ch), tilt_count);
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to save tilt_count tilt counter");
return nvs_commit(relay_chn_nvs);
}
esp_err_t relay_chn_nvs_get_tilt_count(uint8_t ch, uint32_t *forward_count, uint32_t *reverse_count)
esp_err_t relay_chn_nvs_get_tilt_count(uint8_t ch, uint16_t *tilt_count)
{
ESP_RETURN_ON_FALSE(forward_count != NULL && reverse_count != NULL,
ESP_RETURN_ON_FALSE(tilt_count != NULL,
ESP_ERR_INVALID_ARG, TAG, "Counter pointers are NULL");
esp_err_t ret = nvs_get_u32(relay_chn_nvs, RELAY_CHN_KEY_TFWD(ch), forward_count);
if (ret != ESP_OK) {
return ret; // Return error if the key does not exist
}
return nvs_get_u32(relay_chn_nvs, RELAY_CHN_KEY_TREV(ch), reverse_count);
return nvs_get_u16(relay_chn_nvs, RELAY_CHN_KEY_TCNT(ch), tilt_count);
}
#endif // RELAY_CHN_ENABLE_TILTING