5 Commits

Author SHA1 Message Date
0f2abc6047 Implement specific *all functions
Specific `_*all` and `_*all_with` functions were implemented to make
the API more concise and clean, and to replace the use of
`RELAY_CHN_ID_ALL`, which caused confusion within the API.

Refs #1085
2025-08-25 18:50:21 +03:00
be4a2ebef6 Fix build path 2025-08-25 18:24:37 +03:00
6ff16b5797 Fix config parameters check
Unnecessary `#if CONFIG_FOO == 1` checks were removed and the
statements were simplified to` #if CONFIG_FOO`.
`#ifdef CONFIG_FOO` statements were also changed to `#if CONFIG_FOO`
to keep the style as uniform as possible.

Refs #1085
2025-08-25 10:56:01 +03:00
5afefc4dc0 Use original names for config parameters
The config parameter names defined in the relay_chn_defs.h file
have been changed back to their original names (with the CONFIG_ prefix),
so that they are not confused with application-level defines.

Refs #1085
2025-08-25 10:24:13 +03:00
9d3f8ddbff Fix implicit-fallthrough warning.
Refs #1085.
2025-08-25 09:40:18 +03:00
27 changed files with 765 additions and 429 deletions

View File

@@ -3,7 +3,7 @@
{
"name": "ESP-IDF",
"compilerPath": "${config:idf.toolsPath}/tools/riscv32-esp-elf/esp-14.2.0_20241119/riscv32-esp-elf/bin/riscv32-esp-elf-gcc",
"compileCommands": "${config:idf.buildPath}/compile_commands.json",
"compileCommands": "${workspaceFolder}/test_apps/build/compile_commands.json",
"includePath": [
"${config:idf.espIdfPath}/components/**",
"${config:idf.espIdfPathWin}/components/**",

View File

@@ -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_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

View File

@@ -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"
@@ -64,7 +63,7 @@ esp_err_t relay_chn_register_listener(relay_chn_state_listener_t listener);
*/
void relay_chn_unregister_listener(relay_chn_state_listener_t listener);
#if RELAY_CHN_COUNT > 1
#if CONFIG_RELAY_CHN_COUNT > 1
/**
* @brief Get the state of the specified relay channel.
*
@@ -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,7 +186,19 @@ void relay_chn_flip_direction(uint8_t chn_id);
*/
relay_chn_direction_t relay_chn_get_direction(uint8_t chn_id);
#if RELAY_CHN_ENABLE_RUN_LIMIT == 1
/**
* @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,10 +235,35 @@ 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);
#endif // RELAY_CHN_ENABLE_RUN_LIMIT == 1
/**
* @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
#if CONFIG_RELAY_CHN_ENABLE_TILTING == 1
#if CONFIG_RELAY_CHN_ENABLE_TILTING
/**
* @brief Enables automatic tilting for the specified relay channel.
@@ -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,11 +377,23 @@ 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
#else // RELAY_CHN_COUNT > 1
#else // CONFIG_RELAY_CHN_COUNT > 1
/**
* @brief Get the state of the relay channel.
@@ -299,7 +453,7 @@ void relay_chn_flip_direction(void);
*/
relay_chn_direction_t relay_chn_get_direction(void);
#if RELAY_CHN_ENABLE_RUN_LIMIT == 1
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
/**
* @brief Get the run limit for the channel
*
@@ -320,10 +474,10 @@ uint16_t relay_chn_get_run_limit(void);
* @param time_sec The run limit time in seconds.
*/
void relay_chn_set_run_limit(uint16_t time_sec);
#endif // RELAY_CHN_ENABLE_RUN_LIMIT == 1
#endif // CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT == 1
#if CONFIG_RELAY_CHN_ENABLE_TILTING == 1
#if CONFIG_RELAY_CHN_ENABLE_TILTING
/**
* @brief Enables automatic tilting for the relay channel.
@@ -377,7 +531,7 @@ uint8_t relay_chn_tilt_get_sensitivity(void);
#endif // CONFIG_RELAY_CHN_ENABLE_TILTING
#endif // RELAY_CHN_COUNT > 1
#endif // CONFIG_RELAY_CHN_COUNT > 1
#ifdef __cplusplus
}

View File

@@ -4,7 +4,7 @@
* SPDX-License-Identifier: MIT
*
* An adapter header to expose the appropriate API functions to the public API
* depending on the RELAY_CHN_COUNT value which determines single or multi mode.
* depending on the CONFIG_RELAY_CHN_COUNT value which determines single or multi mode.
*/
#pragma once
@@ -13,7 +13,7 @@
extern "C" {
#endif
#if RELAY_CHN_COUNT > 1
#if CONFIG_RELAY_CHN_COUNT > 1
/**
* @brief Get the current state of a relay channel.
*
@@ -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,27 +142,52 @@ 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);
}
#if RELAY_CHN_ENABLE_RUN_LIMIT == 1
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,16 +218,56 @@ 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);
}
#endif // RELAY_CHN_ENABLE_RUN_LIMIT == 1
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
@@ -209,7 +347,7 @@ static inline relay_chn_direction_t relay_chn_get_direction(void)
return relay_chn_ctl_get_direction();
}
#if RELAY_CHN_ENABLE_RUN_LIMIT == 1
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
/**
* @brief Get the run limit for the channel
*
@@ -233,9 +371,9 @@ static inline void relay_chn_set_run_limit(uint16_t time_sec)
{
relay_chn_ctl_set_run_limit(time_sec);
}
#endif // RELAY_CHN_ENABLE_RUN_LIMIT == 1
#endif // CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT == 1
#endif // RELAY_CHN_COUNT > 1
#endif // CONFIG_RELAY_CHN_COUNT > 1
#ifdef __cplusplus
}

View File

@@ -1,40 +0,0 @@
/*
* SPDX-FileCopyrightText: 2025 Kozmotronik Tech
*
* SPDX-License-Identifier: MIT
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
/* Config defines for covenient writing */
#define RELAY_CHN_OPPOSITE_INERTIA_MS CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS
#define RELAY_CHN_COUNT CONFIG_RELAY_CHN_COUNT
#define RELAY_CHN_ENABLE_TILTING CONFIG_RELAY_CHN_ENABLE_TILTING
#define RELAY_CHN_ENABLE_NVS CONFIG_RELAY_CHN_ENABLE_NVS
#define RELAY_CHN_ENABLE_RUN_LIMIT CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
#if RELAY_CHN_ENABLE_NVS == 1
#define RELAY_CHN_NVS_NAMESPACE CONFIG_RELAY_CHN_NVS_NAMESPACE
#define RELAY_CHN_NVS_CUSTOM_PARTITION CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION
#if RELAY_CHN_NVS_CUSTOM_PARTITION == 1
#define RELAY_CHN_NVS_CUSTOM_PARTITION_NAME CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION_NAME
#endif
#endif
#if RELAY_CHN_COUNT > 1
#define RELAY_CHN_ID_ALL RELAY_CHN_COUNT /*!< Special ID to address all channels */
#endif
#if RELAY_CHN_ENABLE_RUN_LIMIT == 1
#define RELAY_CHN_RUN_LIMIT_MIN_SEC CONFIG_RELAY_CHN_RUN_LIMIT_MIN_SEC
#define RELAY_CHN_RUN_LIMIT_MAX_SEC CONFIG_RELAY_CHN_RUN_LIMIT_MAX_SEC
#define RELAY_CHN_RUN_LIMIT_DEFAULT_SEC CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC
#endif
#ifdef __cplusplus
}
#endif

View File

@@ -7,7 +7,6 @@
#pragma once
#include <stdint.h>
#include "relay_chn_defs.h"
#ifdef __cplusplus
extern "C" {
@@ -33,7 +32,7 @@ typedef enum relay_chn_state_enum {
RELAY_CHN_STATE_REVERSE, /*!< The relay channel is running in the reverse direction */
RELAY_CHN_STATE_FORWARD_PENDING, /*!< The relay channel is pending to run in the forward direction */
RELAY_CHN_STATE_REVERSE_PENDING, /*!< The relay channel is pending to run in the reverse direction */
#if CONFIG_RELAY_CHN_ENABLE_TILTING == 1
#if CONFIG_RELAY_CHN_ENABLE_TILTING
RELAY_CHN_STATE_TILT_FORWARD, /*!< The relay channel is tilting for forward */
RELAY_CHN_STATE_TILT_REVERSE, /*!< The relay channel is tilting for reverse */
#endif

View File

@@ -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"
@@ -28,7 +27,7 @@ extern "C" {
*/
esp_err_t relay_chn_init_timer(relay_chn_ctl_t *chn_ctl);
#if RELAY_CHN_ENABLE_RUN_LIMIT == 1
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
/**
* @brief Initializes the relay channel run limit timer.
*
@@ -40,7 +39,7 @@ esp_err_t relay_chn_init_timer(relay_chn_ctl_t *chn_ctl);
* @return esp_err_t ESP_OK on success, or an error code on failure.
*/
esp_err_t relay_chn_init_run_limit_timer(relay_chn_ctl_t *chn_ctl);
#endif // RELAY_CHN_ENABLE_RUN_LIMIT
#endif // CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
/**
* @brief Issues a command to the relay channel.
@@ -102,7 +101,7 @@ void relay_chn_update_state(relay_chn_ctl_t *chn_ctl, relay_chn_state_t new_stat
*/
char *relay_chn_state_str(relay_chn_state_t state);
#if RELAY_CHN_COUNT > 1
#if CONFIG_RELAY_CHN_COUNT > 1
/**
* @brief Check if the provided channel ID is valid.
*
@@ -111,7 +110,7 @@ char *relay_chn_state_str(relay_chn_state_t state);
* @return false Channel ID is invalid.
*/
bool relay_chn_is_channel_id_valid(uint8_t chn_id);
#endif // RELAY_CHN_COUNT > 1
#endif // CONFIG_RELAY_CHN_COUNT > 1
#ifdef __cplusplus
}

View File

@@ -31,7 +31,7 @@ esp_err_t relay_chn_ctl_init(relay_chn_output_t *output, relay_chn_run_info_t *r
*/
void relay_chn_ctl_deinit(void);
#if RELAY_CHN_COUNT > 1
#if CONFIG_RELAY_CHN_COUNT > 1
/**
* @brief Get the control structure for a specific relay channel.
*
@@ -49,7 +49,7 @@ relay_chn_ctl_t *relay_chn_ctl_get(uint8_t chn_id);
relay_chn_ctl_t *relay_chn_ctl_get_all(void);
#else
relay_chn_ctl_t *relay_chn_ctl_get(void);
#endif // RELAY_CHN_COUNT > 1
#endif // CONFIG_RELAY_CHN_COUNT > 1
#ifdef __cplusplus
}

View File

@@ -45,7 +45,7 @@ esp_err_t relay_chn_nvs_set_direction(uint8_t ch, relay_chn_direction_t directio
*/
esp_err_t relay_chn_nvs_get_direction(uint8_t ch, relay_chn_direction_t *direction);
#if RELAY_CHN_ENABLE_RUN_LIMIT == 1
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
/**
* @brief Store relay channel run limit in NVS.
*
@@ -63,9 +63,9 @@ esp_err_t relay_chn_nvs_set_run_limit(uint8_t ch, uint16_t time_sec);
* @return ESP_OK on success, error code otherwise.
*/
esp_err_t relay_chn_nvs_get_run_limit(uint8_t ch, uint16_t *time_sec);
#endif // RELAY_CHN_ENABLE_RUN_LIMIT == 1
#endif // CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT == 1
#ifdef RELAY_CHN_ENABLE_TILTING
#if CONFIG_RELAY_CHN_ENABLE_TILTING
/**
* @brief Store tilt sensitivity in NVS.
*
@@ -101,7 +101,7 @@ esp_err_t relay_chn_nvs_set_tilt_count(uint8_t ch, uint16_t tilt_count);
* @return ESP_OK on success, error code otherwise.
*/
esp_err_t relay_chn_nvs_get_tilt_count(uint8_t ch, uint16_t *tilt_count);
#endif // RELAY_CHN_ENABLE_TILTING
#endif // CONFIG_RELAY_CHN_ENABLE_TILTING
/**
* @brief Erase all keys in the NVS namespace.

View File

@@ -36,7 +36,7 @@ esp_err_t relay_chn_output_init(const uint8_t* gpio_map, uint8_t gpio_count);
*/
void relay_chn_output_deinit(void);
#if RELAY_CHN_COUNT > 1
#if CONFIG_RELAY_CHN_COUNT > 1
/**
* @brief Get the relay channel output object for a specific channel.
*
@@ -59,7 +59,7 @@ relay_chn_output_t *relay_chn_output_get_all(void);
* @return Pointer to relay channel output object.
*/
relay_chn_output_t *relay_chn_output_get(void);
#endif // RELAY_CHN_COUNT > 1
#endif // CONFIG_RELAY_CHN_COUNT > 1
/**
* @brief Stop the relay channel output.

View File

@@ -45,7 +45,7 @@ typedef struct {
uint32_t last_run_cmd_time_ms; /*!< The time in milliseconds when the last run command was issued */
} relay_chn_run_info_t;
#if RELAY_CHN_ENABLE_TILTING == 1
#if CONFIG_RELAY_CHN_ENABLE_TILTING
/// @brief Tilt commands.
typedef enum {
RELAY_CHN_TILT_CMD_NONE, /*!< No command */
@@ -68,11 +68,11 @@ typedef struct {
relay_chn_output_t *output; /*!< Output configuration of the relay channel */
relay_chn_cmd_t pending_cmd; /*!< The command that is pending to be issued */
esp_timer_handle_t inertia_timer; /*!< Timer to handle the opposite direction inertia time */
#if RELAY_CHN_ENABLE_RUN_LIMIT == 1
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
esp_timer_handle_t run_limit_timer; /*!< Timer to handle the run limit */
uint16_t run_limit_sec; /*!< Run limit in seconds */
#endif
#if RELAY_CHN_ENABLE_TILTING == 1
#if CONFIG_RELAY_CHN_ENABLE_TILTING
relay_chn_tilt_ctl_t *tilt_ctl; /*!< Pointer to the tilt control structure if tilting is enabled */
#endif
} relay_chn_ctl_t;

View File

@@ -21,7 +21,7 @@ extern "C" {
*/
void relay_chn_run_info_init(void);
#if RELAY_CHN_COUNT > 1
#if CONFIG_RELAY_CHN_COUNT > 1
/**
* @brief Get run information object for a specific relay channel.
*
@@ -43,7 +43,7 @@ relay_chn_run_info_t *relay_chn_run_info_get_all(void);
* @return Pointer to run information structure.
*/
relay_chn_run_info_t *relay_chn_run_info_get(void);
#endif // RELAY_CHN_COUNT > 1
#endif // CONFIG_RELAY_CHN_COUNT > 1
/**
* @brief Get the last run command for a relay channel.

View File

@@ -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 ""

View File

@@ -12,11 +12,11 @@
#include "relay_chn_run_info.h"
#include "relay_chn_ctl.h"
#if RELAY_CHN_ENABLE_TILTING == 1
#if CONFIG_RELAY_CHN_ENABLE_TILTING
#include "relay_chn_tilt.h"
#endif
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
#include "relay_chn_nvs.h"
#endif
@@ -35,7 +35,7 @@ typedef struct relay_chn_listener_entry_type {
// The list that holds references to the registered listeners.
static List_t relay_chn_listener_list;
#if RELAY_CHN_ENABLE_RUN_LIMIT == 1
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
/*
* Run limit timer callback immediately dispatches a STOP command for the
* relevant channel as soon as the run limit time times out
@@ -90,7 +90,7 @@ esp_err_t relay_chn_create(const uint8_t* gpio_map, uint8_t gpio_count)
ESP_RETURN_ON_FALSE(gpio_map != NULL, ESP_ERR_INVALID_ARG, TAG, "gpio_map cannot be NULL");
esp_err_t ret;
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
ret = relay_chn_nvs_init();
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to initialize NVS for relay channel");
#endif
@@ -102,7 +102,7 @@ esp_err_t relay_chn_create(const uint8_t* gpio_map, uint8_t gpio_count)
// Initialize the run info
relay_chn_run_info_init();
#if RELAY_CHN_COUNT > 1
#if CONFIG_RELAY_CHN_COUNT > 1
relay_chn_output_t *outputs = relay_chn_output_get_all();
relay_chn_run_info_t *run_infos = relay_chn_run_info_get_all();
#else
@@ -114,13 +114,13 @@ esp_err_t relay_chn_create(const uint8_t* gpio_map, uint8_t gpio_count)
ret = relay_chn_ctl_init(outputs, run_infos);
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to initialize relay channel control");
#if RELAY_CHN_ENABLE_TILTING == 1
#if CONFIG_RELAY_CHN_ENABLE_TILTING
// Initialize the tilt feature
#if RELAY_CHN_COUNT > 1
#if CONFIG_RELAY_CHN_COUNT > 1
relay_chn_ctl_t *chn_ctls = relay_chn_ctl_get_all();
#else
relay_chn_ctl_t *chn_ctls = relay_chn_ctl_get();
#endif // RELAY_CHN_COUNT > 1
#endif // CONFIG_RELAY_CHN_COUNT > 1
ret = relay_chn_tilt_init(chn_ctls); // Initialize tilt feature
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to initialize tilt feature");
#endif
@@ -133,13 +133,13 @@ esp_err_t relay_chn_create(const uint8_t* gpio_map, uint8_t gpio_count)
void relay_chn_destroy(void)
{
#if RELAY_CHN_ENABLE_TILTING == 1
#if CONFIG_RELAY_CHN_ENABLE_TILTING
relay_chn_tilt_deinit();
#endif
relay_chn_ctl_deinit();
relay_chn_output_deinit();
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
relay_chn_nvs_deinit();
#endif
@@ -328,7 +328,7 @@ void relay_chn_issue_cmd(relay_chn_ctl_t* chn_ctl, relay_chn_cmd_t cmd)
// since the last run command stopped and decide whether to run the command immediately or wait
uint32_t last_run_cmd_time_ms = relay_chn_run_info_get_last_run_cmd_time_ms(chn_ctl->run_info);
uint32_t inertia_time_passed_ms = (uint32_t) (esp_timer_get_time() / 1000) - last_run_cmd_time_ms;
uint32_t inertia_time_ms = RELAY_CHN_OPPOSITE_INERTIA_MS - inertia_time_passed_ms;
uint32_t inertia_time_ms = CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS - inertia_time_passed_ms;
if (inertia_time_ms > 0) {
chn_ctl->pending_cmd = cmd;
relay_chn_state_t new_state = cmd == RELAY_CHN_CMD_FORWARD
@@ -366,10 +366,10 @@ void relay_chn_issue_cmd(relay_chn_ctl_t* chn_ctl, relay_chn_cmd_t cmd)
relay_chn_state_t new_state = cmd == RELAY_CHN_CMD_FORWARD
? RELAY_CHN_STATE_FORWARD_PENDING : RELAY_CHN_STATE_REVERSE_PENDING;
relay_chn_update_state(chn_ctl, new_state);
relay_chn_start_esp_timer_once(chn_ctl->inertia_timer, RELAY_CHN_OPPOSITE_INERTIA_MS);
relay_chn_start_esp_timer_once(chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS);
break;
#if RELAY_CHN_ENABLE_TILTING == 1
#if CONFIG_RELAY_CHN_ENABLE_TILTING
case RELAY_CHN_STATE_TILT_FORWARD:
// Terminate tilting first
relay_chn_tilt_dispatch_cmd(chn_ctl->tilt_ctl, RELAY_CHN_TILT_CMD_STOP);
@@ -377,7 +377,7 @@ void relay_chn_issue_cmd(relay_chn_ctl_t* chn_ctl, relay_chn_cmd_t cmd)
// Schedule for running forward
chn_ctl->pending_cmd = cmd;
relay_chn_update_state(chn_ctl, RELAY_CHN_STATE_FORWARD_PENDING);
relay_chn_start_esp_timer_once(chn_ctl->inertia_timer, RELAY_CHN_OPPOSITE_INERTIA_MS);
relay_chn_start_esp_timer_once(chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS);
} else if (cmd == RELAY_CHN_CMD_REVERSE) {
// Run directly since it is the same direction
relay_chn_dispatch_cmd(chn_ctl, cmd);
@@ -395,7 +395,7 @@ void relay_chn_issue_cmd(relay_chn_ctl_t* chn_ctl, relay_chn_cmd_t cmd)
// Schedule for running reverse
chn_ctl->pending_cmd = cmd;
relay_chn_update_state(chn_ctl, RELAY_CHN_STATE_REVERSE_PENDING);
relay_chn_start_esp_timer_once(chn_ctl->inertia_timer, RELAY_CHN_OPPOSITE_INERTIA_MS);
relay_chn_start_esp_timer_once(chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS);
}
break;
#endif
@@ -404,16 +404,16 @@ void relay_chn_issue_cmd(relay_chn_ctl_t* chn_ctl, relay_chn_cmd_t cmd)
}
}
#if RELAY_CHN_COUNT > 1
#if CONFIG_RELAY_CHN_COUNT > 1
bool relay_chn_is_channel_id_valid(uint8_t chn_id)
{
bool valid = (chn_id < 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);
}
return valid;
}
#endif // RELAY_CHN_COUNT > 1
#endif // CONFIG_RELAY_CHN_COUNT > 1
static void relay_chn_execute_idle(relay_chn_ctl_t *chn_ctl)
@@ -437,7 +437,7 @@ static void relay_chn_execute_stop(relay_chn_ctl_t *chn_ctl)
// Invalidate the channel's timer if it is active
esp_timer_stop(chn_ctl->inertia_timer);
#if RELAY_CHN_ENABLE_RUN_LIMIT == 1
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
esp_timer_stop(chn_ctl->run_limit_timer);
#endif
@@ -448,7 +448,7 @@ static void relay_chn_execute_stop(relay_chn_ctl_t *chn_ctl)
relay_chn_run_info_set_last_run_cmd_time_ms(chn_ctl->run_info, (uint32_t)(esp_timer_get_time() / 1000));
// Schedule a free command for the channel
chn_ctl->pending_cmd = RELAY_CHN_CMD_IDLE;
relay_chn_start_esp_timer_once(chn_ctl->inertia_timer, RELAY_CHN_OPPOSITE_INERTIA_MS);
relay_chn_start_esp_timer_once(chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS);
} else {
// If the channel was not running one of the run or fwd, issue a free command immediately
// relay_chn_dispatch_cmd(chn_ctl, RELAY_CHN_CMD_IDLE);
@@ -465,7 +465,7 @@ static void relay_chn_execute_forward(relay_chn_ctl_t *chn_ctl)
relay_chn_run_info_set_last_run_cmd(chn_ctl->run_info, RELAY_CHN_CMD_FORWARD);
relay_chn_update_state(chn_ctl, RELAY_CHN_STATE_FORWARD);
#if RELAY_CHN_ENABLE_RUN_LIMIT == 1
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
relay_chn_start_esp_timer_once(chn_ctl->run_limit_timer, chn_ctl->run_limit_sec * 1000);
#endif
}
@@ -479,7 +479,7 @@ static void relay_chn_execute_reverse(relay_chn_ctl_t *chn_ctl)
relay_chn_run_info_set_last_run_cmd(chn_ctl->run_info, RELAY_CHN_CMD_REVERSE);
relay_chn_update_state(chn_ctl, RELAY_CHN_STATE_REVERSE);
#if RELAY_CHN_ENABLE_RUN_LIMIT == 1
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
relay_chn_start_esp_timer_once(chn_ctl->run_limit_timer, chn_ctl->run_limit_sec * 1000);
#endif
}
@@ -489,7 +489,7 @@ static void relay_chn_execute_flip(relay_chn_ctl_t *chn_ctl)
relay_chn_output_flip(chn_ctl->output);
// Set an inertia on the channel to prevent any immediate movement
chn_ctl->pending_cmd = RELAY_CHN_CMD_IDLE;
relay_chn_start_esp_timer_once(chn_ctl->inertia_timer, RELAY_CHN_OPPOSITE_INERTIA_MS);
relay_chn_start_esp_timer_once(chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS);
}
// Dispatch relay channel command
@@ -516,7 +516,7 @@ void relay_chn_dispatch_cmd(relay_chn_ctl_t *chn_ctl, relay_chn_cmd_t cmd) {
ESP_LOGD(TAG, "Unknown relay channel command!");
}
#if RELAY_CHN_ENABLE_TILTING == 1
#if CONFIG_RELAY_CHN_ENABLE_TILTING
// Reset the tilt counter when the command is either FORWARD or REVERSE
if (cmd == RELAY_CHN_CMD_FORWARD || cmd == RELAY_CHN_CMD_REVERSE) {
relay_chn_tilt_reset_count(chn_ctl->tilt_ctl);
@@ -557,7 +557,7 @@ char *relay_chn_state_str(relay_chn_state_t state)
return "FORWARD_PENDING";
case RELAY_CHN_STATE_REVERSE_PENDING:
return "REVERSE_PENDING";
#if RELAY_CHN_ENABLE_TILTING == 1
#if CONFIG_RELAY_CHN_ENABLE_TILTING
case RELAY_CHN_STATE_TILT_FORWARD:
return "TILT_FORWARD";
case RELAY_CHN_STATE_TILT_REVERSE:

View File

@@ -10,20 +10,20 @@
#include "relay_chn_ctl.h"
#include "relay_chn_output.h"
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
#include "relay_chn_nvs.h"
#endif
static const char *TAG = "RELAY_CHN_CTL";
static relay_chn_ctl_t chn_ctls[RELAY_CHN_COUNT];
static relay_chn_ctl_t chn_ctls[CONFIG_RELAY_CHN_COUNT];
esp_err_t relay_chn_ctl_init(relay_chn_output_t *outputs, relay_chn_run_info_t *run_infos)
{
// Initialize all relay channels
esp_err_t ret;
for (int i = 0; i < RELAY_CHN_COUNT; i++) {
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
relay_chn_ctl_t* chn_ctl = &chn_ctls[i];
relay_chn_output_t* output = &outputs[i];
relay_chn_run_info_t* run_info = &run_infos[i];
@@ -34,9 +34,9 @@ esp_err_t relay_chn_ctl_init(relay_chn_output_t *outputs, relay_chn_run_info_t *
chn_ctl->output = output;
chn_ctl->run_info = run_info;
#if RELAY_CHN_ENABLE_RUN_LIMIT == 1
uint16_t run_limit_sec = RELAY_CHN_RUN_LIMIT_DEFAULT_SEC;
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
uint16_t run_limit_sec = CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC;
#if CONFIG_RELAY_CHN_ENABLE_NVS
// Load run limit value from NVS
ret = relay_chn_nvs_get_run_limit(chn_ctl->id, &run_limit_sec);
if (ret != ESP_OK && ret != ESP_ERR_NVS_NOT_FOUND) {
@@ -55,13 +55,13 @@ esp_err_t relay_chn_ctl_init(relay_chn_output_t *outputs, relay_chn_run_info_t *
void relay_chn_ctl_deinit()
{
for (int i = 0; i < RELAY_CHN_COUNT; i++) {
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
relay_chn_ctl_t* chn_ctl = &chn_ctls[i];
if (chn_ctl->inertia_timer != NULL) {
esp_timer_delete(chn_ctl->inertia_timer);
chn_ctl->inertia_timer = NULL;
}
#if RELAY_CHN_ENABLE_RUN_LIMIT == 1
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
if (chn_ctl->run_limit_timer != NULL) {
esp_timer_delete(chn_ctl->run_limit_timer);
chn_ctl->run_limit_timer = NULL;
@@ -72,117 +72,178 @@ 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 < RELAY_CHN_COUNT; i++) {
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
relay_chn_issue_cmd(&chn_ctls[i], 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;
}
relay_chn_ctl_t *chn_ctl = &chn_ctls[chn_id];
return relay_chn_output_get_direction(chn_ctl->output);
return relay_chn_is_channel_id_valid(chn_id)
? relay_chn_output_get_direction(chn_ctls[chn_id].output)
: RELAY_CHN_DIRECTION_DEFAULT;
}
#if RELAY_CHN_ENABLE_RUN_LIMIT == 1
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);
}
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 > RELAY_CHN_RUN_LIMIT_MAX_SEC)
time_sec = RELAY_CHN_RUN_LIMIT_MAX_SEC;
else if (time_sec < RELAY_CHN_RUN_LIMIT_MIN_SEC)
time_sec = 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 RELAY_CHN_ENABLE_NVS == 1
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)

View File

@@ -10,7 +10,7 @@
#include "relay_chn_ctl.h"
#include "relay_chn_output.h"
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
#include "relay_chn_nvs.h"
#endif
@@ -27,10 +27,10 @@ esp_err_t relay_chn_ctl_init(relay_chn_output_t *output, relay_chn_run_info_t *r
chn_ctl.pending_cmd = RELAY_CHN_CMD_NONE;
chn_ctl.output = output;
chn_ctl.run_info = run_info;
#if RELAY_CHN_ENABLE_RUN_LIMIT == 1
uint16_t run_limit_sec = RELAY_CHN_RUN_LIMIT_DEFAULT_SEC;
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
uint16_t run_limit_sec = CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC;
esp_err_t ret;
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
// Load run limit value from NVS
ret = relay_chn_nvs_get_run_limit(chn_ctl.id, &run_limit_sec);
if (ret != ESP_OK && ret != ESP_ERR_NVS_NOT_FOUND) {
@@ -51,7 +51,7 @@ void relay_chn_ctl_deinit()
esp_timer_delete(chn_ctl.inertia_timer);
chn_ctl.inertia_timer = NULL;
}
#if RELAY_CHN_ENABLE_RUN_LIMIT == 1
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
if (chn_ctl.run_limit_timer != NULL) {
esp_timer_delete(chn_ctl.run_limit_timer);
chn_ctl.run_limit_timer = NULL;
@@ -95,7 +95,7 @@ relay_chn_direction_t relay_chn_ctl_get_direction()
return relay_chn_output_get_direction(chn_ctl.output);
}
#if RELAY_CHN_ENABLE_RUN_LIMIT == 1
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
uint16_t relay_chn_ctl_get_run_limit()
{
return chn_ctl.run_limit_sec;
@@ -104,14 +104,14 @@ uint16_t relay_chn_ctl_get_run_limit()
void relay_chn_ctl_set_run_limit(uint16_t time_sec)
{
// Check for boundaries
if (time_sec > RELAY_CHN_RUN_LIMIT_MAX_SEC)
time_sec = RELAY_CHN_RUN_LIMIT_MAX_SEC;
else if (time_sec < RELAY_CHN_RUN_LIMIT_MIN_SEC)
time_sec = RELAY_CHN_RUN_LIMIT_MIN_SEC;
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;
chn_ctl.run_limit_sec = time_sec;
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
relay_chn_nvs_set_run_limit(chn_ctl.id, time_sec);
#endif
}

View File

@@ -8,10 +8,10 @@
#include "relay_chn_nvs.h"
#define RELAY_CHN_KEY_DIR "dir" /*!< Direction key */
#if RELAY_CHN_ENABLE_RUN_LIMIT == 1
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
#define RELAY_CHN_KEY_RLIM(ch) "rlim_%d" /*!< Run limit key */
#endif
#ifdef RELAY_CHN_ENABLE_TILTING
#if CONFIG_RELAY_CHN_ENABLE_TILTING
#define RELAY_CHN_KEY_TSENS(ch) "tsens_%d" /*!< Tilt sensitivity key */
#define RELAY_CHN_KEY_TCNT(ch) "tcnt_%d" /*!< Tilt count key */
#endif
@@ -23,22 +23,22 @@ static nvs_handle_t relay_chn_nvs;
esp_err_t relay_chn_nvs_init()
{
esp_err_t ret;
#if RELAY_CHN_NVS_CUSTOM_PARTITION == 1
ret = nvs_open_from_partition(RELAY_CHN_NVS_CUSTOM_PARTITION_NAME,
RELAY_CHN_NVS_NAMESPACE,
#if CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION
ret = nvs_open_from_partition(CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION_NAME,
CONFIG_RELAY_CHN_NVS_NAMESPACE,
NVS_READWRITE,
&relay_chn_nvs);
ESP_RETURN_ON_ERROR(ret,
TAG,
"Failed to open NVS namespace '%s' from partition '%s' with error %s",
RELAY_CHN_NVS_NAMESPACE,
RELAY_CHN_NVS_CUSTOM_PARTITION_NAME,
CONFIG_RELAY_CHN_NVS_NAMESPACE,
CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION_NAME,
esp_err_to_name(ret));
#else
ret = nvs_open(RELAY_CHN_NVS_NAMESPACE, NVS_READWRITE, &relay_chn_nvs);
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to open NVS namespace '%s'", RELAY_CHN_NVS_NAMESPACE);
#endif // RELAY_CHN_NVS_CUSTOM_PARTITION
ret = nvs_open(CONFIG_RELAY_CHN_NVS_NAMESPACE, NVS_READWRITE, &relay_chn_nvs);
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to open NVS namespace '%s'", CONFIG_RELAY_CHN_NVS_NAMESPACE);
#endif // CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION
return ESP_OK;
}
@@ -72,7 +72,7 @@ esp_err_t relay_chn_nvs_get_direction(uint8_t ch, relay_chn_direction_t *directi
return ESP_OK;
}
#if RELAY_CHN_ENABLE_RUN_LIMIT == 1
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
esp_err_t relay_chn_nvs_set_run_limit(uint8_t ch, uint16_t time_sec)
{
esp_err_t ret = nvs_set_u16(relay_chn_nvs, RELAY_CHN_KEY_RLIM(ch), time_sec);
@@ -86,9 +86,9 @@ esp_err_t relay_chn_nvs_get_run_limit(uint8_t ch, uint16_t *time_sec)
return nvs_get_u16(relay_chn_nvs, RELAY_CHN_KEY_RLIM(ch), time_sec);
}
#endif // RELAY_CHN_ENABLE_RUN_LIMIT == 1
#endif // CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT == 1
#ifdef RELAY_CHN_ENABLE_TILTING
#if CONFIG_RELAY_CHN_ENABLE_TILTING
esp_err_t relay_chn_nvs_set_tilt_sensitivity(uint8_t ch, uint8_t sensitivity)
{
esp_err_t ret = nvs_set_u8(relay_chn_nvs, RELAY_CHN_KEY_TSENS(ch), sensitivity);
@@ -117,13 +117,13 @@ esp_err_t relay_chn_nvs_get_tilt_count(uint8_t ch, uint16_t *tilt_count)
ESP_ERR_INVALID_ARG, TAG, "Counter pointers are NULL");
return nvs_get_u16(relay_chn_nvs, RELAY_CHN_KEY_TCNT(ch), tilt_count);
}
#endif // RELAY_CHN_ENABLE_TILTING
#endif // CONFIG_RELAY_CHN_ENABLE_TILTING
esp_err_t relay_chn_nvs_erase_all()
{
// Erase all key-value pairs in the relay_chn NVS namespace
esp_err_t ret = nvs_erase_all(relay_chn_nvs);
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to erase all keys in NVS namespace '%s'", RELAY_CHN_NVS_NAMESPACE);
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to erase all keys in NVS namespace '%s'", CONFIG_RELAY_CHN_NVS_NAMESPACE);
// Commit the changes
return nvs_commit(relay_chn_nvs);

View File

@@ -6,19 +6,18 @@
#include "esp_check.h"
#include "esp_log.h"
#include "relay_chn_defs.h"
#include "relay_chn_output.h"
#include "relay_chn_core.h"
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
#include "relay_chn_nvs.h"
#endif
static const char *TAG = "RELAY_CHN_OUTPUT";
#if RELAY_CHN_COUNT > 1
static relay_chn_output_t outputs[RELAY_CHN_COUNT];
#if CONFIG_RELAY_CHN_COUNT > 1
static relay_chn_output_t outputs[CONFIG_RELAY_CHN_COUNT];
#else
static relay_chn_output_t output;
#endif
@@ -27,16 +26,16 @@ static relay_chn_output_t output;
static esp_err_t relay_chn_output_check_gpio_capabilities(uint8_t gpio_count)
{
// Check if the device's GPIOs are enough for the number of channels
if (RELAY_CHN_COUNT > (GPIO_PIN_COUNT / 2)) {
if (CONFIG_RELAY_CHN_COUNT > (GPIO_PIN_COUNT / 2)) {
ESP_LOGE(TAG, "Not enough GPIOs for the number of channels!");
ESP_LOGE(TAG, "Max available num of channels: %d, requested channels: %d", GPIO_PIN_COUNT / 2, RELAY_CHN_COUNT);
ESP_LOGE(TAG, "Max available num of channels: %d, requested channels: %d", GPIO_PIN_COUNT / 2, CONFIG_RELAY_CHN_COUNT);
return ESP_ERR_INVALID_ARG;
}
// Check if the provided GPIOs correspond to the number of channels
if (gpio_count != RELAY_CHN_COUNT * 2) {
if (gpio_count != CONFIG_RELAY_CHN_COUNT * 2) {
ESP_LOGE(TAG, "Invalid number of GPIOs provided: %d", gpio_count);
ESP_LOGE(TAG, "Expected number of GPIOs: %d", RELAY_CHN_COUNT * 2);
ESP_LOGE(TAG, "Expected number of GPIOs: %d", CONFIG_RELAY_CHN_COUNT * 2);
return ESP_ERR_INVALID_ARG;
}
return ESP_OK;
@@ -73,7 +72,7 @@ static esp_err_t relay_chn_output_ctl_init(relay_chn_output_t *output,
return ESP_OK;
}
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
static esp_err_t relay_chn_output_load_direction(uint8_t ch, relay_chn_direction_t *direction)
{
esp_err_t ret = relay_chn_nvs_get_direction(ch, direction);
@@ -93,15 +92,15 @@ esp_err_t relay_chn_output_init(const uint8_t* gpio_map, uint8_t gpio_count)
ret = relay_chn_output_check_gpio_capabilities(gpio_count);
ESP_RETURN_ON_ERROR(ret, TAG, "Device does not support the provided GPIOs");
#if RELAY_CHN_COUNT > 1
for (int i = 0; i < RELAY_CHN_COUNT; i++) {
#if CONFIG_RELAY_CHN_COUNT > 1
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
relay_chn_output_t* output = &outputs[i];
int gpio_index = i << 1; // gpio_index = i * 2
gpio_num_t forward_pin = (gpio_num_t) gpio_map[gpio_index];
gpio_num_t reverse_pin = (gpio_num_t) gpio_map[gpio_index + 1];
relay_chn_direction_t direction = RELAY_CHN_DIRECTION_DEFAULT;
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
// If NVS storage is enabled, retrieve the direction from storage
ret = relay_chn_output_load_direction(i, &direction);
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load direction from storage for channel %d", i);
@@ -111,7 +110,7 @@ esp_err_t relay_chn_output_init(const uint8_t* gpio_map, uint8_t gpio_count)
}
#else
relay_chn_direction_t direction = RELAY_CHN_DIRECTION_DEFAULT;
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
// If NVS storage is enabled, retrieve the direction from storage
ret = relay_chn_output_load_direction(0, &direction);
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load direction from storage for channel %d", 0);
@@ -130,16 +129,16 @@ static void relay_chn_output_ctl_deinit(relay_chn_output_t *output)
void relay_chn_output_deinit()
{
#if RELAY_CHN_COUNT > 1
for (int i = 0; i < RELAY_CHN_COUNT; i++) {
#if CONFIG_RELAY_CHN_COUNT > 1
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
relay_chn_output_ctl_deinit(&outputs[i]);
}
#else
relay_chn_output_ctl_deinit(&output);
#endif // RELAY_CHN_COUNT > 1
#endif // CONFIG_RELAY_CHN_COUNT > 1
}
#if RELAY_CHN_COUNT > 1
#if CONFIG_RELAY_CHN_COUNT > 1
relay_chn_output_t *relay_chn_output_get(uint8_t chn_id)
{
if (!relay_chn_is_channel_id_valid(chn_id)) {
@@ -157,7 +156,7 @@ relay_chn_output_t *relay_chn_output_get(void)
{
return &output;
}
#endif // RELAY_CHN_COUNT > 1
#endif // CONFIG_RELAY_CHN_COUNT > 1
esp_err_t relay_chn_output_stop(relay_chn_output_t *output)
{
@@ -194,10 +193,10 @@ void relay_chn_output_flip(relay_chn_output_t *output)
? RELAY_CHN_DIRECTION_FLIPPED
: RELAY_CHN_DIRECTION_DEFAULT;
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
uint8_t ch = 0;
#if RELAY_CHN_COUNT > 1
for (uint8_t i = 0; i < RELAY_CHN_COUNT; i++) {
#if CONFIG_RELAY_CHN_COUNT > 1
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
if (output == &outputs[i]) {
ch = i;
break;

View File

@@ -8,16 +8,16 @@
#include "relay_chn_run_info.h"
#if RELAY_CHN_COUNT > 1
static relay_chn_run_info_t run_infos[RELAY_CHN_COUNT];
#if CONFIG_RELAY_CHN_COUNT > 1
static relay_chn_run_info_t run_infos[CONFIG_RELAY_CHN_COUNT];
#else
static relay_chn_run_info_t run_info;
#endif
void relay_chn_run_info_init()
{
#if RELAY_CHN_COUNT > 1
for (int i = 0; i < RELAY_CHN_COUNT; i++) {
#if CONFIG_RELAY_CHN_COUNT > 1
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
run_infos[i].last_run_cmd = RELAY_CHN_CMD_NONE;
run_infos[i].last_run_cmd_time_ms = 0;
}
@@ -27,7 +27,7 @@ void relay_chn_run_info_init()
#endif
}
#if RELAY_CHN_COUNT > 1
#if CONFIG_RELAY_CHN_COUNT > 1
relay_chn_run_info_t *relay_chn_run_info_get(uint8_t chn_id)
{
if (!relay_chn_is_channel_id_valid(chn_id)) {
@@ -45,7 +45,7 @@ relay_chn_run_info_t *relay_chn_run_info_get()
{
return &run_info;
}
#endif // RELAY_CHN_COUNT > 1
#endif // CONFIG_RELAY_CHN_COUNT > 1
relay_chn_cmd_t relay_chn_run_info_get_last_run_cmd(relay_chn_run_info_t *run_info)
{

View File

@@ -10,7 +10,7 @@
#include "relay_chn_run_info.h"
#include "relay_chn_tilt.h"
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
#include "relay_chn_nvs.h"
#define RELAY_CHN_TILT_FLUSH_DEBOUNCE_MS 3000
@@ -63,14 +63,14 @@ typedef struct relay_chn_tilt_ctl {
relay_chn_tilt_timing_t tilt_timing; /*!< Tilt timing structure */
uint16_t tilt_count; /*!< Tilt count to manage forward and reverse tilts */
esp_timer_handle_t tilt_timer; /*!< Tilt timer handle */
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
esp_timer_handle_t flush_timer; /*!< Flush timer to avoid frequent write of tilt counters */
#endif
} relay_chn_tilt_ctl_t;
#if RELAY_CHN_COUNT > 1
static relay_chn_tilt_ctl_t tilt_ctls[RELAY_CHN_COUNT];
#if CONFIG_RELAY_CHN_COUNT > 1
static relay_chn_tilt_ctl_t tilt_ctls[CONFIG_RELAY_CHN_COUNT];
#else
static relay_chn_tilt_ctl_t tilt_ctl;
#endif
@@ -87,7 +87,7 @@ static uint32_t relay_chn_tilt_get_required_timing_before_tilting(relay_chn_tilt
uint32_t last_run_cmd_time_ms = relay_chn_run_info_get_last_run_cmd_time_ms(tilt_ctl->chn_ctl->run_info);
uint32_t inertia_time_passed_ms = (uint32_t) (esp_timer_get_time() / 1000) - last_run_cmd_time_ms;
return RELAY_CHN_OPPOSITE_INERTIA_MS - inertia_time_passed_ms;
return CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS - inertia_time_passed_ms;
}
// Issue a tilt command to a specific relay channel.
@@ -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
@@ -116,7 +117,7 @@ static void relay_chn_tilt_issue_cmd(relay_chn_tilt_ctl_t *tilt_ctl, relay_chn_t
case RELAY_CHN_STATE_REVERSE_PENDING:
// Issue a stop command first so that the timer and pending cmd get cleared
relay_chn_dispatch_cmd(tilt_ctl->chn_ctl, RELAY_CHN_CMD_STOP);
// break not put intentionally
// FALLTHRU
case RELAY_CHN_STATE_STOPPED: {
// Check if channel needs timing before tilting
uint32_t req_timing_ms = relay_chn_tilt_get_required_timing_before_tilting(tilt_ctl, cmd);
@@ -136,7 +137,7 @@ static void relay_chn_tilt_issue_cmd(relay_chn_tilt_ctl_t *tilt_ctl, relay_chn_t
relay_chn_dispatch_cmd(tilt_ctl->chn_ctl, RELAY_CHN_CMD_STOP);
// Schedule for tilting
tilt_ctl->step = RELAY_CHN_TILT_STEP_PENDING;
relay_chn_start_esp_timer_once(tilt_ctl->tilt_timer, RELAY_CHN_OPPOSITE_INERTIA_MS);
relay_chn_start_esp_timer_once(tilt_ctl->tilt_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS);
} else if (cmd == RELAY_CHN_TILT_CMD_REVERSE) {
// Stop the running channel first
relay_chn_dispatch_cmd(tilt_ctl->chn_ctl, RELAY_CHN_CMD_STOP);
@@ -151,7 +152,7 @@ static void relay_chn_tilt_issue_cmd(relay_chn_tilt_ctl_t *tilt_ctl, relay_chn_t
relay_chn_dispatch_cmd(tilt_ctl->chn_ctl, RELAY_CHN_CMD_STOP);
// Schedule for tilting
tilt_ctl->step = RELAY_CHN_TILT_STEP_PENDING;
relay_chn_start_esp_timer_once(tilt_ctl->tilt_timer, RELAY_CHN_OPPOSITE_INERTIA_MS);
relay_chn_start_esp_timer_once(tilt_ctl->tilt_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS);
} else if (cmd == RELAY_CHN_TILT_CMD_FORWARD) {
// Stop the running channel first
relay_chn_dispatch_cmd(tilt_ctl->chn_ctl, RELAY_CHN_CMD_STOP);
@@ -176,79 +177,68 @@ static void relay_chn_tilt_issue_auto(relay_chn_tilt_ctl_t *tilt_ctl)
}
}
#if 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 < 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);
#if CONFIG_RELAY_CHN_COUNT > 1
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);
}
}
static void relay_chn_tilt_issue_cmd_on_all_channels(relay_chn_tilt_cmd_t cmd)
void relay_chn_tilt_auto(uint8_t chn_id)
{
for (int i = 0; i < RELAY_CHN_COUNT; i++) {
relay_chn_tilt_ctl_t* tilt_ctl = &tilt_ctls[i];
relay_chn_tilt_issue_cmd(tilt_ctl, cmd);
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 < 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);
}
}
#else // RELAY_CHN_COUNT > 1
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()
{
@@ -269,7 +259,7 @@ void relay_chn_tilt_stop()
{
relay_chn_tilt_dispatch_cmd(&tilt_ctl, RELAY_CHN_TILT_CMD_STOP);
}
#endif // RELAY_CHN_COUNT > 1
#endif // CONFIG_RELAY_CHN_COUNT > 1
static void relay_chn_tilt_set_timing_values(relay_chn_tilt_timing_t *tilt_timing,
uint8_t sensitivity,
@@ -314,48 +304,64 @@ static void relay_chn_tilt_compute_set_sensitivity(relay_chn_tilt_ctl_t *tilt_ct
}
}
#if RELAY_CHN_COUNT > 1
#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 < 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);
#endif // CONFIG_RELAY_CHN_ENABLE_NVS
}
#if RELAY_CHN_ENABLE_NVS == 1
relay_chn_nvs_set_tilt_sensitivity(chn_id, sensitivity);
#endif // 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 < 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 < 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;
}
@@ -365,22 +371,22 @@ void relay_chn_tilt_set_sensitivity(uint8_t sensitivity)
{
relay_chn_tilt_compute_set_sensitivity(&tilt_ctl, sensitivity);
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
relay_chn_nvs_set_tilt_sensitivity(0, sensitivity);
#endif // RELAY_CHN_ENABLE_NVS
#endif // CONFIG_RELAY_CHN_ENABLE_NVS
}
uint8_t relay_chn_tilt_get_sensitivity()
{
return tilt_ctl.tilt_timing.sensitivity;
}
#endif // RELAY_CHN_COUNT > 1
#endif // CONFIG_RELAY_CHN_COUNT > 1
void relay_chn_tilt_reset_count(relay_chn_tilt_ctl_t *tilt_ctl)
{
tilt_ctl->tilt_count = 0;
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
esp_timer_stop(tilt_ctl->flush_timer);
#endif
}
@@ -450,7 +456,7 @@ static uint16_t relay_chn_tilt_count_update(relay_chn_tilt_ctl_t *tilt_ctl)
return 0;
}
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
static esp_err_t relay_chn_tilt_save_tilt_count(relay_chn_tilt_ctl_t *tilt_ctl)
{
// Save the tilt count to NVS storage
@@ -486,7 +492,7 @@ static void relay_chn_tilt_execute_stop(relay_chn_tilt_ctl_t *tilt_ctl)
}
relay_chn_dispatch_cmd(tilt_ctl->chn_ctl, RELAY_CHN_CMD_STOP);
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
// Start the flush debounce timer
relay_chn_start_esp_timer_once(tilt_ctl->flush_timer, RELAY_CHN_TILT_FLUSH_DEBOUNCE_MS);
#endif
@@ -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:
@@ -599,7 +606,7 @@ static void relay_chn_tilt_timer_cb(void *arg)
}
}
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
static esp_err_t relay_chn_tilt_load_sensitivity(uint8_t ch, uint8_t *sensitivity)
{
esp_err_t ret = relay_chn_nvs_get_tilt_sensitivity(ch, sensitivity);
@@ -622,7 +629,7 @@ static esp_err_t relay_chn_tilt_load_tilt_count(uint8_t ch, uint16_t *tilt_count
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load tilt counters for channel %d", ch);
return ESP_OK;
}
#endif // RELAY_CHN_ENABLE_NVS
#endif // CONFIG_RELAY_CHN_ENABLE_NVS
static esp_err_t relay_chn_tilt_ctl_init(relay_chn_tilt_ctl_t *tilt_ctl,
relay_chn_ctl_t *chn_ctl,
@@ -648,7 +655,7 @@ static esp_err_t relay_chn_tilt_ctl_init(relay_chn_tilt_ctl_t *tilt_ctl,
esp_err_t ret = esp_timer_create(&timer_args, &tilt_ctl->tilt_timer);
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to create tilt timer for channel %d", chn_ctl->id);
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
// Create flush timer for the tilt counters
snprintf(timer_name, sizeof(timer_name), "relay_chn_%2d_tilt_flush_timer", chn_ctl->id);
timer_args.callback = relay_chn_tilt_flush_timer_cb;
@@ -664,17 +671,18 @@ esp_err_t relay_chn_tilt_init(relay_chn_ctl_t *chn_ctls)
uint8_t sensitivity;
uint16_t tilt_count;
#if RELAY_CHN_COUNT > 1
for (int i = 0; i < RELAY_CHN_COUNT; i++) {
#if RELAY_CHN_ENABLE_NVS == 1
esp_err_t ret = relay_chn_tilt_load_sensitivity(i, &sensitivity);
#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
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);
#else
sensitivity = RELAY_CHN_TILT_DEFAULT_SENSITIVITY;
tilt_count = 0;
#endif // RELAY_CHN_ENABLE_NVS == 1
#endif // CONFIG_RELAY_CHN_ENABLE_NVS == 1
ret = relay_chn_tilt_ctl_init(&tilt_ctls[i], &chn_ctls[i], tilt_count, sensitivity);
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to init tilt control for channel %d", i);
}
@@ -682,14 +690,14 @@ esp_err_t relay_chn_tilt_init(relay_chn_ctl_t *chn_ctls)
#else
sensitivity = RELAY_CHN_TILT_DEFAULT_SENSITIVITY;
tilt_count = 0;
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
esp_err_t ret = relay_chn_tilt_load_sensitivity(0, &sensitivity);
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load tilt sensitivity for channel %d", 0);
ret = relay_chn_tilt_load_tilt_count(0, &tilt_count);
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load tilt count for channel %d", 0);
#endif // RELAY_CHN_ENABLE_NVS == 1
#endif // CONFIG_RELAY_CHN_ENABLE_NVS == 1
return relay_chn_tilt_ctl_init(&tilt_ctl, chn_ctls, tilt_count, sensitivity);
#endif // RELAY_CHN_COUNT > 1
#endif // CONFIG_RELAY_CHN_COUNT > 1
}
void relay_chn_tilt_ctl_deinit(relay_chn_tilt_ctl_t *tilt_ctl)
@@ -698,21 +706,21 @@ void relay_chn_tilt_ctl_deinit(relay_chn_tilt_ctl_t *tilt_ctl)
esp_timer_delete(tilt_ctl->tilt_timer);
tilt_ctl->tilt_timer = NULL;
}
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
if (tilt_ctl->flush_timer != NULL) {
esp_timer_delete(tilt_ctl->flush_timer);
tilt_ctl->flush_timer = NULL;
}
#endif // RELAY_CHN_ENABLE_NVS == 1
#endif // CONFIG_RELAY_CHN_ENABLE_NVS == 1
}
void relay_chn_tilt_deinit()
{
#if RELAY_CHN_COUNT > 1
for (int i = 0; i < RELAY_CHN_COUNT; i++) {
#if CONFIG_RELAY_CHN_COUNT > 1
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
relay_chn_tilt_ctl_deinit(&tilt_ctls[i]);
}
#else
relay_chn_tilt_ctl_deinit(&tilt_ctl);
#endif // RELAY_CHN_COUNT > 1
#endif // CONFIG_RELAY_CHN_COUNT > 1
}

View File

@@ -7,7 +7,7 @@
#include "unity_test_runner.h"
#include "test_common.h"
#if RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
#include "nvs_flash.h"
#include "relay_chn_nvs.h"
#endif
@@ -28,18 +28,18 @@ void tearDown()
reset_channels_to_idle_state();
}
#if CONFIG_RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
static void test_nvs_flash_init(void)
{
esp_err_t ret;
#if RELAY_CHN_NVS_CUSTOM_PARTITION == 1
ret = nvs_flash_init_partition(RELAY_CHN_NVS_CUSTOM_PARTITION_NAME);
#if CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION
ret = nvs_flash_init_partition(CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION_NAME);
ESP_LOGI(TEST_TAG, "test_nvs_flash_init: NVS flash init partition return: %s", esp_err_to_name(ret));
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
// NVS partition is truncated and needs to be erased
ret = nvs_flash_erase_partition(RELAY_CHN_NVS_CUSTOM_PARTITION_NAME);
ret = nvs_flash_erase_partition(CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION_NAME);
if (ret == ESP_OK) {
ret = nvs_flash_init_partition(RELAY_CHN_NVS_CUSTOM_PARTITION_NAME);
ret = nvs_flash_init_partition(CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION_NAME);
}
}
#else
@@ -57,12 +57,12 @@ TEST_ESP_OK(ret);
}
#endif
#if CONFIG_RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
static void test_nvs_flash_deinit(void)
{
esp_err_t ret;
#if RELAY_CHN_NVS_CUSTOM_PARTITION == 1
ret = nvs_flash_deinit_partition(RELAY_CHN_NVS_CUSTOM_PARTITION_NAME);
#if CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION
ret = nvs_flash_deinit_partition(CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION_NAME);
#else
ret = nvs_flash_deinit();
#endif
@@ -72,7 +72,7 @@ static void test_nvs_flash_deinit(void)
void app_main(void)
{
#if CONFIG_RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
// Init NVS once for all tests
test_nvs_flash_init();
#endif
@@ -98,7 +98,7 @@ void app_main(void)
// Destroy relay_chn
relay_chn_destroy();
#if CONFIG_RELAY_CHN_ENABLE_NVS == 1
#if CONFIG_RELAY_CHN_ENABLE_NVS
// Deinit NVS
test_nvs_flash_deinit();
#endif

View File

@@ -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));

View File

@@ -8,7 +8,7 @@ TEST_CASE("relay_chn_create handles invalid arguments", "[relay_chn][core]")
// 1. Test with NULL gpio_map
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_create(NULL, gpio_count));
// 2. Test with incorrect gpio_count (must be RELAY_CHN_COUNT * 2)
// 2. Test with incorrect gpio_count (must be CONFIG_RELAY_CHN_COUNT * 2)
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_create(gpio_map, gpio_count - 1));
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_create(gpio_map, 1));
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_create(gpio_map, 0));
@@ -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
@@ -344,7 +343,7 @@ TEST_CASE("Direction flip handles invalid channel ID gracefully", "[relay_chn][c
TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, relay_chn_get_direction(invalid_ch));
}
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT == 1
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
#define TEST_RUN_LIMIT_SEC 5
#define TEST_SHORT_RUN_LIMIT_SEC 2
// ### Run Limit Tests
@@ -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));

View File

@@ -8,7 +8,7 @@ TEST_CASE("relay_chn_create handles invalid arguments", "[relay_chn][core]")
// 1. Test with NULL gpio_map
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_create(NULL, gpio_count));
// 2. Test with incorrect gpio_count (must be RELAY_CHN_COUNT * 2)
// 2. Test with incorrect gpio_count (must be CONFIG_RELAY_CHN_COUNT * 2)
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_create(gpio_map, gpio_count - 1));
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_create(gpio_map, 1));
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_create(gpio_map, 0));
@@ -173,7 +173,7 @@ TEST_CASE("Flipping a running channel stops it and flips direction", "[relay_chn
TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_FLIPPED, relay_chn_get_direction());
}
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT == 1
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
#define TEST_RUN_LIMIT_SEC 5
#define TEST_SHORT_RUN_LIMIT_SEC 2
// ### Run Limit Tests

