51 lines
1.6 KiB
C
51 lines
1.6 KiB
C
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "configuration.h"
|
|
#include "scd4x_data.h"
|
|
#include "nvs_flash.h"
|
|
#include "sdkconfig.h"
|
|
#include "esp_log.h"
|
|
|
|
void init_conf_from_nvs(configuration_data_t* conf){
|
|
nvs_handle_t nvs;
|
|
ESP_ERROR_CHECK(nvs_open("battms", NVS_READONLY, &nvs));
|
|
conf->battery_conf = get_battery_configuration(nvs);
|
|
|
|
ESP_ERROR_CHECK(nvs_open("leds", NVS_READONLY, &nvs));
|
|
conf->leds = get_led_configuration(nvs);
|
|
nvs_get_u8(nvs, "led_nb", &conf->led_nb);
|
|
|
|
ESP_ERROR_CHECK(nvs_open("main", NVS_READONLY, &nvs)); // todo: move to namespaces
|
|
conf->sensor = get_sensor_configuration(nvs);
|
|
conf->measure = conf->sensor->measure;
|
|
nvs_get_u8(nvs, "wireless_conf", &conf->wireless);
|
|
conf->wifi_config = get_ap_config(nvs);
|
|
size_t str_size = sizeof(conf->hostname);
|
|
ESP_ERROR_CHECK(nvs_get_str(nvs, "name", conf->hostname, &str_size));
|
|
ESP_LOGI("AAA", "%d", str_size);
|
|
}
|
|
|
|
battery_conf_t* get_battery_configuration(nvs_handle_t nvs){
|
|
battery_conf_t* conf = malloc(sizeof(battery_conf_t));
|
|
ESP_ERROR_CHECK(nvs_get_u16(nvs, "poll_delay", &conf->poll_delay));
|
|
|
|
uint16_t min_v, max_v;
|
|
nvs_get_u16(nvs, "min_v", &min_v);
|
|
nvs_get_u16(nvs, "max_v", &max_v);
|
|
|
|
uint16_t min_raw = min_v * (1<<CONFIG_BATTMS_ADC_BITWIDTH) / (110*3.55);
|
|
uint16_t max_raw = max_v * (1<<CONFIG_BATTMS_ADC_BITWIDTH) / (110*3.55);
|
|
battery_data_t data = {
|
|
.battery_percent = 0,
|
|
.min_raw = min_raw,
|
|
.scale = (max_raw - min_raw) / 100,
|
|
};
|
|
|
|
battery_data_t* data_h = malloc(sizeof(battery_data_t));
|
|
memcpy(data_h, &data, sizeof(battery_data_t));
|
|
conf->data = data_h;
|
|
conf->adc_channel = CONFIG_BATTMS_CHANNEL;
|
|
return conf;
|
|
} |