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.
137 lines
3.7 KiB
C
137 lines
3.7 KiB
C
/*
|
|
* SPDX-FileCopyrightText: 2025 Kozmotronik Tech
|
|
*
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
#include "esp_check.h"
|
|
#include "relay_chn_priv_types.h"
|
|
#include "relay_chn_core.h"
|
|
#include "relay_chn_ctl.h"
|
|
#include "relay_chn_output.h"
|
|
|
|
static const char *TAG = "RELAY_CHN_CTL";
|
|
|
|
static relay_chn_ctl_t chn_ctls[RELAY_CHN_COUNT];
|
|
|
|
|
|
esp_err_t relay_chn_ctl_init(relay_chn_output_t *outputs, relay_chn_run_info_t *run_infos)
|
|
{
|
|
// Initialize all relay channels
|
|
esp_err_t ret;
|
|
for (int i = 0; i < RELAY_CHN_COUNT; i++) {
|
|
relay_chn_ctl_t* chn_ctl = &chn_ctls[i];
|
|
relay_chn_output_t* output = &outputs[i];
|
|
relay_chn_run_info_t* run_info = &run_infos[i];
|
|
|
|
chn_ctl->id = i;
|
|
chn_ctl->state = RELAY_CHN_STATE_IDLE;
|
|
chn_ctl->pending_cmd = RELAY_CHN_CMD_NONE;
|
|
|
|
chn_ctl->output = output;
|
|
chn_ctl->run_info = run_info;
|
|
ret = relay_chn_init_timer(chn_ctl); // Create direction change inertia timer
|
|
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to create relay channel timer for channel %d", i);
|
|
}
|
|
return ESP_OK;
|
|
}
|
|
|
|
void relay_chn_ctl_deinit()
|
|
{
|
|
for (int i = 0; i < RELAY_CHN_COUNT; i++) {
|
|
relay_chn_ctl_t* chn_ctl = &chn_ctls[i];
|
|
if (chn_ctl->inertia_timer != NULL) {
|
|
esp_timer_delete(chn_ctl->inertia_timer);
|
|
chn_ctl->inertia_timer = NULL;
|
|
}
|
|
}
|
|
}
|
|
|
|
relay_chn_state_t relay_chn_ctl_get_state(uint8_t chn_id)
|
|
{
|
|
if (!relay_chn_is_channel_id_valid(chn_id) || chn_id == RELAY_CHN_ID_ALL) {
|
|
return RELAY_CHN_STATE_UNDEFINED;
|
|
}
|
|
return chn_ctls[chn_id].state;
|
|
}
|
|
|
|
char *relay_chn_ctl_get_state_str(uint8_t chn_id)
|
|
{
|
|
if (!relay_chn_is_channel_id_valid(chn_id) || chn_id == RELAY_CHN_ID_ALL) {
|
|
return relay_chn_state_str(RELAY_CHN_STATE_UNDEFINED);
|
|
}
|
|
return relay_chn_state_str(chn_ctls[chn_id].state);
|
|
}
|
|
|
|
static void relay_chn_ctl_issue_cmd_on_all_channels(relay_chn_cmd_t cmd)
|
|
{
|
|
for (int i = 0; i < RELAY_CHN_COUNT; i++) {
|
|
relay_chn_issue_cmd(&chn_ctls[i], cmd);
|
|
}
|
|
}
|
|
|
|
void relay_chn_ctl_run_forward(uint8_t chn_id)
|
|
{
|
|
if (!relay_chn_is_channel_id_valid(chn_id)) return;
|
|
|
|
if (chn_id == RELAY_CHN_ID_ALL) {
|
|
relay_chn_ctl_issue_cmd_on_all_channels(RELAY_CHN_CMD_FORWARD);
|
|
return;
|
|
}
|
|
relay_chn_issue_cmd(&chn_ctls[chn_id], RELAY_CHN_CMD_FORWARD);
|
|
}
|
|
|
|
void relay_chn_ctl_run_reverse(uint8_t chn_id)
|
|
{
|
|
if (!relay_chn_is_channel_id_valid(chn_id)) return;
|
|
|
|
if (chn_id == RELAY_CHN_ID_ALL) {
|
|
relay_chn_ctl_issue_cmd_on_all_channels(RELAY_CHN_CMD_REVERSE);
|
|
return;
|
|
}
|
|
relay_chn_issue_cmd(&chn_ctls[chn_id], RELAY_CHN_CMD_REVERSE);
|
|
}
|
|
|
|
void relay_chn_ctl_stop(uint8_t chn_id)
|
|
{
|
|
if (!relay_chn_is_channel_id_valid(chn_id)) return;
|
|
|
|
if (chn_id == RELAY_CHN_ID_ALL) {
|
|
relay_chn_ctl_issue_cmd_on_all_channels(RELAY_CHN_CMD_STOP);
|
|
return;
|
|
}
|
|
relay_chn_issue_cmd(&chn_ctls[chn_id], RELAY_CHN_CMD_STOP);
|
|
}
|
|
|
|
void relay_chn_ctl_flip_direction(uint8_t chn_id)
|
|
{
|
|
if (!relay_chn_is_channel_id_valid(chn_id)) return;
|
|
|
|
if (chn_id == RELAY_CHN_ID_ALL) {
|
|
relay_chn_ctl_issue_cmd_on_all_channels(RELAY_CHN_CMD_FLIP);
|
|
return;
|
|
}
|
|
relay_chn_issue_cmd(&chn_ctls[chn_id], RELAY_CHN_CMD_FLIP);
|
|
}
|
|
|
|
relay_chn_direction_t relay_chn_ctl_get_direction(uint8_t chn_id)
|
|
{
|
|
if (!relay_chn_is_channel_id_valid(chn_id)) {
|
|
return RELAY_CHN_DIRECTION_DEFAULT;
|
|
}
|
|
relay_chn_ctl_t *chn_ctl = &chn_ctls[chn_id];
|
|
return relay_chn_output_get_direction(chn_ctl->output);
|
|
}
|
|
|
|
relay_chn_ctl_t *relay_chn_ctl_get(uint8_t chn_id)
|
|
{
|
|
if (!relay_chn_is_channel_id_valid(chn_id)) {
|
|
return NULL;
|
|
}
|
|
return &chn_ctls[chn_id];
|
|
}
|
|
|
|
relay_chn_ctl_t *relay_chn_ctl_get_all(void)
|
|
{
|
|
return chn_ctls;
|
|
} |