Files
relay_chn/private_include/relay_chn_core.h
ismail 61f8ed440e Add single channel mode feature.
The addition of a single-channel mode implied further modularisation of the component. This commit has broken the component down into the following modules to avoid a huge single source file and to make unit testing easier.

The modules:

- Separation of public and private code
- *types and *defs
- public relay_chn API
- *adapter
- *output
- *run_info
- *core
- *ctl (control)
- *tilt

Closes #957.
2025-08-13 18:31:05 +03:00

114 lines
3.4 KiB
C

/*
* SPDX-FileCopyrightText: 2025 Kozmotronik Tech
*
* SPDX-License-Identifier: MIT
*/
#pragma once
#include "esp_err.h"
#include "esp_log.h"
#include "esp_event_base.h"
#include "esp_event.h"
#include "esp_timer.h"
#include "relay_chn_defs.h"
#include "relay_chn_types.h"
#include "relay_chn_priv_types.h"
#ifdef __cplusplus
extern "C" {
#endif
/// Event base used by *_core, *_ctl, and *_tilt modules.
ESP_EVENT_DECLARE_BASE(RELAY_CHN_CMD_EVENT);
/**
* @brief Initializes the relay channel timer.
*
* This function creates a timer for the relay channel to handle direction change inertia.
* Required by *_ctl_* module.
*
* @param chn_ctl Pointer to the relay channel control structure.
* @return esp_err_t ESP_OK on success, or an error code on failure.
*/
esp_err_t relay_chn_init_timer(relay_chn_ctl_t *chn_ctl);
/**
* @brief Issues a command to the relay channel.
*
* Evaluates the current state of the relay channel and issues the command accordingly.
* Required by *_core, *_ctl_* and *_tilt modules.
*
* @param chn_ctl Pointer to the relay channel control structure.
* @param cmd The command to issue.
*/
void relay_chn_issue_cmd(relay_chn_ctl_t* chn_ctl, relay_chn_cmd_t cmd);
/**
* @brief Dispatches a relay channel command to the event loop.
*
* @param chn_ctl Pointer to the relay channel control structure.
* @param cmd The command to dispatch.
*/
void relay_chn_dispatch_cmd(relay_chn_ctl_t *chn_ctl, 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.
*/
char *relay_chn_cmd_str(relay_chn_cmd_t cmd);
/**
* @brief Starts the ESP timer once with the specified time in milliseconds.
*
* Starts the ESP timer to run once after the specified time.
* If the timer is already running, it stops it first and then starts it again.
* Required by *_ctl_* and *_tilt modules.
*
* @param esp_timer The ESP timer handle.
* @param time_ms The time in milliseconds to wait before the timer expires.
* @return esp_err_t ESP_OK on success, or an error code on failure.
*/
esp_err_t relay_chn_start_esp_timer_once(esp_timer_handle_t esp_timer, uint32_t time_ms);
/**
* @brief Updates the state of the relay channel and notifies listeners.
*
* This function updates the state of the relay channel and notifies all registered listeners
* about the state change.
* Required by *_ctl_* and *_tilt modules.
*
* @param chn_ctl Pointer to the relay channel control structure.
* @param new_state The new state to set for the relay channel.
*/
void relay_chn_update_state(relay_chn_ctl_t *chn_ctl, relay_chn_state_t new_state);
/**
* @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);
#if RELAY_CHN_COUNT > 1
/**
* @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.
*/
bool relay_chn_is_channel_id_valid(uint8_t chn_id);
#endif // RELAY_CHN_COUNT > 1
#if RELAY_CHN_ENABLE_TILTING == 1
/// Relay channel event loop handle declaration for *_tilt module.
extern esp_event_loop_handle_t relay_chn_event_loop;
#endif // RELAY_CHN_ENABLE_TILTING
#ifdef __cplusplus
}
#endif