Unnecessary `#if CONFIG_FOO == 1` checks were removed and the statements were simplified to` #if CONFIG_FOO`. `#ifdef CONFIG_FOO` statements were also changed to `#if CONFIG_FOO` to keep the style as uniform as possible. Refs #1085
137 lines
5.0 KiB
C
137 lines
5.0 KiB
C
/*
|
|
* SPDX-FileCopyrightText: 2025 Kozmotronik Tech
|
|
*
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
#include "esp_check.h"
|
|
#include "relay_chn_nvs.h"
|
|
|
|
#define RELAY_CHN_KEY_DIR "dir" /*!< Direction key */
|
|
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
|
|
#define RELAY_CHN_KEY_RLIM(ch) "rlim_%d" /*!< Run limit key */
|
|
#endif
|
|
#if CONFIG_RELAY_CHN_ENABLE_TILTING
|
|
#define RELAY_CHN_KEY_TSENS(ch) "tsens_%d" /*!< Tilt sensitivity key */
|
|
#define RELAY_CHN_KEY_TCNT(ch) "tcnt_%d" /*!< Tilt count key */
|
|
#endif
|
|
|
|
static const char *TAG = "RELAY_CHN_NVS";
|
|
|
|
static nvs_handle_t relay_chn_nvs;
|
|
|
|
esp_err_t relay_chn_nvs_init()
|
|
{
|
|
esp_err_t ret;
|
|
#if CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION
|
|
ret = nvs_open_from_partition(CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION_NAME,
|
|
CONFIG_RELAY_CHN_NVS_NAMESPACE,
|
|
NVS_READWRITE,
|
|
&relay_chn_nvs);
|
|
|
|
ESP_RETURN_ON_ERROR(ret,
|
|
TAG,
|
|
"Failed to open NVS namespace '%s' from partition '%s' with error %s",
|
|
CONFIG_RELAY_CHN_NVS_NAMESPACE,
|
|
CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION_NAME,
|
|
esp_err_to_name(ret));
|
|
#else
|
|
ret = nvs_open(CONFIG_RELAY_CHN_NVS_NAMESPACE, NVS_READWRITE, &relay_chn_nvs);
|
|
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to open NVS namespace '%s'", CONFIG_RELAY_CHN_NVS_NAMESPACE);
|
|
#endif // CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION
|
|
return ESP_OK;
|
|
}
|
|
|
|
esp_err_t relay_chn_nvs_set_direction(uint8_t ch, relay_chn_direction_t direction)
|
|
{
|
|
uint8_t direction_val;
|
|
esp_err_t ret = nvs_get_u8(relay_chn_nvs, RELAY_CHN_KEY_DIR, &direction_val);
|
|
if (ret == ESP_ERR_NVS_NOT_FOUND) {
|
|
// The key does not exist yet, set it to zero which is the default direction
|
|
direction_val = RELAY_CHN_DIRECTION_DEFAULT;
|
|
} else if (ret != ESP_OK) {
|
|
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 |= (((uint8_t) direction) << ch); // Set the new direction bit
|
|
ret = nvs_set_u8(relay_chn_nvs, RELAY_CHN_KEY_DIR, direction_val);
|
|
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to set direction for channel %d", ch);
|
|
return nvs_commit(relay_chn_nvs);
|
|
}
|
|
|
|
esp_err_t relay_chn_nvs_get_direction(uint8_t ch, relay_chn_direction_t *direction)
|
|
{
|
|
ESP_RETURN_ON_FALSE(direction != NULL, ESP_ERR_INVALID_ARG, TAG, "Direction pointer is NULL");
|
|
|
|
uint8_t direction_val;
|
|
esp_err_t ret = nvs_get_u8(relay_chn_nvs, RELAY_CHN_KEY_DIR, &direction_val);
|
|
if (ret != ESP_OK) {
|
|
return ret; // Return error if the key does not exist
|
|
}
|
|
*direction = (relay_chn_direction_t)((direction_val >> ch) & 0x01);
|
|
return ESP_OK;
|
|
}
|
|
|
|
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
|
|
esp_err_t relay_chn_nvs_set_run_limit(uint8_t ch, uint16_t time_sec)
|
|
{
|
|
esp_err_t ret = nvs_set_u16(relay_chn_nvs, RELAY_CHN_KEY_RLIM(ch), time_sec);
|
|
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to set run limit for channel %d", ch);
|
|
return nvs_commit(relay_chn_nvs);
|
|
}
|
|
|
|
esp_err_t relay_chn_nvs_get_run_limit(uint8_t ch, uint16_t *time_sec)
|
|
{
|
|
ESP_RETURN_ON_FALSE(time_sec != NULL, ESP_ERR_INVALID_ARG, TAG, "Run limit value pointer is NULL");
|
|
|
|
return nvs_get_u16(relay_chn_nvs, RELAY_CHN_KEY_RLIM(ch), time_sec);
|
|
}
|
|
#endif // CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT == 1
|
|
|
|
#if CONFIG_RELAY_CHN_ENABLE_TILTING
|
|
esp_err_t relay_chn_nvs_set_tilt_sensitivity(uint8_t ch, uint8_t sensitivity)
|
|
{
|
|
esp_err_t ret = nvs_set_u8(relay_chn_nvs, RELAY_CHN_KEY_TSENS(ch), sensitivity);
|
|
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to set tilt sensitivity for channel %d", ch);
|
|
return nvs_commit(relay_chn_nvs);
|
|
}
|
|
|
|
esp_err_t relay_chn_nvs_get_tilt_sensitivity(uint8_t ch, uint8_t *sensitivity)
|
|
{
|
|
ESP_RETURN_ON_FALSE(sensitivity != NULL, ESP_ERR_INVALID_ARG, TAG, "Sensitivity pointer is NULL");
|
|
|
|
return nvs_get_u8(relay_chn_nvs, RELAY_CHN_KEY_TSENS(ch), sensitivity);
|
|
}
|
|
|
|
esp_err_t relay_chn_nvs_set_tilt_count(uint8_t ch, uint16_t tilt_count)
|
|
{
|
|
esp_err_t ret;
|
|
ret = nvs_set_u16(relay_chn_nvs, RELAY_CHN_KEY_TCNT(ch), tilt_count);
|
|
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to save tilt_count tilt counter");
|
|
return nvs_commit(relay_chn_nvs);
|
|
}
|
|
|
|
esp_err_t relay_chn_nvs_get_tilt_count(uint8_t ch, uint16_t *tilt_count)
|
|
{
|
|
ESP_RETURN_ON_FALSE(tilt_count != NULL,
|
|
ESP_ERR_INVALID_ARG, TAG, "Counter pointers are NULL");
|
|
return nvs_get_u16(relay_chn_nvs, RELAY_CHN_KEY_TCNT(ch), tilt_count);
|
|
}
|
|
#endif // CONFIG_RELAY_CHN_ENABLE_TILTING
|
|
|
|
esp_err_t relay_chn_nvs_erase_all()
|
|
{
|
|
// Erase all key-value pairs in the relay_chn NVS namespace
|
|
esp_err_t ret = nvs_erase_all(relay_chn_nvs);
|
|
ESP_RETURN_ON_ERROR(ret, TAG, "Failed to erase all keys in NVS namespace '%s'", CONFIG_RELAY_CHN_NVS_NAMESPACE);
|
|
|
|
// Commit the changes
|
|
return nvs_commit(relay_chn_nvs);
|
|
}
|
|
|
|
esp_err_t relay_chn_nvs_deinit()
|
|
{
|
|
nvs_close(relay_chn_nvs);
|
|
return ESP_OK;
|
|
}
|