Use new config parser for calibration

This commit is contained in:
Mario Hüttel 2020-11-01 00:59:46 +01:00
parent a3778fcb6e
commit 1eeaf3d892
2 changed files with 63 additions and 82 deletions

View File

@ -71,6 +71,7 @@ static int parse_value(struct config_parser_entry *entry, char *value_start_tok
if (endptr == value_start_token) if (endptr == value_start_token)
return -1; return -1;
entry->type = CONFIG_PARSER_TYPE_FLOAT; entry->type = CONFIG_PARSER_TYPE_FLOAT;
goto exit;
} }
if (value_start_token[0] != '-') { if (value_start_token[0] != '-') {
@ -81,6 +82,7 @@ static int parse_value(struct config_parser_entry *entry, char *value_start_tok
return -1; return -1;
} }
entry->type = CONFIG_PARSER_TYPE_UINT; entry->type = CONFIG_PARSER_TYPE_UINT;
goto exit;
} else { } else {
/* Try parsing as int */ /* Try parsing as int */
entry->value.int_val = strtod(value_start_token, &endptr); entry->value.int_val = strtod(value_start_token, &endptr);
@ -90,6 +92,7 @@ static int parse_value(struct config_parser_entry *entry, char *value_start_tok
entry->type = CONFIG_PARSER_TYPE_INT; entry->type = CONFIG_PARSER_TYPE_INT;
} }
exit:
return 0; return 0;
} }
@ -128,7 +131,7 @@ enum config_parser_ret config_parser_get_line(config_parser_handle_t handle, str
} }
break; break;
case 2: /* VALUE */ case 2: /* VALUE */
if (!parse_value(entry, token)) if (parse_value(entry, token))
return CONFIG_PARSER_LINE_MALFORM; return CONFIG_PARSER_LINE_MALFORM;
break; break;
default: default:
@ -136,7 +139,7 @@ enum config_parser_ret config_parser_get_line(config_parser_handle_t handle, str
} }
token_round++; token_round++;
strtok(NULL, token_delim); token = strtok(NULL, token_delim);
} }
return CONFIG_PARSER_OK; return CONFIG_PARSER_OK;

View File

@ -25,6 +25,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <config-parser/config-parser.h>
static char workbuff[256];
static void get_controller_folder_path(char *path, size_t size) static void get_controller_folder_path(char *path, size_t size)
{ {
@ -46,7 +49,7 @@ static void get_controller_settings_path(char *path, size_t size, const char *se
char folder[48]; char folder[48];
get_controller_folder_path(folder, sizeof(folder)); get_controller_folder_path(folder, sizeof(folder));
snprintf(path, size, "%s/%s.dat", folder, setting); snprintf(path, size, "%s/%s.conf", folder, setting);
} }
/** /**
@ -79,104 +82,79 @@ static int create_controller_folder(void)
return ret; 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) int sd_card_settings_save_calibration(float sens_deviation, float offset, bool active)
{ {
int status; char path[200];
char path[128]; FRESULT res = FR_OK;
char buff[64]; int ret;
UINT bw;
FIL file; FIL file;
FRESULT res;
int ret = 0;
status = create_controller_folder(); get_controller_settings_path(path, sizeof(path), "calibration");
if (status < 0)
if (create_controller_folder() < 0)
return -2; return -2;
get_controller_settings_path(path, sizeof(path), "offset"); if (!active) {
if (active) { res = f_unlink(path);
res = f_open(&file, path, FA_CREATE_ALWAYS | FA_WRITE); goto check_fresult;
if (res != FR_OK) {
ret = -2;
goto exit_offset;
}
status = snprintf(buff, sizeof(buff), "%f\n", offset);
f_write(&file, buff, status, &bw);
f_close(&file);
} else {
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;
}
status = snprintf(buff, sizeof(buff), "%f\n", sens_deviation); res = f_open(&file, path, FA_CREATE_ALWAYS | FA_WRITE);
f_write(&file, buff, status, &bw); if (res != FR_OK)
f_close(&file); goto check_fresult;
} else {
f_unlink(path); snprintf(path, sizeof(path), "offset = %f\nsensitivity = %f\n", offset, sens_deviation);
} ret = f_puts(path, &file);
exit_sens: if (ret < 0)
goto close_file;
ret = 0;
close_file:
res = f_close(&file);
check_fresult:
if (res != FR_OK)
return -2;
return ret; return ret;
} }
int sd_card_settings_try_load_calibration(float *sens_deviation, float *offset) int sd_card_settings_try_load_calibration(float *sens_deviation, float *offset)
{ {
char path[128]; char path[128];
int status; int status = -1;
int ret = 0; struct config_parser parser;
config_parser_handle_t p;
enum config_parser_ret res;
struct config_parser_entry entry;
bool sens_loaded = false;
bool offset_loaded = false;
if (!sens_deviation || !offset) if (!sens_deviation || !offset)
return -1000; return -1000;
get_controller_settings_path(path, sizeof(path), "offset"); get_controller_settings_path(path, sizeof(path), "calibration");
status = read_settings_file_float(path, offset); p = config_parser_open_file(&parser, false, path, workbuff, sizeof(workbuff));
if (status) { status = 0;
ret = status; do {
goto exit; res = config_parser_get_line(p, &entry);
} if (res == CONFIG_PARSER_OK) {
if (!strcmp(entry.name, "offset") && entry.type == CONFIG_PARSER_TYPE_FLOAT) {
offset_loaded = true;
*offset = entry.value.float_val;
} else if (!strcmp(entry.name, "sensitivity") && entry.type == CONFIG_PARSER_TYPE_FLOAT) {
sens_loaded = true;
*sens_deviation = entry.value.float_val;
}
}
get_controller_settings_path(path, sizeof(path), "sens"); } while (res != CONFIG_PARSER_END_REACHED &&
status = read_settings_file_float(path, sens_deviation); res != CONFIG_PARSER_GENERIC_ERR &&
if (status) { res != CONFIG_PARSER_IOERR);
ret = status;
goto exit; if (sens_loaded && offset_loaded)
} status = 0;
exit:
return ret; return status;
} }