Remove toml. Write calibration to dat files. Implement first draft for reading function
This commit is contained in:
@@ -20,8 +20,11 @@
|
||||
|
||||
#include <reflow-controller/settings/settings-sd-card.h>
|
||||
#include <stm-periph/unique-id.h>
|
||||
#include <toml/toml.h>
|
||||
#include <fatfs/ff.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static void get_controller_folder_path(char *path, size_t size)
|
||||
{
|
||||
@@ -38,6 +41,14 @@ static void get_controller_folder_path(char *path, size_t size)
|
||||
(unsigned int)high, (unsigned int)mid, (unsigned int)low);
|
||||
}
|
||||
|
||||
static void get_controller_settings_path(char *path, size_t size, const char *setting)
|
||||
{
|
||||
char folder[48];
|
||||
|
||||
get_controller_folder_path(folder, sizeof(folder));
|
||||
snprintf(path, size, "%s/%s.dat", folder, setting);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Open or create the controller folder on the SD Card.
|
||||
* @param[in,out] controller_folder
|
||||
@@ -67,50 +78,108 @@ static int create_controller_folder(void)
|
||||
}
|
||||
}
|
||||
|
||||
if (ret >= 0)
|
||||
f_chdir(foldername);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int read_settings_file_float(const char *path, float *value)
|
||||
{
|
||||
FRESULT res;
|
||||
FIL file;
|
||||
int ret = 0;
|
||||
char buff[32];
|
||||
UINT read_count;
|
||||
|
||||
if (!value)
|
||||
return -1002;
|
||||
|
||||
res = f_open(&file, path, FA_READ);
|
||||
if (res == FR_OK) {
|
||||
memset(buff, 0, sizeof(buff));
|
||||
res = f_read(&file, buff, sizeof(buff)-1, &read_count);
|
||||
if (res != FR_OK) {
|
||||
ret = -1;
|
||||
goto close_file;
|
||||
}
|
||||
*value = strtof(buff, NULL);
|
||||
close_file:
|
||||
f_close(&file);
|
||||
} else {
|
||||
ret = -2;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int sd_card_settings_save_calibration(float sens_deviation, float offset, bool active)
|
||||
{
|
||||
FIL cal_file;
|
||||
int status;
|
||||
FRESULT res;
|
||||
char path[128];
|
||||
char buff[64];
|
||||
UINT bw;
|
||||
FIL file;
|
||||
FRESULT res;
|
||||
int ret = 0;
|
||||
char buff[256];
|
||||
|
||||
status = create_controller_folder();
|
||||
|
||||
|
||||
if (status < 0)
|
||||
if (status)
|
||||
return -2;
|
||||
|
||||
/* Create new calibration file */
|
||||
res = f_open(&cal_file, CALIBRATION_FILE_NAME, FA_CREATE_ALWAYS | FA_WRITE);
|
||||
if (res == FR_OK) {
|
||||
status = snprintf(buff, sizeof(buff), "[cal]\noffset = %f\nsensdev = %f\nactive = %s\n",
|
||||
offset, sens_deviation, (active ? "true" : "false"));
|
||||
|
||||
if ((unsigned int)status < sizeof(buff) && status >= 0) {
|
||||
f_write(&cal_file, buff, status, &bw);
|
||||
if (bw == (unsigned int)status)
|
||||
ret = 0;
|
||||
else
|
||||
ret = -3;
|
||||
} else {
|
||||
ret = -4;
|
||||
get_controller_settings_path(path, sizeof(path), "offset");
|
||||
if (active) {
|
||||
res = f_open(&file, path, FA_CREATE_ALWAYS | FA_WRITE);
|
||||
if (res != FR_OK) {
|
||||
ret = -2;
|
||||
goto exit_offset;
|
||||
}
|
||||
|
||||
f_close(&cal_file);
|
||||
|
||||
status = snprintf(buff, sizeof(buff), "%f\n", offset);
|
||||
f_write(&file, buff, status, &bw);
|
||||
f_close(&file);
|
||||
} else {
|
||||
ret = -1;
|
||||
f_unlink(path);
|
||||
}
|
||||
exit_offset:
|
||||
get_controller_settings_path(path, sizeof(path), "sens");
|
||||
if (active) {
|
||||
res = f_open(&file, path, FA_CREATE_ALWAYS | FA_WRITE);
|
||||
if (res != FR_OK) {
|
||||
ret = -2;
|
||||
goto exit_sens;
|
||||
}
|
||||
|
||||
f_chdir("/");
|
||||
|
||||
status = snprintf(buff, sizeof(buff), "%f\n", sens_deviation);
|
||||
f_write(&file, buff, status, &bw);
|
||||
f_close(&file);
|
||||
} else {
|
||||
f_unlink(path);
|
||||
}
|
||||
exit_sens:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int sd_card_settings_try_load_calibration(float *sens_deviation, float *offset)
|
||||
{
|
||||
char path[128];
|
||||
int status;
|
||||
int ret = -2;
|
||||
|
||||
if (!sens_deviation || !offset)
|
||||
return -1000;
|
||||
|
||||
get_controller_settings_path(path, sizeof(path), "offset");
|
||||
status = read_settings_file_float(path, offset);
|
||||
if (status) {
|
||||
ret = status;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
get_controller_settings_path(path, sizeof(path), "sens");
|
||||
status = read_settings_file_float(path, sens_deviation);
|
||||
if (status) {
|
||||
ret = status;
|
||||
goto exit;
|
||||
}
|
||||
exit:
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
@@ -21,8 +21,13 @@
|
||||
#include <reflow-controller/settings/settings.h>
|
||||
#include <reflow-controller/settings/settings-sd-card.h>
|
||||
|
||||
settings_save_calibration(float sens_deviation, float offset, bool active)
|
||||
int settings_save_calibration(float sens_deviation, float offset, bool active)
|
||||
{
|
||||
/* There is no other configuration location besides the SD card (yet) */
|
||||
return sd_card_settings_save_calibration(sens_deviation, offset, active);
|
||||
}
|
||||
|
||||
int settings_load_calibration(float *sens_dev, float *offset)
|
||||
{
|
||||
return sd_card_settings_try_load_calibration(sens_dev, offset);
|
||||
}
|
||||
|
Reference in New Issue
Block a user