5 Commits

Author SHA1 Message Date
2e81966afb Bump version to 0.5.0 and update repo URLs. 2025-07-23 17:37:27 +03:00
5734f47cd3 Merge pull request 'feat/1030-more-unit-tests' (!29) from feat/1030-more-unit-tests into dev
Reviewed-on: #29
2025-07-22 16:59:47 +03:00
d884f5f45c Add missing test cases for tilt API.
Added missing test cases for the tilt API.

Closes #1056.
2025-07-22 10:02:40 +03:00
c7678d6084 Add restart chip to make qemu exit in tests. 2025-07-22 10:00:33 +03:00
8527ebea83 Fix imbalanced tilt counts.
Fixed the tilt count logic that causes an imbalance in tilting within the same run frame but opposite direction.

Fixes #1057.
2025-07-22 09:58:59 +03:00
5 changed files with 120 additions and 8 deletions

View File

@@ -38,7 +38,7 @@ dependencies:
# Add as a custom component from git repository
relay_chn:
git: https://git.kozmotronik.com.tr/KozmotronikTech/relay_chn.git
version: '>=0.4.0'
version: '>=0.5.0'
```
## Usage

View File

@@ -1,6 +1,6 @@
name: relay_chn
version: "0.4.0"
version: "0.5.0"
description: "Custom component for relay channel control"
license: "MIT"
url: "https://git.kozmotronik.com.tr/KozmotronikTech/relay_chn_component"
repository: "https://git.kozmotronik.com.tr/KozmotronikTech/relay_chn_component.git"
url: "https://git.kozmotronik.com.tr/KozmotronikTech/relay_chn"
repository: "https://git.kozmotronik.com.tr/KozmotronikTech/relay_chn.git"

View File

@@ -1268,8 +1268,11 @@ static uint32_t relay_chn_tilt_count_update(relay_chn_t *relay_chn)
return ++relay_chn->tilt_control.tilt_counter.tilt_forward_count;
}
else if (relay_chn->tilt_control.cmd == RELAY_CHN_TILT_CMD_REVERSE) {
if (relay_chn->tilt_control.tilt_counter.tilt_forward_count > 0)
return --relay_chn->tilt_control.tilt_counter.tilt_forward_count;
if (relay_chn->tilt_control.tilt_counter.tilt_forward_count > 0) {
--relay_chn->tilt_control.tilt_counter.tilt_forward_count;
// Still should do one more move, return non-zero value
return 1;
}
else
return 0;
}
@@ -1283,8 +1286,11 @@ static uint32_t relay_chn_tilt_count_update(relay_chn_t *relay_chn)
return ++relay_chn->tilt_control.tilt_counter.tilt_reverse_count;
}
else if (relay_chn->tilt_control.cmd == RELAY_CHN_TILT_CMD_FORWARD) {
if (relay_chn->tilt_control.tilt_counter.tilt_reverse_count > 0)
return --relay_chn->tilt_control.tilt_counter.tilt_reverse_count;
if (relay_chn->tilt_control.tilt_counter.tilt_reverse_count > 0) {
--relay_chn->tilt_control.tilt_counter.tilt_reverse_count;
// Still should do one more move, return non-zero value
return 1;
}
else
return 0;
}

View File

@@ -44,4 +44,6 @@ void app_main(void)
UNITY_END();
ESP_LOGI(TEST_TAG, "All tests complete.");
esp_restart(); // Restart to invoke qemu exit
}

View File

@@ -283,3 +283,107 @@ TEST_CASE("tilt_auto with ID_ALL tilts channels based on last run direction", "[
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(0));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state(1));
}
// Test relay_chn_tilt_auto() chooses correct tilt direction
TEST_CASE("relay_chn_tilt_auto chooses correct direction", "[relay_chn][tilt][auto]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
// Prepare FORWARD
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD);
relay_chn_tilt_auto(ch);
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));
// Prepare REVERSE
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_REVERSE);
relay_chn_tilt_auto(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state(ch));
}
// Test sensitivity set/get
TEST_CASE("relay_chn_tilt_sensitivity_set and get", "[relay_chn][tilt][sensitivity]") {
uint8_t ch = 0;
uint8_t val = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
relay_chn_tilt_sensitivity_set(ch, 0);
TEST_ESP_OK(relay_chn_tilt_sensitivity_get(ch, &val, 1));
TEST_ASSERT_EQUAL_UINT8(0, val);
relay_chn_tilt_sensitivity_set(ch, 50);
TEST_ESP_OK(relay_chn_tilt_sensitivity_get(ch, &val, 1));
TEST_ASSERT_EQUAL_UINT8(50, val);
relay_chn_tilt_sensitivity_set(ch, 100);
TEST_ESP_OK(relay_chn_tilt_sensitivity_get(ch, &val, 1));
TEST_ASSERT_EQUAL_UINT8(100, val);
// Set all channels
relay_chn_tilt_sensitivity_set(RELAY_CHN_ID_ALL, 42);
uint8_t vals[CONFIG_RELAY_CHN_COUNT] = {0};
TEST_ESP_OK(relay_chn_tilt_sensitivity_get(RELAY_CHN_ID_ALL, vals, relay_chn_count));
for (int i = 0; i < relay_chn_count; ++i) {
TEST_ASSERT_EQUAL_UINT8(42, vals[i]);
}
}
// Test tilt counter logic: forward x3, reverse x3, extra reverse fails
TEST_CASE("tilt counter logic: forward and reverse consumption", "[relay_chn][tilt][counter]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD);
// Tilt forward 3 times
for (int i = 0; i < 3; ++i) {
relay_chn_tilt_forward(ch);
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));
}
// 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));
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));
}
}
// Extra reverse tilt should fail (counter exhausted)
relay_chn_tilt_reverse(ch);
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);
}
// Test run command during TILT state
TEST_CASE("run command during TILT state transitions correctly", "[relay_chn][tilt][run-during-tilt]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
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));
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));
// 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);
}