From c496f27222046256601e64866e7b9367ed78b2aa Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 15 Jun 2023 16:09:47 +0200 Subject: [PATCH] configuration saved to nvs --- main/CMakeLists.txt | 2 +- main/configuration.c | 43 +++++++++++++++++++++++++++++++++++++++++++ main/configuration.h | 3 +++ main/gatt_svcs.c | 6 ++++++ main/power_profiler.c | 2 ++ 5 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 main/configuration.c diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 6d1236a..893683b 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,3 +1,3 @@ -idf_component_register(SRCS "power_profiler.c" "resistor_ranges.c" "measure.c" "ble.c" "gatt_svcs.c" +idf_component_register(SRCS "power_profiler.c" "resistor_ranges.c" "measure.c" "ble.c" "gatt_svcs.c" "configuration.c" INCLUDE_DIRS "." REQUIRES "driver" "esp_adc" "bt" "nvs_flash") \ No newline at end of file diff --git a/main/configuration.c b/main/configuration.c new file mode 100644 index 0000000..9ae17a4 --- /dev/null +++ b/main/configuration.c @@ -0,0 +1,43 @@ +#include "esp_err.h" +#include "nvs.h" + +#include "configuration.h" +#include "power_profiler.h" + +void init_configuration_from_nvs(configuration* conf){ + nvs_handle_t handle; + ESP_ERROR_CHECK(nvs_open("storage", NVS_READONLY, &handle)); + + nvs_get_u32(handle, "refresh_delay", &conf->refresh_delay); + nvs_get_u16(handle, "zero_c_nsamp", &conf->zero_cali_nsamp); + nvs_get_i32(handle, "range", &conf->range); + nvs_get_i32(handle, "auto_range", &conf->auto_range); + + for(int i = 0; i < INPUTS_NUM; i++){ + char key[16]; + sprintf(key, "offset%d", i); + nvs_get_u32(handle, key, &conf->offsets[i]); + } + + nvs_close(handle); +} + +void save_configuration_to_nvs(configuration* conf){ + nvs_handle_t handle; + ESP_ERROR_CHECK(nvs_open("storage", NVS_READWRITE, &handle)); + + nvs_set_u32(handle, "refresh_delay", conf->refresh_delay); + nvs_set_u16(handle, "zero_c_nsamp", conf->zero_cali_nsamp); + nvs_set_i32(handle, "range", conf->range); + nvs_set_i32(handle, "auto_range", conf->auto_range); + + for(int i = 0; i < INPUTS_NUM; i++){ + char key[16]; + sprintf(key, "offset%d", i); + nvs_set_u32(handle, key, conf->offsets[i]); + } + + ESP_ERROR_CHECK(nvs_commit(handle)); + + nvs_close(handle); +} \ No newline at end of file diff --git a/main/configuration.h b/main/configuration.h index 66fb805..ebc4088 100644 --- a/main/configuration.h +++ b/main/configuration.h @@ -12,3 +12,6 @@ typedef struct { int range; int auto_range; } configuration; + +void init_configuration_from_nvs(configuration* conf); +void save_configuration_to_nvs(configuration* conf); \ No newline at end of file diff --git a/main/gatt_svcs.c b/main/gatt_svcs.c index d5e00d2..c0bd0b3 100644 --- a/main/gatt_svcs.c +++ b/main/gatt_svcs.c @@ -10,6 +10,7 @@ #include "gatt_svcs.h" #include "BLE_UUID.h" +#include "configuration.h" static struct char_pres_format current_char_pres_format = { .format = FORMAT_UINT32, @@ -139,6 +140,7 @@ int gatt_char_access_sampling_rate(uint16_t conn_handle, uint16_t attr_handle, s rc = gatt_svr_chr_write(ctxt->om, sizeof(data), sizeof(data), &data, NULL); ESP_LOGI("BLE", "Refresh rate : %lu", data); c_ctxt->conf->refresh_delay = data; + save_configuration_to_nvs(c_ctxt->conf); break; case BLE_GATT_ACCESS_OP_READ_DSC: rc = os_mbuf_append(ctxt->om, &refrsh_rate_char_pres_format, CHAR_PRESENTATION_FORMAT_SIZE); @@ -158,12 +160,14 @@ int gatt_char_access_zeros_cali(uint16_t conn_handle, uint16_t attr_handle, stru switch (c_ctxt->handle){ case ZEROS_CALI_ID: start_zero_cali(c_ctxt->meas_ctxt, c_ctxt->conf->zero_cali_nsamp, c_ctxt->conf->offsets); + save_configuration_to_nvs(c_ctxt->conf); break; case ZEROS_CALI_NSAMP_ID: ; uint16_t data; rc = gatt_svr_chr_write(ctxt->om, sizeof(data), sizeof(data), &data, NULL); c_ctxt->conf->zero_cali_nsamp = data; + save_configuration_to_nvs(c_ctxt->conf); break; } } @@ -194,6 +198,7 @@ int gatt_char_access_range(uint16_t conn_handle, uint16_t attr_handle, struct bl uint8_t state; rc = gatt_svr_chr_write(ctxt->om, sizeof(state), sizeof(state), &state, NULL); c_ctxt->conf->auto_range = state; + save_configuration_to_nvs(c_ctxt->conf); break; case CURRENT_RANGE_ID: ; @@ -202,6 +207,7 @@ int gatt_char_access_range(uint16_t conn_handle, uint16_t attr_handle, struct bl ESP_LOGI(TAG, "new range %d", data); c_ctxt->conf->range = data; activate_range(c_ctxt->conf->ranges, data, c_ctxt->conf->ranges_num - 1); + save_configuration_to_nvs(c_ctxt->conf); break; } } diff --git a/main/power_profiler.c b/main/power_profiler.c index d805546..3da6711 100644 --- a/main/power_profiler.c +++ b/main/power_profiler.c @@ -142,6 +142,8 @@ void app_main(void){ main_conf.offsets = offsets; + init_configuration_from_nvs(&main_conf); + uint32_t meas_volts[INPUTS_NUM]; uint32_t meas_amp[INPUTS_NUM]; measurements meas = {