67 Commits

Author SHA1 Message Date
6860dba4d7 Configure project for code coverage. 2025-08-05 14:41:14 +03:00
22668b6759 Merge pull request 'release-0.5.0' (!30) from release-0.5.0 into dev
Reviewed-on: #30
2025-07-23 17:45:26 +03:00
2e81966afb Bump version to 0.5.0 and update repo URLs. 2025-07-23 17:37:27 +03:00
5734f47cd3 Merge pull request 'feat/1030-more-unit-tests' (!29) from feat/1030-more-unit-tests into dev
Reviewed-on: #29
2025-07-22 16:59:47 +03:00
d884f5f45c Add missing test cases for tilt API.
Added missing test cases for the tilt API.

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

Fixes #1057.
2025-07-22 09:58:59 +03:00
f31eae649f Add a shell utility for running tests. 2025-07-21 18:47:41 +03:00
a143484748 Break down tests into categories.
Break down tests into categories to improve maintainability and test granularity. This makes it easier to execute unit tests in CI/CD pipelines.

Closes #1054.
2025-07-21 15:37:49 +03:00
a9a8169710 Add test cases for ID_ALL channel id.
Add test cases to test all relevant functions that support operating with the `RELAY_CHN_ID_ALL` channel id.

Closes #1052.
2025-07-16 11:42:44 +03:00
74f4341c1d Add test cases for direction flip.
Closes #1051.
2025-07-15 16:14:19 +03:00
a587036093 Add tests for init error handling.
Added tests for covering initialization error handling cases like; NULL pointer, invalid GPIO count etc.

This changed implied removing the `relay_chn_create` from the Unity's `setUp` function and place it in each testcase.

Refs #1050, #1030.
2025-07-15 12:23:21 +03:00
82312ba7c3 Add NULL handling for the gpio_num pointer.
Fixes #1050.
2025-07-15 12:17:46 +03:00
db62a7b5b2 Fix listener memory allocation bug.
- Replaced the buggy, oldschool, plain pointer based list approach with more robust FreeRTOS linked list implementation for the listener API. Fixes #1049.

- Added relevant test cases. Refs #1030.
2025-07-14 18:49:47 +03:00
1ee70be715 Fix invalid ID test loops. 2025-07-14 18:49:47 +03:00
f1cb928341 Add missing destroy function.
The component allocates resources (timers, event loop) in relay_chn_create but never frees them. This is a resource leak.
Hence, a destroy function added to free the resources gracefully.

Fixes #1048.
2025-07-14 11:37:14 +03:00
a90649a4d3 Clean and add more tests.
Clean the unnecessary codes and logging macros to keep the test output cleaner.
Add fail tests for the `get_state*` functions.
2025-07-11 18:32:28 +03:00
b239b50abe Fix invalid channel ID handling.
Fix the issue where get_state* functions do not handle when id is RELAY_CHN_ID_ALL.
Fixes #1037.
2025-07-11 18:28:59 +03:00
9b2274ed7c Add default configs for unit tests. 2025-07-11 17:35:50 +03:00
5a38956146 Restructure the project tree.
Restructured the project tree to align with Espressif's project tree pattern and practice. Also updated the affected cmake files.

Fixes: #1033
2025-07-07 22:57:02 +03:00
82168f34eb Delete this ghost file appeared out of nowhere. 2025-07-07 20:25:45 +03:00
0ebe1c791e Merge pull request 'fix/134-testing-issues' (!27) from fix/134-testing-issues into dev
Reviewed-on: KozmotronikTech/relay_chn_component#27
2025-07-07 14:31:27 +03:00
bacbe03e12 Update the manifest file with the latest information. 2025-07-07 09:50:36 +03:00
be09cb883a Correct the installation description.
Fixes  #1027
2025-07-07 09:39:29 +03:00
925fd5de74 Update descriptions about the inertia timing.
Fixes #1027
2025-07-07 09:27:28 +03:00
2e3e92bb63 Fix testing issues and add more tests.
Fixes #134.
Fix unit testing issues. Add more tests to cover more code.
2025-07-04 17:55:33 +03:00
c4482b8d49 Fix unhandled tilt to run mode transitions.
Fixes #1028.
This commit add unhandled logic to the relay_chn_issue_cmd function to handle transitions from tilt mode to run mode.
2025-07-04 17:31:31 +03:00
41c292cc89 Restructure the project tree for unit testing
Restructure the whole project tree so that the component can be unit tested. Also update some cmake files to update the modified paths, update test cases etc.
2025-07-04 00:38:57 +03:00
ed5b86e863 Fix CMakeLists.txt definitions and test cases. 2025-07-03 18:58:09 +03:00
a1c66d51c7 Merge pull request 'Fix error handling issues.' from fix/172-fix-error-handling into dev
Reviewed-on: https://dev.kozmotronik.com.tr/gitea/KozmotronikTech/relay_chn/pulls/25
2025-04-02 14:39:49 +03:00
421dea7d69 Merge pull request 'fix/172-fix-error-handling' (!24) from fix/172-fix-error-handling into fix/134-testing-issues
Reviewed-on: https://dev.kozmotronik.com.tr/gitea/KozmotronikTech/relay_chn/pulls/24
2025-04-02 14:38:32 +03:00
99d753238b Fix error handling issues.
Fix error handling so that the value in ret variable does not become corrupt.
2025-04-02 14:05:48 +03:00
7afe6144bd Set dev branch's upstream. 2025-03-24 09:31:33 +03:00
4f39308f13 Merge pull request 'fix/162-fix-error-handling' from fix/162-fix-error-handling into fix/134-testing-issues
Reviewed-on: https://dev.kozmotronik.com.tr/gitea/KozmotronikTech/relay_chn/pulls/23
2025-03-21 17:30:13 +03:00
fb425edc4b Merge pull request 'release-0.3.1' from release-0.3.1 into main
Reviewed-on: https://dev.kozmotronik.com.tr/gitea/KozmotronikTech/relay_chn/pulls/22
2025-03-21 17:06:05 +03:00
805df016fe Merge pull request 'fix/162-fix-error-handling' from fix/162-fix-error-handling into dev
Reviewed-on: https://dev.kozmotronik.com.tr/gitea/KozmotronikTech/relay_chn/pulls/21
2025-03-21 16:44:34 +03:00
f230477cad Fix error handling in the init function. 2025-03-21 16:42:06 +03:00
e19bd09389 Update gitignore as per esp-idf gitignore. 2025-03-21 16:41:22 +03:00
11786b7a06 Remove unnecessary unity functions. 2025-03-04 09:49:04 +03:00
7c18ddcc04 Fix declarations as per the documents. 2025-03-04 09:48:24 +03:00
e8303a9418 Fix test directory structure. 2025-03-03 16:20:29 +03:00
496755ed56 Merge pull request #3 from kozmotronik/release-0.3.0
Release 0.3.0
2025-03-03 13:58:25 +03:00
5fe76bb738 Merge pull request #2 from kozmotronik/improve-tilt-algorithm
Improve tilt algorithm
2025-03-03 11:53:43 +03:00
46dd0db939 Fix unclosed code fence. 2025-03-03 10:26:52 +03:00
46f7c28829 refactor: Add reverse tilting capability with limit.
This commit adds reverse tilting capability using tilt counters.
Normally tilting action is based on the last run command of the channel
and reverse tilting was not possible before this implementation.

Reverse tilting means when TILT_REVERSE command is issued while the
last run was forward. In this case the channel will not tilt unless it has tilted
forward (TILT_FORWARD) before. If the channel has tilted forward before,
the forward tilt is counted. This tilt count is the limit for reverse tilting.
So when reverse tilting, the channel automatically will issue the TILT_STOP
command as soon as the tilt count value has reached.
2025-02-27 16:51:01 +03:00
0e68c1f627 refactor: Improve tilting code structure and organization.
Improves the code structure by reorganizing functions, minimizing
the interaction with the relay channel driving API and independizing
the tilting codes as much as possible from the plain channel driving
API for more reliable operation.

With this change the tilting API;
  * has its own events and event handler
  * does not depend on the channel's state change listener
  * does not interfere with channel driver API
  * has its own tilt command issuing logic
  * has its own timing management