View File

@@ -41,7 +41,7 @@ TEST_CASE("Test invalid parameters", "[relay_chn][nvs]")
TEST_ESP_OK(relay_chn_nvs_init());
// Test NULL pointer for all channels
for (int channel = 0; channel < RELAY_CHN_COUNT; channel++) {
for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) {
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_direction(channel, NULL));
}
@@ -54,13 +54,13 @@ TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]")
// Store some test data first
relay_chn_direction_t direction = RELAY_CHN_DIRECTION_FLIPPED;
for (int channel = 0; channel < RELAY_CHN_COUNT; channel++) {
for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) {
TEST_ESP_OK(relay_chn_nvs_set_direction(0, direction));
}
#ifdef RELAY_CHN_ENABLE_TILTING
#if CONFIG_RELAY_CHN_ENABLE_TILTING
uint8_t sensitivity = 50;
for (int channel = 0; channel < RELAY_CHN_COUNT; channel++) {
for (int channel = 0; channel < CONFIG_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));
}
@@ -73,7 +73,7 @@ TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]")
relay_chn_direction_t read_direction;
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_direction(0, &read_direction));
#ifdef RELAY_CHN_ENABLE_TILTING
#if CONFIG_RELAY_CHN_ENABLE_TILTING
uint8_t read_sensitivity;
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_sensitivity(0, &read_sensitivity));
@@ -84,7 +84,7 @@ TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]")
TEST_ESP_OK(relay_chn_nvs_deinit());
}
#ifdef CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
TEST_CASE("Test run limit setting and getting", "[relay_chn][nvs][run_limit]")
{
TEST_ESP_OK(relay_chn_nvs_init());
@@ -101,7 +101,7 @@ TEST_CASE("Test run limit setting and getting", "[relay_chn][nvs][run_limit]")
}
#endif
#ifdef RELAY_CHN_ENABLE_TILTING
#if CONFIG_RELAY_CHN_ENABLE_TILTING
TEST_CASE("Test sensitivity setting and getting", "[relay_chn][nvs][tilt]")
{
TEST_ESP_OK(relay_chn_nvs_init());
@@ -110,7 +110,7 @@ TEST_CASE("Test sensitivity setting and getting", "[relay_chn][nvs][tilt]")
uint8_t sensitivity;
// Test all channels
for (int channel = 0; channel < RELAY_CHN_COUNT; channel++) {
for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) {
TEST_ESP_OK(relay_chn_nvs_set_tilt_sensitivity(channel, test_sensitivity));
TEST_ESP_OK(relay_chn_nvs_get_tilt_sensitivity(channel, &sensitivity));
TEST_ASSERT_EQUAL(test_sensitivity, sensitivity);
@@ -127,7 +127,7 @@ TEST_CASE("Test tilt counter operations", "[relay_chn][nvs][tilt]")
uint16_t tilt_count_read;
// Test all channels
for (int channel = 0; channel < RELAY_CHN_COUNT; channel++) {
for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) {
// Test setting counters
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));
@@ -142,11 +142,11 @@ TEST_CASE("Test tilting invalid parameters", "[relay_chn][nvs][tilt]")
TEST_ESP_OK(relay_chn_nvs_init());
// Test NULL pointers for all channels
for (int channel = 0; channel < RELAY_CHN_COUNT; channel++) {
for (int channel = 0; channel < CONFIG_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));
}
TEST_ESP_OK(relay_chn_nvs_deinit());
}
#endif // RELAY_CHN_ENABLE_TILTING
#endif // CONFIG_RELAY_CHN_ENABLE_TILTING

