Cleanup and replace constants

- Delete unused declaration of `g_is_component_initialized`.
- Replace the following constants with approprite config options:
  + `relay_chn_count` > `CONFIG_RELAY_CHN_COUNT`
  + `opposite_inerta_ms` > `CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS`
- Replace the definition of the `test_delay_margin_ms` constant with  `#define TEST_DELAY_MARGIN_MS 50` for preprocessor calculations.
This commit is contained in:
2025-08-26 09:23:06 +03:00
parent 3831384169
commit 396a02b5ae
8 changed files with 160 additions and 169 deletions

View File

@@ -16,7 +16,7 @@
void prepare_channel_for_tilt(uint8_t chn_id, int initial_cmd) {
// Ensure the channel reset tilt control
relay_chn_tilt_stop(chn_id);
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
// Ensure the channel has had a 'last_run_cmd'
if (initial_cmd == RELAY_CHN_CMD_FORWARD) {
@@ -24,9 +24,9 @@ void prepare_channel_for_tilt(uint8_t chn_id, int initial_cmd) {
} else { // Assuming initial_cmd is RELAY_CHN_CMD_REVERSE
relay_chn_run_reverse(chn_id);
}
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); // Allow command to process
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); // Allow command to process
relay_chn_stop(chn_id); // Stop it to set last_run_cmd but return to FREE for next test
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state(chn_id));
}
@@ -40,17 +40,17 @@ TEST_CASE("Run Forward to Tilt Forward transition with inertia", "[relay_chn][ti
// 1. Start in forward direction
relay_chn_run_forward(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state(ch));
// 2. Issue tilt forward command
relay_chn_tilt_forward(ch);
// After tilt command, it should immediately stop and then trigger inertia.
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_STOPPED, relay_chn_get_state(ch));
// Wait for the inertia period (after which the tilt command will be dispatched)
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(ch));
}
@@ -64,15 +64,15 @@ TEST_CASE("Run Reverse to Tilt Reverse transition with inertia", "[relay_chn][ti
// 1. Start in reverse direction
relay_chn_run_reverse(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state(ch));
// 2. Issue tilt reverse command
relay_chn_tilt_reverse(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_STOPPED, relay_chn_get_state(ch));
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state(ch));
}
@@ -88,7 +88,7 @@ TEST_CASE("FREE to Tilt Forward transition with inertia (prepared)", "[relay_chn
// Issue tilt forward command
relay_chn_tilt_forward(ch);
// From FREE state, tilt command should still incur the inertia due to the internal timer logic
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(ch));
}
@@ -103,7 +103,7 @@ TEST_CASE("FREE to Tilt Reverse transition with inertia (prepared)", "[relay_chn
// Issue tilt reverse command
relay_chn_tilt_reverse(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state(ch));
}
@@ -115,14 +115,14 @@ TEST_CASE("Tilt Forward to Run Forward transition with inertia", "[relay_chn][ti
// Prepare channel by running forward first to set last_run_cmd, then tilt
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD);
relay_chn_tilt_forward(ch); // Go to tilt state
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(ch));
// 2. Issue run forward command
relay_chn_run_forward(ch);
// From Tilt to Run in the same logical name but in the opposite direction, inertia is expected.
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD_PENDING, relay_chn_get_state(ch));
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state(ch));
}
@@ -134,13 +134,13 @@ TEST_CASE("Tilt Reverse to Run Reverse transition with inertia", "[relay_chn][ti
// Prepare channel by running reverse first to set last_run_cmd, then tilt
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_REVERSE);
relay_chn_tilt_reverse(ch); // Go to tilt state
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state(ch));
// 2. Issue run reverse command
relay_chn_run_reverse(ch);
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE_PENDING, relay_chn_get_state(ch));
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state(ch));
}
@@ -152,12 +152,12 @@ TEST_CASE("Tilt Forward to Run Reverse transition without inertia", "[relay_chn]
// Prepare channel by running forward first to set last_run_cmd, then tilt
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD);
relay_chn_tilt_forward(ch); // Go to tilt state
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(ch));
// 2. Issue run reverse command (opposite direction)
relay_chn_run_reverse(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state(ch));
}
@@ -169,13 +169,13 @@ TEST_CASE("Tilt to Stop transition without immediate inertia for stop", "[relay_
// Prepare channel by running forward first to set last_run_cmd, then tilt
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD);
relay_chn_tilt_forward(ch); // Go to tilt state
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(ch));
// 2. Issue stop command
relay_chn_tilt_stop(ch);
// Stop command should apply immediately, setting state to FREE since last state was tilt.
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state(ch));
}
@@ -184,16 +184,16 @@ TEST_CASE("Tilt to Stop transition without immediate inertia for stop", "[relay_
TEST_CASE("tilt_forward_all sets all channels to TILT_FORWARD", "[relay_chn][tilt][batch]")
{
// 1. Prepare all channels.
for (uint8_t i = 0; i < relay_chn_count; i++) {
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
prepare_channel_for_tilt(i, RELAY_CHN_CMD_FORWARD);
}
// 2. Issue tilt forward to all channels
relay_chn_tilt_forward_all();
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); // Tilt from FREE doesn't have stop-inertia
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); // Tilt from FREE doesn't have stop-inertia
// 3. Verify all channels are tilting forward
for (uint8_t i = 0; i < relay_chn_count; i++) {
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
ESP_LOGI(TEST_TAG, "Checking channel %d", i);
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(i));
}
@@ -202,16 +202,16 @@ TEST_CASE("tilt_forward_all sets all channels to TILT_FORWARD", "[relay_chn][til
TEST_CASE("tilt_reverse_all sets all channels to TILT_REVERSE", "[relay_chn][tilt][batch]")
{
// 1. Prepare all channels.
for (uint8_t i = 0; i < relay_chn_count; i++) {
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
prepare_channel_for_tilt(i, RELAY_CHN_CMD_REVERSE);
}
// 2. Issue tilt reverse to all channels
relay_chn_tilt_reverse_all();
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
// 3. Verify all channels are tilting reverse
for (uint8_t i = 0; i < relay_chn_count; i++) {
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state(i));
}
}
@@ -219,18 +219,18 @@ TEST_CASE("tilt_reverse_all sets all channels to TILT_REVERSE", "[relay_chn][til
TEST_CASE("tilt_stop_all stops all tilting channels", "[relay_chn][tilt][batch]")
{
// 1. Prepare and start all channels tilting forward
for (uint8_t i = 0; i < relay_chn_count; i++) {
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
prepare_channel_for_tilt(i, RELAY_CHN_CMD_REVERSE);
}
relay_chn_tilt_forward_all();
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
// 2. Stop tilting on all channels
relay_chn_tilt_stop_all();
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
// 3. Verify all channels are free
for (uint8_t i = 0; i < relay_chn_count; i++) {
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
ESP_LOGI(TEST_TAG, "Checking channel %d", i);
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state(i));
}
@@ -239,7 +239,7 @@ TEST_CASE("tilt_stop_all stops all tilting channels", "[relay_chn][tilt][batch]"
TEST_CASE("tilt_auto_all tilts channels based on last run direction", "[relay_chn][tilt][batch]")
{
// This test requires at least 2 channels to demonstrate different behaviors
TEST_ASSERT_GREATER_OR_EQUAL_MESSAGE(2, relay_chn_count, "Test requires at least 2 channels");
TEST_ASSERT_GREATER_OR_EQUAL_MESSAGE(2, CONFIG_RELAY_CHN_COUNT, "Test requires at least 2 channels");
// 1. Prepare channel 0 with last run FORWARD and channel 1 with last run REVERSE
prepare_channel_for_tilt(0, RELAY_CHN_CMD_FORWARD);
@@ -247,7 +247,7 @@ TEST_CASE("tilt_auto_all tilts channels based on last run direction", "[relay_ch
// 2. Issue auto tilt command to all channels
relay_chn_tilt_auto_all();
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); // Tilt from FREE state is dispatched immediately
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); // Tilt from FREE state is dispatched immediately
// 3. Verify channel 0 tilts forward (last run was forward) and channel 1 tilts reverse (last run was reverse)
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(0));
@@ -260,15 +260,15 @@ TEST_CASE("relay_chn_tilt_auto chooses correct direction", "[relay_chn][tilt][au
// Prepare FORWARD
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD);
relay_chn_tilt_auto(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(ch));
relay_chn_tilt_stop(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
// Prepare REVERSE
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_REVERSE);
relay_chn_tilt_auto(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state(ch));
}
@@ -302,26 +302,26 @@ TEST_CASE("tilt counter logic: forward and reverse consumption", "[relay_chn][ti
// Tilt forward 3 times
for (int i = 0; i < 3; ++i) {
relay_chn_tilt_forward(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(ch));
relay_chn_tilt_stop(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
}
// Now tilt reverse 3 times (should succeed)
for (int i = 0; i < 3; ++i) {
relay_chn_tilt_reverse(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
if (i < 3) {
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state(ch));
relay_chn_tilt_stop(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
}
}
// Extra reverse tilt should fail (counter exhausted)
relay_chn_tilt_reverse(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
// Should not enter TILT_REVERSE, should remain FREE or STOPPED
relay_chn_state_t state = relay_chn_get_state(ch);
TEST_ASSERT(state != RELAY_CHN_STATE_TILT_REVERSE);
@@ -332,12 +332,12 @@ TEST_CASE("run command during TILT state transitions correctly", "[relay_chn][ti
uint8_t ch = 0;
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD);
relay_chn_tilt_forward(ch);
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(ch));
// Issue run reverse while in TILT_FORWARD
relay_chn_run_reverse(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
// Should transition to REVERSE or REVERSE_PENDING depending on inertia logic
relay_chn_state_t state = relay_chn_get_state(ch);
TEST_ASSERT(state == RELAY_CHN_STATE_REVERSE || state == RELAY_CHN_STATE_REVERSE_PENDING);