configuration saved to nvs

This commit is contained in:
leo 2023-06-15 16:09:47 +02:00
parent 4a2ae5ee1a
commit c496f27222
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
5 changed files with 55 additions and 1 deletions

View File

@ -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")

43
main/configuration.c Normal file
View File

@ -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);
}

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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 = {