diff --git a/README.md b/README.md index 599a024..0814795 100644 --- a/README.md +++ b/README.md @@ -180,22 +180,22 @@ For multi mode // Run channel #0 forward relay_chn_run_forward(0); // Run all channels forward -relay_chn_run_forward(RELAY_CHN_ID_ALL); +relay_chn_run_forward_all(); // Run channel #1 reverse relay_chn_run_reverse(1); // Run all channels reverse -relay_chn_run_reverse(RELAY_CHN_ID_ALL); +relay_chn_run_reverse_all(); // Stop channel #1 relay_chn_stop(1); // Stop all channels -relay_chn_stop(RELAY_CHN_ID_ALL); +relay_chn_stop_all(); // Flip direction of channel #0 relay_chn_flip_direction(0); // Flip direction of all channels -relay_chn_flip_direction(RELAY_CHN_ID_ALL); +relay_chn_flip_direction_all(); ``` ### 3. Monitor channel state @@ -227,12 +227,21 @@ For multi mode: ```c // Get channel #0 state relay_chn_state_t state = relay_chn_get_state(0); + +// Get states for all channels +relay_chn_state_t states[CONFIG_RELAY_CHN_COUNT]; +relay_chn_get_states(states); + // Get the string representation of the state of the channel #0 char *state_str = relay_chn_get_state_str(0); // Get channel #0 direction relay_chn_direction_t direction = relay_chn_get_direction(0); +// Get directions for all channels +relay_chn_direction_t directions[CONFIG_RELAY_CHN_COUNT]; +relay_chn_get_directions(directions); + /* The listener is same for multi mode */ ``` @@ -261,7 +270,11 @@ uint16_t limit = relay_chn_get_run_limit(0); // Set new run limit for specific channels (in seconds) relay_chn_set_run_limit(0, 120); // Set channel #0 to 120 seconds relay_chn_set_run_limit(1, 180); // Set channel #1 to 180 seconds -relay_chn_set_run_limit(RELAY_CHN_ID_ALL, 90); // Set all channels to 90 seconds +relay_chn_set_run_limit_all_with(90); // Set all channels to 90 seconds + +// Assuming the CONFIG_RELAY_CHN_COUNT is 4 +uint16_t limits_sec[CONFIG_RELAY_CHN_COUNT] = { 30, 35, 40, 45 }; +relay_chn_set_run_limit_all(limits_sec); // Set all channels according to the array ``` > [!NOTE] > When a channel reaches its run limit, it will automatically stop. The run limit timer is reset whenever the channel starts running in either direction. @@ -286,7 +299,7 @@ relay_chn_tilt_reverse(); relay_chn_tilt_stop(); // Set tilting sensitivity (sensitivity as percentage) -relay_chn_tilt_sensitivity_set(90); +relay_chn_tilt_set_sensitivity(90); // Get tilting sensitivity (sensitivty as percentage) uint8_t sensitivity = relay_chn_tilt_get_sensitivity(); @@ -299,27 +312,34 @@ For multi mode: // Start tilting automatically on channel #0 relay_chn_tilt_auto(0); -relay_chn_tilt_auto(RELAY_CHN_ID_ALL); // on all channels +relay_chn_tilt_auto_all(); // on all channels // Tilt forward on channel #1 relay_chn_tilt_forward(1); -relay_chn_tilt_forward(RELAY_CHN_ID_ALL); +relay_chn_tilt_forward_all(); // Tilt reverse on channel #2 relay_chn_tilt_reverse(2); -relay_chn_tilt_reverse(RELAY_CHN_ID_ALL); +relay_chn_tilt_reverse_all(); // Stop tilting on channel #0 relay_chn_tilt_stop(0); -relay_chn_tilt_stop(RELAY_CHN_ID_ALL); +relay_chn_tilt_stop_all(); // Set tilting sensitivity (sensitivity as percentage) for channel #0 -relay_chn_tilt_sensitivity_set(0, 90); -relay_chn_tilt_sensitivity_set(RELAY_CHN_ID_ALL, 90); +relay_chn_tilt_set_sensitivity(0, 90); +relay_chn_tilt_set_sensitivity_all_with(90); + +// Assuming the CONFIG_RELAY_CHN_COUNT is 4 +uint8_t sensitivities[CONFIG_RELAY_CHN_COUNT] = { 90, 85, 80, 75 }; +relay_chn_tilt_set_sensitivity_all(sensitivity); // Set all channels according to the array + +// Get tilt sensitivity for channel #0 +uint8_t sensitivity = relay_chn_tilt_get_sensitivity(0); // Get tilting sensitivity (sensitivty as percentage) -uint8_t sensitivity; -relay_chn_tilt_get_sensitivity(0, &sensitivity, sizeof(sensitivity)); +uint8_t sensitivities[CONFIG_RELAY_CHN_COUNT]; +relay_chn_tilt_get_sensitivity_all(sensitivities); ``` ## License diff --git a/include/relay_chn.h b/include/relay_chn.h index 6f7ee33..9d7a12f 100644 --- a/include/relay_chn.h +++ b/include/relay_chn.h @@ -14,7 +14,6 @@ #pragma once #include "esp_err.h" -#include "relay_chn_defs.h" #include "relay_chn_types.h" #include "relay_chn_adapter.h" @@ -75,6 +74,18 @@ void relay_chn_unregister_listener(relay_chn_state_listener_t listener); */ relay_chn_state_t relay_chn_get_state(uint8_t chn_id); +/** + * @brief Gets the current state of all relay channels. + * + * This function populates an array with the current states of all configured + * relay channels. The caller must ensure the `states` array is large enough + * to hold `CONFIG_RELAY_CHN_COUNT` elements. + * + * @param states Pointer to an array where the states will be stored. + * @return ESP_OK on success, ESP_ERR_INVALID_ARG if `states` is NULL. + */ +esp_err_t relay_chn_ctl_get_state_all(relay_chn_state_t *states); + /** * @brief Get the state string of the specified relay channel. * @@ -100,6 +111,14 @@ char *relay_chn_get_state_str(uint8_t chn_id); */ void relay_chn_run_forward(uint8_t chn_id); +/** + * @brief Commands all configured relay channels to run in the forward direction. + * + * This function iterates through all configured relay channels and issues a command + * to each to move in the forward direction. + */ +void relay_chn_ctl_run_forward_all(void); + /** * @brief Runs the relay channel in reverse. * @@ -109,6 +128,14 @@ void relay_chn_run_forward(uint8_t chn_id); */ void relay_chn_run_reverse(uint8_t chn_id); +/** + * @brief Commands all configured relay channels to run in the reverse direction. + * + * This function iterates through all configured relay channels and issues a command + * to each to move in the reverse direction. + */ +void relay_chn_ctl_run_reverse_all(void); + /** * @brief Stops the relay channel specified by the channel ID. * @@ -120,6 +147,14 @@ void relay_chn_run_reverse(uint8_t chn_id); */ void relay_chn_stop(uint8_t chn_id); +/** + * @brief Commands all configured relay channels to stop. + * + * This function iterates through all configured relay channels and issues a command + * to each to stop any ongoing movement. + */ +void relay_chn_ctl_stop_all(void); + /** * @brief Flips the direction of the specified relay channel. * @@ -131,6 +166,14 @@ void relay_chn_stop(uint8_t chn_id); */ void relay_chn_flip_direction(uint8_t chn_id); +/** + * @brief Flips the logical direction of all configured relay channels. + * + * This function iterates through all configured relay channels and swaps the + * physical GPIO pins assigned to the forward and reverse directions for each. + */ +void relay_chn_ctl_flip_direction_all(void); + /** * @brief Get the direction of the specified relay channel. * @@ -143,6 +186,18 @@ void relay_chn_flip_direction(uint8_t chn_id); */ relay_chn_direction_t relay_chn_get_direction(uint8_t chn_id); +/** + * @brief Gets the current logical direction of all configured relay channels. + * + * This function populates an array with the current logical directions of all + * configured relay channels. The caller must ensure the `directions` array is + * large enough to hold `CONFIG_RELAY_CHN_COUNT` elements. + * + * @param directions Pointer to an array where the directions will be stored. + * @return ESP_OK on success, ESP_ERR_INVALID_ARG if `directions` is NULL. + */ +esp_err_t relay_chn_ctl_get_direction_all(relay_chn_direction_t *directions); + #if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT /** * @brief Get the run limit for the specified channel @@ -154,6 +209,18 @@ relay_chn_direction_t relay_chn_get_direction(uint8_t chn_id); */ uint16_t relay_chn_get_run_limit(uint8_t chn_id); +/** + * @brief Gets the configured run limits for all configured relay channels. + * + * This function populates an array with the run limits (in seconds) of all + * configured relay channels. The caller must ensure the `limits_sec` array is + * large enough to hold `CONFIG_RELAY_CHN_COUNT` elements. + * + * @param limits_sec Pointer to an array where the run limits will be stored. + * @return ESP_OK on success, ESP_ERR_INVALID_ARG if `limits_sec` is NULL. + */ +esp_err_t relay_chn_ctl_get_run_limit_all(uint16_t *limits_sec); + /** * @brief Set the run limit for the specified channel * @@ -168,6 +235,31 @@ uint16_t relay_chn_get_run_limit(uint8_t chn_id); * @param time_sec The run limit time in seconds. */ void relay_chn_set_run_limit(uint8_t chn_id, uint16_t time_sec); + +/** + * @brief Sets the run limits for all configured relay channels. + * + * This function iterates through all configured relay channels and sets their + * run limits based on the values provided in the `limits_sec` array. Each value + * will be clamped within the configured `RELAY_CHN_RUN_LIMIT_MIN_SEC` and + * `RELAY_CHN_RUN_LIMIT_MAX_SEC` boundaries. The new run limits are persisted + * in NVS if enabled. + * + * @param limits_sec Pointer to an array containing the desired run limits in seconds. + * @return ESP_OK on success, ESP_ERR_INVALID_ARG if `limits_sec` is NULL. + */ +esp_err_t relay_chn_ctl_set_run_limit_all(uint16_t *limits_sec); + +/** + * @brief Sets a single run limit value for all configured relay channels. + * + * This function sets the same `limit_sec` value for all configured relay channels. + * The value will be clamped within the configured `RELAY_CHN_RUN_LIMIT_MIN_SEC` + * and `RELAY_CHN_RUN_LIMIT_MAX_SEC` boundaries. + * @param limit_sec The desired run limit in seconds to apply to all channels. + * @return ESP_OK on success. + */ +esp_err_t relay_chn_ctl_set_run_limit_all_with(uint16_t limit_sec); #endif // CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT == 1 @@ -184,6 +276,14 @@ void relay_chn_set_run_limit(uint8_t chn_id, uint16_t time_sec); */ void relay_chn_tilt_auto(uint8_t chn_id); +/** + * @brief Initiates an automatic tilt operation for all configured relay channels. + * + * This function iterates through all configured relay channels and initiates an + * automatic tilt operation for each, based on their individual last run commands. + */ +void relay_chn_tilt_auto_all(void); + /** * @brief Tilts the specified relay channel forward. * @@ -193,6 +293,11 @@ void relay_chn_tilt_auto(uint8_t chn_id); */ void relay_chn_tilt_forward(uint8_t chn_id); +/** + * @brief Initiates a forward tilt operation for all configured relay channels. + */ +void relay_chn_tilt_forward_all(void); + /** * @brief Tilts the specified relay channel reverse. * @@ -202,6 +307,11 @@ void relay_chn_tilt_forward(uint8_t chn_id); */ void relay_chn_tilt_reverse(uint8_t chn_id); +/** + * @brief Initiates a reverse tilt operation for all configured relay channels. + */ +void relay_chn_tilt_reverse_all(void); + /** * @brief Stops the tilting action on the specified relay channel. * @@ -211,6 +321,11 @@ void relay_chn_tilt_reverse(uint8_t chn_id); */ void relay_chn_tilt_stop(uint8_t chn_id); +/** + * @brief Stops any ongoing tilt operation for all configured relay channels. + */ +void relay_chn_tilt_stop_all(void); + /** * @brief Sets the tilting sensitivity for the specified relay channel. * @@ -222,6 +337,33 @@ void relay_chn_tilt_stop(uint8_t chn_id); */ void relay_chn_tilt_set_sensitivity(uint8_t chn_id, uint8_t sensitivity); +/** + * @brief Sets the tilt sensitivity for all configured relay channels. + * + * This function sets the tilt sensitivity for each channel based on the values + * provided in the `sensitivities` array. Each sensitivity value (0-100%) + * determines the `move_time_ms` and `pause_time_ms` for tilt operations. + * The new sensitivities are persisted in NVS if enabled. + * + * @param sensitivities Pointer to an array containing the desired tilt sensitivities. + * + * @return + * - ESP_OK: Success + * - ESP_ERR_INVALID_ARG: When sensitivities parameter is NULL + */ +esp_err_t relay_chn_tilt_set_sensitivity_all(uint8_t *sensitivities); + +/** + * @brief Sets a single tilt sensitivity value for all configured relay channels. + * + * This function sets the same `sensitivity` value for all configured relay channels. + * The sensitivity value (0-100%) determines the `move_time_ms` and `pause_time_ms` + * for tilt operations. The new sensitivities are persisted in NVS if enabled. + * + * @param sensitivity The desired tilt sensitivity in percentage (0-100) to apply to all channels. + */ +void relay_chn_tilt_set_sensitivity_all_with(uint8_t sensitivity); + /** * @brief Gets the tilting sensitivity for the specified relay channel. * @@ -235,7 +377,19 @@ void relay_chn_tilt_set_sensitivity(uint8_t chn_id, uint8_t sensitivity); * - ESP_OK: Success * - ESP_ERR_INVALID_ARG: Invalid argument */ -esp_err_t relay_chn_tilt_get_sensitivity(uint8_t chn_id, uint8_t *sensitivity, size_t length); +uint8_t relay_chn_tilt_get_sensitivity(uint8_t chn_id); + +/** + * @brief Gets the current tilt sensitivities for all configured relay channels. + * + * This function populates an array with the current tilt sensitivities (0-100%) + * of all configured relay channels. The caller must ensure the `sensitivity` array + * is large enough to hold `CONFIG_RELAY_CHN_COUNT` elements. + * + * @param sensitivity Pointer to an array where the sensitivities will be stored. + * @return ESP_OK on success, ESP_ERR_INVALID_ARG if `sensitivity` is NULL. + */ +esp_err_t relay_chn_tilt_get_sensitivity_all(uint8_t *sensitivities); #endif // CONFIG_RELAY_CHN_ENABLE_TILTING diff --git a/include/relay_chn_adapter.h b/include/relay_chn_adapter.h index 885ab95..5e00d91 100644 --- a/include/relay_chn_adapter.h +++ b/include/relay_chn_adapter.h @@ -22,6 +22,19 @@ extern "C" { */ extern relay_chn_state_t relay_chn_ctl_get_state(uint8_t chn_id); +/** + * @brief Gets the current state of all relay channels. + * + * This function populates an array with the current states of all configured + * relay channels. The caller must ensure the `states` array is large enough + * to hold `CONFIG_RELAY_CHN_COUNT` elements. + * + * @param states Pointer to an array where the states will be stored. + * + * @return ESP_OK on success, ESP_ERR_INVALID_ARG if `states` is NULL. + */ +extern esp_err_t relay_chn_ctl_get_state_all(relay_chn_state_t *states); + /** * @brief Get string representation of a relay channel's state. * @@ -33,31 +46,62 @@ extern char *relay_chn_ctl_get_state_str(uint8_t chn_id); /** * @brief Run a relay channel in forward direction. * - * @param[in] chn_id Channel ID to run forward, or RELAY_CHN_ID_ALL for all channels. + * @param[in] chn_id Channel ID to run forward. */ extern void relay_chn_ctl_run_forward(uint8_t chn_id); +/** + * @brief Commands all configured relay channels to run in the forward direction. + * + * This function iterates through all configured relay channels and issues a command + * to each to move in the forward direction. + */ +extern void relay_chn_ctl_run_forward_all(void); + /** * @brief Run a relay channel in reverse direction. * - * @param[in] chn_id Channel ID to run reverse, or RELAY_CHN_ID_ALL for all channels. + * @param[in] chn_id Channel ID to run reverse. */ extern void relay_chn_ctl_run_reverse(uint8_t chn_id); +/** + * @brief Commands all configured relay channels to run in the reverse direction. + * + * This function iterates through all configured relay channels and issues a command + * to each to move in the reverse direction. + */ +extern void relay_chn_ctl_run_reverse_all(void); + /** * @brief Stop a relay channel. * - * @param[in] chn_id Channel ID to stop, or RELAY_CHN_ID_ALL for all channels. + * @param[in] chn_id Channel ID to stop. */ extern void relay_chn_ctl_stop(uint8_t chn_id); +/** + * @brief Commands all configured relay channels to stop. + * + * This function iterates through all configured relay channels and issues a command to each to stop any ongoing movement. + */ +extern void relay_chn_ctl_stop_all(void); + /** * @brief Flip the running direction of a relay channel. * - * @param[in] chn_id Channel ID to flip direction for, or RELAY_CHN_ID_ALL for all channels. + * @param[in] chn_id Channel ID to flip direction for. */ extern void relay_chn_ctl_flip_direction(uint8_t chn_id); +/** + * @brief Flips the logical direction of all configured relay channels. + * + * This function iterates through all configured relay channels and swaps the + * physical GPIO pins assigned to the forward and reverse directions for each. + */ +extern void relay_chn_ctl_flip_direction_all(void); + /** * @brief Get the current direction of a relay channel. * @@ -66,11 +110,28 @@ extern void relay_chn_ctl_flip_direction(uint8_t chn_id); */ extern relay_chn_direction_t relay_chn_ctl_get_direction(uint8_t chn_id); +/** + * @brief Gets the current logical direction of all configured relay channels. + * + * This function populates an array with the current logical directions of all + * configured relay channels. The caller must ensure the `directions` array is + * large enough to hold `CONFIG_RELAY_CHN_COUNT` elements. + * + * @param directions Pointer to an array where the directions will be stored. + * @return ESP_OK on success, ESP_ERR_INVALID_ARG if `directions` is NULL. + */ +esp_err_t relay_chn_ctl_get_direction_all(relay_chn_direction_t *directions); + static inline relay_chn_state_t relay_chn_get_state(uint8_t chn_id) { return relay_chn_ctl_get_state(chn_id); } +static inline esp_err_t relay_chn_get_state_all(relay_chn_state_t *states) +{ + return relay_chn_ctl_get_state_all(states); +} + static inline char *relay_chn_get_state_str(uint8_t chn_id) { return relay_chn_ctl_get_state_str(chn_id); @@ -81,26 +142,51 @@ static inline void relay_chn_run_forward(uint8_t chn_id) relay_chn_ctl_run_forward(chn_id); } +static inline void relay_chn_run_forward_all(void) +{ + relay_chn_ctl_run_forward_all(); +} + static inline void relay_chn_run_reverse(uint8_t chn_id) { relay_chn_ctl_run_reverse(chn_id); } +static inline void relay_chn_run_reverse_all(void) +{ + relay_chn_ctl_run_reverse_all(); +} + static inline void relay_chn_stop(uint8_t chn_id) { relay_chn_ctl_stop(chn_id); } +static inline void relay_chn_stop_all(void) +{ + relay_chn_ctl_stop_all(); +} + static inline void relay_chn_flip_direction(uint8_t chn_id) { relay_chn_ctl_flip_direction(chn_id); } +static inline void relay_chn_flip_direction_all(void) +{ + relay_chn_ctl_flip_direction_all(); +} + static inline relay_chn_direction_t relay_chn_get_direction(uint8_t chn_id) { return relay_chn_ctl_get_direction(chn_id); } +static inline esp_err_t relay_chn_get_direction_all(relay_chn_direction_t *directions) +{ + return relay_chn_ctl_get_direction_all(directions); +} + #if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT /** * @brief Get the run limit for the specified channel @@ -112,6 +198,18 @@ static inline relay_chn_direction_t relay_chn_get_direction(uint8_t chn_id) */ extern uint16_t relay_chn_ctl_get_run_limit(uint8_t chn_id); +/** + * @brief Gets the configured run limits for all configured relay channels. + * + * This function populates an array with the run limits (in seconds) of all + * configured relay channels. The caller must ensure the `limits_sec` array is + * large enough to hold `CONFIG_RELAY_CHN_COUNT` elements. + * + * @param limits_sec Pointer to an array where the run limits will be stored. + * @return ESP_OK on success, ESP_ERR_INVALID_ARG if `limits_sec` is NULL. + */ +esp_err_t relay_chn_ctl_get_run_limit_all(uint16_t *limits_sec); + /** * @brief Set the run limit for the specified channel * @@ -120,15 +218,55 @@ extern uint16_t relay_chn_ctl_get_run_limit(uint8_t chn_id); */ extern void relay_chn_ctl_set_run_limit(uint8_t chn_id, uint16_t time_sec); +/** + * @brief Sets the run limits for all configured relay channels. + * + * This function iterates through all configured relay channels and sets their + * run limits based on the values provided in the `limits_sec` array. Each value + * will be clamped within the configured `RELAY_CHN_RUN_LIMIT_MIN_SEC` and + * `RELAY_CHN_RUN_LIMIT_MAX_SEC` boundaries. The new run limits are persisted + * in NVS if enabled. + * + * @param limits_sec Pointer to an array containing the desired run limits in seconds. + * @return ESP_OK on success, ESP_ERR_INVALID_ARG if `limits_sec` is NULL. + */ +esp_err_t relay_chn_ctl_set_run_limit_all(uint16_t *limits_sec); + +/** + * @brief Sets a single run limit value for all configured relay channels. + * + * This function sets the same `limit_sec` value for all configured relay channels. + * The value will be clamped within the configured `RELAY_CHN_RUN_LIMIT_MIN_SEC` + * and `RELAY_CHN_RUN_LIMIT_MAX_SEC` boundaries. + * @param limit_sec The desired run limit in seconds to apply to all channels. + * @return ESP_OK on success. + */ +esp_err_t relay_chn_ctl_set_run_limit_all_with(uint16_t limit_sec); + static inline uint16_t relay_chn_get_run_limit(uint8_t chn_id) { return relay_chn_ctl_get_run_limit(chn_id); } +static inline esp_err_t relay_chn_get_run_limit_all(uint16_t *limits_sec) +{ + return relay_chn_ctl_get_run_limit_all(limits_sec); +} + static inline void relay_chn_set_run_limit(uint8_t chn_id, uint16_t time_sec) { relay_chn_ctl_set_run_limit(chn_id, time_sec); } + +static inline esp_err_t relay_chn_set_run_limit_all(uint16_t *limits_sec) +{ + return relay_chn_ctl_set_run_limit_all(limits_sec); +} + +static inline esp_err_t relay_chn_set_run_limit_all_with(uint16_t limit_sec) +{ + return relay_chn_ctl_set_run_limit_all_with(limit_sec); +} #endif // CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT == 1 #else diff --git a/include/relay_chn_defs.h b/include/relay_chn_defs.h deleted file mode 100644 index b044f8f..0000000 --- a/include/relay_chn_defs.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2025 Kozmotronik Tech - * - * SPDX-License-Identifier: MIT - */ - - #pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -#if RELAY_CHN_COUNT > 1 -#define RELAY_CHN_ID_ALL RELAY_CHN_COUNT /*!< Special ID to address all channels */ -#endif - -#ifdef __cplusplus -} -#endif \ No newline at end of file diff --git a/include/relay_chn_types.h b/include/relay_chn_types.h index 8960cc2..8f3a5e6 100644 --- a/include/relay_chn_types.h +++ b/include/relay_chn_types.h @@ -7,7 +7,6 @@ #pragma once #include -#include "relay_chn_defs.h" #ifdef __cplusplus extern "C" { diff --git a/private_include/relay_chn_core.h b/private_include/relay_chn_core.h index 62d0e4c..c98bb33 100644 --- a/private_include/relay_chn_core.h +++ b/private_include/relay_chn_core.h @@ -9,7 +9,6 @@ #include "esp_err.h" #include "esp_log.h" #include "esp_timer.h" -#include "relay_chn_defs.h" #include "relay_chn_types.h" #include "relay_chn_priv_types.h" diff --git a/scripts/run_tests.sh b/scripts/run_tests.sh index 2320339..5cd050e 100755 --- a/scripts/run_tests.sh +++ b/scripts/run_tests.sh @@ -10,7 +10,7 @@ if [[ -z "$IDF_PATH" ]]; then fi # ==== 2. Valid Modes and Defaults ==== -valid_test_tags=("core" "tilt" "listener" "all" "relay_chn" "nvs" "run_limit") +valid_test_tags=("core" "tilt" "listener" "all" "relay_chn" "nvs" "run_limit" "batch") arg_tag="all" # Default to 'all' if no tag specified arg_clean=false arg_log=false @@ -24,7 +24,7 @@ print_help() { echo "This script builds and runs tests for the relay_chn component using QEMU." echo "" echo "Arguments:" - echo " -t, --tag [relay_chn|core|tilt|listener|nvs|run_limit|all] Specify which test tag to run." + echo " -t, --tag [relay_chn|core|tilt|listener|nvs|run_limit|batch|all] Specify which test tag to run." echo "" echo " If no tag is specified, it defaults to 'all'." echo "" diff --git a/src/relay_chn_core.c b/src/relay_chn_core.c index f57969e..f062e1b 100644 --- a/src/relay_chn_core.c +++ b/src/relay_chn_core.c @@ -407,7 +407,7 @@ void relay_chn_issue_cmd(relay_chn_ctl_t* chn_ctl, relay_chn_cmd_t cmd) #if CONFIG_RELAY_CHN_COUNT > 1 bool relay_chn_is_channel_id_valid(uint8_t chn_id) { - bool valid = (chn_id < CONFIG_RELAY_CHN_COUNT) || chn_id == RELAY_CHN_ID_ALL; + bool valid = chn_id < CONFIG_RELAY_CHN_COUNT; if (!valid) { ESP_LOGE(TAG, "Invalid channel ID: %d", chn_id); } diff --git a/src/relay_chn_ctl_multi.c b/src/relay_chn_ctl_multi.c index 212b941..14bf3a4 100644 --- a/src/relay_chn_ctl_multi.c +++ b/src/relay_chn_ctl_multi.c @@ -72,20 +72,33 @@ void relay_chn_ctl_deinit() relay_chn_state_t relay_chn_ctl_get_state(uint8_t chn_id) { - if (!relay_chn_is_channel_id_valid(chn_id) || chn_id == RELAY_CHN_ID_ALL) { - return RELAY_CHN_STATE_UNDEFINED; + return relay_chn_is_channel_id_valid(chn_id) ? + chn_ctls[chn_id].state : RELAY_CHN_STATE_UNDEFINED; +} + +esp_err_t relay_chn_ctl_get_state_all(relay_chn_state_t *states) +{ + ESP_RETURN_ON_FALSE(states != NULL, ESP_ERR_INVALID_ARG, TAG, "states cannot be NULL"); + + for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { + relay_chn_state_t *dest_state = &states[i]; + if (dest_state == NULL) { + ESP_LOGW(TAG, "get_state_all: States have been copied until channel %d since states[%d] is NULL", i, i); + break; + } + *dest_state = chn_ctls[i].state; } - return chn_ctls[chn_id].state; + return ESP_OK; } char *relay_chn_ctl_get_state_str(uint8_t chn_id) { - if (!relay_chn_is_channel_id_valid(chn_id) || chn_id == RELAY_CHN_ID_ALL) { - return relay_chn_state_str(RELAY_CHN_STATE_UNDEFINED); - } - return relay_chn_state_str(chn_ctls[chn_id].state); + return relay_chn_is_channel_id_valid(chn_id) + ? relay_chn_state_str(chn_ctls[chn_id].state) + : relay_chn_state_str(RELAY_CHN_STATE_UNDEFINED); } + static void relay_chn_ctl_issue_cmd_on_all_channels(relay_chn_cmd_t cmd) { for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { @@ -95,94 +108,142 @@ static void relay_chn_ctl_issue_cmd_on_all_channels(relay_chn_cmd_t cmd) void relay_chn_ctl_run_forward(uint8_t chn_id) { - if (!relay_chn_is_channel_id_valid(chn_id)) return; + if (relay_chn_is_channel_id_valid(chn_id)) + relay_chn_issue_cmd(&chn_ctls[chn_id], RELAY_CHN_CMD_FORWARD); +} - if (chn_id == RELAY_CHN_ID_ALL) { - relay_chn_ctl_issue_cmd_on_all_channels(RELAY_CHN_CMD_FORWARD); - return; - } - relay_chn_issue_cmd(&chn_ctls[chn_id], RELAY_CHN_CMD_FORWARD); +void relay_chn_ctl_run_forward_all() +{ + relay_chn_ctl_issue_cmd_on_all_channels(RELAY_CHN_CMD_FORWARD); } void relay_chn_ctl_run_reverse(uint8_t chn_id) { - if (!relay_chn_is_channel_id_valid(chn_id)) return; + if (relay_chn_is_channel_id_valid(chn_id)) + relay_chn_issue_cmd(&chn_ctls[chn_id], RELAY_CHN_CMD_REVERSE); +} - if (chn_id == RELAY_CHN_ID_ALL) { - relay_chn_ctl_issue_cmd_on_all_channels(RELAY_CHN_CMD_REVERSE); - return; - } - relay_chn_issue_cmd(&chn_ctls[chn_id], RELAY_CHN_CMD_REVERSE); +void relay_chn_ctl_run_reverse_all() +{ + relay_chn_ctl_issue_cmd_on_all_channels(RELAY_CHN_CMD_REVERSE); } void relay_chn_ctl_stop(uint8_t chn_id) { - if (!relay_chn_is_channel_id_valid(chn_id)) return; + if (relay_chn_is_channel_id_valid(chn_id)) + relay_chn_issue_cmd(&chn_ctls[chn_id], RELAY_CHN_CMD_STOP); +} - if (chn_id == RELAY_CHN_ID_ALL) { - relay_chn_ctl_issue_cmd_on_all_channels(RELAY_CHN_CMD_STOP); - return; - } - relay_chn_issue_cmd(&chn_ctls[chn_id], RELAY_CHN_CMD_STOP); +void relay_chn_ctl_stop_all() +{ + relay_chn_ctl_issue_cmd_on_all_channels(RELAY_CHN_CMD_STOP); } void relay_chn_ctl_flip_direction(uint8_t chn_id) { - if (!relay_chn_is_channel_id_valid(chn_id)) return; + if (relay_chn_is_channel_id_valid(chn_id)) + relay_chn_issue_cmd(&chn_ctls[chn_id], RELAY_CHN_CMD_FLIP); +} - if (chn_id == RELAY_CHN_ID_ALL) { - relay_chn_ctl_issue_cmd_on_all_channels(RELAY_CHN_CMD_FLIP); - return; - } - relay_chn_issue_cmd(&chn_ctls[chn_id], RELAY_CHN_CMD_FLIP); +void relay_chn_ctl_flip_direction_all() +{ + relay_chn_ctl_issue_cmd_on_all_channels(RELAY_CHN_CMD_FLIP); } relay_chn_direction_t relay_chn_ctl_get_direction(uint8_t chn_id) { - if (!relay_chn_is_channel_id_valid(chn_id)) { - return RELAY_CHN_DIRECTION_DEFAULT; + return relay_chn_is_channel_id_valid(chn_id) + ? relay_chn_output_get_direction(chn_ctls[chn_id].output) + : RELAY_CHN_DIRECTION_DEFAULT; +} + +esp_err_t relay_chn_ctl_get_direction_all(relay_chn_direction_t *directions) +{ + ESP_RETURN_ON_FALSE(directions != NULL, ESP_ERR_INVALID_ARG, TAG, "directions cannot be NULL"); + + for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { + relay_chn_direction_t *dest_direction = &directions[i]; + if (dest_direction == NULL) { + ESP_LOGW(TAG, "get_direction_all: Directions have been copied until channel %d since directions[%d] is NULL", i, i); + break; + } + *dest_direction = relay_chn_output_get_direction(chn_ctls[i].output); } - relay_chn_ctl_t *chn_ctl = &chn_ctls[chn_id]; - return relay_chn_output_get_direction(chn_ctl->output); + return ESP_OK; } #if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT uint16_t relay_chn_ctl_get_run_limit(uint8_t chn_id) { - if (!relay_chn_is_channel_id_valid(chn_id) || chn_id == RELAY_CHN_ID_ALL) { - ESP_LOGE(TAG, "get_run_limit: Invalid channel ID: %d", chn_id); - return 0; - } - return chn_ctls[chn_id].run_limit_sec; + return relay_chn_is_channel_id_valid(chn_id) ? chn_ctls[chn_id].run_limit_sec : 0; } -void relay_chn_ctl_set_run_limit(uint8_t chn_id, uint16_t time_sec) +esp_err_t relay_chn_ctl_get_run_limit_all(uint16_t *limits_sec) { - if (!relay_chn_is_channel_id_valid(chn_id) || chn_id == RELAY_CHN_ID_ALL) { + ESP_RETURN_ON_FALSE(limits_sec != NULL, ESP_ERR_INVALID_ARG, TAG, "limits_sec cannot be NULL"); + + for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { + uint16_t *dest_limit_sec = &limits_sec[i]; + if (dest_limit_sec == NULL) { + ESP_LOGW(TAG, "get_run_limit_all: Run limits have been copied until channel %d since limits_sec[%d] is NULL", i, i); + break; + } + *dest_limit_sec = chn_ctls[i].run_limit_sec; + } + return ESP_OK; +} + +static void relay_chn_ctl_set_run_limit_common(uint8_t chn_id, uint16_t limit_sec) +{ + // Check for boundaries + if (limit_sec > CONFIG_RELAY_CHN_RUN_LIMIT_MAX_SEC) + limit_sec = CONFIG_RELAY_CHN_RUN_LIMIT_MAX_SEC; + else if (limit_sec < CONFIG_RELAY_CHN_RUN_LIMIT_MIN_SEC) + limit_sec = CONFIG_RELAY_CHN_RUN_LIMIT_MIN_SEC; + + chn_ctls[chn_id].run_limit_sec = limit_sec; + +#if CONFIG_RELAY_CHN_ENABLE_NVS + relay_chn_nvs_set_run_limit(chn_id, limit_sec); +#endif +} + +void relay_chn_ctl_set_run_limit(uint8_t chn_id, uint16_t limit_sec) +{ + if (!relay_chn_is_channel_id_valid(chn_id)) { ESP_LOGE(TAG, "set_run_limit: Invalid channel ID: %d", chn_id); return; } - - // Check for boundaries - if (time_sec > CONFIG_RELAY_CHN_RUN_LIMIT_MAX_SEC) - time_sec = CONFIG_RELAY_CHN_RUN_LIMIT_MAX_SEC; - else if (time_sec < CONFIG_RELAY_CHN_RUN_LIMIT_MIN_SEC) - time_sec = CONFIG_RELAY_CHN_RUN_LIMIT_MIN_SEC; + relay_chn_ctl_set_run_limit_common(chn_id, limit_sec); +} - chn_ctls[chn_id].run_limit_sec = time_sec; +esp_err_t relay_chn_ctl_set_run_limit_all(uint16_t *limits_sec) +{ + ESP_RETURN_ON_FALSE(limits_sec != NULL, ESP_ERR_INVALID_ARG, TAG, "limits_sec cannot be NULL"); -#if CONFIG_RELAY_CHN_ENABLE_NVS - relay_chn_nvs_set_run_limit(chn_id, time_sec); -#endif -} + for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { + uint16_t *src_limit_sec = &limits_sec[i]; + if (src_limit_sec == NULL) { + ESP_LOGW(TAG, "set_run_limit_all: Run limits have been set until channel %d since limits_sec[%d] is NULL", i, i); + break; + } + relay_chn_ctl_set_run_limit_common(i, *src_limit_sec); + } + return ESP_OK; +} + +esp_err_t relay_chn_ctl_set_run_limit_all_with(uint16_t limit_sec) +{ + for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { + relay_chn_ctl_set_run_limit_common(i, limit_sec); + } + return ESP_OK; +} #endif relay_chn_ctl_t *relay_chn_ctl_get(uint8_t chn_id) { - if (!relay_chn_is_channel_id_valid(chn_id)) { - return NULL; - } - return &chn_ctls[chn_id]; + return relay_chn_is_channel_id_valid(chn_id) ? &chn_ctls[chn_id] : NULL; } relay_chn_ctl_t *relay_chn_ctl_get_all(void) diff --git a/src/relay_chn_output.c b/src/relay_chn_output.c index b7ea44e..b0d2c0f 100644 --- a/src/relay_chn_output.c +++ b/src/relay_chn_output.c @@ -6,7 +6,6 @@ #include "esp_check.h" #include "esp_log.h" -#include "relay_chn_defs.h" #include "relay_chn_output.h" #include "relay_chn_core.h" diff --git a/src/relay_chn_tilt.c b/src/relay_chn_tilt.c index c48c976..99f2409 100644 --- a/src/relay_chn_tilt.c +++ b/src/relay_chn_tilt.c @@ -106,6 +106,7 @@ static void relay_chn_tilt_issue_cmd(relay_chn_tilt_ctl_t *tilt_ctl, relay_chn_t // Set the command that will be processed tilt_ctl->cmd = cmd; + ESP_LOGI(TAG, "relay_chn_tilt_issue_cmd: Command-chn: %d-%d", cmd, tilt_ctl->chn_ctl->id); // TODO delete switch (tilt_ctl->chn_ctl->state) { case RELAY_CHN_STATE_IDLE: // Relay channel is free, tilt can be issued immediately @@ -176,78 +177,67 @@ static void relay_chn_tilt_issue_auto(relay_chn_tilt_ctl_t *tilt_ctl) } } + #if CONFIG_RELAY_CHN_COUNT > 1 -void relay_chn_tilt_auto(uint8_t chn_id) -{ - if (!relay_chn_is_channel_id_valid(chn_id)) { - return; - } - - // Execute for all channels - if (chn_id == RELAY_CHN_ID_ALL) { - for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { - relay_chn_tilt_issue_auto(&tilt_ctls[i]); - } - } - // Execute for a single channel - else { - relay_chn_tilt_ctl_t* tilt_ctl = &tilt_ctls[chn_id]; - relay_chn_tilt_issue_auto(tilt_ctl); - } -} - static void relay_chn_tilt_issue_cmd_on_all_channels(relay_chn_tilt_cmd_t cmd) { for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { relay_chn_tilt_ctl_t* tilt_ctl = &tilt_ctls[i]; + ESP_LOGI(TAG, "issue_cmd_on_all_channels: Command|chn|ctl.id: %d|%d|%d", cmd, i, tilt_ctl->chn_ctl->id); // TODO delete relay_chn_tilt_issue_cmd(tilt_ctl, cmd); } } +void relay_chn_tilt_auto(uint8_t chn_id) +{ + if (relay_chn_is_channel_id_valid(chn_id)) { + relay_chn_tilt_issue_auto(&tilt_ctls[chn_id]); + } +} + +void relay_chn_tilt_auto_all() +{ + for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { + relay_chn_tilt_issue_auto(&tilt_ctls[i]); + } +} + void relay_chn_tilt_forward(uint8_t chn_id) { - if (!relay_chn_is_channel_id_valid(chn_id)) { - return; - } - - if (chn_id == RELAY_CHN_ID_ALL) - relay_chn_tilt_issue_cmd_on_all_channels(RELAY_CHN_TILT_CMD_FORWARD); - else { - relay_chn_tilt_ctl_t* tilt_ctl = &tilt_ctls[chn_id]; - relay_chn_tilt_issue_cmd(tilt_ctl, RELAY_CHN_TILT_CMD_FORWARD); + if (relay_chn_is_channel_id_valid(chn_id)) { + relay_chn_tilt_issue_cmd(&tilt_ctls[chn_id], RELAY_CHN_TILT_CMD_FORWARD); } } +void relay_chn_tilt_forward_all() +{ + relay_chn_tilt_issue_cmd_on_all_channels(RELAY_CHN_TILT_CMD_FORWARD); +} + void relay_chn_tilt_reverse(uint8_t chn_id) { - if (!relay_chn_is_channel_id_valid(chn_id)) { - return; + if (relay_chn_is_channel_id_valid(chn_id)) { + relay_chn_tilt_issue_cmd(&tilt_ctls[chn_id], RELAY_CHN_TILT_CMD_REVERSE); } - - if (chn_id == RELAY_CHN_ID_ALL) +} + +void relay_chn_tilt_reverse_all() +{ relay_chn_tilt_issue_cmd_on_all_channels(RELAY_CHN_TILT_CMD_REVERSE); - else { - relay_chn_tilt_ctl_t* tilt_ctl = &tilt_ctls[chn_id]; - relay_chn_tilt_issue_cmd(tilt_ctl, RELAY_CHN_TILT_CMD_REVERSE); - } } void relay_chn_tilt_stop(uint8_t chn_id) { if (!relay_chn_is_channel_id_valid(chn_id)) { - return; - } - - if (chn_id == RELAY_CHN_ID_ALL) { - for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { - relay_chn_tilt_dispatch_cmd(&tilt_ctls[i], RELAY_CHN_TILT_CMD_STOP); - } - } - else { relay_chn_tilt_dispatch_cmd(&tilt_ctls[chn_id], RELAY_CHN_TILT_CMD_STOP); } } +void relay_chn_tilt_stop_all() +{ + relay_chn_tilt_issue_cmd_on_all_channels(RELAY_CHN_TILT_CMD_STOP); +} + #else // CONFIG_RELAY_CHN_COUNT > 1 void relay_chn_tilt_auto() @@ -317,45 +307,61 @@ static void relay_chn_tilt_compute_set_sensitivity(relay_chn_tilt_ctl_t *tilt_ct #if CONFIG_RELAY_CHN_COUNT > 1 void relay_chn_tilt_set_sensitivity(uint8_t chn_id, uint8_t sensitivity) { - if (!relay_chn_is_channel_id_valid(chn_id)) { - return; - } - - if (chn_id == RELAY_CHN_ID_ALL) { - for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { - relay_chn_tilt_compute_set_sensitivity(&tilt_ctls[i], sensitivity); - } - } - else { + if (relay_chn_is_channel_id_valid(chn_id)) { relay_chn_tilt_compute_set_sensitivity(&tilt_ctls[chn_id], sensitivity); - } - + #if CONFIG_RELAY_CHN_ENABLE_NVS - relay_chn_nvs_set_tilt_sensitivity(chn_id, sensitivity); + relay_chn_nvs_set_tilt_sensitivity(chn_id, sensitivity); #endif // CONFIG_RELAY_CHN_ENABLE_NVS + } } -esp_err_t relay_chn_tilt_get_sensitivity(uint8_t chn_id, uint8_t *sensitivity, size_t length) +esp_err_t relay_chn_tilt_set_sensitivity_all(uint8_t *sensitivities) { - if (!relay_chn_is_channel_id_valid(chn_id)) { - return ESP_ERR_INVALID_ARG; - } - if (sensitivity == NULL) { - ESP_LOGD(TAG, "relay_chn_tilt_get_sensitivity: sensitivity is NULL"); - return ESP_ERR_INVALID_ARG; - } - if (chn_id == RELAY_CHN_ID_ALL) { - if (length < CONFIG_RELAY_CHN_COUNT) { - ESP_LOGD(TAG, "relay_chn_tilt_get_sensitivity: length is too short to store all sensitivity values"); - return ESP_ERR_INVALID_ARG; + ESP_RETURN_ON_FALSE(sensitivities != NULL, ESP_ERR_INVALID_ARG, TAG, "set_sensitivity_all: sensitivities is NULL"); + + for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { + uint8_t *src_sensitivity = &sensitivities[i]; + if (src_sensitivity == NULL) { + ESP_LOGW(TAG, "set_sensitivity_all: Run limits have been set until channel %d since sensitivities[%d] is NULL", i, i); + break; } + relay_chn_tilt_compute_set_sensitivity(&tilt_ctls[i], *src_sensitivity); +#if CONFIG_RELAY_CHN_ENABLE_NVS + relay_chn_nvs_set_tilt_sensitivity(i, *src_sensitivity); +#endif // CONFIG_RELAY_CHN_ENABLE_NVS + } + return ESP_OK; +} - for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { - sensitivity[i] = tilt_ctls[i].tilt_timing.sensitivity; - } - return ESP_OK; +void relay_chn_tilt_set_sensitivity_all_with(uint8_t sensitivity) +{ + for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { + relay_chn_tilt_compute_set_sensitivity(&tilt_ctls[i], sensitivity); +#if CONFIG_RELAY_CHN_ENABLE_NVS + relay_chn_nvs_set_tilt_sensitivity(i, sensitivity); +#endif // CONFIG_RELAY_CHN_ENABLE_NVS + } +} + +uint8_t relay_chn_tilt_get_sensitivity(uint8_t chn_id) +{ + return relay_chn_is_channel_id_valid(chn_id) ? + tilt_ctls[chn_id].tilt_timing.sensitivity : 0; +} + +esp_err_t relay_chn_tilt_get_sensitivity_all(uint8_t *sensitivities) +{ + ESP_RETURN_ON_FALSE(sensitivities != NULL, ESP_ERR_INVALID_ARG, TAG, "get_sensitivity_all: sensitivities is NULL"); + + for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { + uint8_t *dest_sensitivity = &sensitivities[i]; + if (dest_sensitivity == NULL) { + ESP_LOGW(TAG, "get_sensitivity_all: Sensitivites have been copied until channel %d since sensitivities[%d] is NULL", i, i); + break; + } + *dest_sensitivity = tilt_ctls[i].tilt_timing.sensitivity; } - *sensitivity = tilt_ctls[chn_id].tilt_timing.sensitivity; return ESP_OK; } @@ -547,6 +553,7 @@ static void relay_chn_tilt_execute_pause(relay_chn_tilt_ctl_t *tilt_ctl) esp_err_t relay_chn_tilt_dispatch_cmd(relay_chn_tilt_ctl_t *tilt_ctl, relay_chn_tilt_cmd_t cmd) { ESP_LOGD(TAG, "relay_chn_tilt_dispatch_cmd: Command: %d", cmd); + ESP_LOGI(TAG, "tilt_dispatch_cmd: Command-chn: %d-%d", cmd, tilt_ctl->chn_ctl->id); // TODO delete switch(cmd) { case RELAY_CHN_TILT_CMD_STOP: @@ -666,8 +673,9 @@ esp_err_t relay_chn_tilt_init(relay_chn_ctl_t *chn_ctls) #if CONFIG_RELAY_CHN_COUNT > 1 for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { + esp_err_t ret; #if CONFIG_RELAY_CHN_ENABLE_NVS - esp_err_t ret = relay_chn_tilt_load_sensitivity(i, &sensitivity); + 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_count(i, &tilt_count); ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load tilt count for channel %d", i); diff --git a/test_apps/main/test_common.c b/test_apps/main/test_common.c index 3db95de..21986a4 100644 --- a/test_apps/main/test_common.c +++ b/test_apps/main/test_common.c @@ -39,7 +39,7 @@ const uint8_t gpio_count = sizeof(gpio_map) / sizeof(gpio_map[0]); void reset_channels_to_idle_state() { #if CONFIG_RELAY_CHN_COUNT > 1 - relay_chn_stop(RELAY_CHN_ID_ALL); + relay_chn_stop_all(); vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms)); for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state(i)); diff --git a/test_apps/main/test_relay_chn_core_multi.c b/test_apps/main/test_relay_chn_core_multi.c index f684535..b33ab9a 100644 --- a/test_apps/main/test_relay_chn_core_multi.c +++ b/test_apps/main/test_relay_chn_core_multi.c @@ -70,11 +70,10 @@ TEST_CASE("Relay channels run reverse and update state", "[relay_chn][core]") { } -// ### Broadcast Command (RELAY_CHN_ID_ALL) Tests - -TEST_CASE("run_forward with ID_ALL sets all channels to FORWARD", "[relay_chn][core][id_all]") +// ### Batch Control Tests +TEST_CASE("run_forward_all sets all channels to FORWARD", "[relay_chn][core][batch]") { - relay_chn_run_forward(RELAY_CHN_ID_ALL); + relay_chn_run_forward_all(); vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); for (uint8_t i = 0; i < relay_chn_count; i++) { @@ -82,9 +81,9 @@ TEST_CASE("run_forward with ID_ALL sets all channels to FORWARD", "[relay_chn][c } } -TEST_CASE("run_reverse with ID_ALL sets all channels to REVERSE", "[relay_chn][core][id_all]") +TEST_CASE("run_reverse_all sets all channels to REVERSE", "[relay_chn][core][batch]") { - relay_chn_run_reverse(RELAY_CHN_ID_ALL); + relay_chn_run_reverse_all(); vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); for (uint8_t i = 0; i < relay_chn_count; i++) { @@ -92,14 +91,14 @@ TEST_CASE("run_reverse with ID_ALL sets all channels to REVERSE", "[relay_chn][c } } -TEST_CASE("stop with ID_ALL stops all running channels", "[relay_chn][core][id_all]") +TEST_CASE("stop_all stops all running channels", "[relay_chn][core][batch]") { // 1. Start all channels forward to ensure they are in a known running state - relay_chn_run_forward(RELAY_CHN_ID_ALL); + relay_chn_run_forward_all(); vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); - // 2. Stop all channels using the broadcast command - relay_chn_stop(RELAY_CHN_ID_ALL); + // 2. Stop all channels + relay_chn_stop_all(); vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms)); // 3. Verify all channels have transitioned to the FREE state @@ -138,7 +137,7 @@ TEST_CASE("Get state returns UNDEFINED when id is invalid", "[relay_chn][core]") TEST_ASSERT_EQUAL(RELAY_CHN_STATE_UNDEFINED, relay_chn_get_state(invalid_id)); } // Test for running states also - relay_chn_run_forward(RELAY_CHN_ID_ALL); + relay_chn_run_forward_all(); vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); for (uint8_t i = 0; i < relay_chn_count; i++) { int invalid_id = relay_chn_count * 2 + i; @@ -153,7 +152,7 @@ TEST_CASE("Get state string returns UNKNOWN when id is invalid", "[relay_chn][co TEST_ASSERT_EQUAL_STRING("UNKNOWN", relay_chn_get_state_str(invalid_id)); } // Test for running states also - relay_chn_run_forward(RELAY_CHN_ID_ALL); + relay_chn_run_forward_all(); vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); for (uint8_t i = 0; i < relay_chn_count; i++) { int invalid_id = relay_chn_count * 2 + i; @@ -295,10 +294,10 @@ TEST_CASE("Single channel direction can be flipped", "[relay_chn][core][directio TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, relay_chn_get_direction(ch)); } -TEST_CASE("All channels direction can be flipped simultaneously", "[relay_chn][core][direction][id_all]") +TEST_CASE("All channels direction can be flipped simultaneously", "[relay_chn][core][direction][batch]") { // 1. Flip all channels - relay_chn_flip_direction(RELAY_CHN_ID_ALL); + relay_chn_flip_direction_all(); vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms)); // 2. Verify all channels are flipped @@ -307,7 +306,7 @@ TEST_CASE("All channels direction can be flipped simultaneously", "[relay_chn][c } // 3. Flip all back - relay_chn_flip_direction(RELAY_CHN_ID_ALL); + relay_chn_flip_direction_all(); vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms)); // 4. Verify all channels are back to default @@ -380,7 +379,7 @@ TEST_CASE("Test run limit stops channel after timeout", "[relay_chn][run_limit]" relay_chn_set_run_limit(i, TEST_SHORT_RUN_LIMIT_SEC); } - relay_chn_run_forward(RELAY_CHN_ID_ALL); + relay_chn_run_forward_all(); for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { // Check running forward @@ -407,7 +406,7 @@ TEST_CASE("Test run limit reset on direction change and time out finally", "[rel vTaskDelay(1000 / portTICK_PERIOD_MS); // Wait 1 second // Change direction before timeout - relay_chn_run_reverse(RELAY_CHN_ID_ALL); + relay_chn_run_reverse_all(); // Wait for the inertia period (after which the reverse command will be dispatched) vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms)); diff --git a/test_apps/main/test_relay_chn_tilt_multi.c b/test_apps/main/test_relay_chn_tilt_multi.c index 2f17e47..bc4aa32 100644 --- a/test_apps/main/test_relay_chn_tilt_multi.c +++ b/test_apps/main/test_relay_chn_tilt_multi.c @@ -162,7 +162,7 @@ TEST_CASE("Tilt Forward to Run Reverse transition without inertia", "[relay_chn] } // TEST_CASE: Test stopping from a tilt state (no inertia for stop command itself) -// Scenario: RELAY_CHN_STATE_TILT_FORWARD -> (relay_chn_stop) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_IDLE +// Scenario: RELAY_CHN_STATE_TILT_FORWARD -> (relay_chn_tilt_stop) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_IDLE TEST_CASE("Tilt to Stop transition without immediate inertia for stop", "[relay_chn][tilt][inertia]") { uint8_t ch = 0; @@ -173,15 +173,15 @@ TEST_CASE("Tilt to Stop transition without immediate inertia for stop", "[relay_ TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(ch)); // 2. Issue stop command - relay_chn_stop(ch); + 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)); TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state(ch)); } -// ### Tilt Broadcast Command (RELAY_CHN_ID_ALL) Tests +// ### Batch Tilt Control Tests -TEST_CASE("tilt_forward with ID_ALL sets all channels to TILT_FORWARD", "[relay_chn][tilt][id_all]") +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++) { @@ -189,16 +189,17 @@ TEST_CASE("tilt_forward with ID_ALL sets all channels to TILT_FORWARD", "[relay_ } // 2. Issue tilt forward to all channels - relay_chn_tilt_forward(RELAY_CHN_ID_ALL); + relay_chn_tilt_forward_all(); 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++) { + ESP_LOGI(TEST_TAG, "Checking channel %d", i); TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(i)); } } -TEST_CASE("tilt_reverse with ID_ALL sets all channels to TILT_REVERSE", "[relay_chn][tilt][id_all]") +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++) { @@ -206,7 +207,7 @@ TEST_CASE("tilt_reverse with ID_ALL sets all channels to TILT_REVERSE", "[relay_ } // 2. Issue tilt reverse to all channels - relay_chn_tilt_reverse(RELAY_CHN_ID_ALL); + relay_chn_tilt_reverse_all(); vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); // 3. Verify all channels are tilting reverse @@ -215,26 +216,27 @@ TEST_CASE("tilt_reverse with ID_ALL sets all channels to TILT_REVERSE", "[relay_ } } -TEST_CASE("tilt_stop with ID_ALL stops all tilting channels", "[relay_chn][tilt][id_all]") +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++) { prepare_channel_for_tilt(i, RELAY_CHN_CMD_REVERSE); } - relay_chn_tilt_forward(RELAY_CHN_ID_ALL); + relay_chn_tilt_forward_all(); vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); // 2. Stop tilting on all channels - relay_chn_tilt_stop(RELAY_CHN_ID_ALL); + relay_chn_tilt_stop_all(); vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms)); // 3. Verify all channels are free for (uint8_t i = 0; i < relay_chn_count; i++) { + ESP_LOGI(TEST_TAG, "Checking channel %d", i); TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state(i)); } } -TEST_CASE("tilt_auto with ID_ALL tilts channels based on last run direction", "[relay_chn][tilt][id_all]") +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"); @@ -244,7 +246,7 @@ TEST_CASE("tilt_auto with ID_ALL tilts channels based on last run direction", "[ prepare_channel_for_tilt(1, RELAY_CHN_CMD_REVERSE); // 2. Issue auto tilt command to all channels - relay_chn_tilt_auto(RELAY_CHN_ID_ALL); + relay_chn_tilt_auto_all(); 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) @@ -273,26 +275,23 @@ TEST_CASE("relay_chn_tilt_auto chooses correct direction", "[relay_chn][tilt][au // Test sensitivity set/get TEST_CASE("relay_chn_tilt_set_sensitivity and get", "[relay_chn][tilt][sensitivity]") { uint8_t ch = 0; - uint8_t val = 0; relay_chn_tilt_set_sensitivity(ch, 0); - TEST_ESP_OK(relay_chn_tilt_get_sensitivity(ch, &val, 1)); - TEST_ASSERT_EQUAL_UINT8(0, val); + TEST_ASSERT_EQUAL_UINT8(0, relay_chn_tilt_get_sensitivity(ch)); relay_chn_tilt_set_sensitivity(ch, 50); - TEST_ESP_OK(relay_chn_tilt_get_sensitivity(ch, &val, 1)); - TEST_ASSERT_EQUAL_UINT8(50, val); + TEST_ASSERT_EQUAL_UINT8(50, relay_chn_tilt_get_sensitivity(ch)); relay_chn_tilt_set_sensitivity(ch, 100); - TEST_ESP_OK(relay_chn_tilt_get_sensitivity(ch, &val, 1)); - TEST_ASSERT_EQUAL_UINT8(100, val); - + TEST_ASSERT_EQUAL_UINT8(100, relay_chn_tilt_get_sensitivity(ch)); // Set all channels - relay_chn_tilt_set_sensitivity(RELAY_CHN_ID_ALL, 42); + relay_chn_tilt_set_sensitivity_all_with(42); + uint8_t vals[CONFIG_RELAY_CHN_COUNT] = {0}; - TEST_ESP_OK(relay_chn_tilt_get_sensitivity(RELAY_CHN_ID_ALL, vals, relay_chn_count)); - for (int i = 0; i < relay_chn_count; ++i) { - TEST_ASSERT_EQUAL_UINT8(42, vals[i]); - } + uint8_t expect[CONFIG_RELAY_CHN_COUNT]; + memset(expect, 42, CONFIG_RELAY_CHN_COUNT); + + TEST_ESP_OK(relay_chn_tilt_get_sensitivity_all(vals)); + TEST_ASSERT_EQUAL_UINT8_ARRAY(expect, vals, CONFIG_RELAY_CHN_COUNT); } // Test tilt counter logic: forward x3, reverse x3, extra reverse fails