2025-02-26 17:06:10 +03:00
Kozmotronik
9ff243c673 Merge pull request 'fix-v0.2.1' (#19) from fix-v0.2.1 into dev
Reviewed-on: https://kozmotronik.nohost.me/gitea/KozmotronikTech/relay_chn/pulls/19
2025-02-24 13:03:47 +03:00
330c996b7b Fix sensitivity setting and getting bugs.
"relay_chn_tilt_sensitivity_set" and "relay_chn_tilt_sensitivity_get"
functions wasn't capable of dealing with "RELAY_CHN_ID_ALL"
channel ID. Hence it was causing load access system errors. This
commit fixes this bug and matures the sensitivity setter and getter.
2025-02-24 10:39:35 +03:00
Kozmotronik
4b8b6fd636 Merge pull request 'release-v0.2.1' (#15) from release-v0.2.1 into main
Reviewed-on: https://kozmotronik.nohost.me/gitea/KozmotronikTech/relay_chn/pulls/15
2025-02-22 11:22:28 +03:00
Kozmotronik
8d96914a38 Merge pull request 'fix-update-documentation' (#14) from fix-update-documentation into dev
Reviewed-on: https://kozmotronik.nohost.me/gitea/KozmotronikTech/relay_chn/pulls/14
2025-02-22 11:13:25 +03:00
b7a23f3633 Add missing docs for the tilting interface. 2025-02-22 10:59:02 +03:00
df935a593b Update Readme and add some tilting feature details. 2025-02-22 10:24:39 +03:00
Kozmotronik
f72fe6b1f0 Merge pull request 'Fix movement transition issue.' (#12) from movement-transition-patch into dev
Reviewed-on: https://kozmotronik.nohost.me/gitea/KozmotronikTech/relay_chn/pulls/12
2025-02-22 09:01:06 +03:00
Kozmotronik
e54e28020c Fix movement transition issue.
When transitioning the movements directly the channel should be stopped first.
2025-02-22 08:48:22 +03:00
Kozmotronik
f5481f79e7 Merge pull request 'dev' (#7) from dev into main
Reviewed-on: https://kozmotronik.nohost.me/gitea/KozmotronikTech/relay_chn/pulls/7
2025-02-21 14:38:48 +03:00
Kozmotronik
7d3f08b56b Merge pull request 'Add tilt feature, fix bugs, improve code.' (#6) from feature-tilt into dev
Reviewed-on: https://kozmotronik.nohost.me/gitea/KozmotronikTech/relay_chn/pulls/6
2025-02-21 12:46:10 +03:00
a694938224 Add tilt feature, fix bugs, improve code.
* Add tilt feature.

* Fix the following bugs:
  * warning: comparison is always true due to limited range of data type.
  * Remove unnecessary esp_timer checks.
  * The scheduled FREE command disrupts the current command.
  * Fatal pin mapping issue.

* Make code optimizations and improvements:
  * Optimize event loop queue size depending on channel count.
  * Change the channels' starting state to FREE.
  * Remove the unnecessary relay_chn_invalidate_inertia_timer function.
  * Change the relay_chn_start_inertia_timer function as relay_chn_start_esp_timer_once and modify the function so that it be a generic esp timer start function.
  * Optimize the if statement that checks the last run cmd in the relay_chn_execute_stop.
2025-02-21 12:43:00 +03:00
dcb5453522 Resolve unseen conflicts. 2025-02-14 17:01:22 +03:00
feb1f4ac81 Update docs for the state_listener_manager API. 2025-02-14 15:12:37 +03:00
069363205a Rename inertia timer to distinguish timers.
Rename timer member and relevant functions for the purpose they used,
in order to distinguish between timers.
2025-02-14 15:00:02 +03:00
d4fdff949a Rename inertia timer to distinguish timers. 2025-02-14 14:50:35 +03:00
dc2dcfec7d Add support for addressing all relay channels. 2025-02-12 16:50:00 +03:00
8517993358 Update file name according to the extension guide. 2025-02-12 08:56:51 +03:00
e21bfb5b26 Update the root gitignore to ignore the build and unity-app. 2025-02-12 08:44:38 +03:00
27c669066a Add headers and test functions, fix gpio definition 2025-02-11 17:45:17 +03:00
d64370c925 Unify test source files. 2025-02-11 14:41:24 +03:00
b47244b5d1 Move relay_chn.c into src directory. 2025-02-11 14:40:37 +03:00
26 changed files with 5852 additions and 789 deletions

179
.gitignore vendored
View File

@@ -1,102 +1,111 @@
# ---> C
# Prerequisites
*.d
# Object files
.config
*.o
*.ko
*.obj
*.elf
*.pyc
# Linker output
*.ilk
*.map
*.exp
# gtags
GTAGS
GRTAGS
GPATH
# Precompiled Headers
*.gch
*.pch
# emacs
.dir-locals.el
# Libraries
*.lib
*.a
*.la
*.lo
# emacs temp file suffixes
*~
.#*
\#*#
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# eclipse setting
.settings
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# MacOS directory files
.DS_Store
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# cache dir
.cache/
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
# Doc build artifacts
docs/_build/
docs/doxygen_sqlite3.db
# ---> C++
# Prerequisites
*.d
# Downloaded font files
docs/_static/DejaVuSans.ttf
docs/_static/NotoSansSC-Regular.otf
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Components Unit Test Apps files
components/**/build/
components/**/build_*_*/
components/**/sdkconfig
components/**/sdkconfig.old
# Precompiled Headers
*.gch
*.pch
# Example project files
examples/**/build/
examples/**/build_*_*/
examples/**/sdkconfig
examples/**/sdkconfig.old
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Unit test app files
tools/unit-test-app/build
tools/unit-test-app/build_*_*/
tools/unit-test-app/sdkconfig
tools/unit-test-app/sdkconfig.old
# Fortran module files
*.mod
*.smod
# test application build files
tools/test_apps/**/build/
tools/test_apps/**/build_*_*/
tools/test_apps/**/sdkconfig
tools/test_apps/**/sdkconfig.old
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
TEST_LOGS/
build_summary_*.xml
# Executables
*.exe
*.out
*.app
# gcov coverage reports
*.gcda
*.gcno
coverage.info
coverage_report/
# ---> CMake
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
CMakeUserPresets.json
test_multi_heap_host
# VS Code Settings
.vscode/
# VIM files
*.swp
*.swo
# Sublime Text files
*.sublime-project
*.sublime-workspace
# Clion IDE CMake build & config
.idea/
cmake-build-*/
# Results for the checking of the Python coding style and static analysis
.mypy_cache
flake8_output.txt
# ESP-IDF default build directory name
build
# lock files for examples and components
dependencies.lock
# managed_components for examples
managed_components
# pytest log
pytest-embedded/
# legacy one
pytest_embedded_log/
list_job*.txt
size_info*.txt
XUNIT_RESULT*.xml
.manifest_sha
# clang config (for LSP)
.clangd
# Vale
.vale/styles/*

6
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,6 @@
{
"files.associations": {
"relay_chn.h": "c"
},
"idf.port": "/dev/ttyUSB0"
}

View File

@@ -1,4 +1,4 @@
idf_component_register(SRCS "relay_chn.c"
idf_component_register(SRCS "src/relay_chn.c"
INCLUDE_DIRS include
REQUIRES driver
PRIV_REQUIRES esp_timer esp_event)

View File

@@ -17,4 +17,13 @@ menu "Relay Channel Driver Configuration"
help
Number of relay channels between 1 and 8.
config RELAY_CHN_ENABLE_TILTING
bool "Enable tilting on relay channels"
default n
help
This option controls enabling tilting on channels. Tilting makes
a channel move with a specific pattern moving with small steps
at a time. Tilting is specifically designed for controlling some
types of curtains that need to be adjusted to let enter specific
amount of day light.
endmenu

View File

@@ -11,10 +11,15 @@ An ESP-IDF component for controlling relay channels, specifically designed for d
- Forward/Reverse direction control
- Direction flipping capability
- State monitoring and reporting
- Optional sensitivty adjustable tilting feature
## Description
Each relay channel consists of 2 output relays controlled by 2 GPIO pins. The component provides APIs to control these relay pairs while ensuring safe operation, particularly for driving bipolar motors. It prevents short-circuits by automatically managing direction changes with configurable inertia timing.
Each relay channel consists of 2 output relays controlled by 2 GPIO pins. The component provides APIs to control these relay pairs while ensuring safe operation, particularly for driving bipolar motors. To prevent mechanical strain on the motor, the component automatically manages direction changes with a configurable inertia delay, protecting it from abrupt reversals.
It also provides an optional tilting interface per channel base. Tilting makes a channel move with a specific pattern moving with small steps at a time. Tilting is specifically designed for controlling some types of curtains that need to be adjusted to let enter specific amount of day light.
Since it operates on relays, the switching frequency is limited to 10Hz which complies with the most of the general purpose relays' requirements. The minimum frequency is 2Hz and the duty cycle is about 10% in all ranges.
The module also handles all the required timing between the movement transitions automatically to ensure reliable operation.
## Configuration
@@ -22,16 +27,18 @@ Configure the component through menuconfig under "Relay Channel Driver Configura
- `CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS`: Time to wait before changing direction (200-1500ms, default: 800ms)
- `CONFIG_RELAY_CHN_COUNT`: Number of relay channels (1-8, default: 1)
- `CONFIG_RELAY_CHN_ENABLE_TILTING`: Enable tilting interface on all channels. (default: n)
## Installation
1. Copy the component to your project's components directory
2. Add dependency to your project's `idf_component.yml`:
Just add it as a custom dependency to your project's `idf_component.yml`:
```yaml
dependencies:
# Add as a custom component from git repository
relay_chn:
version: "^0.1.0"
git: https://git.kozmotronik.com.tr/KozmotronikTech/relay_chn.git
version: '>=0.5.0'
```
## Usage
@@ -77,6 +84,30 @@ char *state_str = relay_chn_get_state_str(0);
relay_chn_direction_t direction = relay_chn_get_direction(0);
```
### 4. Tilting Interface (if enabled)
```c
// Assuming CONFIG_RELAY_CHN_ENABLE_TILTING is enabled
// Start tilting automatically (channel 0)
relay_chn_tilt_auto(0);
// Tilt forward (channel 0)
relay_chn_tilt_forward(0);
// Tilt reverse (channel 0)
relay_chn_tilt_reverse(0);
// Stop tilting (channel 0)
relay_chn_tilt_stop(0);
// Set tilting sensitivity (channel 0, sensitivity as percentage)
relay_chn_tilt_sensitivity_set(0, 90);
// Get tilting sensitivity (channel 0, sensitivty as percentage)
uint8_t sensitivity = relay_chn_tilt_sensitivity_get(0);
```
## License
[MIT License](LICENSE) - Copyright (c) 2025 kozmotronik.

View File

@@ -1,7 +1,6 @@
name: relay_chn
version: 0.1.0
description: Custom component for relay channel control
dependencies:
idf:
version: ">=4.0"
# TODO: Repo ve belgelendirme bağlantılarını ekle.
version: "0.5.0"
description: "Custom component for relay channel control"
license: "MIT"
url: "https://git.kozmotronik.com.tr/KozmotronikTech/relay_chn"
repository: "https://git.kozmotronik.com.tr/KozmotronikTech/relay_chn.git"

View File

@@ -14,7 +14,8 @@
* One relay channel consists of 2 output relays, hence 2 GPIO pins are required for each relay channel.
* This module provides an API to control the relay channels, specifically to drive bipolar motors.
* It also provides APIs to control the direction of the relay channel, bipolar motors in mind.
* The module also automatically manages the direction change inertia to prevent short-circuiting the motor.
* To prevent mechanical strain on the motor, the component automatically manages direction changes
* with a configurable inertia delay, protecting it from abrupt reversals.
* The STOP command overrides any other command and clears the pending command if any.
*
* The module internally uses a custom esp event loop to handle relay commands serially to ensure
@@ -23,11 +24,14 @@
#include "esp_err.h"
#include "driver/gpio.h"
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#define RELAY_CHN_ID_ALL CONFIG_RELAY_CHN_COUNT ///< Special ID to address all channels
/**
* @brief Enumeration for relay channel direction.
*/
@@ -45,12 +49,17 @@ typedef enum relay_chn_direction_enum relay_chn_direction_t;
* @brief Enums that represent the state of a relay channel.
*/
enum relay_chn_state_enum {
RELAY_CHN_STATE_UNDEFINED, ///< The relay channel state is undefined.
RELAY_CHN_STATE_FREE, ///< The relay channel is free to run or execute commands.
RELAY_CHN_STATE_STOPPED, ///< The relay channel is stopped and not running.
RELAY_CHN_STATE_FORWARD, ///< The relay channel is running in the forward direction.
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
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
};
/**
@@ -58,6 +67,20 @@ enum relay_chn_state_enum {
*/
typedef enum relay_chn_state_enum relay_chn_state_t;
/**
* @brief Relay channel state change listener.
*
* An optional interface to listen to the channel state change events.
* The listeners SHOULD be implemented as light functions and SHOULD NOT contain
* any blocking calls. Otherwise the relay_chn module would not function properly
* since it is designed as event driven.
*
* @param chn_id The ID of the channel whose state has changed.
* @param old_state The old state of the channel.
* @param new_state The new state of the channel.
*/
typedef void (*relay_chn_state_listener_t)(uint8_t chn_id, relay_chn_state_t old_state, relay_chn_state_t new_state);
/**
* @brief Create and initialize relay channels.
@@ -74,6 +97,33 @@ typedef enum relay_chn_state_enum relay_chn_state_t;
*/
esp_err_t relay_chn_create(const gpio_num_t* gpio_map, uint8_t gpio_count);
/**
* @brief Destroy the relay channels and free resources.
*
* This function cleans up the relay channels and releases any resources allocated during their creation.
*/
void relay_chn_destroy(void);
/**
* @brief Register a channel state change listener.
*
* @param listener A function that implements relay_chn_state_listener_t interface.
*
* @return
* - ESP_OK: Success
* - ESP_ERR_INVALID_ARG: Invalid argument
* - ESP_ERR_NO_MEM: No enough memory
* - ESP_FAIL: General failure
*/
esp_err_t relay_chn_register_listener(relay_chn_state_listener_t listener);
/**
* @brief Unregister a channel state change listener.
*
* @param listener A function that implements relay_chn_state_listener_t interface.
*/
void relay_chn_unregister_listener(relay_chn_state_listener_t listener);
/**
* @brief Get the state of the specified relay channel.
*
@@ -100,6 +150,14 @@ relay_chn_state_t relay_chn_get_state(uint8_t chn_id);
*/
char *relay_chn_get_state_str(uint8_t chn_id);
/**
* @brief Return the text presentation of an state.
*
* @param state A state with type of relay_chn_state_t.
* @return char* The text presentation of the state. "UNKNOWN" if the state is not known.
*/
char *relay_chn_state_str(relay_chn_state_t state);
/**
* @brief Runs the relay channel in the forward direction.
*
@@ -153,6 +211,77 @@ void relay_chn_flip_direction(uint8_t chn_id);
*/
relay_chn_direction_t relay_chn_get_direction(uint8_t chn_id);
#if CONFIG_RELAY_CHN_ENABLE_TILTING == 1
/**
* @brief Enables automatic tilting for the specified relay channel.
*
* This function enables automatic tilting mode for the given relay channel. The channel will automatically
* switch between forward and reverse tilting based on some internal sensing mechanism (not detailed here).
* Requires appropriate hardware support and configuration.
*
* @param chn_id The ID of the relay channel to enable automatic tilting.
*/
void relay_chn_tilt_auto(uint8_t chn_id);
/**
* @brief Tilts the specified relay channel forward.
*
* This function initiates a forward tilting action for the specified relay channel. This is a manual tilting
* operation, unlike `relay_chn_tilt_auto()`.
*
* @param chn_id The ID of the relay channel to tilt forward.
*/
void relay_chn_tilt_forward(uint8_t chn_id);
/**
* @brief Tilts the specified relay channel reverse.
*
* This function initiates a reverse tilting action for the specified relay channel. This is a manual tilting
* operation, unlike `relay_chn_tilt_auto()`.
*
* @param chn_id The ID of the relay channel to tilt reverse.
*/
void relay_chn_tilt_reverse(uint8_t chn_id);
/**
* @brief Stops the tilting action on the specified relay channel.
*
* This function stops any ongoing tilting action (automatic or manual) on the specified relay channel.
*
* @param chn_id The ID of the relay channel to stop tilting.
*/
void relay_chn_tilt_stop(uint8_t chn_id);
/**
* @brief Sets the tilting sensitivity for the specified relay channel.
*
* This function sets the sensitivity for the automatic tilting mechanism. A higher sensitivity value
* typically means the channel will react more readily to tilting events.
*
* @param chn_id The ID of the relay channel to set the sensitivity for.
* @param sensitivity The sensitivity in percentage: 0 - 100%.
*/
void relay_chn_tilt_sensitivity_set(uint8_t chn_id, uint8_t sensitivity);
/**
* @brief Gets the tilting sensitivity for the specified relay channel.
*
* This function retrieves the currently set sensitivity for the specified relay channel's automatic
* tilting mechanism.
*
* @param chn_id The ID of the relay channel to get the sensitivity for.
* @param sensitivity The pointer to the memory in to which the sensitivity values will be copied.
* @param length The length of the sensitvity memory.
* @return
* - ESP_OK: Success
* - ESP_ERR_INVALID_ARG: Invalid argument
*/
esp_err_t relay_chn_tilt_sensitivity_get(uint8_t chn_id, uint8_t *sensitivity, size_t length);
#endif // CONFIG_RELAY_CHN_ENABLE_TILTING
#ifdef __cplusplus
}
#endif

View File

@@ -1,580 +0,0 @@
/**
* @file relay_chn.c
*
* @author
* Ismail Sahillioglu <ismailsahillioglu@gmail.com>
*
* @date 2025.02.08
*
* @ingroup relay_chn
*
* @brief This file contains the implementation of the relay channel component.
* @{
*/
#include <stdio.h>
#include "esp_err.h"
#include "esp_log.h"
#include "esp_task.h"
#include "driver/gpio.h"
#include "esp_timer.h"
#include "esp_event_base.h"
#include "esp_event.h"
#include "relay_chn.h"
#include "sdkconfig.h"
// TODO: on_state change API si ekle
#define RELAY_CHN_OPPOSITE_INERTIA_MS CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS
#define RELAY_CHN_COUNT CONFIG_RELAY_CHN_COUNT
const char* TAG = "relay_chn";
ESP_EVENT_DEFINE_BASE(RELAY_CHN_CMD_EVENT);
/**
* @brief Enumeration for relay channel commands.
*/
enum relay_chn_cmd_enum {
RELAY_CHN_CMD_NONE, ///< No command.
RELAY_CHN_CMD_STOP, ///< Stop the relay channel.
RELAY_CHN_CMD_FORWARD, ///< Run the relay channel in the forward direction.
RELAY_CHN_CMD_REVERSE, ///< Run the relay channel in the reverse direction.
RELAY_CHN_CMD_FLIP, ///< Flip the direction of the relay channel.
RELAY_CHN_CMD_FREE ///< Free the relay channel.
};
/// @brief Alias for the enum type relay_chn_cmd_enum.
typedef enum relay_chn_cmd_enum relay_chn_cmd_t;
/**
* @brief Structure to hold runtime information for a relay channel.
*/
typedef struct relay_chn_run_info_type {
relay_chn_cmd_t last_run_cmd; ///< The last run command issued on the relay channel; forward or reverse.
uint32_t last_run_cmd_time_ms; ///< The time in milliseconds when the last run command was issued.
} relay_chn_run_info_t;
/**
* @brief Structure to hold the output configuration of a relay channel.
*/
typedef struct relay_chn_output_type {
gpio_num_t forward_pin; ///< GPIO pin number for the forward direction.
gpio_num_t reverse_pin; ///< GPIO pin number for the reverse direction.
relay_chn_direction_t direction; ///< The current direction of the relay channel.
} relay_chn_output_t;
typedef struct relay_chn_type relay_chn_t; // Forward declaration
/**
* @brief Function pointer type for relay channel command execution functions.
* @param relay_chn Pointer to the relay channel to execute the command on.
*/
typedef void(*relay_chn_cmd_fn_t)(relay_chn_t*);
/**
* @brief Structure to hold the state and configuration of a relay channel.
*/
typedef struct relay_chn_type {
uint8_t id; ///< The ID of the relay channel.
relay_chn_state_t state; ///< The current state of the relay channel.
relay_chn_run_info_t run_info; ///< Runtime information of the relay channel.
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 timer; ///< Timer to handle the opposite direction inertia time.
} relay_chn_t;
static relay_chn_t relay_channels[RELAY_CHN_COUNT];
static esp_event_loop_handle_t relay_chn_event_loop;
// Private function declarations
// Event handler for the relay channel command event
static void relay_chn_event_handler(void* handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data);
/**
* @brief Check if the provided channel ID is valid.
*
* @param chn_id Channel ID to check.
* @return true Channel ID is valid.
* @return false Channel ID is invalid.
*/
static bool relay_chn_is_channel_id_valid(uint8_t chn_id);
/**
* @brief Dispatches a relay channel command to the event loop.
*
* @param relay_chn The relay channel.
* @param cmd The command to dispatch.
*/
static void relay_chn_dispatch_cmd(relay_chn_t *relay_chn, relay_chn_cmd_t cmd);
/**
* @brief Returns the string representation of a relay channel command.
*
* @param cmd The relay channel command.
* @return char* The string representation of the command.
*/
static char *relay_chn_cmd_str(relay_chn_cmd_t cmd);
/**
* @brief Timer callback function for relay channel direction change inertia.
*
* This function is called when the opposite direction inertia timer expires. It checks if the channel
* has a pending command and dispatches it if there is one.
*
* @param arg The channel ID of the relay channel.
*/
static void relay_chn_timer_cb(void* arg)
{
uint8_t chn_id = *(uint8_t*) arg;
if (!relay_chn_is_channel_id_valid(chn_id)) {
ESP_LOGE(TAG, "relay_chn_timer_cb: Invalid relay channel ID!");
return;
}
relay_chn_t* relay_chn = &relay_channels[chn_id];
// Does channel have a pending command?
if (relay_chn->pending_cmd != RELAY_CHN_CMD_NONE) {
relay_chn_dispatch_cmd(relay_chn, relay_chn->pending_cmd);
relay_chn->pending_cmd = RELAY_CHN_CMD_NONE;
}
else {
ESP_LOGE(TAG, "relay_chn_timer_cb: No pending cmd for relay channel %d!", chn_id);
}
}
static esp_err_t relay_chn_init_timer(relay_chn_t *relay_chn)
{
char timer_name[32];
snprintf(timer_name, sizeof(timer_name), "relay_chn_%d_timer", relay_chn->id);
esp_timer_create_args_t timer_args = {
.callback = relay_chn_timer_cb,
.arg = &relay_chn->id,
.name = timer_name
};
return esp_timer_create(&timer_args, &relay_chn->timer);
}
/**
* @brief Check if the provided GPIO pin number is valid for the current device.
*
* @param gpio The GPIO pin number to check.
* @return true GPIO pin number is valid.
* @return false GPIO pin number is invalid.
*/
static bool relay_chn_is_gpio_valid(gpio_num_t gpio)
{
return gpio >= 0 && gpio < GPIO_PIN_COUNT;
}
static esp_err_t relay_chn_create_event_loop()
{
esp_event_loop_args_t loop_args = {
.queue_size = 10,
.task_name = "relay_chn_event_loop",
.task_priority = ESP_TASKD_EVENT_PRIO - 1,
.task_stack_size = 2048,
.task_core_id = tskNO_AFFINITY
};
esp_err_t ret = esp_event_loop_create(&loop_args, &relay_chn_event_loop);
ret |= esp_event_handler_register_with(relay_chn_event_loop,
RELAY_CHN_CMD_EVENT,
ESP_EVENT_ANY_ID,
relay_chn_event_handler, NULL);
return ret;
}
esp_err_t relay_chn_create(const gpio_num_t* gpio_map, 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)) {
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);
return ESP_ERR_INVALID_ARG;
}
// Check if the provided GPIOs correspond to the number of channels
if (gpio_count != 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);
return ESP_ERR_INVALID_ARG;
}
esp_err_t ret;
for (int i = 0; i < RELAY_CHN_COUNT; i++) {
gpio_num_t forward_pin = gpio_map[i];
gpio_num_t reverse_pin = gpio_map[i+1];
// Check if the GPIOs are valid
if (!relay_chn_is_gpio_valid(forward_pin)) {
ESP_LOGE(TAG, "Invalid GPIO pin number: %d", forward_pin);
return ESP_ERR_INVALID_ARG;
}
if (!relay_chn_is_gpio_valid(reverse_pin)) {
ESP_LOGE(TAG, "Invalid GPIO pin number: %d", reverse_pin);
return ESP_ERR_INVALID_ARG;
}
// Check if the GPIOs are valid
// Initialize the GPIOs
ret = gpio_reset_pin(forward_pin);
ret |= gpio_set_direction(forward_pin, GPIO_MODE_OUTPUT);
ret |= gpio_reset_pin(reverse_pin);
ret |= gpio_set_direction(reverse_pin, GPIO_MODE_OUTPUT);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize GPIOs relay channel %d!", i);
return ret;
}
// Initialize the GPIOs
// Initialize the relay channel
relay_chn_t* relay_chn = &relay_channels[i];
relay_chn->id = i;
relay_chn->output.forward_pin = forward_pin;
relay_chn->output.reverse_pin = reverse_pin;
relay_chn->output.direction = RELAY_CHN_DIRECTION_DEFAULT;
relay_chn->state = RELAY_CHN_STATE_STOPPED;
relay_chn->pending_cmd = RELAY_CHN_CMD_NONE;
relay_chn->run_info.last_run_cmd = RELAY_CHN_CMD_NONE;
ret |= relay_chn_init_timer(relay_chn);// Create direction change inertia timer
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize relay channel %d!", i);
return ret;
}
}
// Create relay channel command event loop
ret |= relay_chn_create_event_loop();
return ret;
}
/**
* @brief Check channel ID validity
*
* @param chn_id Channel ID to check
* @return true If channel is valid
* @return false If channel is invalid
*/
static bool relay_chn_is_channel_id_valid(uint8_t chn_id)
{
bool valid = chn_id >= 0 && chn_id < RELAY_CHN_COUNT;
if (!valid) {
ESP_LOGE(TAG, "Invalid channel ID: %d", chn_id);
}
return valid;
}
// Dispatch relay channel command to its event loop
static void relay_chn_dispatch_cmd(relay_chn_t *relay_chn, relay_chn_cmd_t cmd) {
if (cmd == RELAY_CHN_CMD_NONE) {
return;
}
esp_event_post_to(relay_chn_event_loop,
RELAY_CHN_CMD_EVENT,
cmd,
&relay_chn->id,
sizeof(relay_chn->id), portMAX_DELAY);
}
static esp_err_t relay_chn_invalidate_timer(relay_chn_t *relay_chn)
{
if (esp_timer_is_active(relay_chn->timer)) {
return esp_timer_stop(relay_chn->timer);
}
return ESP_OK;
}
static esp_err_t relay_chn_start_timer(relay_chn_t *relay_chn, uint32_t time_ms)
{
// Invalidate the channel's timer if it is active
relay_chn_invalidate_timer(relay_chn);
return esp_timer_start_once(relay_chn->timer, time_ms * 1000);
}
/**
* @brief The command issuer function.
*
* This function is the deciding logic for issuing a command to a relay channel. It evaluates
* the current state of the channel before issuing the command. Then it decides whether to run
* the command immediately or wait for the opposite inertia time.
*
* The STOP command is an exception, it is always run immediately since it is safe in any case.
*
* Another special consideration is the FLIP command. If the channel is running, the FLIP command
* is issued after the channel is stopped. If the channel is stopped, the FLIP command is issued
* immediately.
*
* @param relay_chn The relay channel to issue the command to.
* @param cmd The command to issue.
*/
static void relay_chn_issue_cmd(relay_chn_t* relay_chn, relay_chn_cmd_t cmd)
{
if (cmd == RELAY_CHN_CMD_NONE) {
return;
}
if (cmd == RELAY_CHN_CMD_STOP) {
if (relay_chn->state == RELAY_CHN_STATE_STOPPED) {
return; // Do nothing if already stopped
}
// If the command is STOP, issue it immediately
relay_chn_dispatch_cmd(relay_chn, cmd);
return;
}
// Evaluate the channel's next move depending on its status
switch (relay_chn->state)
{
case RELAY_CHN_STATE_FREE:
// If the channel is free, run the command immediately
relay_chn_dispatch_cmd(relay_chn, cmd);
break;
case RELAY_CHN_STATE_FORWARD_PENDING:
case RELAY_CHN_STATE_REVERSE_PENDING:
// The channel is already waiting for the opposite inertia time,
// so do nothing unless the command is STOP
if (cmd == RELAY_CHN_CMD_STOP) {
relay_chn_dispatch_cmd(relay_chn, cmd);
}
break;
case RELAY_CHN_STATE_STOPPED:
if (relay_chn->run_info.last_run_cmd == cmd) {
// If the last run command is the same as the current command, run the command immediately
relay_chn_dispatch_cmd(relay_chn, cmd);
}
else {
// If the last run command is different from the current command, calculate the time passed
// since the last run command stopped and decide whether to run the command immediately or wait
uint32_t inertia_time_passed_ms = (uint32_t) (esp_timer_get_time() / 1000) - relay_chn->run_info.last_run_cmd_time_ms;
uint32_t inertia_time_ms = RELAY_CHN_OPPOSITE_INERTIA_MS - inertia_time_passed_ms;
if (inertia_time_ms > 0) {
relay_chn->pending_cmd = cmd;
relay_chn->state = cmd == RELAY_CHN_CMD_FORWARD
? RELAY_CHN_STATE_FORWARD_PENDING
: RELAY_CHN_STATE_REVERSE_PENDING;
// If the time passed is less than the opposite inertia time, wait for the remaining time
relay_chn_start_timer(relay_chn, inertia_time_ms);
}
else {
// If the time passed is more than the opposite inertia time, run the command immediately
relay_chn_dispatch_cmd(relay_chn, cmd);
}
}
break;
case RELAY_CHN_STATE_FORWARD:
case RELAY_CHN_STATE_REVERSE:
if (cmd == RELAY_CHN_CMD_FLIP) {
// If the command is FLIP, stop the running channel first, then issue the FLIP command
relay_chn_dispatch_cmd(relay_chn, RELAY_CHN_CMD_STOP);
relay_chn_dispatch_cmd(relay_chn, cmd);
return;
}
if (relay_chn->run_info.last_run_cmd == cmd) {
// If the last run command is the same as the current command, do nothing
return;
}
// If the last run command is different from the current command, wait for the opposite inertia time
relay_chn->pending_cmd = cmd;
relay_chn->state = cmd == RELAY_CHN_CMD_FORWARD ? RELAY_CHN_STATE_FORWARD_PENDING : RELAY_CHN_STATE_REVERSE_PENDING;
relay_chn_start_timer(relay_chn, RELAY_CHN_OPPOSITE_INERTIA_MS);
break;
default: ESP_LOGD(TAG, "relay_chn_evaluate: Unknown relay channel state!");
}
}
/* relay_chn APIs */
relay_chn_state_t relay_chn_get_state(uint8_t chn_id)
{
if (!relay_chn_is_channel_id_valid(chn_id)) {
return RELAY_CHN_STATE_STOPPED;
}
return relay_channels[chn_id].state;
}
char *relay_chn_get_state_str(uint8_t chn_id)
{
if (!relay_chn_is_channel_id_valid(chn_id)) {
return "INVALID";
}
switch (relay_channels[chn_id].state) {
case RELAY_CHN_STATE_FREE:
return "FREE";
case RELAY_CHN_STATE_STOPPED:
return "STOPPED";
case RELAY_CHN_STATE_FORWARD:
return "FORWARD";
case RELAY_CHN_STATE_REVERSE:
return "REVERSE";
case RELAY_CHN_STATE_FORWARD_PENDING:
return "FORWARD_PENDING";
case RELAY_CHN_STATE_REVERSE_PENDING:
return "REVERSE_PENDING";
default:
return "UNKNOWN";
}
}
void relay_chn_run_forward(uint8_t chn_id)
{
if (!relay_chn_is_channel_id_valid(chn_id)) return;
relay_chn_t* relay_chn = &relay_channels[chn_id];
relay_chn_issue_cmd(relay_chn, RELAY_CHN_CMD_FORWARD);
}
void relay_chn_run_reverse(uint8_t chn_id)
{
if (!relay_chn_is_channel_id_valid(chn_id)) return;
relay_chn_t* relay_chn = &relay_channels[chn_id];
relay_chn_issue_cmd(relay_chn, RELAY_CHN_CMD_REVERSE);
}
void relay_chn_stop(uint8_t chn_id)
{
if (!relay_chn_is_channel_id_valid(chn_id)) return;
relay_chn_t* relay_chn = &relay_channels[chn_id];
relay_chn_issue_cmd(relay_chn, RELAY_CHN_CMD_STOP);
}
void relay_chn_flip_direction(uint8_t chn_id)
{
if (!relay_chn_is_channel_id_valid(chn_id)) return;
relay_chn_t* relay_chn = &relay_channels[chn_id];
relay_chn_issue_cmd(relay_chn, RELAY_CHN_CMD_FLIP);
}
relay_chn_direction_t relay_chn_get_direction(uint8_t chn_id)
{
if (!relay_chn_is_channel_id_valid(chn_id)) {
return RELAY_CHN_DIRECTION_DEFAULT;
}
return relay_channels[chn_id].output.direction;
}
/* relay_chn APIs */
static void relay_chn_execute_stop(relay_chn_t *relay_chn)
{
gpio_set_level(relay_chn->output.forward_pin, 0);
gpio_set_level(relay_chn->output.reverse_pin, 0);
relay_chn->state = RELAY_CHN_STATE_STOPPED;
// If there is any pending command, cancel it since the STOP command is issued right after it
relay_chn->pending_cmd = RELAY_CHN_CMD_NONE;
// Invalidate the channel's timer if it is active
relay_chn_invalidate_timer(relay_chn);
// If the channel was running, schedule a free command for the channel
relay_chn_cmd_t last_run_cmd = relay_chn->run_info.last_run_cmd;
if (last_run_cmd == RELAY_CHN_CMD_FORWARD || last_run_cmd == RELAY_CHN_CMD_REVERSE) {
// Record the command's last run time
relay_chn->run_info.last_run_cmd_time_ms = esp_timer_get_time() / 1000;
// Schedule a free command for the channel
relay_chn->pending_cmd = RELAY_CHN_CMD_FREE;
relay_chn_start_timer(relay_chn, RELAY_CHN_OPPOSITE_INERTIA_MS);
} else {
// If the channel was not running, issue a free command immediately
relay_chn_dispatch_cmd(relay_chn, RELAY_CHN_CMD_FREE);
}
}
static void relay_chn_execute_forward(relay_chn_t *relay_chn)
{
gpio_set_level(relay_chn->output.reverse_pin, 0);
gpio_set_level(relay_chn->output.forward_pin, 1);
relay_chn->state = RELAY_CHN_STATE_FORWARD;
relay_chn->run_info.last_run_cmd = RELAY_CHN_CMD_FORWARD;
}
static void relay_chn_execute_reverse(relay_chn_t *relay_chn)
{
gpio_set_level(relay_chn->output.forward_pin, 0);
gpio_set_level(relay_chn->output.reverse_pin, 1);
relay_chn->state = RELAY_CHN_STATE_REVERSE;
relay_chn->run_info.last_run_cmd = RELAY_CHN_CMD_REVERSE;
}
static void relay_chn_execute_flip(relay_chn_t *relay_chn)
{
// Flip the output GPIO pins
gpio_num_t temp = relay_chn->output.forward_pin;
relay_chn->output.forward_pin = relay_chn->output.reverse_pin;
relay_chn->output.reverse_pin = temp;
// Flip the direction
relay_chn->output.direction = (relay_chn->output.direction == RELAY_CHN_DIRECTION_DEFAULT)
? RELAY_CHN_DIRECTION_FLIPPED
: RELAY_CHN_DIRECTION_DEFAULT;
// Set an inertia on the channel to prevent any immediate movement
relay_chn->pending_cmd = RELAY_CHN_CMD_FREE;
relay_chn_start_timer(relay_chn, RELAY_CHN_OPPOSITE_INERTIA_MS);
}
void relay_chn_execute_free(relay_chn_t *relay_chn)
{
relay_chn->state = RELAY_CHN_STATE_FREE;
relay_chn->pending_cmd = RELAY_CHN_CMD_NONE;
// Invalidate the channel's timer if it is active
relay_chn_invalidate_timer(relay_chn);
}
static void relay_chn_event_handler(void* handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
{
uint8_t chn_id = *(uint8_t*) event_data;
if (!relay_chn_is_channel_id_valid(chn_id)) {
return;
}
relay_chn_t* relay_chn = &relay_channels[chn_id];
ESP_LOGD(TAG, "relay_chn_event_handler: Channel %d, Command: %s", relay_chn->id, relay_chn_cmd_str(event_id));
switch (event_id) {
case RELAY_CHN_CMD_STOP:
relay_chn_execute_stop(relay_chn);
break;
case RELAY_CHN_CMD_FORWARD:
relay_chn_execute_forward(relay_chn);
break;
case RELAY_CHN_CMD_REVERSE:
relay_chn_execute_reverse(relay_chn);
break;
case RELAY_CHN_CMD_FLIP:
relay_chn_execute_flip(relay_chn);
break;
case RELAY_CHN_CMD_FREE:
relay_chn_execute_free(relay_chn);
break;
default:
ESP_LOGD(TAG, "Unknown relay channel command!");
}
}
static char *relay_chn_cmd_str(relay_chn_cmd_t cmd)
{
switch (cmd) {
case RELAY_CHN_CMD_STOP:
return "STOP";
case RELAY_CHN_CMD_FORWARD:
return "FORWARD";
case RELAY_CHN_CMD_REVERSE:
return "REVERSE";
case RELAY_CHN_CMD_FLIP:
return "FLIP";
case RELAY_CHN_CMD_FREE:
return "FREE";
default:
return "UNKNOWN";
}
}
/// @}

156
scripts/run_tests.sh Executable file
View File

@@ -0,0 +1,156 @@
#!/usr/bin/env bash
set -e
# ==== 1. Check ESP-IDF environment ====
if [[ -z "$IDF_PATH" ]]; then
echo "❌ ESP-IDF environment not found. Please source the export.sh file first:"
echo "'. $HOME/esp/esp-idf/export.sh' or wherever the ESP-IDF is installed"
exit 1
fi
# ==== 2. Valid Modes and Defaults ====
valid_test_tags=("core" "tilt" "listener" "all" "relay_chn")
arg_tag="all" # Default to 'all' if no tag specified
arg_clean=false
arg_log=false
arg_dry_run=false
arg_sdkconfig_file=""
flag_file=false
print_help() {
echo "Usage: $0 -t <tags> [OPTIONS]"
echo ""
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|all] Specify which test tag to run."
echo ""
echo " If no tag is specified, it defaults to 'all'."
echo ""
echo "Options:"
echo " -f, --file <path> Specify a custom sdkconfig file to use for the build."
echo " Defaults to 'sdkconfig.defaults' if not provided."
echo " -c, --clean Perform a 'fullclean' before building the tests."
echo " -l, --log Log the test output to a timestamped file."
echo " -n, --dry-run Build the project without running qemu."
echo " -h, --help Show this help message and exit."
}
help() {
print_help
exit 0
}
usage() {
print_help
exit 1
}
# ==== 3. Argument Parsing ====
while [[ $# -gt 0 ]]; do
case $1 in
--tag|-t)
arg_tag="$2"
shift 2
;;
--file|-f)
arg_sdkconfig_file="$2"
flag_file=true
shift 2
;;
--clean|-c)
arg_clean=true
shift
;;
--log|-l)
arg_log=true
shift
;;
--dry-run|-n)
arg_dry_run=true
shift
;;
--help|-h)
help
;;
*)
usage
;;
esac
done
# ==== 4. Validity Check ====
if [[ ! " ${valid_test_tags[*]} " =~ " $arg_tag " ]]; then
echo "❌ Invalid mode: '$arg_tag'"
usage
fi
# ==== 5. Resolve Paths and Switch to Working Directory ====
script_dir=$(dirname "$(readlink -f "$0")")
project_root=$(dirname "$script_dir")
echo "🔍 Searching for 'test_apps' directory in '$project_root'..."
test_apps_dir=$(find "$project_root" -type d -name "test_apps" | head -n 1)
if [[ -z "$test_apps_dir" || ! -d "$test_apps_dir" ]]; then
echo "❌ 'test_apps' directory not found within the project root: '$project_root'"
echo " Please ensure the script is in a 'scripts' directory and 'test_apps' is a sibling."
exit 1
fi
echo "✅ Found 'test_apps' at: $test_apps_dir"
if $flag_file; then
if [[ -z "$arg_sdkconfig_file" || ! -f "$arg_sdkconfig_file" ]]; then
echo "❌ Invalid or missing file: '$arg_sdkconfig_file'"
usage
fi
# Resolve to an absolute path to work correctly after changing directory
arg_sdkconfig_file=$(readlink -f "$arg_sdkconfig_file")
else
echo "⚠️ No SDK configuration file provided. Using default sdkconfig."
arg_sdkconfig_file="$test_apps_dir/sdkconfig.defaults"
fi
echo "🧪 Test mode: $arg_tag"
echo "🧹 Clean: $arg_clean | 📄 Log: $arg_log"
echo "📂 Changing to working directory: $test_apps_dir"
cd "$test_apps_dir" || exit 1
# ==== 6. Clean if requested ====
if $arg_clean; then
echo "🧹 Doing Fullclean..."
idf.py fullclean
rm sdkconfig
fi
# ==== 7. Building and Running Tests ====
# In some locales, we can get errors like: "Error: unknown opcode or format name 'wsr.IBREAKA1'"
# The 'LC_ALL=C' env variable is set to ensure consistent locale settings.
LC_ALL=C \
SDKCONFIG_DEFAULTS="$arg_sdkconfig_file" \
RELAY_CHN_UNITY_TEST_GROUP_TAG="$arg_tag" \
idf.py reconfigure build
echo "🚀 Running test with QEMU..."
if $arg_log; then
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
LOGFILE="test_log_${arg_tag}_$TIMESTAMP.txt"
if $arg_dry_run; then
echo "🔍 Dry run mode: Logging to $LOGFILE but not executing." | tee "$LOGFILE"
echo "Command: idf.py qemu" | tee "$LOGFILE"
else
echo "📜 Logging test output to: $LOGFILE"
idf.py qemu --qemu-extra-args "-no-reboot" | tee "$LOGFILE"
fi
else
if $arg_dry_run; then
echo "🔍 Dry run mode: Not executing idf.py qemu."
echo "Command: idf.py qemu"
else
echo "🚀 Running idf.py qemu..."
idf.py qemu --qemu-extra-args "-no-reboot"
fi
fi

