saving config added

This commit is contained in:
Ayzen
2026-10-13 17:49:53 +03:00
parent 4548c1f2a0
commit f7d9d1ce0e
7 changed files with 167 additions and 50 deletions
+60 -5
View File
@@ -13,7 +13,8 @@
#include "ad9102_device.h"
#include "storage_sd.h"
#define PROFILE_REPOSITORY_BUFFER_SIZE 1024u
#define PROFILE_REPOSITORY_INDEX_BUFFER_SIZE 1024u
#define PROFILE_REPOSITORY_INI_BUFFER_SIZE 4096u
static char *profile_repository_trim(char *text)
{
@@ -114,6 +115,52 @@ static void profile_repository_reset_profile(profile_t *profile)
profile->ds1809.apply_position = false;
}
/**
* @brief Apply a `<prefix><field>=value` sweep key.
*
* @return true when @p key carries @p prefix, i.e. belongs to this sweep.
*/
static bool profile_repository_apply_sweep_key(sweep_config_t *sweep,
const char *prefix,
const char *key,
const char *value)
{
const size_t prefix_length = strlen(prefix);
const char *field;
if (strncmp(key, prefix, prefix_length) != 0)
{
return false;
}
field = key + prefix_length;
if (strcmp(field, "enable") == 0)
{
sweep->enabled = (uint8_t)profile_repository_parse_u16(value);
}
else if (strcmp(field, "min_code") == 0)
{
sweep->min_code = profile_repository_parse_u16(value);
}
else if (strcmp(field, "max_code") == 0)
{
sweep->max_code = profile_repository_parse_u16(value);
}
else if (strcmp(field, "step_code") == 0)
{
sweep->step_code = profile_repository_parse_u16(value);
}
else if (strcmp(field, "step_period_us") == 0)
{
sweep->step_period_us = profile_repository_parse_u16(value);
}
else if (strcmp(field, "pause_ms") == 0)
{
sweep->pause_ms = profile_repository_parse_u16(value);
}
return true;
}
static void profile_repository_apply_key_value(profile_t *profile, const char *key, const char *value)
{
bool bool_value;
@@ -123,6 +170,12 @@ static void profile_repository_apply_key_value(profile_t *profile, const char *k
return;
}
if (profile_repository_apply_sweep_key(&profile->laser2_current_sweep, "laser2_current_sweep_", key, value) ||
profile_repository_apply_sweep_key(&profile->stm32_dac_sweep, "stm32_dac_sweep_", key, value))
{
return;
}
if (strcmp(key, "profile_name") == 0)
{
profile_repository_copy_string(profile->display_name, sizeof(profile->display_name), value);
@@ -351,13 +404,15 @@ static void profile_repository_apply_key_value(profile_t *profile, const char *k
static bool profile_repository_load_ini_file(const char *profile_path, profile_t *profile)
{
char buffer[PROFILE_REPOSITORY_BUFFER_SIZE];
/* Static: profiles are parsed only from the main loop, and the stack already holds FatFs' FIL. */
static char buffer[PROFILE_REPOSITORY_INI_BUFFER_SIZE];
UINT bytes_read = 0u;
char *cursor;
FRESULT result;
result = storage_sd_read_bytes(profile_path, 0u, buffer, sizeof(buffer) - 1u, &bytes_read);
if (result != FR_OK)
result = storage_sd_read_bytes(profile_path, 0u, buffer, sizeof(buffer), &bytes_read);
/* A file that fills the whole buffer leaves no room for the terminator: reject it rather than drop keys. */
if ((result != FR_OK) || (bytes_read >= sizeof(buffer)))
{
return false;
}
@@ -412,7 +467,7 @@ static bool profile_repository_load_by_valid_line_index(uint16_t target_index,
profile_t *out_profile,
uint16_t *out_total_valid_lines)
{
char buffer[PROFILE_REPOSITORY_BUFFER_SIZE];
char buffer[PROFILE_REPOSITORY_INDEX_BUFFER_SIZE];
UINT bytes_read = 0u;
char *cursor;
uint16_t valid_line_index = 0u;