opt/1085-optimization-and-cleanup #37
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
static const char *TAG = "RELAY_CHN_CTL";
|
static const char *TAG = "RELAY_CHN_CTL";
|
||||||
|
|
||||||
static relay_chn_ctl_t chn_ctls[CONFIG_RELAY_CHN_COUNT];
|
static relay_chn_ctl_t s_chn_ctls[CONFIG_RELAY_CHN_COUNT];
|
||||||
|
|
||||||
|
|
||||||
esp_err_t relay_chn_ctl_init(relay_chn_output_t *outputs, relay_chn_run_info_t *run_infos)
|
esp_err_t relay_chn_ctl_init(relay_chn_output_t *outputs, relay_chn_run_info_t *run_infos)
|
||||||
@@ -24,7 +24,7 @@ esp_err_t relay_chn_ctl_init(relay_chn_output_t *outputs, relay_chn_run_info_t *
|
|||||||
// Initialize all relay channels
|
// Initialize all relay channels
|
||||||
esp_err_t ret;
|
esp_err_t ret;
|
||||||
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
||||||
relay_chn_ctl_t* chn_ctl = &chn_ctls[i];
|
relay_chn_ctl_t* chn_ctl = &s_chn_ctls[i];
|
||||||
relay_chn_output_t* output = &outputs[i];
|
relay_chn_output_t* output = &outputs[i];
|
||||||
relay_chn_run_info_t* run_info = &run_infos[i];
|
relay_chn_run_info_t* run_info = &run_infos[i];
|
||||||
|
|
||||||
@@ -54,7 +54,7 @@ esp_err_t relay_chn_ctl_init(relay_chn_output_t *outputs, relay_chn_run_info_t *
|
|||||||
void relay_chn_ctl_deinit()
|
void relay_chn_ctl_deinit()
|
||||||
{
|
{
|
||||||
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
||||||
relay_chn_ctl_t* chn_ctl = &chn_ctls[i];
|
relay_chn_ctl_t* chn_ctl = &s_chn_ctls[i];
|
||||||
if (chn_ctl->inertia_timer != NULL) {
|
if (chn_ctl->inertia_timer != NULL) {
|
||||||
esp_timer_delete(chn_ctl->inertia_timer);
|
esp_timer_delete(chn_ctl->inertia_timer);
|
||||||
chn_ctl->inertia_timer = NULL;
|
chn_ctl->inertia_timer = NULL;
|
||||||
@@ -71,7 +71,7 @@ void relay_chn_ctl_deinit()
|
|||||||
relay_chn_state_t relay_chn_ctl_get_state(uint8_t chn_id)
|
relay_chn_state_t relay_chn_ctl_get_state(uint8_t chn_id)
|
||||||
{
|
{
|
||||||
return relay_chn_is_channel_id_valid(chn_id) ?
|
return relay_chn_is_channel_id_valid(chn_id) ?
|
||||||
chn_ctls[chn_id].state : RELAY_CHN_STATE_UNDEFINED;
|
s_chn_ctls[chn_id].state : RELAY_CHN_STATE_UNDEFINED;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t relay_chn_ctl_get_state_all(relay_chn_state_t *states)
|
esp_err_t relay_chn_ctl_get_state_all(relay_chn_state_t *states)
|
||||||
@@ -84,7 +84,7 @@ esp_err_t relay_chn_ctl_get_state_all(relay_chn_state_t *states)
|
|||||||
ESP_LOGW(TAG, "get_state_all: States have been copied until channel %d since states[%d] is NULL", i, i);
|
ESP_LOGW(TAG, "get_state_all: States have been copied until channel %d since states[%d] is NULL", i, i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
*dest_state = chn_ctls[i].state;
|
*dest_state = s_chn_ctls[i].state;
|
||||||
}
|
}
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
@@ -92,7 +92,7 @@ esp_err_t relay_chn_ctl_get_state_all(relay_chn_state_t *states)
|
|||||||
char *relay_chn_ctl_get_state_str(uint8_t chn_id)
|
char *relay_chn_ctl_get_state_str(uint8_t chn_id)
|
||||||
{
|
{
|
||||||
return relay_chn_is_channel_id_valid(chn_id)
|
return relay_chn_is_channel_id_valid(chn_id)
|
||||||
? relay_chn_state_str(chn_ctls[chn_id].state)
|
? relay_chn_state_str(s_chn_ctls[chn_id].state)
|
||||||
: relay_chn_state_str(RELAY_CHN_STATE_UNDEFINED);
|
: relay_chn_state_str(RELAY_CHN_STATE_UNDEFINED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,14 +100,14 @@ char *relay_chn_ctl_get_state_str(uint8_t chn_id)
|
|||||||
static void relay_chn_ctl_issue_cmd_on_all_channels(relay_chn_cmd_t cmd)
|
static void relay_chn_ctl_issue_cmd_on_all_channels(relay_chn_cmd_t cmd)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
||||||
relay_chn_issue_cmd(&chn_ctls[i], cmd);
|
relay_chn_issue_cmd(&s_chn_ctls[i], cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void relay_chn_ctl_run_forward(uint8_t chn_id)
|
void relay_chn_ctl_run_forward(uint8_t chn_id)
|
||||||
{
|
{
|
||||||
if (relay_chn_is_channel_id_valid(chn_id))
|
if (relay_chn_is_channel_id_valid(chn_id))
|
||||||
relay_chn_issue_cmd(&chn_ctls[chn_id], RELAY_CHN_CMD_FORWARD);
|
relay_chn_issue_cmd(&s_chn_ctls[chn_id], RELAY_CHN_CMD_FORWARD);
|
||||||
}
|
}
|
||||||
|
|
||||||
void relay_chn_ctl_run_forward_all()
|
void relay_chn_ctl_run_forward_all()
|
||||||
@@ -118,7 +118,7 @@ void relay_chn_ctl_run_forward_all()
|
|||||||
void relay_chn_ctl_run_reverse(uint8_t chn_id)
|
void relay_chn_ctl_run_reverse(uint8_t chn_id)
|
||||||
{
|
{
|
||||||
if (relay_chn_is_channel_id_valid(chn_id))
|
if (relay_chn_is_channel_id_valid(chn_id))
|
||||||
relay_chn_issue_cmd(&chn_ctls[chn_id], RELAY_CHN_CMD_REVERSE);
|
relay_chn_issue_cmd(&s_chn_ctls[chn_id], RELAY_CHN_CMD_REVERSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void relay_chn_ctl_run_reverse_all()
|
void relay_chn_ctl_run_reverse_all()
|
||||||
@@ -129,7 +129,7 @@ void relay_chn_ctl_run_reverse_all()
|
|||||||
void relay_chn_ctl_stop(uint8_t chn_id)
|
void relay_chn_ctl_stop(uint8_t chn_id)
|
||||||
{
|
{
|
||||||
if (relay_chn_is_channel_id_valid(chn_id))
|
if (relay_chn_is_channel_id_valid(chn_id))
|
||||||
relay_chn_issue_cmd(&chn_ctls[chn_id], RELAY_CHN_CMD_STOP);
|
relay_chn_issue_cmd(&s_chn_ctls[chn_id], RELAY_CHN_CMD_STOP);
|
||||||
}
|
}
|
||||||
|
|
||||||
void relay_chn_ctl_stop_all()
|
void relay_chn_ctl_stop_all()
|
||||||
@@ -140,7 +140,7 @@ void relay_chn_ctl_stop_all()
|
|||||||
void relay_chn_ctl_flip_direction(uint8_t chn_id)
|
void relay_chn_ctl_flip_direction(uint8_t chn_id)
|
||||||
{
|
{
|
||||||
if (relay_chn_is_channel_id_valid(chn_id))
|
if (relay_chn_is_channel_id_valid(chn_id))
|
||||||
relay_chn_issue_cmd(&chn_ctls[chn_id], RELAY_CHN_CMD_FLIP);
|
relay_chn_issue_cmd(&s_chn_ctls[chn_id], RELAY_CHN_CMD_FLIP);
|
||||||
}
|
}
|
||||||
|
|
||||||
void relay_chn_ctl_flip_direction_all()
|
void relay_chn_ctl_flip_direction_all()
|
||||||
@@ -151,7 +151,7 @@ void relay_chn_ctl_flip_direction_all()
|
|||||||
relay_chn_direction_t relay_chn_ctl_get_direction(uint8_t chn_id)
|
relay_chn_direction_t relay_chn_ctl_get_direction(uint8_t chn_id)
|
||||||
{
|
{
|
||||||
return relay_chn_is_channel_id_valid(chn_id)
|
return relay_chn_is_channel_id_valid(chn_id)
|
||||||
? relay_chn_output_get_direction(chn_ctls[chn_id].output)
|
? relay_chn_output_get_direction(s_chn_ctls[chn_id].output)
|
||||||
: RELAY_CHN_DIRECTION_DEFAULT;
|
: RELAY_CHN_DIRECTION_DEFAULT;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,7 +165,7 @@ esp_err_t relay_chn_ctl_get_direction_all(relay_chn_direction_t *directions)
|
|||||||
ESP_LOGW(TAG, "get_direction_all: Directions have been copied until channel %d since directions[%d] is NULL", i, i);
|
ESP_LOGW(TAG, "get_direction_all: Directions have been copied until channel %d since directions[%d] is NULL", i, i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
*dest_direction = relay_chn_output_get_direction(chn_ctls[i].output);
|
*dest_direction = relay_chn_output_get_direction(s_chn_ctls[i].output);
|
||||||
}
|
}
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
@@ -173,7 +173,7 @@ esp_err_t relay_chn_ctl_get_direction_all(relay_chn_direction_t *directions)
|
|||||||
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
|
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
|
||||||
uint16_t relay_chn_ctl_get_run_limit(uint8_t chn_id)
|
uint16_t relay_chn_ctl_get_run_limit(uint8_t chn_id)
|
||||||
{
|
{
|
||||||
return relay_chn_is_channel_id_valid(chn_id) ? chn_ctls[chn_id].run_limit_sec : 0;
|
return relay_chn_is_channel_id_valid(chn_id) ? s_chn_ctls[chn_id].run_limit_sec : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t relay_chn_ctl_get_run_limit_all(uint16_t *limits_sec)
|
esp_err_t relay_chn_ctl_get_run_limit_all(uint16_t *limits_sec)
|
||||||
@@ -186,7 +186,7 @@ esp_err_t relay_chn_ctl_get_run_limit_all(uint16_t *limits_sec)
|
|||||||
ESP_LOGW(TAG, "get_run_limit_all: Run limits have been copied until channel %d since limits_sec[%d] is NULL", i, i);
|
ESP_LOGW(TAG, "get_run_limit_all: Run limits have been copied until channel %d since limits_sec[%d] is NULL", i, i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
*dest_limit_sec = chn_ctls[i].run_limit_sec;
|
*dest_limit_sec = s_chn_ctls[i].run_limit_sec;
|
||||||
}
|
}
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
@@ -199,7 +199,7 @@ static void relay_chn_ctl_set_run_limit_common(uint8_t chn_id, uint16_t limit_se
|
|||||||
else if (limit_sec < CONFIG_RELAY_CHN_RUN_LIMIT_MIN_SEC)
|
else if (limit_sec < CONFIG_RELAY_CHN_RUN_LIMIT_MIN_SEC)
|
||||||
limit_sec = CONFIG_RELAY_CHN_RUN_LIMIT_MIN_SEC;
|
limit_sec = CONFIG_RELAY_CHN_RUN_LIMIT_MIN_SEC;
|
||||||
|
|
||||||
chn_ctls[chn_id].run_limit_sec = limit_sec;
|
s_chn_ctls[chn_id].run_limit_sec = limit_sec;
|
||||||
|
|
||||||
#if CONFIG_RELAY_CHN_ENABLE_NVS
|
#if CONFIG_RELAY_CHN_ENABLE_NVS
|
||||||
relay_chn_nvs_set_run_limit(chn_id, limit_sec);
|
relay_chn_nvs_set_run_limit(chn_id, limit_sec);
|
||||||
@@ -241,10 +241,10 @@ esp_err_t relay_chn_ctl_set_run_limit_all_with(uint16_t limit_sec)
|
|||||||
|
|
||||||
relay_chn_ctl_t *relay_chn_ctl_get(uint8_t chn_id)
|
relay_chn_ctl_t *relay_chn_ctl_get(uint8_t chn_id)
|
||||||
{
|
{
|
||||||
return relay_chn_is_channel_id_valid(chn_id) ? &chn_ctls[chn_id] : NULL;
|
return relay_chn_is_channel_id_valid(chn_id) ? &s_chn_ctls[chn_id] : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
relay_chn_ctl_t *relay_chn_ctl_get_all(void)
|
relay_chn_ctl_t *relay_chn_ctl_get_all(void)
|
||||||
{
|
{
|
||||||
return chn_ctls;
|
return s_chn_ctls;
|
||||||
}
|
}
|
||||||
@@ -16,43 +16,43 @@
|
|||||||
|
|
||||||
static const char *TAG = "RELAY_CHN_CTL";
|
static const char *TAG = "RELAY_CHN_CTL";
|
||||||
|
|
||||||
static relay_chn_ctl_t chn_ctl;
|
static relay_chn_ctl_t s_chn_ctl;
|
||||||
|
|
||||||
|
|
||||||
esp_err_t relay_chn_ctl_init(relay_chn_output_t *output, relay_chn_run_info_t *run_info)
|
esp_err_t relay_chn_ctl_init(relay_chn_output_t *output, relay_chn_run_info_t *run_info)
|
||||||
{
|
{
|
||||||
// Initialize the relay channel
|
// Initialize the relay channel
|
||||||
chn_ctl.id = 0; // Single channel, so ID is 0
|
s_chn_ctl.id = 0; // Single channel, so ID is 0
|
||||||
chn_ctl.state = RELAY_CHN_STATE_IDLE;
|
s_chn_ctl.state = RELAY_CHN_STATE_IDLE;
|
||||||
chn_ctl.pending_cmd = RELAY_CHN_CMD_NONE;
|
s_chn_ctl.pending_cmd = RELAY_CHN_CMD_NONE;
|
||||||
chn_ctl.output = output;
|
s_chn_ctl.output = output;
|
||||||
chn_ctl.run_info = run_info;
|
s_chn_ctl.run_info = run_info;
|
||||||
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
|
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
|
||||||
uint16_t run_limit_sec = CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC;
|
uint16_t run_limit_sec = CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC;
|
||||||
esp_err_t ret;
|
esp_err_t ret;
|
||||||
#if CONFIG_RELAY_CHN_ENABLE_NVS
|
#if CONFIG_RELAY_CHN_ENABLE_NVS
|
||||||
// Load run limit value from NVS
|
// Load run limit value from NVS
|
||||||
ret = relay_chn_nvs_get_run_limit(chn_ctl.id, &run_limit_sec, CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC);
|
ret = relay_chn_nvs_get_run_limit(s_chn_ctl.id, &run_limit_sec, CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC);
|
||||||
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load run limit from NVS with error: %s", esp_err_to_name(ret));
|
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load run limit from NVS with error: %s", esp_err_to_name(ret));
|
||||||
#endif
|
#endif
|
||||||
chn_ctl.run_limit_sec = run_limit_sec;
|
s_chn_ctl.run_limit_sec = run_limit_sec;
|
||||||
ret = relay_chn_init_run_limit_timer(&chn_ctl);
|
ret = relay_chn_init_run_limit_timer(&s_chn_ctl);
|
||||||
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to initialize run limit timer");
|
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to initialize run limit timer");
|
||||||
#endif
|
#endif
|
||||||
return relay_chn_init_timer(&chn_ctl); // Create direction change inertia timer
|
return relay_chn_init_timer(&s_chn_ctl); // Create direction change inertia timer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void relay_chn_ctl_deinit()
|
void relay_chn_ctl_deinit()
|
||||||
{
|
{
|
||||||
if (chn_ctl.inertia_timer != NULL) {
|
if (s_chn_ctl.inertia_timer != NULL) {
|
||||||
esp_timer_delete(chn_ctl.inertia_timer);
|
esp_timer_delete(s_chn_ctl.inertia_timer);
|
||||||
chn_ctl.inertia_timer = NULL;
|
s_chn_ctl.inertia_timer = NULL;
|
||||||
}
|
}
|
||||||
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
|
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
|
||||||
if (chn_ctl.run_limit_timer != NULL) {
|
if (s_chn_ctl.run_limit_timer != NULL) {
|
||||||
esp_timer_delete(chn_ctl.run_limit_timer);
|
esp_timer_delete(s_chn_ctl.run_limit_timer);
|
||||||
chn_ctl.run_limit_timer = NULL;
|
s_chn_ctl.run_limit_timer = NULL;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -60,43 +60,43 @@ void relay_chn_ctl_deinit()
|
|||||||
/* relay_chn APIs */
|
/* relay_chn APIs */
|
||||||
relay_chn_state_t relay_chn_ctl_get_state()
|
relay_chn_state_t relay_chn_ctl_get_state()
|
||||||
{
|
{
|
||||||
return chn_ctl.state;
|
return s_chn_ctl.state;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *relay_chn_ctl_get_state_str()
|
char *relay_chn_ctl_get_state_str()
|
||||||
{
|
{
|
||||||
return relay_chn_state_str(chn_ctl.state);
|
return relay_chn_state_str(s_chn_ctl.state);
|
||||||
}
|
}
|
||||||
|
|
||||||
void relay_chn_ctl_run_forward()
|
void relay_chn_ctl_run_forward()
|
||||||
{
|
{
|
||||||
relay_chn_issue_cmd(&chn_ctl, RELAY_CHN_CMD_FORWARD);
|
relay_chn_issue_cmd(&s_chn_ctl, RELAY_CHN_CMD_FORWARD);
|
||||||
}
|
}
|
||||||
|
|
||||||
void relay_chn_ctl_run_reverse()
|
void relay_chn_ctl_run_reverse()
|
||||||
{
|
{
|
||||||
relay_chn_issue_cmd(&chn_ctl, RELAY_CHN_CMD_REVERSE);
|
relay_chn_issue_cmd(&s_chn_ctl, RELAY_CHN_CMD_REVERSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void relay_chn_ctl_stop()
|
void relay_chn_ctl_stop()
|
||||||
{
|
{
|
||||||
relay_chn_issue_cmd(&chn_ctl, RELAY_CHN_CMD_STOP);
|
relay_chn_issue_cmd(&s_chn_ctl, RELAY_CHN_CMD_STOP);
|
||||||
}
|
}
|
||||||
|
|
||||||
void relay_chn_ctl_flip_direction()
|
void relay_chn_ctl_flip_direction()
|
||||||
{
|
{
|
||||||
relay_chn_issue_cmd(&chn_ctl, RELAY_CHN_CMD_FLIP);
|
relay_chn_issue_cmd(&s_chn_ctl, RELAY_CHN_CMD_FLIP);
|
||||||
}
|
}
|
||||||
|
|
||||||
relay_chn_direction_t relay_chn_ctl_get_direction()
|
relay_chn_direction_t relay_chn_ctl_get_direction()
|
||||||
{
|
{
|
||||||
return relay_chn_output_get_direction(chn_ctl.output);
|
return relay_chn_output_get_direction(s_chn_ctl.output);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
|
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
|
||||||
uint16_t relay_chn_ctl_get_run_limit()
|
uint16_t relay_chn_ctl_get_run_limit()
|
||||||
{
|
{
|
||||||
return chn_ctl.run_limit_sec;
|
return s_chn_ctl.run_limit_sec;
|
||||||
}
|
}
|
||||||
|
|
||||||
void relay_chn_ctl_set_run_limit(uint16_t limit_sec)
|
void relay_chn_ctl_set_run_limit(uint16_t limit_sec)
|
||||||
@@ -107,10 +107,10 @@ void relay_chn_ctl_set_run_limit(uint16_t limit_sec)
|
|||||||
else if (limit_sec < CONFIG_RELAY_CHN_RUN_LIMIT_MIN_SEC)
|
else if (limit_sec < CONFIG_RELAY_CHN_RUN_LIMIT_MIN_SEC)
|
||||||
limit_sec = CONFIG_RELAY_CHN_RUN_LIMIT_MIN_SEC;
|
limit_sec = CONFIG_RELAY_CHN_RUN_LIMIT_MIN_SEC;
|
||||||
|
|
||||||
chn_ctl.run_limit_sec = limit_sec;
|
s_chn_ctl.run_limit_sec = limit_sec;
|
||||||
|
|
||||||
#if CONFIG_RELAY_CHN_ENABLE_NVS
|
#if CONFIG_RELAY_CHN_ENABLE_NVS
|
||||||
relay_chn_nvs_set_run_limit(chn_ctl.id, limit_sec);
|
relay_chn_nvs_set_run_limit(s_chn_ctl.id, limit_sec);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -118,5 +118,5 @@ void relay_chn_ctl_set_run_limit(uint16_t limit_sec)
|
|||||||
|
|
||||||
relay_chn_ctl_t *relay_chn_ctl_get()
|
relay_chn_ctl_t *relay_chn_ctl_get()
|
||||||
{
|
{
|
||||||
return &chn_ctl;
|
return &s_chn_ctl;
|
||||||
}
|
}
|
||||||
@@ -48,22 +48,22 @@ typedef struct {
|
|||||||
} relay_chn_notify_msg_t;
|
} relay_chn_notify_msg_t;
|
||||||
|
|
||||||
// The list that holds references to the registered listeners.
|
// The list that holds references to the registered listeners.
|
||||||
static List_t listeners;
|
static List_t s_listeners;
|
||||||
|
|
||||||
static QueueHandle_t notify_msg_queue = NULL;
|
static QueueHandle_t s_notify_queue = NULL;
|
||||||
static TaskHandle_t notify_task_handle = NULL;
|
static TaskHandle_t s_notify_task = NULL;
|
||||||
|
|
||||||
static void relay_chn_notify_task(void *arg);
|
static void relay_chn_notify_task(void *arg);
|
||||||
|
|
||||||
|
|
||||||
esp_err_t relay_chn_notify_init(void)
|
esp_err_t relay_chn_notify_init(void)
|
||||||
{
|
{
|
||||||
if (notify_msg_queue != NULL) {
|
if (s_notify_queue != NULL) {
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
notify_msg_queue = xQueueCreate(RELAY_CHN_NOTIFY_QUEUE_LEN, sizeof(relay_chn_notify_msg_t));
|
s_notify_queue = xQueueCreate(RELAY_CHN_NOTIFY_QUEUE_LEN, sizeof(relay_chn_notify_msg_t));
|
||||||
if (!notify_msg_queue) {
|
if (!s_notify_queue) {
|
||||||
ESP_LOGE(TAG, "Failed to create notify queue");
|
ESP_LOGE(TAG, "Failed to create notify queue");
|
||||||
return ESP_ERR_NO_MEM;
|
return ESP_ERR_NO_MEM;
|
||||||
}
|
}
|
||||||
@@ -71,34 +71,34 @@ esp_err_t relay_chn_notify_init(void)
|
|||||||
// Create the notify dispatcher task
|
// Create the notify dispatcher task
|
||||||
BaseType_t ret = xTaskCreate(relay_chn_notify_task, "task_rlch_ntfy",
|
BaseType_t ret = xTaskCreate(relay_chn_notify_task, "task_rlch_ntfy",
|
||||||
RELAY_CHN_NOTIFY_TASK_STACK, NULL,
|
RELAY_CHN_NOTIFY_TASK_STACK, NULL,
|
||||||
RELAY_CHN_NOTIFY_TASK_PRIO, ¬ify_task_handle);
|
RELAY_CHN_NOTIFY_TASK_PRIO, &s_notify_task);
|
||||||
if (ret != pdPASS) {
|
if (ret != pdPASS) {
|
||||||
ESP_LOGE(TAG, "Failed to create notify task");
|
ESP_LOGE(TAG, "Failed to create notify task");
|
||||||
return ESP_ERR_NO_MEM;
|
return ESP_ERR_NO_MEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init the state listener list
|
// Init the state listener list
|
||||||
vListInitialise(&listeners);
|
vListInitialise(&s_listeners);
|
||||||
|
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
void relay_chn_notify_deinit(void)
|
void relay_chn_notify_deinit(void)
|
||||||
{
|
{
|
||||||
if (notify_task_handle != NULL) {
|
if (s_notify_task != NULL) {
|
||||||
vTaskDelete(notify_task_handle);
|
vTaskDelete(s_notify_task);
|
||||||
notify_task_handle = NULL;
|
s_notify_task = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (notify_msg_queue != NULL) {
|
if (s_notify_queue != NULL) {
|
||||||
vQueueDelete(notify_msg_queue);
|
vQueueDelete(s_notify_queue);
|
||||||
notify_msg_queue = NULL;
|
s_notify_queue = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!listLIST_IS_EMPTY(&listeners)) {
|
if (!listLIST_IS_EMPTY(&s_listeners)) {
|
||||||
// Free the listeners
|
// Free the listeners
|
||||||
while (listCURRENT_LIST_LENGTH(&listeners) > 0) {
|
while (listCURRENT_LIST_LENGTH(&s_listeners) > 0) {
|
||||||
ListItem_t *pxItem = listGET_HEAD_ENTRY(&listeners);
|
ListItem_t *pxItem = listGET_HEAD_ENTRY(&s_listeners);
|
||||||
relay_chn_listener_entry_t *entry = listGET_LIST_ITEM_OWNER(pxItem);
|
relay_chn_listener_entry_t *entry = listGET_LIST_ITEM_OWNER(pxItem);
|
||||||
uxListRemove(pxItem);
|
uxListRemove(pxItem);
|
||||||
free(entry);
|
free(entry);
|
||||||
@@ -118,14 +118,14 @@ void relay_chn_notify_deinit(void)
|
|||||||
*/
|
*/
|
||||||
static relay_chn_listener_entry_t* find_listener_entry(relay_chn_state_listener_t listener)
|
static relay_chn_listener_entry_t* find_listener_entry(relay_chn_state_listener_t listener)
|
||||||
{
|
{
|
||||||
if (listLIST_IS_EMPTY(&listeners)) {
|
if (listLIST_IS_EMPTY(&s_listeners)) {
|
||||||
ESP_LOGD(TAG, "No listeners registered");
|
ESP_LOGD(TAG, "No listeners registered");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Iterate through the linked list of listeners
|
// Iterate through the linked list of listeners
|
||||||
for (ListItem_t *pxListItem = listGET_HEAD_ENTRY(&listeners);
|
for (ListItem_t *pxListItem = listGET_HEAD_ENTRY(&s_listeners);
|
||||||
pxListItem != listGET_END_MARKER(&listeners);
|
pxListItem != listGET_END_MARKER(&s_listeners);
|
||||||
pxListItem = listGET_NEXT(pxListItem)) {
|
pxListItem = listGET_NEXT(pxListItem)) {
|
||||||
|
|
||||||
relay_chn_listener_entry_t *entry = (relay_chn_listener_entry_t *) listGET_LIST_ITEM_OWNER(pxListItem);
|
relay_chn_listener_entry_t *entry = (relay_chn_listener_entry_t *) listGET_LIST_ITEM_OWNER(pxListItem);
|
||||||
@@ -154,7 +154,7 @@ static void do_add_listener(relay_chn_state_listener_t listener)
|
|||||||
entry->listener = listener;
|
entry->listener = listener;
|
||||||
vListInitialiseItem(&(entry->list_item));
|
vListInitialiseItem(&(entry->list_item));
|
||||||
listSET_LIST_ITEM_OWNER(&(entry->list_item), (void *)entry);
|
listSET_LIST_ITEM_OWNER(&(entry->list_item), (void *)entry);
|
||||||
vListInsertEnd(&listeners, &(entry->list_item));
|
vListInsertEnd(&s_listeners, &(entry->list_item));
|
||||||
ESP_LOGD(TAG, "Registered listener %p", listener);
|
ESP_LOGD(TAG, "Registered listener %p", listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,19 +170,19 @@ static void do_remove_listener(relay_chn_state_listener_t listener)
|
|||||||
ESP_LOGD(TAG, "Listener %p not found for unregistration.", listener);
|
ESP_LOGD(TAG, "Listener %p not found for unregistration.", listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (listLIST_IS_EMPTY(&listeners)) {
|
if (listLIST_IS_EMPTY(&s_listeners)) {
|
||||||
// Flush all pending notifications in the queue
|
// Flush all pending notifications in the queue
|
||||||
xQueueReset(notify_msg_queue);
|
xQueueReset(s_notify_queue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t relay_chn_notify_add_listener(relay_chn_state_listener_t listener)
|
esp_err_t relay_chn_notify_add_listener(relay_chn_state_listener_t listener)
|
||||||
{
|
{
|
||||||
ESP_RETURN_ON_FALSE(listener, ESP_ERR_INVALID_ARG, TAG, "Listener cannot be NULL");
|
ESP_RETURN_ON_FALSE(listener, ESP_ERR_INVALID_ARG, TAG, "Listener cannot be NULL");
|
||||||
ESP_RETURN_ON_FALSE(notify_msg_queue, ESP_ERR_INVALID_STATE, TAG, "Notify module not initialized");
|
ESP_RETURN_ON_FALSE(s_notify_queue, ESP_ERR_INVALID_STATE, TAG, "Notify module not initialized");
|
||||||
|
|
||||||
relay_chn_notify_msg_t msg = { .cmd = RELAY_CHN_NOTIFY_CMD_ADD_LISTENER, .payload.listener = listener };
|
relay_chn_notify_msg_t msg = { .cmd = RELAY_CHN_NOTIFY_CMD_ADD_LISTENER, .payload.listener = listener };
|
||||||
if (xQueueSend(notify_msg_queue, &msg, 0) != pdTRUE) {
|
if (xQueueSend(s_notify_queue, &msg, 0) != pdTRUE) {
|
||||||
ESP_LOGE(TAG, "Notify queue is full, failed to queue add_listener");
|
ESP_LOGE(TAG, "Notify queue is full, failed to queue add_listener");
|
||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
@@ -195,20 +195,20 @@ void relay_chn_notify_remove_listener(relay_chn_state_listener_t listener)
|
|||||||
ESP_LOGD(TAG, "Cannot unregister a NULL listener.");
|
ESP_LOGD(TAG, "Cannot unregister a NULL listener.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!notify_msg_queue) {
|
if (!s_notify_queue) {
|
||||||
ESP_LOGE(TAG, "Notify module not initialized, cannot remove listener");
|
ESP_LOGE(TAG, "Notify module not initialized, cannot remove listener");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
relay_chn_notify_msg_t msg = { .cmd = RELAY_CHN_NOTIFY_CMD_REMOVE_LISTENER, .payload.listener = listener };
|
relay_chn_notify_msg_t msg = { .cmd = RELAY_CHN_NOTIFY_CMD_REMOVE_LISTENER, .payload.listener = listener };
|
||||||
if (xQueueSendToFront(notify_msg_queue, &msg, 0) != pdTRUE) {
|
if (xQueueSendToFront(s_notify_queue, &msg, 0) != pdTRUE) {
|
||||||
ESP_LOGW(TAG, "Notify queue is full, failed to queue remove_listener");
|
ESP_LOGW(TAG, "Notify queue is full, failed to queue remove_listener");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t relay_chn_notify_state_change(uint8_t chn_id, relay_chn_state_t old_state, relay_chn_state_t new_state)
|
esp_err_t relay_chn_notify_state_change(uint8_t chn_id, relay_chn_state_t old_state, relay_chn_state_t new_state)
|
||||||
{
|
{
|
||||||
if (!notify_msg_queue) {
|
if (!s_notify_queue) {
|
||||||
return ESP_ERR_INVALID_STATE;
|
return ESP_ERR_INVALID_STATE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -220,7 +220,7 @@ esp_err_t relay_chn_notify_state_change(uint8_t chn_id, relay_chn_state_t old_st
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Try to send, do not wait if the queue is full
|
// Try to send, do not wait if the queue is full
|
||||||
if (xQueueSend(notify_msg_queue, &msg, 0) != pdTRUE) {
|
if (xQueueSend(s_notify_queue, &msg, 0) != pdTRUE) {
|
||||||
ESP_LOGW(TAG, "Notify queue is full, dropping event: %d -> %d for #%d", old_state, new_state, chn_id);
|
ESP_LOGW(TAG, "Notify queue is full, dropping event: %d -> %d for #%d", old_state, new_state, chn_id);
|
||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
@@ -231,8 +231,8 @@ static void do_notify(relay_chn_notify_event_data_t *event_data)
|
|||||||
{
|
{
|
||||||
// Iterate through the linked list of listeners and notify them.
|
// Iterate through the linked list of listeners and notify them.
|
||||||
// No mutex is needed as this is the only task accessing the list.
|
// No mutex is needed as this is the only task accessing the list.
|
||||||
for (ListItem_t *pxListItem = listGET_HEAD_ENTRY(&listeners);
|
for (ListItem_t *pxListItem = listGET_HEAD_ENTRY(&s_listeners);
|
||||||
pxListItem != listGET_END_MARKER(&listeners);
|
pxListItem != listGET_END_MARKER(&s_listeners);
|
||||||
pxListItem = listGET_NEXT(pxListItem)) {
|
pxListItem = listGET_NEXT(pxListItem)) {
|
||||||
relay_chn_listener_entry_t *entry = (relay_chn_listener_entry_t *) listGET_LIST_ITEM_OWNER(pxListItem);
|
relay_chn_listener_entry_t *entry = (relay_chn_listener_entry_t *) listGET_LIST_ITEM_OWNER(pxListItem);
|
||||||
if (entry && entry->listener) {
|
if (entry && entry->listener) {
|
||||||
@@ -247,7 +247,7 @@ static void relay_chn_notify_task(void *arg)
|
|||||||
{
|
{
|
||||||
relay_chn_notify_msg_t msg;
|
relay_chn_notify_msg_t msg;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (xQueueReceive(notify_msg_queue, &msg, portMAX_DELAY) == pdTRUE) {
|
if (xQueueReceive(s_notify_queue, &msg, portMAX_DELAY) == pdTRUE) {
|
||||||
switch (msg.cmd) {
|
switch (msg.cmd) {
|
||||||
case RELAY_CHN_NOTIFY_CMD_BROADCAST: {
|
case RELAY_CHN_NOTIFY_CMD_BROADCAST: {
|
||||||
do_notify(&msg.payload.event_data);
|
do_notify(&msg.payload.event_data);
|
||||||
|
|||||||
@@ -61,10 +61,10 @@ typedef struct {
|
|||||||
|
|
||||||
static const char *TAG = "RELAY_CHN_NVS";
|
static const char *TAG = "RELAY_CHN_NVS";
|
||||||
|
|
||||||
static nvs_handle_t relay_chn_nvs;
|
static nvs_handle_t s_relay_chn_nvs;
|
||||||
static QueueHandle_t nvs_queue_handle = NULL;
|
static QueueHandle_t s_nvs_ops_queue = NULL;
|
||||||
static TaskHandle_t nvs_task_handle = NULL;
|
static TaskHandle_t s_nvs_ops_task = NULL;
|
||||||
static SemaphoreHandle_t deinit_sem = NULL;
|
static SemaphoreHandle_t s_nvs_deinit_sem = NULL;
|
||||||
|
|
||||||
|
|
||||||
static void relay_chn_nvs_task(void *arg);
|
static void relay_chn_nvs_task(void *arg);
|
||||||
@@ -72,25 +72,25 @@ static void relay_chn_nvs_task(void *arg);
|
|||||||
esp_err_t relay_chn_nvs_init()
|
esp_err_t relay_chn_nvs_init()
|
||||||
{
|
{
|
||||||
// Already initialized?
|
// Already initialized?
|
||||||
if (nvs_queue_handle != NULL) {
|
if (s_nvs_ops_queue != NULL) {
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
deinit_sem = xSemaphoreCreateBinary();
|
s_nvs_deinit_sem = xSemaphoreCreateBinary();
|
||||||
if (!deinit_sem) {
|
if (!s_nvs_deinit_sem) {
|
||||||
ESP_LOGE(TAG, "Failed to create deinit semaphore");
|
ESP_LOGE(TAG, "Failed to create deinit semaphore");
|
||||||
return ESP_ERR_NO_MEM;
|
return ESP_ERR_NO_MEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
nvs_queue_handle = xQueueCreate(RELAY_CHN_NVS_QUEUE_LEN, sizeof(relay_chn_nvs_msg_t));
|
s_nvs_ops_queue = xQueueCreate(RELAY_CHN_NVS_QUEUE_LEN, sizeof(relay_chn_nvs_msg_t));
|
||||||
if (!nvs_queue_handle) {
|
if (!s_nvs_ops_queue) {
|
||||||
ESP_LOGE(TAG, "Failed to create NVS queue");
|
ESP_LOGE(TAG, "Failed to create NVS queue");
|
||||||
return ESP_ERR_NO_MEM;
|
return ESP_ERR_NO_MEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
BaseType_t res = xTaskCreate(relay_chn_nvs_task, "task_rlch_nvs",
|
BaseType_t res = xTaskCreate(relay_chn_nvs_task, "task_rlch_nvs",
|
||||||
RELAY_CHN_NVS_TASK_STACK, NULL,
|
RELAY_CHN_NVS_TASK_STACK, NULL,
|
||||||
RELAY_CHN_NVS_TASK_PRIO, &nvs_task_handle);
|
RELAY_CHN_NVS_TASK_PRIO, &s_nvs_ops_task);
|
||||||
if (res != pdPASS) {
|
if (res != pdPASS) {
|
||||||
ESP_LOGE(TAG, "Failed to create NVS task");
|
ESP_LOGE(TAG, "Failed to create NVS task");
|
||||||
return ESP_ERR_NO_MEM;
|
return ESP_ERR_NO_MEM;
|
||||||
@@ -101,7 +101,7 @@ esp_err_t relay_chn_nvs_init()
|
|||||||
ret = nvs_open_from_partition(CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION_NAME,
|
ret = nvs_open_from_partition(CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION_NAME,
|
||||||
CONFIG_RELAY_CHN_NVS_NAMESPACE,
|
CONFIG_RELAY_CHN_NVS_NAMESPACE,
|
||||||
NVS_READWRITE,
|
NVS_READWRITE,
|
||||||
&relay_chn_nvs);
|
&s_relay_chn_nvs);
|
||||||
|
|
||||||
ESP_RETURN_ON_ERROR(ret,
|
ESP_RETURN_ON_ERROR(ret,
|
||||||
TAG,
|
TAG,
|
||||||
@@ -110,7 +110,7 @@ esp_err_t relay_chn_nvs_init()
|
|||||||
CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION_NAME,
|
CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION_NAME,
|
||||||
esp_err_to_name(ret));
|
esp_err_to_name(ret));
|
||||||
#else
|
#else
|
||||||
ret = nvs_open(CONFIG_RELAY_CHN_NVS_NAMESPACE, NVS_READWRITE, &relay_chn_nvs);
|
ret = nvs_open(CONFIG_RELAY_CHN_NVS_NAMESPACE, NVS_READWRITE, &s_relay_chn_nvs);
|
||||||
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to open NVS namespace '%s'", CONFIG_RELAY_CHN_NVS_NAMESPACE);
|
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to open NVS namespace '%s'", CONFIG_RELAY_CHN_NVS_NAMESPACE);
|
||||||
#endif // CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION
|
#endif // CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
@@ -118,19 +118,19 @@ esp_err_t relay_chn_nvs_init()
|
|||||||
|
|
||||||
static esp_err_t relay_chn_nvs_enqueue(relay_chn_nvs_msg_t *msg, const char *op_name)
|
static esp_err_t relay_chn_nvs_enqueue(relay_chn_nvs_msg_t *msg, const char *op_name)
|
||||||
{
|
{
|
||||||
if (!nvs_queue_handle) {
|
if (!s_nvs_ops_queue) {
|
||||||
return ESP_ERR_INVALID_STATE;
|
return ESP_ERR_INVALID_STATE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (msg->op == RELAY_CHN_NVS_OP_DEINIT || msg->op == RELAY_CHN_NVS_OP_ERASE_ALL) {
|
if (msg->op == RELAY_CHN_NVS_OP_DEINIT || msg->op == RELAY_CHN_NVS_OP_ERASE_ALL) {
|
||||||
// Send DEINIT or ERASE_ALL to the front and wait up to 1 sec if needed
|
// Send DEINIT or ERASE_ALL to the front and wait up to 1 sec if needed
|
||||||
if (xQueueSendToFront(nvs_queue_handle, msg, pdMS_TO_TICKS(1000)) != pdTRUE) {
|
if (xQueueSendToFront(s_nvs_ops_queue, msg, pdMS_TO_TICKS(1000)) != pdTRUE) {
|
||||||
ESP_LOGW(TAG, "NVS queue is full, dropping %s for #%d", op_name, msg->ch);
|
ESP_LOGW(TAG, "NVS queue is full, dropping %s for #%d", op_name, msg->ch);
|
||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Send async
|
// Send async
|
||||||
if (xQueueSend(nvs_queue_handle, msg, 0) != pdTRUE) {
|
if (xQueueSend(s_nvs_ops_queue, msg, 0) != pdTRUE) {
|
||||||
ESP_LOGW(TAG, "NVS queue is full, dropping %s for #%d", op_name, msg->ch);
|
ESP_LOGW(TAG, "NVS queue is full, dropping %s for #%d", op_name, msg->ch);
|
||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
@@ -151,13 +151,13 @@ esp_err_t relay_chn_nvs_set_direction(uint8_t ch, relay_chn_direction_t directio
|
|||||||
static esp_err_t relay_chn_nvs_task_set_direction(uint8_t ch, uint8_t direction)
|
static esp_err_t relay_chn_nvs_task_set_direction(uint8_t ch, uint8_t direction)
|
||||||
{
|
{
|
||||||
uint8_t direction_val = 0;
|
uint8_t direction_val = 0;
|
||||||
esp_err_t ret = nvs_get_u8(relay_chn_nvs, RELAY_CHN_KEY_DIR, &direction_val);
|
esp_err_t ret = nvs_get_u8(s_relay_chn_nvs, RELAY_CHN_KEY_DIR, &direction_val);
|
||||||
if (ret != ESP_OK && ret != ESP_ERR_NVS_NOT_FOUND) {
|
if (ret != ESP_OK && ret != ESP_ERR_NVS_NOT_FOUND) {
|
||||||
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to get direction from NVS with error: %s", esp_err_to_name(ret));
|
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to get direction from NVS with error: %s", esp_err_to_name(ret));
|
||||||
}
|
}
|
||||||
direction_val &= ~(1 << ch); // Clear the bit for the channel
|
direction_val &= ~(1 << ch); // Clear the bit for the channel
|
||||||
direction_val |= (((uint8_t) direction) << ch); // Set the new direction bit
|
direction_val |= (((uint8_t) direction) << ch); // Set the new direction bit
|
||||||
ret = nvs_set_u8(relay_chn_nvs, RELAY_CHN_KEY_DIR, direction_val);
|
ret = nvs_set_u8(s_relay_chn_nvs, RELAY_CHN_KEY_DIR, direction_val);
|
||||||
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to set direction for channel %d", ch);
|
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to set direction for channel %d", ch);
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
@@ -167,7 +167,7 @@ esp_err_t relay_chn_nvs_get_direction(uint8_t ch, relay_chn_direction_t *directi
|
|||||||
ESP_RETURN_ON_FALSE(direction != NULL, ESP_ERR_INVALID_ARG, TAG, "Direction pointer is NULL");
|
ESP_RETURN_ON_FALSE(direction != NULL, ESP_ERR_INVALID_ARG, TAG, "Direction pointer is NULL");
|
||||||
|
|
||||||
uint8_t direction_val;
|
uint8_t direction_val;
|
||||||
esp_err_t ret = nvs_get_u8(relay_chn_nvs, RELAY_CHN_KEY_DIR, &direction_val);
|
esp_err_t ret = nvs_get_u8(s_relay_chn_nvs, RELAY_CHN_KEY_DIR, &direction_val);
|
||||||
if (ret == ESP_ERR_NVS_NOT_FOUND) {
|
if (ret == ESP_ERR_NVS_NOT_FOUND) {
|
||||||
*direction = default_val;
|
*direction = default_val;
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
@@ -196,9 +196,9 @@ static esp_err_t relay_chn_nvs_task_set_run_limit(uint8_t ch, uint16_t limit_sec
|
|||||||
#if CONFIG_RELAY_CHN_COUNT > 1
|
#if CONFIG_RELAY_CHN_COUNT > 1
|
||||||
char key[NVS_KEY_NAME_MAX_SIZE];
|
char key[NVS_KEY_NAME_MAX_SIZE];
|
||||||
snprintf(key, sizeof(key), RELAY_CHN_KEY_RLIM_FMT, ch);
|
snprintf(key, sizeof(key), RELAY_CHN_KEY_RLIM_FMT, ch);
|
||||||
ret = nvs_set_u16(relay_chn_nvs, key, limit_sec);
|
ret = nvs_set_u16(s_relay_chn_nvs, key, limit_sec);
|
||||||
#else
|
#else
|
||||||
ret = nvs_set_u16(relay_chn_nvs, RELAY_CHN_KEY_RLIM, limit_sec);
|
ret = nvs_set_u16(s_relay_chn_nvs, RELAY_CHN_KEY_RLIM, limit_sec);
|
||||||
#endif
|
#endif
|
||||||
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to set run limit for channel %d", ch);
|
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to set run limit for channel %d", ch);
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
@@ -212,9 +212,9 @@ esp_err_t relay_chn_nvs_get_run_limit(uint8_t ch, uint16_t *limit_sec, uint16_t
|
|||||||
#if CONFIG_RELAY_CHN_COUNT > 1
|
#if CONFIG_RELAY_CHN_COUNT > 1
|
||||||
char key[NVS_KEY_NAME_MAX_SIZE];
|
char key[NVS_KEY_NAME_MAX_SIZE];
|
||||||
snprintf(key, sizeof(key), RELAY_CHN_KEY_RLIM_FMT, ch);
|
snprintf(key, sizeof(key), RELAY_CHN_KEY_RLIM_FMT, ch);
|
||||||
ret = nvs_get_u16(relay_chn_nvs, key, limit_sec);
|
ret = nvs_get_u16(s_relay_chn_nvs, key, limit_sec);
|
||||||
#else
|
#else
|
||||||
ret = nvs_get_u16(relay_chn_nvs, RELAY_CHN_KEY_RLIM, limit_sec);
|
ret = nvs_get_u16(s_relay_chn_nvs, RELAY_CHN_KEY_RLIM, limit_sec);
|
||||||
#endif
|
#endif
|
||||||
if (ret == ESP_ERR_NVS_NOT_FOUND) {
|
if (ret == ESP_ERR_NVS_NOT_FOUND) {
|
||||||
*limit_sec = default_val;
|
*limit_sec = default_val;
|
||||||
@@ -241,9 +241,9 @@ static esp_err_t relay_chn_nvs_task_set_tilt_sensitivity(uint8_t ch, uint8_t sen
|
|||||||
#if CONFIG_RELAY_CHN_COUNT > 1
|
#if CONFIG_RELAY_CHN_COUNT > 1
|
||||||
char key[NVS_KEY_NAME_MAX_SIZE];
|
char key[NVS_KEY_NAME_MAX_SIZE];
|
||||||
snprintf(key, sizeof(key), RELAY_CHN_KEY_TSENS_FMT, ch);
|
snprintf(key, sizeof(key), RELAY_CHN_KEY_TSENS_FMT, ch);
|
||||||
ret = nvs_set_u8(relay_chn_nvs, key, sensitivity);
|
ret = nvs_set_u8(s_relay_chn_nvs, key, sensitivity);
|
||||||
#else
|
#else
|
||||||
ret = nvs_set_u8(relay_chn_nvs, RELAY_CHN_KEY_TSENS, sensitivity);
|
ret = nvs_set_u8(s_relay_chn_nvs, RELAY_CHN_KEY_TSENS, sensitivity);
|
||||||
#endif
|
#endif
|
||||||
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to set tilt sensitivity for channel %d", ch);
|
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to set tilt sensitivity for channel %d", ch);
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
@@ -257,9 +257,9 @@ esp_err_t relay_chn_nvs_get_tilt_sensitivity(uint8_t ch, uint8_t *sensitivity, u
|
|||||||
#if CONFIG_RELAY_CHN_COUNT > 1
|
#if CONFIG_RELAY_CHN_COUNT > 1
|
||||||
char key[NVS_KEY_NAME_MAX_SIZE];
|
char key[NVS_KEY_NAME_MAX_SIZE];
|
||||||
snprintf(key, sizeof(key), RELAY_CHN_KEY_TSENS_FMT, ch);
|
snprintf(key, sizeof(key), RELAY_CHN_KEY_TSENS_FMT, ch);
|
||||||
ret = nvs_get_u8(relay_chn_nvs, key, sensitivity);
|
ret = nvs_get_u8(s_relay_chn_nvs, key, sensitivity);
|
||||||
#else
|
#else
|
||||||
ret = nvs_get_u8(relay_chn_nvs, RELAY_CHN_KEY_TSENS, sensitivity);
|
ret = nvs_get_u8(s_relay_chn_nvs, RELAY_CHN_KEY_TSENS, sensitivity);
|
||||||
#endif
|
#endif
|
||||||
if (ret == ESP_ERR_NVS_NOT_FOUND) {
|
if (ret == ESP_ERR_NVS_NOT_FOUND) {
|
||||||
*sensitivity = default_val;
|
*sensitivity = default_val;
|
||||||
@@ -284,9 +284,9 @@ static esp_err_t relay_chn_nvs_task_set_tilt_count(uint8_t ch, uint16_t tilt_cou
|
|||||||
#if CONFIG_RELAY_CHN_COUNT > 1
|
#if CONFIG_RELAY_CHN_COUNT > 1
|
||||||
char key[NVS_KEY_NAME_MAX_SIZE];
|
char key[NVS_KEY_NAME_MAX_SIZE];
|
||||||
snprintf(key, sizeof(key), RELAY_CHN_KEY_TCNT_FMT, ch);
|
snprintf(key, sizeof(key), RELAY_CHN_KEY_TCNT_FMT, ch);
|
||||||
ret = nvs_set_u16(relay_chn_nvs, key, tilt_count);
|
ret = nvs_set_u16(s_relay_chn_nvs, key, tilt_count);
|
||||||
#else
|
#else
|
||||||
ret = nvs_set_u16(relay_chn_nvs, RELAY_CHN_KEY_TCNT, tilt_count);
|
ret = nvs_set_u16(s_relay_chn_nvs, RELAY_CHN_KEY_TCNT, tilt_count);
|
||||||
#endif
|
#endif
|
||||||
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to save tilt_count tilt counter");
|
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to save tilt_count tilt counter");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
@@ -300,9 +300,9 @@ esp_err_t relay_chn_nvs_get_tilt_count(uint8_t ch, uint16_t *tilt_count, uint16_
|
|||||||
#if CONFIG_RELAY_CHN_COUNT > 1
|
#if CONFIG_RELAY_CHN_COUNT > 1
|
||||||
char key[NVS_KEY_NAME_MAX_SIZE];
|
char key[NVS_KEY_NAME_MAX_SIZE];
|
||||||
snprintf(key, sizeof(key), RELAY_CHN_KEY_TCNT_FMT, ch);
|
snprintf(key, sizeof(key), RELAY_CHN_KEY_TCNT_FMT, ch);
|
||||||
ret = nvs_get_u16(relay_chn_nvs, key, tilt_count);
|
ret = nvs_get_u16(s_relay_chn_nvs, key, tilt_count);
|
||||||
#else
|
#else
|
||||||
ret = nvs_get_u16(relay_chn_nvs, RELAY_CHN_KEY_TCNT, tilt_count);
|
ret = nvs_get_u16(s_relay_chn_nvs, RELAY_CHN_KEY_TCNT, tilt_count);
|
||||||
#endif
|
#endif
|
||||||
if (ret == ESP_ERR_NVS_NOT_FOUND) {
|
if (ret == ESP_ERR_NVS_NOT_FOUND) {
|
||||||
*tilt_count = default_val;
|
*tilt_count = default_val;
|
||||||
@@ -331,38 +331,38 @@ static esp_err_t do_nvs_deinit()
|
|||||||
static esp_err_t do_nvs_erase_all()
|
static esp_err_t do_nvs_erase_all()
|
||||||
{
|
{
|
||||||
// Flush all pending SET operations since ERASE_ALL requested
|
// Flush all pending SET operations since ERASE_ALL requested
|
||||||
xQueueReset(nvs_queue_handle);
|
xQueueReset(s_nvs_ops_queue);
|
||||||
// Erase all key-value pairs in the relay_chn NVS namespace
|
// Erase all key-value pairs in the relay_chn NVS namespace
|
||||||
esp_err_t ret = nvs_erase_all(relay_chn_nvs);
|
esp_err_t ret = nvs_erase_all(s_relay_chn_nvs);
|
||||||
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to erase all keys in NVS namespace '%s'", CONFIG_RELAY_CHN_NVS_NAMESPACE);
|
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to erase all keys in NVS namespace '%s'", CONFIG_RELAY_CHN_NVS_NAMESPACE);
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
void relay_chn_nvs_deinit()
|
void relay_chn_nvs_deinit()
|
||||||
{
|
{
|
||||||
if (nvs_task_handle) {
|
if (s_nvs_ops_task) {
|
||||||
if (do_nvs_deinit() == ESP_OK) {
|
if (do_nvs_deinit() == ESP_OK) {
|
||||||
if (deinit_sem && xSemaphoreTake(deinit_sem, pdMS_TO_TICKS(2000)) != pdTRUE) {
|
if (s_nvs_deinit_sem && xSemaphoreTake(s_nvs_deinit_sem, pdMS_TO_TICKS(2000)) != pdTRUE) {
|
||||||
ESP_LOGE(TAG, "Failed to get deinit confirmation from NVS task. Forcing deletion.");
|
ESP_LOGE(TAG, "Failed to get deinit confirmation from NVS task. Forcing deletion.");
|
||||||
vTaskDelete(nvs_task_handle); // Last resort
|
vTaskDelete(s_nvs_ops_task); // Last resort
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGE(TAG, "Failed to send deinit message to NVS task. Forcing deletion.");
|
ESP_LOGE(TAG, "Failed to send deinit message to NVS task. Forcing deletion.");
|
||||||
vTaskDelete(nvs_task_handle);
|
vTaskDelete(s_nvs_ops_task);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (nvs_queue_handle) {
|
if (s_nvs_ops_queue) {
|
||||||
vQueueDelete(nvs_queue_handle);
|
vQueueDelete(s_nvs_ops_queue);
|
||||||
nvs_queue_handle = NULL;
|
s_nvs_ops_queue = NULL;
|
||||||
}
|
}
|
||||||
if (deinit_sem) {
|
if (s_nvs_deinit_sem) {
|
||||||
vSemaphoreDelete(deinit_sem);
|
vSemaphoreDelete(s_nvs_deinit_sem);
|
||||||
deinit_sem = NULL;
|
s_nvs_deinit_sem = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close NVS handle here, after task has stopped and queue is deleted.
|
// Close NVS handle here, after task has stopped and queue is deleted.
|
||||||
nvs_close(relay_chn_nvs);
|
nvs_close(s_relay_chn_nvs);
|
||||||
nvs_task_handle = NULL;
|
s_nvs_ops_task = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static esp_err_t relay_chn_nvs_task_process_message(const relay_chn_nvs_msg_t *msg, bool *running, bool *dirty)
|
static esp_err_t relay_chn_nvs_task_process_message(const relay_chn_nvs_msg_t *msg, bool *running, bool *dirty)
|
||||||
@@ -417,7 +417,7 @@ static void relay_chn_nvs_task(void *arg)
|
|||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
// Block indefinitely waiting for the first message of a potential batch.
|
// Block indefinitely waiting for the first message of a potential batch.
|
||||||
if (xQueueReceive(nvs_queue_handle, &msg, portMAX_DELAY) == pdTRUE) {
|
if (xQueueReceive(s_nvs_ops_queue, &msg, portMAX_DELAY) == pdTRUE) {
|
||||||
// A batch of operations has started. Use a do-while to process the first message
|
// A batch of operations has started. Use a do-while to process the first message
|
||||||
// and any subsequent messages that arrive within the timeout.
|
// and any subsequent messages that arrive within the timeout.
|
||||||
do {
|
do {
|
||||||
@@ -425,11 +425,11 @@ static void relay_chn_nvs_task(void *arg)
|
|||||||
if (ret != ESP_OK) {
|
if (ret != ESP_OK) {
|
||||||
ESP_LOGE(TAG, "Failed to process operation %d for #%d with error %s", msg.op, msg.ch, esp_err_to_name(ret));
|
ESP_LOGE(TAG, "Failed to process operation %d for #%d with error %s", msg.op, msg.ch, esp_err_to_name(ret));
|
||||||
}
|
}
|
||||||
} while (running && xQueueReceive(nvs_queue_handle, &msg, pdMS_TO_TICKS(RELAY_CHN_NVS_COMMIT_TIMEOUT_MS)) == pdTRUE);
|
} while (running && xQueueReceive(s_nvs_ops_queue, &msg, pdMS_TO_TICKS(RELAY_CHN_NVS_COMMIT_TIMEOUT_MS)) == pdTRUE);
|
||||||
|
|
||||||
// The burst of messages is over (timeout occurred). Commit if anything changed.
|
// The burst of messages is over (timeout occurred). Commit if anything changed.
|
||||||
if (dirty) {
|
if (dirty) {
|
||||||
esp_err_t commit_ret = nvs_commit(relay_chn_nvs);
|
esp_err_t commit_ret = nvs_commit(s_relay_chn_nvs);
|
||||||
if (commit_ret == ESP_OK) {
|
if (commit_ret == ESP_OK) {
|
||||||
dirty = false;
|
dirty = false;
|
||||||
} else {
|
} else {
|
||||||
@@ -442,11 +442,11 @@ static void relay_chn_nvs_task(void *arg)
|
|||||||
|
|
||||||
// Before exiting, do one final commit if there are pending changes.
|
// Before exiting, do one final commit if there are pending changes.
|
||||||
if (dirty) {
|
if (dirty) {
|
||||||
if (nvs_commit(relay_chn_nvs) != ESP_OK) {
|
if (nvs_commit(s_relay_chn_nvs) != ESP_OK) {
|
||||||
ESP_LOGE(TAG, "Final NVS commit failed on deinit");
|
ESP_LOGE(TAG, "Final NVS commit failed on deinit");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
xSemaphoreGive(deinit_sem);
|
xSemaphoreGive(s_nvs_deinit_sem);
|
||||||
nvs_task_handle = NULL;
|
s_nvs_ops_task = NULL;
|
||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
}
|
}
|
||||||
@@ -17,9 +17,9 @@
|
|||||||
static const char *TAG = "RELAY_CHN_OUTPUT";
|
static const char *TAG = "RELAY_CHN_OUTPUT";
|
||||||
|
|
||||||
#if CONFIG_RELAY_CHN_COUNT > 1
|
#if CONFIG_RELAY_CHN_COUNT > 1
|
||||||
static relay_chn_output_t outputs[CONFIG_RELAY_CHN_COUNT];
|
static relay_chn_output_t s_outputs[CONFIG_RELAY_CHN_COUNT];
|
||||||
#else
|
#else
|
||||||
static relay_chn_output_t output;
|
static relay_chn_output_t s_output;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
@@ -90,7 +90,7 @@ esp_err_t relay_chn_output_init(const uint8_t* gpio_map, uint8_t gpio_count)
|
|||||||
|
|
||||||
#if CONFIG_RELAY_CHN_COUNT > 1
|
#if CONFIG_RELAY_CHN_COUNT > 1
|
||||||
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
||||||
relay_chn_output_t* output = &outputs[i];
|
relay_chn_output_t* output = &s_outputs[i];
|
||||||
int gpio_index = i << 1; // gpio_index = i * 2
|
int gpio_index = i << 1; // gpio_index = i * 2
|
||||||
gpio_num_t forward_pin = (gpio_num_t) gpio_map[gpio_index];
|
gpio_num_t forward_pin = (gpio_num_t) gpio_map[gpio_index];
|
||||||
gpio_num_t reverse_pin = (gpio_num_t) gpio_map[gpio_index + 1];
|
gpio_num_t reverse_pin = (gpio_num_t) gpio_map[gpio_index + 1];
|
||||||
@@ -111,7 +111,7 @@ esp_err_t relay_chn_output_init(const uint8_t* gpio_map, uint8_t gpio_count)
|
|||||||
ret = relay_chn_output_load_direction(0, &direction);
|
ret = relay_chn_output_load_direction(0, &direction);
|
||||||
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load direction from storage for channel %d", 0);
|
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load direction from storage for channel %d", 0);
|
||||||
#endif
|
#endif
|
||||||
ret = relay_chn_output_ctl_init(&output, gpio_map[0], gpio_map[1], direction);
|
ret = relay_chn_output_ctl_init(&s_output, gpio_map[0], gpio_map[1], direction);
|
||||||
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to initialize relay channel");
|
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to initialize relay channel");
|
||||||
#endif
|
#endif
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
@@ -127,10 +127,10 @@ void relay_chn_output_deinit()
|
|||||||
{
|
{
|
||||||
#if CONFIG_RELAY_CHN_COUNT > 1
|
#if CONFIG_RELAY_CHN_COUNT > 1
|
||||||
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
||||||
relay_chn_output_ctl_deinit(&outputs[i]);
|
relay_chn_output_ctl_deinit(&s_outputs[i]);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
relay_chn_output_ctl_deinit(&output);
|
relay_chn_output_ctl_deinit(&s_output);
|
||||||
#endif // CONFIG_RELAY_CHN_COUNT > 1
|
#endif // CONFIG_RELAY_CHN_COUNT > 1
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,17 +140,17 @@ relay_chn_output_t *relay_chn_output_get(uint8_t chn_id)
|
|||||||
if (!relay_chn_is_channel_id_valid(chn_id)) {
|
if (!relay_chn_is_channel_id_valid(chn_id)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return &outputs[chn_id];
|
return &s_outputs[chn_id];
|
||||||
}
|
}
|
||||||
|
|
||||||
relay_chn_output_t *relay_chn_output_get_all(void)
|
relay_chn_output_t *relay_chn_output_get_all(void)
|
||||||
{
|
{
|
||||||
return outputs;
|
return s_outputs;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
relay_chn_output_t *relay_chn_output_get(void)
|
relay_chn_output_t *relay_chn_output_get(void)
|
||||||
{
|
{
|
||||||
return &output;
|
return &s_output;
|
||||||
}
|
}
|
||||||
#endif // CONFIG_RELAY_CHN_COUNT > 1
|
#endif // CONFIG_RELAY_CHN_COUNT > 1
|
||||||
|
|
||||||
@@ -193,7 +193,7 @@ void relay_chn_output_flip(relay_chn_output_t *output)
|
|||||||
uint8_t ch = 0;
|
uint8_t ch = 0;
|
||||||
#if CONFIG_RELAY_CHN_COUNT > 1
|
#if CONFIG_RELAY_CHN_COUNT > 1
|
||||||
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
||||||
if (output == &outputs[i]) {
|
if (output == &s_outputs[i]) {
|
||||||
ch = i;
|
ch = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,21 +9,21 @@
|
|||||||
|
|
||||||
|
|
||||||
#if CONFIG_RELAY_CHN_COUNT > 1
|
#if CONFIG_RELAY_CHN_COUNT > 1
|
||||||
static relay_chn_run_info_t run_infos[CONFIG_RELAY_CHN_COUNT];
|
static relay_chn_run_info_t s_run_infos[CONFIG_RELAY_CHN_COUNT];
|
||||||
#else
|
#else
|
||||||
static relay_chn_run_info_t run_info;
|
static relay_chn_run_info_t s_run_info;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void relay_chn_run_info_init()
|
void relay_chn_run_info_init()
|
||||||
{
|
{
|
||||||
#if CONFIG_RELAY_CHN_COUNT > 1
|
#if CONFIG_RELAY_CHN_COUNT > 1
|
||||||
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
||||||
run_infos[i].last_run_cmd = RELAY_CHN_CMD_NONE;
|
s_run_infos[i].last_run_cmd = RELAY_CHN_CMD_NONE;
|
||||||
run_infos[i].last_run_cmd_time_ms = 0;
|
s_run_infos[i].last_run_cmd_time_ms = 0;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
run_info.last_run_cmd = RELAY_CHN_CMD_NONE;
|
s_run_info.last_run_cmd = RELAY_CHN_CMD_NONE;
|
||||||
run_info.last_run_cmd_time_ms = 0;
|
s_run_info.last_run_cmd_time_ms = 0;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,17 +33,17 @@ relay_chn_run_info_t *relay_chn_run_info_get(uint8_t chn_id)
|
|||||||
if (!relay_chn_is_channel_id_valid(chn_id)) {
|
if (!relay_chn_is_channel_id_valid(chn_id)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return &run_infos[chn_id];
|
return &s_run_infos[chn_id];
|
||||||
}
|
}
|
||||||
|
|
||||||
relay_chn_run_info_t *relay_chn_run_info_get_all()
|
relay_chn_run_info_t *relay_chn_run_info_get_all()
|
||||||
{
|
{
|
||||||
return run_infos;
|
return s_run_infos;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
relay_chn_run_info_t *relay_chn_run_info_get()
|
relay_chn_run_info_t *relay_chn_run_info_get()
|
||||||
{
|
{
|
||||||
return &run_info;
|
return &s_run_info;
|
||||||
}
|
}
|
||||||
#endif // CONFIG_RELAY_CHN_COUNT > 1
|
#endif // CONFIG_RELAY_CHN_COUNT > 1
|
||||||
|
|
||||||
|
|||||||
@@ -71,9 +71,9 @@ typedef struct relay_chn_tilt_ctl {
|
|||||||
|
|
||||||
|
|
||||||
#if CONFIG_RELAY_CHN_COUNT > 1
|
#if CONFIG_RELAY_CHN_COUNT > 1
|
||||||
static relay_chn_tilt_ctl_t tilt_ctls[CONFIG_RELAY_CHN_COUNT];
|
static relay_chn_tilt_ctl_t s_tilt_ctls[CONFIG_RELAY_CHN_COUNT];
|
||||||
#else
|
#else
|
||||||
static relay_chn_tilt_ctl_t tilt_ctl;
|
static relay_chn_tilt_ctl_t s_tilt_ctl;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
@@ -209,7 +209,7 @@ uint8_t relay_chn_tilt_get_default_sensitivity()
|
|||||||
static void relay_chn_tilt_issue_cmd_on_all_channels(relay_chn_tilt_cmd_t cmd)
|
static void relay_chn_tilt_issue_cmd_on_all_channels(relay_chn_tilt_cmd_t cmd)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
||||||
relay_chn_tilt_ctl_t* tilt_ctl = &tilt_ctls[i];
|
relay_chn_tilt_ctl_t* tilt_ctl = &s_tilt_ctls[i];
|
||||||
relay_chn_tilt_issue_cmd(tilt_ctl, cmd);
|
relay_chn_tilt_issue_cmd(tilt_ctl, cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -217,21 +217,21 @@ static void relay_chn_tilt_issue_cmd_on_all_channels(relay_chn_tilt_cmd_t cmd)
|
|||||||
void relay_chn_tilt_auto(uint8_t chn_id)
|
void relay_chn_tilt_auto(uint8_t chn_id)
|
||||||
{
|
{
|
||||||
if (relay_chn_is_channel_id_valid(chn_id)) {
|
if (relay_chn_is_channel_id_valid(chn_id)) {
|
||||||
relay_chn_tilt_issue_auto(&tilt_ctls[chn_id]);
|
relay_chn_tilt_issue_auto(&s_tilt_ctls[chn_id]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void relay_chn_tilt_auto_all()
|
void relay_chn_tilt_auto_all()
|
||||||
{
|
{
|
||||||
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
||||||
relay_chn_tilt_issue_auto(&tilt_ctls[i]);
|
relay_chn_tilt_issue_auto(&s_tilt_ctls[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void relay_chn_tilt_forward(uint8_t chn_id)
|
void relay_chn_tilt_forward(uint8_t chn_id)
|
||||||
{
|
{
|
||||||
if (relay_chn_is_channel_id_valid(chn_id)) {
|
if (relay_chn_is_channel_id_valid(chn_id)) {
|
||||||
relay_chn_tilt_issue_cmd(&tilt_ctls[chn_id], RELAY_CHN_TILT_CMD_FORWARD);
|
relay_chn_tilt_issue_cmd(&s_tilt_ctls[chn_id], RELAY_CHN_TILT_CMD_FORWARD);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,7 +243,7 @@ void relay_chn_tilt_forward_all()
|
|||||||
void relay_chn_tilt_reverse(uint8_t chn_id)
|
void relay_chn_tilt_reverse(uint8_t chn_id)
|
||||||
{
|
{
|
||||||
if (relay_chn_is_channel_id_valid(chn_id)) {
|
if (relay_chn_is_channel_id_valid(chn_id)) {
|
||||||
relay_chn_tilt_issue_cmd(&tilt_ctls[chn_id], RELAY_CHN_TILT_CMD_REVERSE);
|
relay_chn_tilt_issue_cmd(&s_tilt_ctls[chn_id], RELAY_CHN_TILT_CMD_REVERSE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -255,7 +255,7 @@ void relay_chn_tilt_reverse_all()
|
|||||||
void relay_chn_tilt_stop(uint8_t chn_id)
|
void relay_chn_tilt_stop(uint8_t chn_id)
|
||||||
{
|
{
|
||||||
if (!relay_chn_is_channel_id_valid(chn_id)) {
|
if (!relay_chn_is_channel_id_valid(chn_id)) {
|
||||||
relay_chn_tilt_dispatch_cmd(&tilt_ctls[chn_id], RELAY_CHN_TILT_CMD_STOP);
|
relay_chn_tilt_dispatch_cmd(&s_tilt_ctls[chn_id], RELAY_CHN_TILT_CMD_STOP);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -268,22 +268,22 @@ void relay_chn_tilt_stop_all()
|
|||||||
|
|
||||||
void relay_chn_tilt_auto()
|
void relay_chn_tilt_auto()
|
||||||
{
|
{
|
||||||
relay_chn_tilt_issue_auto(&tilt_ctl);
|
relay_chn_tilt_issue_auto(&s_tilt_ctl);
|
||||||
}
|
}
|
||||||
|
|
||||||
void relay_chn_tilt_forward()
|
void relay_chn_tilt_forward()
|
||||||
{
|
{
|
||||||
relay_chn_tilt_issue_cmd(&tilt_ctl, RELAY_CHN_TILT_CMD_FORWARD);
|
relay_chn_tilt_issue_cmd(&s_tilt_ctl, RELAY_CHN_TILT_CMD_FORWARD);
|
||||||
}
|
}
|
||||||
|
|
||||||
void relay_chn_tilt_reverse()
|
void relay_chn_tilt_reverse()
|
||||||
{
|
{
|
||||||
relay_chn_tilt_issue_cmd(&tilt_ctl, RELAY_CHN_TILT_CMD_REVERSE);
|
relay_chn_tilt_issue_cmd(&s_tilt_ctl, RELAY_CHN_TILT_CMD_REVERSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void relay_chn_tilt_stop()
|
void relay_chn_tilt_stop()
|
||||||
{
|
{
|
||||||
relay_chn_tilt_dispatch_cmd(&tilt_ctl, RELAY_CHN_TILT_CMD_STOP);
|
relay_chn_tilt_dispatch_cmd(&s_tilt_ctl, RELAY_CHN_TILT_CMD_STOP);
|
||||||
}
|
}
|
||||||
#endif // CONFIG_RELAY_CHN_COUNT > 1
|
#endif // CONFIG_RELAY_CHN_COUNT > 1
|
||||||
|
|
||||||
@@ -335,7 +335,7 @@ void relay_chn_tilt_set_sensitivity(uint8_t chn_id, uint8_t sensitivity)
|
|||||||
{
|
{
|
||||||
if (relay_chn_is_channel_id_valid(chn_id)) {
|
if (relay_chn_is_channel_id_valid(chn_id)) {
|
||||||
ADJUST_TILT_SENS_BOUNDARIES(sensitivity);
|
ADJUST_TILT_SENS_BOUNDARIES(sensitivity);
|
||||||
relay_chn_tilt_compute_set_sensitivity(&tilt_ctls[chn_id], sensitivity);
|
relay_chn_tilt_compute_set_sensitivity(&s_tilt_ctls[chn_id], sensitivity);
|
||||||
|
|
||||||
#if CONFIG_RELAY_CHN_ENABLE_NVS
|
#if CONFIG_RELAY_CHN_ENABLE_NVS
|
||||||
relay_chn_nvs_set_tilt_sensitivity(chn_id, sensitivity);
|
relay_chn_nvs_set_tilt_sensitivity(chn_id, sensitivity);
|
||||||
@@ -354,7 +354,7 @@ esp_err_t relay_chn_tilt_set_sensitivity_all(uint8_t *sensitivities)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ADJUST_TILT_SENS_BOUNDARIES(*src_sensitivity);
|
ADJUST_TILT_SENS_BOUNDARIES(*src_sensitivity);
|
||||||
relay_chn_tilt_compute_set_sensitivity(&tilt_ctls[i], *src_sensitivity);
|
relay_chn_tilt_compute_set_sensitivity(&s_tilt_ctls[i], *src_sensitivity);
|
||||||
#if CONFIG_RELAY_CHN_ENABLE_NVS
|
#if CONFIG_RELAY_CHN_ENABLE_NVS
|
||||||
relay_chn_nvs_set_tilt_sensitivity(i, *src_sensitivity);
|
relay_chn_nvs_set_tilt_sensitivity(i, *src_sensitivity);
|
||||||
#endif // CONFIG_RELAY_CHN_ENABLE_NVS
|
#endif // CONFIG_RELAY_CHN_ENABLE_NVS
|
||||||
@@ -366,7 +366,7 @@ void relay_chn_tilt_set_sensitivity_all_with(uint8_t sensitivity)
|
|||||||
{
|
{
|
||||||
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
||||||
ADJUST_TILT_SENS_BOUNDARIES(sensitivity);
|
ADJUST_TILT_SENS_BOUNDARIES(sensitivity);
|
||||||
relay_chn_tilt_compute_set_sensitivity(&tilt_ctls[i], sensitivity);
|
relay_chn_tilt_compute_set_sensitivity(&s_tilt_ctls[i], sensitivity);
|
||||||
#if CONFIG_RELAY_CHN_ENABLE_NVS
|
#if CONFIG_RELAY_CHN_ENABLE_NVS
|
||||||
relay_chn_nvs_set_tilt_sensitivity(i, sensitivity);
|
relay_chn_nvs_set_tilt_sensitivity(i, sensitivity);
|
||||||
#endif // CONFIG_RELAY_CHN_ENABLE_NVS
|
#endif // CONFIG_RELAY_CHN_ENABLE_NVS
|
||||||
@@ -376,7 +376,7 @@ void relay_chn_tilt_set_sensitivity_all_with(uint8_t sensitivity)
|
|||||||
uint8_t relay_chn_tilt_get_sensitivity(uint8_t chn_id)
|
uint8_t relay_chn_tilt_get_sensitivity(uint8_t chn_id)
|
||||||
{
|
{
|
||||||
return relay_chn_is_channel_id_valid(chn_id) ?
|
return relay_chn_is_channel_id_valid(chn_id) ?
|
||||||
tilt_ctls[chn_id].tilt_timing.sensitivity : 0;
|
s_tilt_ctls[chn_id].tilt_timing.sensitivity : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t relay_chn_tilt_get_sensitivity_all(uint8_t *sensitivities)
|
esp_err_t relay_chn_tilt_get_sensitivity_all(uint8_t *sensitivities)
|
||||||
@@ -389,7 +389,7 @@ esp_err_t relay_chn_tilt_get_sensitivity_all(uint8_t *sensitivities)
|
|||||||
ESP_LOGW(TAG, "get_sensitivity_all: Sensitivites have been copied until channel %d since sensitivities[%d] is NULL", i, i);
|
ESP_LOGW(TAG, "get_sensitivity_all: Sensitivites have been copied until channel %d since sensitivities[%d] is NULL", i, i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
*dest_sensitivity = tilt_ctls[i].tilt_timing.sensitivity;
|
*dest_sensitivity = s_tilt_ctls[i].tilt_timing.sensitivity;
|
||||||
}
|
}
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
@@ -399,7 +399,7 @@ esp_err_t relay_chn_tilt_get_sensitivity_all(uint8_t *sensitivities)
|
|||||||
void relay_chn_tilt_set_sensitivity(uint8_t sensitivity)
|
void relay_chn_tilt_set_sensitivity(uint8_t sensitivity)
|
||||||
{
|
{
|
||||||
ADJUST_TILT_SENS_BOUNDARIES(sensitivity);
|
ADJUST_TILT_SENS_BOUNDARIES(sensitivity);
|
||||||
relay_chn_tilt_compute_set_sensitivity(&tilt_ctl, sensitivity);
|
relay_chn_tilt_compute_set_sensitivity(&s_tilt_ctl, sensitivity);
|
||||||
|
|
||||||
#if CONFIG_RELAY_CHN_ENABLE_NVS
|
#if CONFIG_RELAY_CHN_ENABLE_NVS
|
||||||
relay_chn_nvs_set_tilt_sensitivity(0, sensitivity);
|
relay_chn_nvs_set_tilt_sensitivity(0, sensitivity);
|
||||||
@@ -408,7 +408,7 @@ void relay_chn_tilt_set_sensitivity(uint8_t sensitivity)
|
|||||||
|
|
||||||
uint8_t relay_chn_tilt_get_sensitivity()
|
uint8_t relay_chn_tilt_get_sensitivity()
|
||||||
{
|
{
|
||||||
return tilt_ctl.tilt_timing.sensitivity;
|
return s_tilt_ctl.tilt_timing.sensitivity;
|
||||||
}
|
}
|
||||||
#endif // CONFIG_RELAY_CHN_COUNT > 1
|
#endif // CONFIG_RELAY_CHN_COUNT > 1
|
||||||
|
|
||||||
@@ -707,7 +707,7 @@ esp_err_t relay_chn_tilt_init(relay_chn_ctl_t *chn_ctls)
|
|||||||
sensitivity = RELAY_CHN_TILT_DEFAULT_SENSITIVITY;
|
sensitivity = RELAY_CHN_TILT_DEFAULT_SENSITIVITY;
|
||||||
tilt_count = 0;
|
tilt_count = 0;
|
||||||
#endif // CONFIG_RELAY_CHN_ENABLE_NVS == 1
|
#endif // CONFIG_RELAY_CHN_ENABLE_NVS == 1
|
||||||
ret = relay_chn_tilt_ctl_init(&tilt_ctls[i], &chn_ctls[i], tilt_count, sensitivity);
|
ret = relay_chn_tilt_ctl_init(&s_tilt_ctls[i], &chn_ctls[i], tilt_count, sensitivity);
|
||||||
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to init tilt control for channel %d", i);
|
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to init tilt control for channel %d", i);
|
||||||
}
|
}
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
@@ -720,7 +720,7 @@ esp_err_t relay_chn_tilt_init(relay_chn_ctl_t *chn_ctls)
|
|||||||
ret = relay_chn_tilt_load_tilt_count(0, &tilt_count);
|
ret = relay_chn_tilt_load_tilt_count(0, &tilt_count);
|
||||||
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load tilt count for channel %d", 0);
|
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load tilt count for channel %d", 0);
|
||||||
#endif // CONFIG_RELAY_CHN_ENABLE_NVS == 1
|
#endif // CONFIG_RELAY_CHN_ENABLE_NVS == 1
|
||||||
return relay_chn_tilt_ctl_init(&tilt_ctl, chn_ctls, tilt_count, sensitivity);
|
return relay_chn_tilt_ctl_init(&s_tilt_ctl, chn_ctls, tilt_count, sensitivity);
|
||||||
#endif // CONFIG_RELAY_CHN_COUNT > 1
|
#endif // CONFIG_RELAY_CHN_COUNT > 1
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -742,9 +742,9 @@ void relay_chn_tilt_deinit()
|
|||||||
{
|
{
|
||||||
#if CONFIG_RELAY_CHN_COUNT > 1
|
#if CONFIG_RELAY_CHN_COUNT > 1
|
||||||
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
||||||
relay_chn_tilt_ctl_deinit(&tilt_ctls[i]);
|
relay_chn_tilt_ctl_deinit(&s_tilt_ctls[i]);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
relay_chn_tilt_ctl_deinit(&tilt_ctl);
|
relay_chn_tilt_ctl_deinit(&s_tilt_ctl);
|
||||||
#endif // CONFIG_RELAY_CHN_COUNT > 1
|
#endif // CONFIG_RELAY_CHN_COUNT > 1
|
||||||
}
|
}
|
||||||
@@ -6,20 +6,20 @@
|
|||||||
|
|
||||||
#include "test_common.h"
|
#include "test_common.h"
|
||||||
|
|
||||||
relay_chn_state_t states[CONFIG_RELAY_CHN_COUNT], expect_states[CONFIG_RELAY_CHN_COUNT];
|
static relay_chn_state_t s_states[CONFIG_RELAY_CHN_COUNT], s_expect_states[CONFIG_RELAY_CHN_COUNT];
|
||||||
relay_chn_direction_t directions[CONFIG_RELAY_CHN_COUNT], expect_directions[CONFIG_RELAY_CHN_COUNT];
|
static relay_chn_direction_t s_directions[CONFIG_RELAY_CHN_COUNT], s_expect_directions[CONFIG_RELAY_CHN_COUNT];
|
||||||
|
|
||||||
static void test_set_expected_state_all(relay_chn_state_t state)
|
static void test_set_expected_state_all(relay_chn_state_t state)
|
||||||
{
|
{
|
||||||
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
||||||
expect_states[i] = state;
|
s_expect_states[i] = state;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_set_expected_direction_all(relay_chn_direction_t direction)
|
static void test_set_expected_direction_all(relay_chn_direction_t direction)
|
||||||
{
|
{
|
||||||
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
||||||
expect_directions[i] = direction;
|
s_expect_directions[i] = direction;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,24 +231,24 @@ TEST_CASE("Forward to Reverse transition with opposite inertia", "[relay_chn][co
|
|||||||
// 1. Start in forward direction
|
// 1. Start in forward direction
|
||||||
relay_chn_run_forward_all();
|
relay_chn_run_forward_all();
|
||||||
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); // Short delay for state stabilization
|
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); // Short delay for state stabilization
|
||||||
TEST_ESP_OK(relay_chn_get_state_all(states));
|
TEST_ESP_OK(relay_chn_get_state_all(s_states));
|
||||||
test_set_expected_state_all(RELAY_CHN_STATE_FORWARD);
|
test_set_expected_state_all(RELAY_CHN_STATE_FORWARD);
|
||||||
TEST_ASSERT_EQUAL_UINT_ARRAY(expect_states, states, CONFIG_RELAY_CHN_COUNT);
|
TEST_ASSERT_EQUAL_UINT_ARRAY(s_expect_states, s_states, CONFIG_RELAY_CHN_COUNT);
|
||||||
|
|
||||||
// 2. Issue reverse command
|
// 2. Issue reverse command
|
||||||
relay_chn_run_reverse_all();
|
relay_chn_run_reverse_all();
|
||||||
// Immediately after the command, the motor should be stopped
|
// Immediately after the command, the motor should be stopped
|
||||||
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
|
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
|
||||||
TEST_ESP_OK(relay_chn_get_state_all(states));
|
TEST_ESP_OK(relay_chn_get_state_all(s_states));
|
||||||
test_set_expected_state_all(RELAY_CHN_STATE_REVERSE_PENDING);
|
test_set_expected_state_all(RELAY_CHN_STATE_REVERSE_PENDING);
|
||||||
TEST_ASSERT_EQUAL_UINT_ARRAY(expect_states, states, CONFIG_RELAY_CHN_COUNT);
|
TEST_ASSERT_EQUAL_UINT_ARRAY(s_expect_states, s_states, CONFIG_RELAY_CHN_COUNT);
|
||||||
|
|
||||||
// Wait for the inertia period (after which the reverse command will be dispatched)
|
// Wait for the inertia period (after which the reverse command will be dispatched)
|
||||||
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
|
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
|
||||||
// Should now be in reverse state
|
// Should now be in reverse state
|
||||||
TEST_ESP_OK(relay_chn_get_state_all(states));
|
TEST_ESP_OK(relay_chn_get_state_all(s_states));
|
||||||
test_set_expected_state_all(RELAY_CHN_STATE_REVERSE);
|
test_set_expected_state_all(RELAY_CHN_STATE_REVERSE);
|
||||||
TEST_ASSERT_EQUAL_UINT_ARRAY(expect_states, states, CONFIG_RELAY_CHN_COUNT);
|
TEST_ASSERT_EQUAL_UINT_ARRAY(s_expect_states, s_states, CONFIG_RELAY_CHN_COUNT);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TEST_CASE: Test transition from reverse to forward with inertia and state checks
|
// TEST_CASE: Test transition from reverse to forward with inertia and state checks
|
||||||
@@ -258,22 +258,22 @@ TEST_CASE("Reverse to Forward transition with opposite inertia", "[relay_chn][co
|
|||||||
// 1. Start in reverse direction
|
// 1. Start in reverse direction
|
||||||
relay_chn_run_reverse_all();
|
relay_chn_run_reverse_all();
|
||||||
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
|
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
|
||||||
TEST_ESP_OK(relay_chn_get_state_all(states));
|
TEST_ESP_OK(relay_chn_get_state_all(s_states));
|
||||||
test_set_expected_state_all(RELAY_CHN_STATE_REVERSE);
|
test_set_expected_state_all(RELAY_CHN_STATE_REVERSE);
|
||||||
TEST_ASSERT_EQUAL_UINT_ARRAY(expect_states, states, CONFIG_RELAY_CHN_COUNT);
|
TEST_ASSERT_EQUAL_UINT_ARRAY(s_expect_states, s_states, CONFIG_RELAY_CHN_COUNT);
|
||||||
|
|
||||||
// 2. Issue forward command
|
// 2. Issue forward command
|
||||||
relay_chn_run_forward_all();
|
relay_chn_run_forward_all();
|
||||||
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
|
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
|
||||||
TEST_ESP_OK(relay_chn_get_state_all(states));
|
TEST_ESP_OK(relay_chn_get_state_all(s_states));
|
||||||
test_set_expected_state_all(RELAY_CHN_STATE_FORWARD_PENDING);
|
test_set_expected_state_all(RELAY_CHN_STATE_FORWARD_PENDING);
|
||||||
TEST_ASSERT_EQUAL_UINT_ARRAY(expect_states, states, CONFIG_RELAY_CHN_COUNT);
|
TEST_ASSERT_EQUAL_UINT_ARRAY(s_expect_states, s_states, CONFIG_RELAY_CHN_COUNT);
|
||||||
|
|
||||||
// Wait for inertia
|
// Wait for inertia
|
||||||
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
|
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
|
||||||
TEST_ESP_OK(relay_chn_get_state_all(states));
|
TEST_ESP_OK(relay_chn_get_state_all(s_states));
|
||||||
test_set_expected_state_all(RELAY_CHN_STATE_FORWARD);
|
test_set_expected_state_all(RELAY_CHN_STATE_FORWARD);
|
||||||
TEST_ASSERT_EQUAL_UINT_ARRAY(expect_states, states, CONFIG_RELAY_CHN_COUNT);
|
TEST_ASSERT_EQUAL_UINT_ARRAY(s_expect_states, s_states, CONFIG_RELAY_CHN_COUNT);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TEST_CASE: Test issuing the same run command while already running (no inertia expected)
|
// TEST_CASE: Test issuing the same run command while already running (no inertia expected)
|
||||||
@@ -283,18 +283,18 @@ TEST_CASE("Running in same direction does not incur inertia", "[relay_chn][core]
|
|||||||
// 1. Start in forward direction
|
// 1. Start in forward direction
|
||||||
relay_chn_run_forward_all();
|
relay_chn_run_forward_all();
|
||||||
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
|
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
|
||||||
TEST_ESP_OK(relay_chn_get_state_all(states));
|
TEST_ESP_OK(relay_chn_get_state_all(s_states));
|
||||||
test_set_expected_state_all(RELAY_CHN_STATE_FORWARD);
|
test_set_expected_state_all(RELAY_CHN_STATE_FORWARD);
|
||||||
TEST_ASSERT_EQUAL_UINT_ARRAY(expect_states, states, CONFIG_RELAY_CHN_COUNT);
|
TEST_ASSERT_EQUAL_UINT_ARRAY(s_expect_states, s_states, CONFIG_RELAY_CHN_COUNT);
|
||||||
|
|
||||||
// 2. Issue the same forward command again
|
// 2. Issue the same forward command again
|
||||||
relay_chn_run_forward_all();
|
relay_chn_run_forward_all();
|
||||||
// As per the code, is_direction_opposite_to_current_motion should return false, so no inertia.
|
// 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.
|
// Just a short delay to check state remains the same.
|
||||||
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
|
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
|
||||||
TEST_ESP_OK(relay_chn_get_state_all(states));
|
TEST_ESP_OK(relay_chn_get_state_all(s_states));
|
||||||
test_set_expected_state_all(RELAY_CHN_STATE_FORWARD);
|
test_set_expected_state_all(RELAY_CHN_STATE_FORWARD);
|
||||||
TEST_ASSERT_EQUAL_UINT_ARRAY(expect_states, states, CONFIG_RELAY_CHN_COUNT);
|
TEST_ASSERT_EQUAL_UINT_ARRAY(s_expect_states, s_states, CONFIG_RELAY_CHN_COUNT);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ### Direction Flipping Tests
|
// ### Direction Flipping Tests
|
||||||
@@ -336,44 +336,44 @@ TEST_CASE("All channels direction can be flipped simultaneously", "[relay_chn][c
|
|||||||
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
|
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
|
||||||
|
|
||||||
// 2. Verify all channels are flipped
|
// 2. Verify all channels are flipped
|
||||||
TEST_ESP_OK(relay_chn_get_direction_all(directions));
|
TEST_ESP_OK(relay_chn_get_direction_all(s_directions));
|
||||||
test_set_expected_direction_all(RELAY_CHN_DIRECTION_FLIPPED);
|
test_set_expected_direction_all(RELAY_CHN_DIRECTION_FLIPPED);
|
||||||
TEST_ASSERT_EQUAL_UINT_ARRAY(expect_directions, directions, CONFIG_RELAY_CHN_COUNT);
|
TEST_ASSERT_EQUAL_UINT_ARRAY(s_expect_directions, s_directions, CONFIG_RELAY_CHN_COUNT);
|
||||||
|
|
||||||
// 3. Flip all back
|
// 3. Flip all back
|
||||||
relay_chn_flip_direction_all();
|
relay_chn_flip_direction_all();
|
||||||
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
|
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
|
||||||
|
|
||||||
// 4. Verify all channels are back to default
|
// 4. Verify all channels are back to default
|
||||||
TEST_ESP_OK(relay_chn_get_direction_all(directions));
|
TEST_ESP_OK(relay_chn_get_direction_all(s_directions));
|
||||||
test_set_expected_direction_all(RELAY_CHN_DIRECTION_DEFAULT);
|
test_set_expected_direction_all(RELAY_CHN_DIRECTION_DEFAULT);
|
||||||
TEST_ASSERT_EQUAL_UINT_ARRAY(expect_directions, directions, CONFIG_RELAY_CHN_COUNT);
|
TEST_ASSERT_EQUAL_UINT_ARRAY(s_expect_directions, s_directions, CONFIG_RELAY_CHN_COUNT);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Flipping a running channel stops it and flips direction", "[relay_chn][core][direction]")
|
TEST_CASE("Flipping a running channel stops it and flips direction", "[relay_chn][core][direction]")
|
||||||
{
|
{
|
||||||
// 1. Start channel running and verify state
|
// 1. Start channel running and verify state
|
||||||
relay_chn_run_forward_all();
|
relay_chn_run_forward_all();
|
||||||
TEST_ESP_OK(relay_chn_get_state_all(states));
|
TEST_ESP_OK(relay_chn_get_state_all(s_states));
|
||||||
test_set_expected_state_all(RELAY_CHN_STATE_FORWARD);
|
test_set_expected_state_all(RELAY_CHN_STATE_FORWARD);
|
||||||
TEST_ASSERT_EQUAL_UINT_ARRAY(expect_states, states, CONFIG_RELAY_CHN_COUNT);
|
TEST_ASSERT_EQUAL_UINT_ARRAY(s_expect_states, s_states, CONFIG_RELAY_CHN_COUNT);
|
||||||
// 2. Flip the direction while running
|
// 2. Flip the direction while running
|
||||||
relay_chn_flip_direction_all();
|
relay_chn_flip_direction_all();
|
||||||
// 3. The channel should stop as part of the flip process
|
// 3. The channel should stop as part of the flip process
|
||||||
TEST_ESP_OK(relay_chn_get_state_all(states));
|
TEST_ESP_OK(relay_chn_get_state_all(s_states));
|
||||||
test_set_expected_state_all(RELAY_CHN_STATE_STOPPED);
|
test_set_expected_state_all(RELAY_CHN_STATE_STOPPED);
|
||||||
TEST_ASSERT_EQUAL_UINT_ARRAY(expect_states, states, CONFIG_RELAY_CHN_COUNT);
|
TEST_ASSERT_EQUAL_UINT_ARRAY(s_expect_states, s_states, CONFIG_RELAY_CHN_COUNT);
|
||||||
|
|
||||||
// 4. Wait for the flip inertia to pass, after which it should be idle and FLIPPED
|
// 4. Wait for the flip inertia to pass, after which it should be idle and FLIPPED
|
||||||
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
|
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
|
||||||
|
|
||||||
TEST_ESP_OK(relay_chn_get_state_all(states));
|
TEST_ESP_OK(relay_chn_get_state_all(s_states));
|
||||||
test_set_expected_state_all(RELAY_CHN_STATE_IDLE);
|
test_set_expected_state_all(RELAY_CHN_STATE_IDLE);
|
||||||
TEST_ASSERT_EQUAL_UINT_ARRAY(expect_states, states, CONFIG_RELAY_CHN_COUNT);
|
TEST_ASSERT_EQUAL_UINT_ARRAY(s_expect_states, s_states, CONFIG_RELAY_CHN_COUNT);
|
||||||
|
|
||||||
TEST_ESP_OK(relay_chn_get_direction_all(directions));
|
TEST_ESP_OK(relay_chn_get_direction_all(s_directions));
|
||||||
test_set_expected_direction_all(RELAY_CHN_DIRECTION_FLIPPED);
|
test_set_expected_direction_all(RELAY_CHN_DIRECTION_FLIPPED);
|
||||||
TEST_ASSERT_EQUAL_UINT_ARRAY(expect_directions, directions, CONFIG_RELAY_CHN_COUNT);
|
TEST_ASSERT_EQUAL_UINT_ARRAY(s_expect_directions, s_directions, CONFIG_RELAY_CHN_COUNT);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Direction flip handles invalid channel ID gracefully", "[relay_chn][core][direction]")
|
TEST_CASE("Direction flip handles invalid channel ID gracefully", "[relay_chn][core][direction]")
|
||||||
@@ -387,7 +387,7 @@ TEST_CASE("Direction flip handles invalid channel ID gracefully", "[relay_chn][c
|
|||||||
TEST_CASE("get_state_all retrieves all channel states", "[relay_chn][core][batch]")
|
TEST_CASE("get_state_all retrieves all channel states", "[relay_chn][core][batch]")
|
||||||
{
|
{
|
||||||
// 1. All should be IDLE initially
|
// 1. All should be IDLE initially
|
||||||
TEST_ESP_OK(relay_chn_get_state_all(states));
|
TEST_ESP_OK(relay_chn_get_state_all(s_states));
|
||||||
test_set_expected_state_all(RELAY_CHN_STATE_IDLE);
|
test_set_expected_state_all(RELAY_CHN_STATE_IDLE);
|
||||||
|
|
||||||
// 2. Set some states
|
// 2. Set some states
|
||||||
@@ -403,30 +403,30 @@ TEST_CASE("get_state_all retrieves all channel states", "[relay_chn][core][batch
|
|||||||
// 3. Get all states and verify
|
// 3. Get all states and verify
|
||||||
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
||||||
if (i % 2 == 0) {
|
if (i % 2 == 0) {
|
||||||
expect_states[i] = RELAY_CHN_STATE_FORWARD;
|
s_expect_states[i] = RELAY_CHN_STATE_FORWARD;
|
||||||
} else {
|
} else {
|
||||||
expect_states[i] = RELAY_CHN_STATE_REVERSE;
|
s_expect_states[i] = RELAY_CHN_STATE_REVERSE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TEST_ESP_OK(relay_chn_get_state_all(states));
|
TEST_ESP_OK(relay_chn_get_state_all(s_states));
|
||||||
TEST_ASSERT_EQUAL_UINT_ARRAY(expect_states, states, CONFIG_RELAY_CHN_COUNT);
|
TEST_ASSERT_EQUAL_UINT_ARRAY(s_expect_states, s_states, CONFIG_RELAY_CHN_COUNT);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("get_direction_all retrieves all channel directions", "[relay_chn][core][direction][batch]")
|
TEST_CASE("get_direction_all retrieves all channel directions", "[relay_chn][core][direction][batch]")
|
||||||
{
|
{
|
||||||
// 1. All should be default initially
|
// 1. All should be default initially
|
||||||
TEST_ESP_OK(relay_chn_get_direction_all(directions));
|
TEST_ESP_OK(relay_chn_get_direction_all(s_directions));
|
||||||
test_set_expected_direction_all(RELAY_CHN_DIRECTION_DEFAULT);
|
test_set_expected_direction_all(RELAY_CHN_DIRECTION_DEFAULT);
|
||||||
TEST_ASSERT_EQUAL_UINT_ARRAY(expect_directions, directions, CONFIG_RELAY_CHN_COUNT);
|
TEST_ASSERT_EQUAL_UINT_ARRAY(s_expect_directions, s_directions, CONFIG_RELAY_CHN_COUNT);
|
||||||
|
|
||||||
// 2. Flip all
|
// 2. Flip all
|
||||||
relay_chn_flip_direction_all();
|
relay_chn_flip_direction_all();
|
||||||
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
|
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
|
||||||
|
|
||||||
// 3. Get all directions and verify
|
// 3. Get all directions and verify
|
||||||
TEST_ESP_OK(relay_chn_get_direction_all(directions));
|
TEST_ESP_OK(relay_chn_get_direction_all(s_directions));
|
||||||
test_set_expected_direction_all(RELAY_CHN_DIRECTION_FLIPPED);
|
test_set_expected_direction_all(RELAY_CHN_DIRECTION_FLIPPED);
|
||||||
TEST_ASSERT_EQUAL_UINT_ARRAY(expect_directions, directions, CONFIG_RELAY_CHN_COUNT);
|
TEST_ASSERT_EQUAL_UINT_ARRAY(s_expect_directions, s_directions, CONFIG_RELAY_CHN_COUNT);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("get_all functions handle NULL arguments", "[relay_chn][core][batch]")
|
TEST_CASE("get_all functions handle NULL arguments", "[relay_chn][core][batch]")
|
||||||
|
|||||||
Reference in New Issue
Block a user