1470
src/relay_chn.c Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,8 +0,0 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components"
"../../relay_chn")
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(relay_chn_test)

View File

@@ -1,3 +0,0 @@
idf_component_register(SRCS_DIRS "."
PRIV_INCLUDE_DIRS "."
PRIV_REQUIRES unity test_utils relay_chn)

View File

@@ -1,75 +0,0 @@
#include "unity.h"
#include "relay_chn.h"
const gpip_num_t gpio_map[] = {GPIO_NUM_4, GPIO_NUM_5, GPIO_NUM_18, GPIO_NUM_19};
const uint8_t gpio_count = sizeof(gpio_map) / sizeof(gpio_map[0]);
const uint8_t relay_chn_count = gpio_count / 2;
TEST_CASE("relay chn inits correctly", "[relay_chn]")
{
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
}
TEST_CASE("Relay channels run forward and update state", "[relay_chn][forward]")
{
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
// Test forward run on all channels
for (uint8_t i = 0; i < relay_chn_count; i++) {
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_STOPPED, relay_chn_get_state(i));
relay_chn_run_forward(i); // Run the channel forward
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state(i));
relay_chn_stop(i); // Stop the channel
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_STOPPED, relay_chn_get_state(i));
relay_chn_flip_direction(i); // Flip the direction
TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_FLIPPED, relay_chn_get_direction(i));
relay_chn_run_forward(i); // Run the channel forward
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state(i));
relay_chn_stop(i); // Stop the channel
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_STOPPED, relay_chn_get_state(i));
}
}
TEST_CASE("Relay channels run reverse and update state", "[relay_chn][reverse]")
{
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
// Test reverse run on all channels
for (uint8_t i = 0; i < relay_chn_count; i++) {
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_STOPPED, relay_chn_get_state(i));
relay_chn_run_reverse(i); // Run the channel reverse
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state(i));
relay_chn_stop(i); // Stop the channel
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_STOPPED, relay_chn_get_state(i));
relay_chn_flip_direction(i); // Flip the direction
TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_FLIPPED, relay_chn_get_direction(i));
relay_chn_run_reverse(i); // Run the channel forward
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state(i));
relay_chn_stop(i); // Stop the channel
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_STOPPED, relay_chn_get_state(i));
}
}
static void check_channels_state_unchanged(void)
{
for (uint8_t i = 0; i < relay_chn_count; i++) {
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_STOPPED, relay_chn_get_state(i));
TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, relay_chn_get_direction(i));
}
}
TEST_CASE("Relay channels do not change state for invalid channel", "[relay_chn][invalid]")
{
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
// Test invalid channel run
relay_chn_run_forward(relay_chn_count + 1); // Run the channel forward
check_channels_state_unchanged();
relay_chn_run_reverse(relay_chn_count + 1); // Run the channel reverse
check_channels_state_unchanged();
relay_chn_stop(relay_chn_count + 1); // Stop the channel
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_STOPPED, relay_chn_get_state(relay_chn_count + 1));
check_channels_state_unchanged();
relay_chn_flip_direction(relay_chn_count + 1); // Flip the direction
check_channels_state_unchanged();
}