View File

@@ -53,7 +53,7 @@ TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]")
relay_chn_direction_t direction = RELAY_CHN_DIRECTION_FLIPPED;
TEST_ESP_OK(relay_chn_nvs_set_direction(0, direction));
#ifdef RELAY_CHN_ENABLE_TILTING
#if CONFIG_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));
@@ -66,7 +66,7 @@ TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]")
relay_chn_direction_t read_direction;
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_direction(0, &read_direction));
#ifdef RELAY_CHN_ENABLE_TILTING
#if CONFIG_RELAY_CHN_ENABLE_TILTING
uint8_t read_sensitivity;
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_sensitivity(0, &read_sensitivity));
@@ -77,7 +77,7 @@ TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]")
TEST_ESP_OK(relay_chn_nvs_deinit());
}
#ifdef CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
TEST_CASE("Test run limit setting and getting", "[relay_chn][nvs][run_limit]")
{
TEST_ESP_OK(relay_chn_nvs_init());
@@ -93,7 +93,7 @@ TEST_CASE("Test run limit setting and getting", "[relay_chn][nvs][run_limit]")
}
#endif
#ifdef RELAY_CHN_ENABLE_TILTING
#if CONFIG_RELAY_CHN_ENABLE_TILTING
TEST_CASE("Test sensitivity setting and getting", "[relay_chn][nvs][tilt]")
{
TEST_ESP_OK(relay_chn_nvs_init());
@@ -134,4 +134,4 @@ TEST_CASE("Test tilting invalid parameters", "[relay_chn][nvs][tilt]")
TEST_ESP_OK(relay_chn_nvs_deinit());
}
#endif // RELAY_CHN_ENABLE_TILTING
#endif // CONFIG_RELAY_CHN_ENABLE_TILTING

View File

@@ -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