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

@@ -62,7 +62,7 @@ TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]")
uint8_t sensitivity = 50;
for (int channel = 0; channel < RELAY_CHN_COUNT; channel++) {
TEST_ESP_OK(relay_chn_nvs_set_tilt_sensitivity(0, sensitivity));
TEST_ESP_OK(relay_chn_nvs_set_tilt_count(0, 100, 200));
TEST_ESP_OK(relay_chn_nvs_set_tilt_count(0, 100));
}
#endif
@@ -77,8 +77,8 @@ TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]")
uint8_t read_sensitivity;
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_sensitivity(0, &read_sensitivity));
uint32_t fwd_count, rev_count;
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_count(0, &fwd_count, &rev_count));
uint16_t tilt_count;
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_count(0, &tilt_count));
#endif
TEST_ESP_OK(relay_chn_nvs_deinit());
@@ -106,17 +106,15 @@ TEST_CASE("Test tilt counter operations", "[relay_chn][nvs][tilt]")
{
TEST_ESP_OK(relay_chn_nvs_init());
const uint32_t fwd_count = 100;
const uint32_t rev_count = 200;
uint32_t fwd_read, rev_read;
const uint16_t tilt_count = 100;
uint16_t tilt_count_read;
// Test all channels
for (int channel = 0; channel < RELAY_CHN_COUNT; channel++) {
// Test setting counters
TEST_ESP_OK(relay_chn_nvs_set_tilt_count(channel, fwd_count, rev_count));
TEST_ESP_OK(relay_chn_nvs_get_tilt_count(channel, &fwd_read, &rev_read));
TEST_ASSERT_EQUAL(fwd_count, fwd_read);
TEST_ASSERT_EQUAL(rev_count, rev_read);
TEST_ESP_OK(relay_chn_nvs_set_tilt_count(channel, tilt_count));
TEST_ESP_OK(relay_chn_nvs_get_tilt_count(channel, &tilt_count_read));
TEST_ASSERT_EQUAL(tilt_count, tilt_count_read);
}
TEST_ESP_OK(relay_chn_nvs_deinit());
@@ -126,13 +124,10 @@ TEST_CASE("Test tilting invalid parameters", "[relay_chn][nvs][tilt]")
{
TEST_ESP_OK(relay_chn_nvs_init());
uint32_t fwd_count, rev_count;
// Test NULL pointers for all channels
for (int channel = 0; channel < RELAY_CHN_COUNT; channel++) {
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_sensitivity(channel, NULL));
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_count(channel, NULL, &rev_count));
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_count(channel, &fwd_count, NULL));
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_count(channel, NULL));
}
TEST_ESP_OK(relay_chn_nvs_deinit());

View File

@@ -56,7 +56,7 @@ TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]")
#ifdef RELAY_CHN_ENABLE_TILTING
uint8_t sensitivity = 50;
TEST_ESP_OK(relay_chn_nvs_set_tilt_sensitivity(0, sensitivity));
TEST_ESP_OK(relay_chn_nvs_set_tilt_count(0, 100, 200));
TEST_ESP_OK(relay_chn_nvs_set_tilt_count(0, 100));
#endif
// Test erase all
@@ -70,8 +70,8 @@ TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]")
uint8_t read_sensitivity;
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_sensitivity(0, &read_sensitivity));
uint32_t fwd_count, rev_count;
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_count(0, &fwd_count, &rev_count));
uint16_t tilt_count;
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_count(0, &tilt_count));
#endif
TEST_ESP_OK(relay_chn_nvs_deinit());
@@ -96,16 +96,14 @@ TEST_CASE("Test tilt counter operations", "[relay_chn][nvs][tilt]")
{
TEST_ESP_OK(relay_chn_nvs_init());
const uint32_t fwd_count = 100;
const uint32_t rev_count = 200;
const uint16_t tilt_count = 100;
// Test setting counters
TEST_ESP_OK(relay_chn_nvs_set_tilt_count(0, fwd_count, rev_count));
TEST_ESP_OK(relay_chn_nvs_set_tilt_count(0, tilt_count));
uint32_t fwd_read, rev_read;
TEST_ESP_OK(relay_chn_nvs_get_tilt_count(0, &fwd_read, &rev_read));
TEST_ASSERT_EQUAL(fwd_count, fwd_read);
TEST_ASSERT_EQUAL(rev_count, rev_read);
uint16_t tilt_count_read;
TEST_ESP_OK(relay_chn_nvs_get_tilt_count(0, &tilt_count_read));
TEST_ASSERT_EQUAL(tilt_count, tilt_count_read);
TEST_ESP_OK(relay_chn_nvs_deinit());
}
@@ -114,12 +112,9 @@ TEST_CASE("Test tilting invalid parameters", "[relay_chn][nvs][tilt]")
{
TEST_ESP_OK(relay_chn_nvs_init());
uint32_t fwd_count, rev_count;
// Test NULL pointers
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_sensitivity(0, NULL));
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_count(0, NULL, &rev_count));
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_count(0, &fwd_count, NULL));
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_count(0, NULL));
TEST_ESP_OK(relay_chn_nvs_deinit());
}