own config module
This commit is contained in:
177
main/config.c
Normal file
177
main/config.c
Normal file
@ -0,0 +1,177 @@
|
||||
/**
|
||||
* @file config.c
|
||||
* @brief Config module implementation
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_system.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "nvs.h"
|
||||
#include "esp_timer.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
static const char *TAG = "CONFIG";
|
||||
|
||||
#define NVS_NAMESPACE "led_ctrl"
|
||||
#define CONFIG_MAGIC 0xDEADBEEF
|
||||
|
||||
#define HARDCODED_CONFIG
|
||||
#define HARDCODED_CONFIG_LED_STRIP_A_PIN 12U
|
||||
#define HARDCODED_CONFIG_LED_STRIP_B_PIN 14U
|
||||
#define HARDCODED_CONFIG_LED_STRIP_A_COUNT 7U
|
||||
#define HARDCODED_CONFIG_LED_STRIP_B_COUNT 7U
|
||||
#define HARDCODED_CONFIG_PWM_PIN 13U
|
||||
|
||||
// Global state
|
||||
static config_t current_config = {
|
||||
.led_pin_strip_a = -1,
|
||||
.led_pin_strip_b = -1,
|
||||
.led_count_strip_a = -1,
|
||||
.led_count_strip_b = -1,
|
||||
.pwm_pin = -1,
|
||||
.magic = CONFIG_MAGIC};
|
||||
|
||||
// NVS Functions
|
||||
static esp_err_t load_config_from_nvs(void)
|
||||
{
|
||||
nvs_handle_t nvs_handle;
|
||||
esp_err_t err = nvs_open(NVS_NAMESPACE, NVS_READONLY, &nvs_handle);
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGW(TAG, "NVS not found, using defaults");
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
|
||||
size_t required_size = sizeof(config_t);
|
||||
err = nvs_get_blob(nvs_handle, "config", ¤t_config, &required_size);
|
||||
nvs_close(nvs_handle);
|
||||
|
||||
if (err != ESP_OK || current_config.magic != CONFIG_MAGIC)
|
||||
{
|
||||
ESP_LOGW(TAG, "Invalid config in NVS, using defaults");
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Loaded config from NVS");
|
||||
ESP_LOGI(TAG, " Strip A: GPIO%d", current_config.led_pin_strip_a);
|
||||
ESP_LOGI(TAG, " Strip B: GPIO%d", current_config.led_pin_strip_b);
|
||||
ESP_LOGI(TAG, " PWM Pin: GPIO%d", current_config.pwm_pin);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t save_config_to_nvs(void)
|
||||
{
|
||||
nvs_handle_t nvs_handle;
|
||||
esp_err_t err = nvs_open(NVS_NAMESPACE, NVS_READWRITE, &nvs_handle);
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
return err;
|
||||
}
|
||||
|
||||
current_config.magic = CONFIG_MAGIC;
|
||||
err = nvs_set_blob(nvs_handle, "config", ¤t_config, sizeof(config_t));
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
err = nvs_commit(nvs_handle);
|
||||
}
|
||||
|
||||
nvs_close(nvs_handle);
|
||||
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
ESP_LOGI(TAG, "Config saved to NVS");
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to save config: %s", esp_err_to_name(err));
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
esp_err_t config_reset_config(void)
|
||||
{
|
||||
current_config.led_pin_strip_a = -1;
|
||||
current_config.led_pin_strip_b = -1;
|
||||
current_config.led_count_strip_a = -1;
|
||||
current_config.led_count_strip_b = -1;
|
||||
current_config.pwm_pin = -1;
|
||||
current_config.magic = CONFIG_MAGIC;
|
||||
|
||||
return save_config_to_nvs();
|
||||
}
|
||||
|
||||
void config_get_config(config_t *const cnf)
|
||||
{
|
||||
cnf->led_pin_strip_a = current_config.led_pin_strip_a;
|
||||
cnf->led_pin_strip_b = current_config.led_pin_strip_b;
|
||||
cnf->led_count_strip_a = current_config.led_count_strip_a;
|
||||
cnf->led_count_strip_b = current_config.led_count_strip_b;
|
||||
cnf->pwm_pin = current_config.pwm_pin;
|
||||
}
|
||||
|
||||
esp_err_t config_update_config(const config_t *config)
|
||||
{
|
||||
if (!config)
|
||||
{
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
// Reinitialize if pins changed
|
||||
bool pins_changed = (current_config.led_pin_strip_a != config->led_pin_strip_a) ||
|
||||
(current_config.led_pin_strip_b != config->led_pin_strip_b) ||
|
||||
(current_config.pwm_pin != config->pwm_pin);
|
||||
|
||||
memcpy(¤t_config, config, sizeof(config_t));
|
||||
esp_err_t err = save_config_to_nvs();
|
||||
|
||||
if (err == ESP_OK && pins_changed)
|
||||
{
|
||||
ESP_LOGI(TAG, "Restarting to apply new pin configuration...");
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
esp_restart();
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
// Main initialization
|
||||
esp_err_t config_init(void)
|
||||
{
|
||||
esp_err_t ret;
|
||||
|
||||
ESP_LOGI(TAG, "Initializing Config Controller...");
|
||||
|
||||
// Initialize NVS
|
||||
ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
|
||||
{
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK(ret);
|
||||
|
||||
#ifdef HARDCODED_CONFIG
|
||||
current_config.led_pin_strip_a = HARDCODED_CONFIG_LED_STRIP_A_PIN;
|
||||
current_config.led_pin_strip_b = HARDCODED_CONFIG_LED_STRIP_B_PIN;
|
||||
current_config.led_count_strip_a = HARDCODED_CONFIG_LED_STRIP_A_COUNT;
|
||||
current_config.led_count_strip_b = HARDCODED_CONFIG_LED_STRIP_B_COUNT;
|
||||
current_config.pwm_pin = HARDCODED_CONFIG_PWM_PIN;
|
||||
current_config.magic = CONFIG_MAGIC;
|
||||
|
||||
save_config_to_nvs();
|
||||
#endif
|
||||
|
||||
// Load configuration
|
||||
load_config_from_nvs();
|
||||
|
||||
ESP_LOGI(TAG, "Config system initialized successfully");
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
Reference in New Issue
Block a user