View File

@@ -1,21 +0,0 @@
#include <stdio.h>
#include <string.h>
#include "unity.h"
#include "unity_test_runner.h"
static void print_banner(const char*);
void app_main(void) {
print_banner("Starting interactive test menu");
/* This function will not return, and will be busy waiting for UART input.
* Make sure that task watchdog is disabled if you use this function.
*/
unity_run_menu();
}
static void print_banner(const char* text)
{
printf("\n##### %s #####\n\n", text);
}

View File

@@ -1,5 +0,0 @@
# For IDF 5.0
CONFIG_ESP_TASK_WDT_EN=n
# For IDF4.4
CONFIG_ESP_TASK_WDT=n

29
test_apps/CMakeLists.txt Normal file
View File

@@ -0,0 +1,29 @@
# This is the project CMakeLists.txt file for the test subproject
cmake_minimum_required(VERSION 3.5)
# Define component search paths
# IMPORTANT: We should tell to the ESP-IDF
# where it can find relay_chn component.
# We add the 'relay_chn' root directory to the EXTRA_COMPONENT_DIRS by specifying: "../"
set(EXTRA_COMPONENT_DIRS "../")
# "Trim" the build. Include the minimal set of components, main, and anything it depends on.
set(COMPONENTS main)
# Include ESP-IDF project build system
# include($ENV{IDF_PATH}/tools/cmake/project.cmake)
file(TO_NATIVE_PATH "$ENV{IDF_PATH}/tools/cmake/project.cmake" _project_path)
# Inject the test tag into the build
add_compile_definitions(RELAY_CHN_UNITY_TEST_GROUP_TAG=\"$ENV{RELAY_CHN_UNITY_TEST_GROUP_TAG}\")
include(${_project_path})
# Define the name of this project
project(relay_chn_test)
file(TO_NATIVE_PATH "${CMAKE_CURRENT_BINARY_DIR}/coverage_report" _coverage_path)
idf_create_coverage_report(${_coverage_path})
idf_clean_coverage_report(${_coverage_path})

View File

@@ -0,0 +1,27 @@
# === These files must be included in any case ===
set(srcs "test_common.c"
"test_app_main.c"
"test_relay_chn_core.c"
"test_relay_chn_listener.c")
if(CONFIG_RELAY_CHN_ENABLE_TILTING)
list(APPEND srcs "test_relay_chn_tilt.c")
endif()
message(STATUS "srcs=${srcs}")
# In order for the cases defined by `TEST_CASE` to be linked into the final elf,
# the component can be registered as WHOLE_ARCHIVE
idf_component_register(
SRCS ${srcs}
INCLUDE_DIRS "."
REQUIRES unity relay_chn app_trace
WHOLE_ARCHIVE
)
set_source_files_properties(
"test_common.c"
"test_app_main.c"
"test_relay_chn_core.c"
"test_relay_chn_listener.c"
PROPERTIES COMPILE_FLAGS --coverage)

View File

@@ -0,0 +1,49 @@
#include "esp_log.h"
#include "esp_system.h"
#include "test_common.h"
#include "unity.h"
#include "unity_internals.h"
#include "unity_test_runner.h"
#include <stdbool.h>
#ifndef RELAY_CHN_UNITY_TEST_GROUP_TAG
#warning "RELAY_CHN_UNITY_TEST_GROUP_TAG is not defined, using default 'relay_chn'"
#define RELAY_CHN_UNITY_TEST_GROUP_TAG "relay_chn"
#endif
void setUp()
{
g_is_component_initialized = false;
}
void tearDown()
{
// Clean up after each test
if (g_is_component_initialized) {
relay_chn_destroy();
g_is_component_initialized = false;
}
}
void app_main(void)
{
UNITY_BEGIN();
// Log general test information
ESP_LOGI(TEST_TAG, "Available test count: %d", unity_get_test_count());
ESP_LOGI(TEST_TAG, "Running tests for tag: %s", RELAY_CHN_UNITY_TEST_GROUP_TAG);
if (strncmp(RELAY_CHN_UNITY_TEST_GROUP_TAG, "all", strlen("all")) == 0) {
unity_run_all_tests();
}
else {
unity_run_tests_by_tag(RELAY_CHN_UNITY_TEST_GROUP_TAG, false);
}
UNITY_END();
ESP_LOGI(TEST_TAG, "All tests complete.");
esp_restart(); // Restart to invoke qemu exit
}

View File

@@ -0,0 +1,17 @@
#include "test_common.h"
const char *TEST_TAG = "RELAY_CHN_TEST";
// GPIO eşlemesi (örn: GPIO_NUM_4 vs GPIO_NUM_5)
const gpio_num_t gpio_map[] = {
GPIO_NUM_4, GPIO_NUM_5, GPIO_NUM_18, GPIO_NUM_19
};
const uint8_t gpio_count = sizeof(gpio_map) / sizeof(gpio_map[0]);
const uint8_t relay_chn_count = gpio_count / 2;
// Konfigürasyon tabanlı inertia süresi
const uint32_t opposite_inertia_ms = CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS;
const uint32_t test_delay_margin_ms = 50; // ms toleransı
bool g_is_component_initialized = false;

View File

@@ -0,0 +1,25 @@
#pragma once
#include <string.h> // For memset
#include "unity.h"
#include "relay_chn.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
// Test log tag
extern const char *TEST_TAG;
// GPIO konfigürasyonları
extern const gpio_num_t gpio_map[];
extern const uint8_t gpio_count;
extern const uint8_t relay_chn_count;
// Config parametreleri
extern const uint32_t opposite_inertia_ms;
extern const uint32_t test_delay_margin_ms;
// Init durumu
extern bool g_is_component_initialized;

View File

@@ -0,0 +1,403 @@
#include "test_common.h"
// --- Initialization Tests ---
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)
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));
// 3. Test with invalid GPIO numbers (GPIO_NUM_MAX is an invalid GPIO for output)
gpio_num_t invalid_gpio_map[] = {GPIO_NUM_4, GPIO_NUM_MAX, GPIO_NUM_18, GPIO_NUM_19};
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_create(invalid_gpio_map, gpio_count));
}
// --- Basic Functionality Tests ---
// TEST_CASE: Test that relay channels initialize correctly to RELAY_CHN_STATE_FREE
TEST_CASE("Relay channels initialize correctly to FREE state", "[relay_chn][core]") {
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
for (uint8_t i = 0; i < relay_chn_count; i++) {
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FREE, relay_chn_get_state(i));
}
}
// TEST_CASE: Test that relays do nothing when an invlid channel id given
TEST_CASE("Run forward does nothing if channel id is invalid", "[relay_chn][core]") {
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
for (uint8_t i = 0; i < relay_chn_count; i++) {
int invalid_id = relay_chn_count * 2 + i;
relay_chn_run_forward(invalid_id); // relay_chn_run_forward returns void
// Short delay for state to update
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FREE, relay_chn_get_state(i));
}
}
// TEST_CASE: Test that relays run in the forward direction and update their state
TEST_CASE("Relay channels run forward and update state", "[relay_chn][core]") {
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
for (uint8_t i = 0; i < relay_chn_count; i++) {
relay_chn_run_forward(i); // relay_chn_run_forward returns void
// Short delay for state to update
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state(i));
}
}
// TEST_CASE: Test that relays do nothing when an invlid channel id given
TEST_CASE("Run reverse does nothing if channel id is invalid", "[relay_chn][core]") {
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
// Verify that no valid channels were affected
for (uint8_t i = 0; i < relay_chn_count; i++) {
int invalid_id = relay_chn_count * 2 + i;
// Call run_reverse with an invalid ID
relay_chn_run_reverse(invalid_id);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FREE, relay_chn_get_state(i));
}
}
// TEST_CASE: Test that relays run in the reverse direction and update their state
TEST_CASE("Relay channels run reverse and update state", "[relay_chn][core]") {
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
for (uint8_t i = 0; i < relay_chn_count; i++) {
relay_chn_run_reverse(i); // relay_chn_run_reverse returns void
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state(i));
}
}
// ### Broadcast Command (RELAY_CHN_ID_ALL) Tests
TEST_CASE("run_forward with ID_ALL sets all channels to FORWARD", "[relay_chn][core][id_all]")
{
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
relay_chn_run_forward(RELAY_CHN_ID_ALL);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
for (uint8_t i = 0; i < relay_chn_count; i++) {
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state(i));
}
}
TEST_CASE("run_reverse with ID_ALL sets all channels to REVERSE", "[relay_chn][core][id_all]")
{
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
relay_chn_run_reverse(RELAY_CHN_ID_ALL);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
for (uint8_t i = 0; i < relay_chn_count; i++) {
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state(i));
}
}
TEST_CASE("stop with ID_ALL stops all running channels", "[relay_chn][core][id_all]")
{
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
// 1. Start all channels forward to ensure they are in a known running state
relay_chn_run_forward(RELAY_CHN_ID_ALL);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
// 2. Stop all channels using the broadcast command
relay_chn_stop(RELAY_CHN_ID_ALL);
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
// 3. Verify all channels have transitioned to the FREE state
for (uint8_t i = 0; i < relay_chn_count; i++) {
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FREE, relay_chn_get_state(i));
}
}
// TEST_CASE: Test that relays stop and transition to RELAY_CHN_STATE_FREE
// This test also verifies the transition to FREE state after a STOP command.
TEST_CASE("Relay channels stop and update to FREE state", "[relay_chn][core]") {
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
for (uint8_t i = 0; i < relay_chn_count; i++) {
// First, run forward to test stopping and transitioning to FREE state
relay_chn_run_forward(i); // relay_chn_run_forward returns void
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state(i));
// Now, issue the stop command
relay_chn_stop(i); // relay_chn_stop returns void
// Immediately after stop, state should be STOPPED
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_STOPPED, relay_chn_get_state(i));
// Then, wait for the inertia period for it to transition to RELAY_CHN_STATE_FREE
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FREE, relay_chn_get_state(i));
}
}
// TEST_CASE: Get state should return UNDEFINED when id is not valid
TEST_CASE("Get state returns UNDEFINED when id is invalid", "[relay_chn][core]") {
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
for (uint8_t i = 0; i < relay_chn_count; i++) {
int invalid_id = relay_chn_count * 2 + i;
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);
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;
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_UNDEFINED, relay_chn_get_state(invalid_id));
}
}
// TEST_CASE: Get state string should return "UNKNOWN" when id is not valid
TEST_CASE("Get state string returns UNKNOWN when id is invalid", "[relay_chn][core]") {
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
for (uint8_t i = 0; i < relay_chn_count; i++) {
int invalid_id = relay_chn_count * 2 + i;
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);
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;
TEST_ASSERT_EQUAL_STRING("UNKNOWN", relay_chn_get_state_str(invalid_id));
}
}
// TEST_CASE: Test independent operation of multiple relay channels
TEST_CASE("Multiple channels can operate independently", "[relay_chn][core]") {
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
if (relay_chn_count >= 2) {
// Start Channel 0 in forward direction
relay_chn_run_forward(0); // relay_chn_run_forward returns void
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state(0));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FREE, relay_chn_get_state(1)); // Other channel should not be affected
// Start Channel 1 in reverse direction
relay_chn_run_reverse(1); // relay_chn_run_reverse returns void
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state(0));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state(1));
// Stop Channel 0 and wait for it to become FREE
relay_chn_stop(0); // relay_chn_stop returns void
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FREE, relay_chn_get_state(0));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state(1)); // Other channel should continue running
// Stop Channel 1 and wait for it to become FREE
relay_chn_stop(1); // relay_chn_stop returns void
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FREE, relay_chn_get_state(0));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FREE, relay_chn_get_state(1));
} else {
ESP_LOGW("TEST", "Skipping 'Multiple channels can operate independently' test: Not enough channels available.");
}
}
// ### Inertia and State Transition Tests
// This section specifically targets the inertia periods and complex state transitions as per the component's logic.
// TEST_CASE: Test transition from forward to reverse with inertia and state checks
// Scenario: RELAY_CHN_STATE_FORWARD -> (relay_chn_run_reverse) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_REVERSE
TEST_CASE("Forward to Reverse transition with opposite inertia", "[relay_chn][core][inertia]") {
uint8_t ch = 0; // Channel to test
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
// 1. Start in forward direction
relay_chn_run_forward(ch); // relay_chn_run_forward returns void
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); // Short delay for state stabilization
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state(ch));
// 2. Issue reverse command
relay_chn_run_reverse(ch); // relay_chn_run_reverse returns void
// Immediately after the command, the motor should be stopped
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE_PENDING, relay_chn_get_state(ch));
// Wait for the inertia period (after which the reverse command will be dispatched)
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state(ch)); // Should now be in reverse state
}
// TEST_CASE: Test transition from reverse to forward with inertia and state checks
// Scenario: RELAY_CHN_STATE_REVERSE -> (relay_chn_run_forward) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_FORWARD
TEST_CASE("Reverse to Forward transition with opposite inertia", "[relay_chn][core][inertia]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
// 1. Start in reverse direction
relay_chn_run_reverse(ch); // relay_chn_run_reverse returns void
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state(ch));
// 2. Issue forward command
relay_chn_run_forward(ch); // relay_chn_run_forward returns void
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD_PENDING, relay_chn_get_state(ch));
// Wait for inertia
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state(ch));
}
// TEST_CASE: Test issuing the same run command while already running (no inertia expected)
// Scenario: RELAY_CHN_STATE_FORWARD -> (relay_chn_run_forward) -> RELAY_CHN_STATE_FORWARD
TEST_CASE("Running in same direction does not incur inertia", "[relay_chn][core][inertia]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
// 1. Start in forward direction
relay_chn_run_forward(ch); // relay_chn_run_forward returns void
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state(ch));
// 2. Issue the same forward command again
relay_chn_run_forward(ch); // relay_chn_run_forward returns void
// As per the code, is_direction_opposite_to_current_motion should return false, so no inertia.
// Just a short delay to check state remains the same.
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state(ch));
}
// TEST_CASE: Test transition from FREE state to running (no inertia expected)
// Scenario: RELAY_CHN_STATE_FREE -> (relay_chn_run_forward) -> RELAY_CHN_STATE_FORWARD
TEST_CASE("FREE to Running transition without inertia", "[relay_chn][core][inertia]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
// setUp() should have already brought the channel to FREE state
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FREE, relay_chn_get_state(ch));
// Start in forward direction
relay_chn_run_forward(ch); // relay_chn_run_forward returns void
// No inertia is expected when starting from FREE state.
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state(ch));
}
// ### Direction Flipping Tests
TEST_CASE("Single channel direction can be flipped", "[relay_chn][core][direction]")
{
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
const uint8_t ch = 0;
// 1. Initial direction should be default
TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, relay_chn_get_direction(ch));
// 2. Flip the direction
relay_chn_flip_direction(ch);
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms)); // Wait for flip inertia
// 3. Verify direction is flipped
TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_FLIPPED, relay_chn_get_direction(ch));
// 4. Flip back
relay_chn_flip_direction(ch);
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms)); // Wait for flip inertia
// 5. Verify direction is back to default
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_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
// 1. Flip all channels
relay_chn_flip_direction(RELAY_CHN_ID_ALL);
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
// 2. Verify all channels are flipped
for (uint8_t i = 0; i < relay_chn_count; i++) {
TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_FLIPPED, relay_chn_get_direction(i));
}
// 3. Flip all back
relay_chn_flip_direction(RELAY_CHN_ID_ALL);
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
// 4. Verify all channels are back to default
for (uint8_t i = 0; i < relay_chn_count; i++) {
TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, relay_chn_get_direction(i));
}
}
TEST_CASE("Flipping a running channel stops it and flips direction", "[relay_chn][core][direction]")
{
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
const uint8_t ch = 0;
// 1. Start channel running and verify state
relay_chn_run_forward(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state(ch));
// 2. Flip the direction while running
relay_chn_flip_direction(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); // Give time for events to process
// 3. The channel should stop as part of the flip process
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_STOPPED, relay_chn_get_state(ch));
// 4. Wait for the flip inertia to pass, after which it should be FREE and FLIPPED
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FREE, relay_chn_get_state(ch));
TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_FLIPPED, relay_chn_get_direction(ch));
}
TEST_CASE("Direction flip handles invalid channel ID gracefully", "[relay_chn][core][direction]")
{
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
const uint8_t invalid_ch = relay_chn_count + 5;
relay_chn_flip_direction(invalid_ch); // Call with an invalid ID
TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, relay_chn_get_direction(invalid_ch));
}

