Add NVS support for relay channel config persistence
- Introduced NVS configuration options in Kconfig. - Implemented NVS initialization and deinitialization in relay_chn_core. - Added functions for storing and retrieving relay channel direction and tilt sensitivity in NVS. - Updated relay_chn_tilt and relay_chn_output to utilize NVS for state management. - Created relay_chn_nvs.c and relay_chn_nvs.h for NVS-related functionalities. Closes #1074.
This commit is contained in:
@@ -10,6 +10,12 @@
|
||||
#include "relay_chn_run_info.h"
|
||||
#include "relay_chn_tilt.h"
|
||||
|
||||
#if RELAY_CHN_ENABLE_NVS == 1
|
||||
#include "relay_chn_nvs.h"
|
||||
|
||||
#define RELAY_CHN_TILT_FLUSH_DEBOUNCE_MS 3000
|
||||
#endif
|
||||
|
||||
|
||||
static const char *TAG = "RELAY_CHN_TILT";
|
||||
|
||||
@@ -65,6 +71,9 @@ typedef struct relay_chn_tilt_ctl {
|
||||
relay_chn_tilt_timing_t tilt_timing; /*!< Tilt timing structure */
|
||||
relay_chn_tilt_counter_t tilt_counter; /*!< Tilt counter structure */
|
||||
esp_timer_handle_t tilt_timer; /*!< Tilt timer handle */
|
||||
#if RELAY_CHN_ENABLE_NVS == 1
|
||||
esp_timer_handle_t flush_timer; /*!< Flush timer to avoid frequent write of tilt counters */
|
||||
#endif
|
||||
} relay_chn_tilt_ctl_t;
|
||||
|
||||
|
||||
@@ -295,7 +304,7 @@ static void relay_chn_tilt_set_timing_values(relay_chn_tilt_timing_t *tilt_timin
|
||||
tilt_timing->pause_time_ms = pause_time_ms;
|
||||
}
|
||||
|
||||
static void _relay_chn_tilt_sensitivity_set(relay_chn_tilt_ctl_t *tilt_ctl, uint8_t sensitivity)
|
||||
static void relay_chn_tilt_compute_set_sensitivity(relay_chn_tilt_ctl_t *tilt_ctl, uint8_t sensitivity)
|
||||
{
|
||||
if (sensitivity >= 100) {
|
||||
relay_chn_tilt_set_timing_values(&tilt_ctl->tilt_timing,
|
||||
@@ -309,6 +318,12 @@ static void _relay_chn_tilt_sensitivity_set(relay_chn_tilt_ctl_t *tilt_ctl, uint
|
||||
RELAY_CHN_TILT_RUN_MIN_MS,
|
||||
RELAY_CHN_TILT_PAUSE_MIN_MS);
|
||||
}
|
||||
else if (sensitivity == RELAY_CHN_TILT_DEFAULT_SENSITIVITY) {
|
||||
relay_chn_tilt_set_timing_values(&tilt_ctl->tilt_timing,
|
||||
sensitivity,
|
||||
RELAY_CHN_TILT_DEFAULT_RUN_MS,
|
||||
RELAY_CHN_TILT_DEFAULT_PAUSE_MS);
|
||||
}
|
||||
else {
|
||||
// Compute the new timing values from the sensitivity percent value by using linear interpolation
|
||||
uint32_t tilt_run_time_ms = 0, tilt_pause_time_ms = 0;
|
||||
@@ -331,12 +346,16 @@ void relay_chn_tilt_set_sensitivity(uint8_t chn_id, uint8_t sensitivity)
|
||||
|
||||
if (chn_id == RELAY_CHN_ID_ALL) {
|
||||
for (int i = 0; i < RELAY_CHN_COUNT; i++) {
|
||||
_relay_chn_tilt_sensitivity_set(&tilt_ctls[i], sensitivity);
|
||||
relay_chn_tilt_compute_set_sensitivity(&tilt_ctls[i], sensitivity);
|
||||
}
|
||||
}
|
||||
else {
|
||||
_relay_chn_tilt_sensitivity_set(&tilt_ctls[chn_id], sensitivity);
|
||||
relay_chn_tilt_compute_set_sensitivity(&tilt_ctls[chn_id], sensitivity);
|
||||
}
|
||||
|
||||
#if RELAY_CHN_ENABLE_NVS == 1
|
||||
relay_chn_nvs_set_tilt_sensitivity(chn_id, sensitivity);
|
||||
#endif // RELAY_CHN_ENABLE_NVS
|
||||
}
|
||||
|
||||
esp_err_t relay_chn_tilt_get_sensitivity(uint8_t chn_id, uint8_t *sensitivity, size_t length)
|
||||
@@ -367,7 +386,11 @@ esp_err_t relay_chn_tilt_get_sensitivity(uint8_t chn_id, uint8_t *sensitivity, s
|
||||
|
||||
void relay_chn_tilt_set_sensitivity(uint8_t sensitivity)
|
||||
{
|
||||
_relay_chn_tilt_sensitivity_set(&tilt_ctl, sensitivity);
|
||||
relay_chn_tilt_compute_set_sensitivity(&tilt_ctl, sensitivity);
|
||||
|
||||
#if RELAY_CHN_ENABLE_NVS == 1
|
||||
relay_chn_nvs_set_tilt_sensitivity(0, sensitivity);
|
||||
#endif // RELAY_CHN_ENABLE_NVS
|
||||
}
|
||||
|
||||
uint8_t relay_chn_tilt_get_sensitivity()
|
||||
@@ -380,6 +403,10 @@ void relay_chn_tilt_reset_count(relay_chn_tilt_ctl_t *tilt_ctl)
|
||||
{
|
||||
tilt_ctl->tilt_counter.tilt_forward_count = 0;
|
||||
tilt_ctl->tilt_counter.tilt_reverse_count = 0;
|
||||
|
||||
#if RELAY_CHN_ENABLE_NVS == 1
|
||||
esp_timer_stop(tilt_ctl->flush_timer);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -449,6 +476,31 @@ static uint32_t relay_chn_tilt_count_update(relay_chn_tilt_ctl_t *tilt_ctl)
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if RELAY_CHN_ENABLE_NVS == 1
|
||||
static esp_err_t relay_chn_tilt_save_tilt_counter(relay_chn_tilt_ctl_t *tilt_ctl)
|
||||
{
|
||||
// Save the tilt count to NVS storage
|
||||
esp_err_t ret = relay_chn_nvs_set_tilt_count(tilt_ctl->chn_ctl->id,
|
||||
tilt_ctl->tilt_counter.tilt_forward_count,
|
||||
tilt_ctl->tilt_counter.tilt_reverse_count);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "relay_chn_tilt_execute_stop: Failed to save tilt count for channel #%d: %s", tilt_ctl->chn_ctl->id, esp_err_to_name(ret));
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void relay_chn_tilt_flush_timer_cb(void *arg)
|
||||
{
|
||||
relay_chn_tilt_ctl_t* tilt_ctl = (relay_chn_tilt_ctl_t*) arg;
|
||||
ESP_RETURN_VOID_ON_FALSE(tilt_ctl != NULL, TAG, "relay_chn_tilt_flush_timer_cb: timer arg is NULL");
|
||||
// Save the tilt count to storage
|
||||
esp_err_t ret = relay_chn_tilt_save_tilt_counter(tilt_ctl);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "relay_chn_tilt_execute_stop: Failed to save tilt count for channel #%d: %s", tilt_ctl->chn_ctl->id, esp_err_to_name(ret));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void relay_chn_tilt_execute_stop(relay_chn_tilt_ctl_t *tilt_ctl)
|
||||
{
|
||||
// Stop the channel's timer if active
|
||||
@@ -461,6 +513,11 @@ static void relay_chn_tilt_execute_stop(relay_chn_tilt_ctl_t *tilt_ctl)
|
||||
ESP_LOGE(TAG, "relay_chn_tilt_execute_stop: Failed to output stop for relay channel #%d!", tilt_ctl->chn_ctl->id);
|
||||
}
|
||||
relay_chn_dispatch_cmd(tilt_ctl->chn_ctl, RELAY_CHN_CMD_STOP);
|
||||
|
||||
#if RELAY_CHN_ENABLE_NVS == 1
|
||||
// Start the flush debounce timer
|
||||
relay_chn_start_esp_timer_once(tilt_ctl->flush_timer, RELAY_CHN_TILT_FLUSH_DEBOUNCE_MS);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void relay_chn_tilt_execute_forward(relay_chn_tilt_ctl_t *tilt_ctl)
|
||||
@@ -544,7 +601,7 @@ static void relay_chn_tilt_event_handler(void *handler_arg, esp_event_base_t eve
|
||||
static void relay_chn_tilt_timer_cb(void *arg)
|
||||
{
|
||||
relay_chn_tilt_ctl_t* tilt_ctl = (relay_chn_tilt_ctl_t*) arg;
|
||||
ESP_RETURN_VOID_ON_FALSE(tilt_ctl != NULL, TAG, "relay_chn_tilt_timer_cb: event_data is NULL");
|
||||
ESP_RETURN_VOID_ON_FALSE(tilt_ctl != NULL, TAG, "relay_chn_tilt_timer_cb: timer arg is NULL");
|
||||
|
||||
switch (tilt_ctl->step)
|
||||
{
|
||||
@@ -571,17 +628,44 @@ static void relay_chn_tilt_timer_cb(void *arg)
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t relay_chn_tilt_ctl_init(relay_chn_tilt_ctl_t *tilt_ctl, relay_chn_ctl_t *chn_ctl)
|
||||
#if RELAY_CHN_ENABLE_NVS == 1
|
||||
static esp_err_t relay_chn_tilt_load_sensitivity(uint8_t ch, uint8_t *sensitivity)
|
||||
{
|
||||
esp_err_t ret = relay_chn_nvs_get_tilt_sensitivity(ch, sensitivity);
|
||||
if (ret == ESP_ERR_NVS_NOT_FOUND) {
|
||||
*sensitivity = RELAY_CHN_TILT_DEFAULT_SENSITIVITY;
|
||||
return ESP_OK;
|
||||
}
|
||||
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load tilt sensitivity for channel %d", ch);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t relay_chn_tilt_load_tilt_counter(uint8_t ch, relay_chn_tilt_counter_t *tilt_counter)
|
||||
{
|
||||
esp_err_t ret = relay_chn_nvs_get_tilt_count(ch, &tilt_counter->tilt_forward_count, &tilt_counter->tilt_reverse_count);
|
||||
if (ret == ESP_ERR_NVS_NOT_FOUND) {
|
||||
ESP_LOGD(TAG, "relay_chn_tilt_load_tilt_counter: No tilt counters found in NVS for channel %d, initializing to zero", ch);
|
||||
tilt_counter->tilt_forward_count = 0;
|
||||
tilt_counter->tilt_reverse_count = 0;
|
||||
return ESP_OK;
|
||||
}
|
||||
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load tilt counters for channel %d", ch);
|
||||
return ESP_OK;
|
||||
}
|
||||
#endif // RELAY_CHN_ENABLE_NVS
|
||||
|
||||
static esp_err_t relay_chn_tilt_ctl_init(relay_chn_tilt_ctl_t *tilt_ctl, relay_chn_ctl_t *chn_ctl,
|
||||
relay_chn_tilt_counter_t *tilt_counter, uint8_t sensitivity)
|
||||
{
|
||||
tilt_ctl->cmd = RELAY_CHN_TILT_CMD_NONE;
|
||||
tilt_ctl->step = RELAY_CHN_TILT_STEP_NONE;
|
||||
tilt_ctl->tilt_timing.sensitivity = RELAY_CHN_TILT_DEFAULT_SENSITIVITY;
|
||||
tilt_ctl->tilt_timing.move_time_ms = RELAY_CHN_TILT_DEFAULT_RUN_MS;
|
||||
tilt_ctl->tilt_timing.pause_time_ms = RELAY_CHN_TILT_DEFAULT_PAUSE_MS;
|
||||
relay_chn_tilt_reset_count(tilt_ctl);
|
||||
relay_chn_tilt_compute_set_sensitivity(tilt_ctl, sensitivity);
|
||||
// Init tilt counters
|
||||
tilt_ctl->tilt_counter.tilt_forward_count = tilt_counter->tilt_forward_count;
|
||||
tilt_ctl->tilt_counter.tilt_reverse_count = tilt_counter->tilt_reverse_count;
|
||||
|
||||
tilt_ctl->chn_ctl = chn_ctl;
|
||||
tilt_ctl->chn_ctl->tilt_ctl = tilt_ctl; //
|
||||
tilt_ctl->chn_ctl->tilt_ctl = tilt_ctl;
|
||||
|
||||
// Create tilt timer for the channel
|
||||
char timer_name[32];
|
||||
@@ -591,17 +675,50 @@ esp_err_t relay_chn_tilt_ctl_init(relay_chn_tilt_ctl_t *tilt_ctl, relay_chn_ctl_
|
||||
.arg = tilt_ctl,
|
||||
.name = timer_name
|
||||
};
|
||||
return esp_timer_create(&timer_args, &tilt_ctl->tilt_timer);
|
||||
esp_err_t ret = esp_timer_create(&timer_args, &tilt_ctl->tilt_timer);
|
||||
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to create tilt timer for channel %d", chn_ctl->id);
|
||||
|
||||
#if RELAY_CHN_ENABLE_NVS == 1
|
||||
// Create flush timer for the tilt counters
|
||||
snprintf(timer_name, sizeof(timer_name), "relay_chn_%2d_tilt_flush_timer", chn_ctl->id);
|
||||
timer_args.callback = relay_chn_tilt_flush_timer_cb;
|
||||
timer_args.name = timer_name;
|
||||
ret = esp_timer_create(&timer_args, &tilt_ctl->flush_timer);
|
||||
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to create tilt flush timer for channel %d", chn_ctl->id);
|
||||
#endif
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t relay_chn_tilt_init(relay_chn_ctl_t *chn_ctls)
|
||||
{
|
||||
uint8_t sensitivity;
|
||||
relay_chn_tilt_counter_t tilt_counter;
|
||||
|
||||
#if RELAY_CHN_COUNT > 1
|
||||
for (int i = 0; i < RELAY_CHN_COUNT; i++) {
|
||||
relay_chn_tilt_ctl_init(&tilt_ctls[i], &chn_ctls[i]);
|
||||
}
|
||||
#if RELAY_CHN_ENABLE_NVS == 1
|
||||
esp_err_t ret = relay_chn_tilt_load_sensitivity(i, &sensitivity);
|
||||
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load tilt sensitivity for channel %d", i);
|
||||
ret = relay_chn_tilt_load_tilt_counter(i, &tilt_counter);
|
||||
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load tilt counters for channel %d", i);
|
||||
#else
|
||||
relay_chn_tilt_ctl_init(&tilt_ctl, chn_ctls);
|
||||
sensitivity = RELAY_CHN_TILT_DEFAULT_SENSITIVITY;
|
||||
tilt_counter.tilt_forward_count = 0;
|
||||
tilt_counter.tilt_reverse_count = 0;
|
||||
#endif // RELAY_CHN_ENABLE_NVS == 1
|
||||
relay_chn_tilt_ctl_init(&tilt_ctls[i], &chn_ctls[i], &tilt_counter, sensitivity);
|
||||
}
|
||||
#else
|
||||
sensitivity = RELAY_CHN_TILT_DEFAULT_SENSITIVITY;
|
||||
tilt_counter.tilt_forward_count = 0;
|
||||
tilt_counter.tilt_reverse_count = 0;
|
||||
#if RELAY_CHN_ENABLE_NVS == 1
|
||||
esp_err_t ret = relay_chn_tilt_load_sensitivity(0, &sensitivity);
|
||||
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load tilt sensitivity for channel %d", 0);
|
||||
ret = relay_chn_tilt_load_tilt_counter(0, &tilt_counter);
|
||||
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to load tilt counters for channel %d", 0);
|
||||
#endif // RELAY_CHN_ENABLE_NVS == 1
|
||||
relay_chn_tilt_ctl_init(&tilt_ctl, chn_ctls, &tilt_counter, sensitivity);
|
||||
#endif // RELAY_CHN_COUNT > 1
|
||||
|
||||
return esp_event_handler_register_with(relay_chn_event_loop,
|
||||
@@ -616,6 +733,12 @@ void relay_chn_tilt_ctl_deinit(relay_chn_tilt_ctl_t *tilt_ctl)
|
||||
esp_timer_delete(tilt_ctl->tilt_timer);
|
||||
tilt_ctl->tilt_timer = NULL;
|
||||
}
|
||||
#if RELAY_CHN_ENABLE_NVS == 1
|
||||
if (tilt_ctl->flush_timer != NULL) {
|
||||
esp_timer_delete(tilt_ctl->flush_timer);
|
||||
tilt_ctl->flush_timer = NULL;
|
||||
}
|
||||
#endif // RELAY_CHN_ENABLE_NVS == 1
|
||||
}
|
||||
|
||||
void relay_chn_tilt_deinit()
|
||||
|
||||
Reference in New Issue
Block a user