aux/util: Store calibration config non-destructively

On save
* delete only nodes pertaining to calibration
* add new calibration nodes
* write config file
This commit is contained in:
Christoph Haag 2021-02-18 03:05:39 +01:00
parent de169bf575
commit c077788436
4 changed files with 72 additions and 29 deletions

View file

@ -26,6 +26,15 @@ DEBUG_GET_ONCE_OPTION(active_config, "P_OVERRIDE_ACTIVE_CONFIG", NULL)
#define CONFIG_FILE_NAME "config_v0.json"
void
u_config_json_close(struct u_config_json *json)
{
if (json->root != NULL) {
cJSON_Delete(json->root);
json->root = NULL;
}
json->file_loaded = false;
}
void
u_config_json_open_or_create_main_file(struct u_config_json *json)
@ -344,3 +353,50 @@ u_config_json_get_tracking_settings(struct u_config_json *json, struct xrt_setti
return true;
}
static void
u_config_write(struct u_config_json *json)
{
char *str = cJSON_Print(json->root);
U_LOG_D("%s", str);
FILE *config_file = u_file_open_file_in_config_dir(CONFIG_FILE_NAME, "w");
fprintf(config_file, "%s\n", str);
fflush(config_file);
fclose(config_file);
config_file = NULL;
free(str);
}
void
u_config_json_save_calibration(struct u_config_json *json, struct xrt_settings_tracking *settings)
{
cJSON *root = json->root;
cJSON *t = cJSON_GetObjectItem(root, "tracking");
if (!t) {
t = cJSON_AddObjectToObject(root, "tracking");
}
cJSON_DeleteItemFromObject(t, "version");
cJSON_AddNumberToObject(t, "version", 0);
cJSON_DeleteItemFromObject(t, "camera_name");
cJSON_AddStringToObject(t, "camera_name", settings->camera_name);
cJSON_DeleteItemFromObject(t, "camera_mode");
cJSON_AddNumberToObject(t, "camera_mode", settings->camera_mode);
cJSON_DeleteItemFromObject(t, "camera_type");
switch (settings->camera_type) {
case XRT_SETTINGS_CAMERA_TYPE_REGULAR_MONO: cJSON_AddStringToObject(t, "camera_type", "regular_mono"); break;
case XRT_SETTINGS_CAMERA_TYPE_REGULAR_SBS: cJSON_AddStringToObject(t, "camera_type", "regular_sbs"); break;
case XRT_SETTINGS_CAMERA_TYPE_PS4: cJSON_AddStringToObject(t, "camera_type", "ps4"); break;
case XRT_SETTINGS_CAMERA_TYPE_LEAP_MOTION: cJSON_AddStringToObject(t, "camera_type", "leap_motion"); break;
}
cJSON_DeleteItemFromObject(t, "calibration_path");
cJSON_AddStringToObject(t, "calibration_path", settings->calibration_path);
u_config_write(json);
}

View file

@ -34,6 +34,9 @@ struct u_config_json
cJSON *root;
};
void
u_config_json_close(struct u_config_json *json);
/*!
* Load the JSON config file.
*
@ -42,6 +45,13 @@ struct u_config_json
void
u_config_json_open_or_create_main_file(struct u_config_json *json);
/*!
* Writes back calibration settings to the main config file.
*
* @ingroup aux_util
*/
void
u_config_json_save_calibration(struct u_config_json *json, struct xrt_settings_tracking *settings);
/*!
* Read from the JSON loaded json config file and returns the active config,
* can be overridden by `P_OVERRIDE_ACTIVE_CONFIG` envirmental variable.

View file

@ -13,6 +13,7 @@
#include "util/u_sink.h"
#include "util/u_file.h"
#include "util/u_json.h"
#include "util/u_config_json.h"
#ifdef XRT_HAVE_OPENCV
#include "tracking/t_tracking.h"
@ -102,31 +103,10 @@ save_calibration(struct calibration_scene *cs)
* Camera config file.
*
*/
cJSON *root = cJSON_CreateObject();
cJSON *t = cJSON_AddObjectToObject(root, "tracking");
cJSON_AddNumberToObject(t, "version", 0);
cJSON_AddStringToObject(t, "camera_name", cs->settings->camera_name);
cJSON_AddNumberToObject(t, "camera_mode", cs->settings->camera_mode);
switch (cs->settings->camera_type) {
case XRT_SETTINGS_CAMERA_TYPE_REGULAR_MONO: cJSON_AddStringToObject(t, "camera_type", "regular_mono"); break;
case XRT_SETTINGS_CAMERA_TYPE_REGULAR_SBS: cJSON_AddStringToObject(t, "camera_type", "regular_sbs"); break;
case XRT_SETTINGS_CAMERA_TYPE_PS4: cJSON_AddStringToObject(t, "camera_type", "ps4"); break;
case XRT_SETTINGS_CAMERA_TYPE_LEAP_MOTION: cJSON_AddStringToObject(t, "camera_type", "leap_motion"); break;
}
cJSON_AddStringToObject(t, "calibration_path", cs->settings->calibration_path);
char *str = cJSON_Print(root);
U_LOG_D("%s", str);
cJSON_Delete(root);
FILE *config_file = u_file_open_file_in_config_dir("config_v0.json", "w");
fprintf(config_file, "%s\n", str);
fflush(config_file);
fclose(config_file);
config_file = NULL;
free(str);
struct u_config_json json;
u_config_json_open_or_create_main_file(&json);
u_config_json_save_calibration(&json, cs->settings);
u_config_json_close(&json);
/*
*

View file

@ -558,10 +558,7 @@ teardown(struct prober *p)
p_libusb_teardown(p);
#endif
if (p->json.root != NULL) {
cJSON_Delete(p->json.root);
p->json.root = NULL;
}
u_config_json_close(&p->json);
free(p->disabled_drivers);
}