View File

@@ -0,0 +1,135 @@
#include "test_common.h"
// --- Listener Test Globals ---
typedef struct {
uint8_t chn_id;
relay_chn_state_t old_state;
relay_chn_state_t new_state;
int call_count;
} listener_callback_info_t;
static listener_callback_info_t listener1_info;
static listener_callback_info_t listener2_info;
// --- Listener Test Helper Functions ---
// Clear the memory from possible garbage values
static void reset_listener_info(listener_callback_info_t* info) {
memset(info, 0, sizeof(listener_callback_info_t));
}
static void test_listener_1(uint8_t chn_id, relay_chn_state_t old_state, relay_chn_state_t new_state) {
listener1_info.chn_id = chn_id;
listener1_info.old_state = old_state;
listener1_info.new_state = new_state;
listener1_info.call_count++;
}
static void test_listener_2(uint8_t chn_id, relay_chn_state_t old_state, relay_chn_state_t new_state) {
listener2_info.chn_id = chn_id;
listener2_info.old_state = old_state;
listener2_info.new_state = new_state;
listener2_info.call_count++;
}
// ### Listener Functionality Tests
TEST_CASE("Listener is called on state change", "[relay_chn][listener]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
reset_listener_info(&listener1_info);
// 1. Register the listener
TEST_ESP_OK(relay_chn_register_listener(test_listener_1));
// 2. Trigger a state change
relay_chn_run_forward(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); // Allow event to be processed
// 3. Verify the listener was called with correct parameters
TEST_ASSERT_EQUAL(1, listener1_info.call_count);
TEST_ASSERT_EQUAL(ch, listener1_info.chn_id);
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FREE, listener1_info.old_state);
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, listener1_info.new_state);
// 4. Unregister to clean up
relay_chn_unregister_listener(test_listener_1);
}
TEST_CASE("Unregistered listener is not called", "[relay_chn][listener]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
reset_listener_info(&listener1_info);
// 1. Register and then immediately unregister the listener
TEST_ESP_OK(relay_chn_register_listener(test_listener_1));
relay_chn_unregister_listener(test_listener_1);
// 2. Trigger a state change
relay_chn_run_forward(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
// 3. Verify the listener was NOT called
TEST_ASSERT_EQUAL(0, listener1_info.call_count);
}
TEST_CASE("Multiple listeners are called on state change", "[relay_chn][listener]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
reset_listener_info(&listener1_info);
reset_listener_info(&listener2_info);
// 1. Register two different listeners
TEST_ESP_OK(relay_chn_register_listener(test_listener_1));
TEST_ESP_OK(relay_chn_register_listener(test_listener_2));
// 2. Trigger a state change
relay_chn_run_forward(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
// 3. Verify listener 1 was called correctly
TEST_ASSERT_EQUAL(1, listener1_info.call_count);
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FREE, listener1_info.old_state);
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, listener1_info.new_state);
// 4. Verify listener 2 was also called correctly
TEST_ASSERT_EQUAL(1, listener2_info.call_count);
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FREE, listener2_info.old_state);
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, listener2_info.new_state);
// 5. Clean up
relay_chn_unregister_listener(test_listener_1);
relay_chn_unregister_listener(test_listener_2);
}
TEST_CASE("Listener registration handles invalid arguments and duplicates", "[relay_chn][listener]") {
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
reset_listener_info(&listener1_info);
// 1. Registering a NULL listener should fail
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_register_listener(NULL));
// 2. Unregistering a NULL listener should not crash
relay_chn_unregister_listener(NULL);
// 3. Registering the same listener twice should be handled gracefully
TEST_ESP_OK(relay_chn_register_listener(test_listener_1));
TEST_ESP_OK(relay_chn_register_listener(test_listener_1)); // Second call should be a no-op
// 4. Trigger a state change and verify the listener is only called ONCE
relay_chn_run_forward(0);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(1, listener1_info.call_count);
// 5. Clean up
relay_chn_unregister_listener(test_listener_1);
}

View File

@@ -0,0 +1,389 @@
#include "test_common.h"
// ### Tilt Functionality Tests (Conditional)
// This section will only be compiled if **`CONFIG_RELAY_CHN_ENABLE_TILTING`** is defined as **`1`** in `sdkconfig`.
#ifndef CONFIG_RELAY_CHN_ENABLE_TILTING
#error "This test requires CONFIG_RELAY_CHN_ENABLE_TILTING"
#endif
#define RELAY_CHN_CMD_FORWARD 1
#define RELAY_CHN_CMD_REVERSE 2
// Helper function to prepare channel for tilt tests
void prepare_channel_for_tilt(uint8_t chn_id, int initial_cmd) {
// Ensure the channel has had a 'last_run_cmd'
if (initial_cmd == RELAY_CHN_CMD_FORWARD) {
relay_chn_run_forward(chn_id);
} else { // Assuming initial_cmd is RELAY_CHN_CMD_REVERSE
relay_chn_run_reverse(chn_id);
}
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); // Allow command to process
relay_chn_stop(chn_id); // Stop it to set last_run_cmd but return to FREE for next test
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FREE, relay_chn_get_state(chn_id));
}
// TEST_CASE: Test transition from running forward to tilt forward
// Scenario: RELAY_CHN_STATE_FORWARD -> (relay_chn_tilt_forward) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_TILT_FORWARD
TEST_CASE("Run Forward to Tilt Forward transition with inertia", "[relay_chn][tilt][inertia]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
// Prepare channel by running forward first to set last_run_cmd
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD);
// 1. Start in forward direction
relay_chn_run_forward(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state(ch));
// 2. Issue tilt forward command
relay_chn_tilt_forward(ch);
// After tilt command, it should immediately stop and then trigger inertia.
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_STOPPED, relay_chn_get_state(ch));
// Wait for the inertia period (after which the tilt command will be dispatched)
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(ch));
}
// TEST_CASE: Test transition from running reverse to tilt reverse
// Scenario: RELAY_CHN_STATE_REVERSE -> (relay_chn_tilt_reverse) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_TILT_REVERSE
TEST_CASE("Run Reverse to Tilt Reverse transition with inertia", "[relay_chn][tilt][inertia]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
// Prepare channel by running reverse first to set last_run_cmd
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_REVERSE);
// 1. Start in reverse direction
relay_chn_run_reverse(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state(ch));
// 2. Issue tilt reverse command
relay_chn_tilt_reverse(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_STOPPED, relay_chn_get_state(ch));
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state(ch));
}
// TEST_CASE: Test transition from FREE state to tilt forward (now with preparation)
// Scenario: RELAY_CHN_STATE_FREE -> (prepare) -> RELAY_CHN_STATE_FREE -> (relay_chn_tilt_forward) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_TILT_FORWARD
TEST_CASE("FREE to Tilt Forward transition with inertia (prepared)", "[relay_chn][tilt][inertia]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
// Prepare channel by running forward first to set last_run_cmd
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD);
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FREE, relay_chn_get_state(ch)); // Ensure we are back to FREE
// Issue tilt forward command
relay_chn_tilt_forward(ch);
// From FREE state, tilt command should still incur the inertia due to the internal timer logic
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(ch));
}
// TEST_CASE: Test transition from FREE state to tilt reverse (now with preparation)
// Scenario: RELAY_CHN_STATE_FREE -> (prepare) -> RELAY_CHN_STATE_FREE -> (relay_chn_tilt_reverse) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_TILT_REVERSE
TEST_CASE("FREE to Tilt Reverse transition with inertia (prepared)", "[relay_chn][tilt][inertia]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
// Prepare channel by running reverse first to set last_run_cmd
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_REVERSE);
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FREE, relay_chn_get_state(ch)); // Ensure we are back to FREE
// Issue tilt reverse command
relay_chn_tilt_reverse(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state(ch));
}
// TEST_CASE: Test transition from tilt forward to run forward (inertia expected for run)
// Scenario: RELAY_CHN_STATE_TILT_FORWARD -> (relay_chn_run_forward) -> RELAY_CHN_STATE_FORWARD
TEST_CASE("Tilt Forward to Run Forward transition with inertia", "[relay_chn][tilt][inertia]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
// Prepare channel by running forward first to set last_run_cmd, then tilt
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD);
relay_chn_tilt_forward(ch); // Go to tilt state
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(ch));
// 2. Issue run forward command
relay_chn_run_forward(ch);
// From Tilt to Run in the same logical name but in the opposite direction, inertia is expected.
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD_PENDING, relay_chn_get_state(ch));
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state(ch));
}
// TEST_CASE: Test transition from tilt reverse to run reverse (no inertia expected for run)
// Scenario: RELAY_CHN_STATE_TILT_REVERSE -> (relay_chn_run_reverse) -> RELAY_CHN_STATE_REVERSE
TEST_CASE("Tilt Reverse to Run Reverse transition with inertia", "[relay_chn][tilt][inertia]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
// Prepare channel by running reverse first to set last_run_cmd, then tilt
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_REVERSE);
relay_chn_tilt_reverse(ch); // Go to tilt state
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state(ch));
// 2. Issue run reverse command
relay_chn_run_reverse(ch);
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE_PENDING, relay_chn_get_state(ch));
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state(ch));
}
// TEST_CASE: Test transition from tilt forward to run reverse (without inertia)
// Scenario: RELAY_CHN_STATE_TILT_FORWARD -> (relay_chn_run_reverse) -> RELAY_CHN_STATE_REVERSE
TEST_CASE("Tilt Forward to Run Reverse transition without inertia", "[relay_chn][tilt][inertia]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
// Prepare channel by running forward first to set last_run_cmd, then tilt
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD);
relay_chn_tilt_forward(ch); // Go to tilt state
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(ch));
// 2. Issue run reverse command (opposite direction)
relay_chn_run_reverse(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state(ch));
}
// 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_FREE
TEST_CASE("Tilt to Stop transition without immediate inertia for stop", "[relay_chn][tilt][inertia]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
// Prepare channel by running forward first to set last_run_cmd, then tilt
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD);
relay_chn_tilt_forward(ch); // Go to tilt state
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(ch));
// 2. Issue stop command
relay_chn_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_FREE, relay_chn_get_state(ch));
}
// ### Tilt Broadcast Command (RELAY_CHN_ID_ALL) Tests
TEST_CASE("tilt_forward with ID_ALL sets all channels to TILT_FORWARD", "[relay_chn][tilt][id_all]")
{
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
// 1. Prepare all channels.
for (uint8_t i = 0; i < relay_chn_count; i++) {
prepare_channel_for_tilt(i, RELAY_CHN_CMD_FORWARD);
}
// 2. Issue tilt forward to all channels
relay_chn_tilt_forward(RELAY_CHN_ID_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++) {
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_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
// 1. Prepare all channels.
for (uint8_t i = 0; i < relay_chn_count; i++) {
prepare_channel_for_tilt(i, RELAY_CHN_CMD_REVERSE);
}
// 2. Issue tilt reverse to all channels
relay_chn_tilt_reverse(RELAY_CHN_ID_ALL);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
// 3. Verify all channels are tilting reverse
for (uint8_t i = 0; i < relay_chn_count; i++) {
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state(i));
}
}
TEST_CASE("tilt_stop with ID_ALL stops all tilting channels", "[relay_chn][tilt][id_all]")
{
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
// 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);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
// 2. Stop tilting on all channels
relay_chn_tilt_stop(RELAY_CHN_ID_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++) {
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FREE, 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_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
// 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");
// 1. Prepare channel 0 with last run FORWARD and channel 1 with last run REVERSE
prepare_channel_for_tilt(0, RELAY_CHN_CMD_FORWARD);
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);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); // Tilt from FREE state is dispatched immediately
// 3. Verify channel 0 tilts forward (last run was forward) and channel 1 tilts reverse (last run was reverse)
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(0));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state(1));
}
// Test relay_chn_tilt_auto() chooses correct tilt direction
TEST_CASE("relay_chn_tilt_auto chooses correct direction", "[relay_chn][tilt][auto]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
// Prepare FORWARD
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD);
relay_chn_tilt_auto(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(ch));
relay_chn_tilt_stop(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
// Prepare REVERSE
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_REVERSE);
relay_chn_tilt_auto(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state(ch));
}
// Test sensitivity set/get
TEST_CASE("relay_chn_tilt_sensitivity_set and get", "[relay_chn][tilt][sensitivity]") {
uint8_t ch = 0;
uint8_t val = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
relay_chn_tilt_sensitivity_set(ch, 0);
TEST_ESP_OK(relay_chn_tilt_sensitivity_get(ch, &val, 1));
TEST_ASSERT_EQUAL_UINT8(0, val);
relay_chn_tilt_sensitivity_set(ch, 50);
TEST_ESP_OK(relay_chn_tilt_sensitivity_get(ch, &val, 1));
TEST_ASSERT_EQUAL_UINT8(50, val);
relay_chn_tilt_sensitivity_set(ch, 100);
TEST_ESP_OK(relay_chn_tilt_sensitivity_get(ch, &val, 1));
TEST_ASSERT_EQUAL_UINT8(100, val);
// Set all channels
relay_chn_tilt_sensitivity_set(RELAY_CHN_ID_ALL, 42);
uint8_t vals[CONFIG_RELAY_CHN_COUNT] = {0};
TEST_ESP_OK(relay_chn_tilt_sensitivity_get(RELAY_CHN_ID_ALL, vals, relay_chn_count));
for (int i = 0; i < relay_chn_count; ++i) {
TEST_ASSERT_EQUAL_UINT8(42, vals[i]);
}
}
// Test tilt counter logic: forward x3, reverse x3, extra reverse fails
TEST_CASE("tilt counter logic: forward and reverse consumption", "[relay_chn][tilt][counter]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD);
// Tilt forward 3 times
for (int i = 0; i < 3; ++i) {
relay_chn_tilt_forward(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(ch));
relay_chn_tilt_stop(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
}
// Now tilt reverse 3 times (should succeed)
for (int i = 0; i < 3; ++i) {
relay_chn_tilt_reverse(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
if (i < 3) {
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state(ch));
relay_chn_tilt_stop(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
}
}
// Extra reverse tilt should fail (counter exhausted)
relay_chn_tilt_reverse(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
// Should not enter TILT_REVERSE, should remain FREE or STOPPED
relay_chn_state_t state = relay_chn_get_state(ch);
TEST_ASSERT(state != RELAY_CHN_STATE_TILT_REVERSE);
}
// Test run command during TILT state
TEST_CASE("run command during TILT state transitions correctly", "[relay_chn][tilt][run-during-tilt]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD);
relay_chn_tilt_forward(ch);
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(ch));
// Issue run reverse while in TILT_FORWARD
relay_chn_run_reverse(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
// Should transition to REVERSE or REVERSE_PENDING depending on inertia logic
relay_chn_state_t state = relay_chn_get_state(ch);
TEST_ASSERT(state == RELAY_CHN_STATE_REVERSE || state == RELAY_CHN_STATE_REVERSE_PENDING);
}

1440
test_apps/sdkconfig Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,17 @@
# Disable task WDT for tests
CONFIG_ESP_TASK_WDT_INIT=n
# Relay Channel Driver Default Configuration for Testing
# Keep this as short as possible for tests
CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS=200
CONFIG_RELAY_CHN_COUNT=2
CONFIG_RELAY_CHN_ENABLE_TILTING=y
# App Trace config
CONFIG_APPTRACE_DEST_JTAG=y
CONFIG_APPTRACE_ENABLE=y
CONFIG_APPTRACE_LOCK_ENABLE=y
CONFIG_APPTRACE_ONPANIC_HOST_FLUSH_TMO=-1
CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH=0
CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX=0
CONFIG_APPTRACE_GCOV_ENABLE=y

1415
test_apps/sdkconfig.old Normal file

File diff suppressed because it is too large Load Diff