добавлен экспорт и импорт настроек оболочки и приложения МК
работа с конфигом (чтение запись) перенесена в отдельный файл добавлена возможность через popup сделать define = чему-то
This commit is contained in:
143
McuLib/m/configJs.m
Normal file
143
McuLib/m/configJs.m
Normal file
@@ -0,0 +1,143 @@
|
||||
classdef configJs
|
||||
|
||||
methods(Static)
|
||||
|
||||
function config = update(blockPath, config)
|
||||
if isempty(config)
|
||||
return;
|
||||
end
|
||||
|
||||
mask = Simulink.Mask.get(blockPath);
|
||||
maskParams = mask.Parameters;
|
||||
paramNames = arrayfun(@(p) p.Name, maskParams, 'UniformOutput', false);
|
||||
|
||||
% Обработка остальных секций (с дефайнами)
|
||||
periphs = fieldnames(config);
|
||||
for i = 1:numel(periphs)
|
||||
periph = periphs{i};
|
||||
|
||||
% Проверяем есть ли Defines
|
||||
if ~isfield(config.(periph), 'Defines')
|
||||
continue;
|
||||
end
|
||||
|
||||
defines = config.(periph).Defines;
|
||||
defNames = fieldnames(defines);
|
||||
|
||||
for j = 1:numel(defNames)
|
||||
defPrompt = defNames{j};
|
||||
paramName = matlab.lang.makeValidName(defPrompt);
|
||||
|
||||
% Проверка, существует ли параметр с таким именем
|
||||
if ismember(paramName, paramNames)
|
||||
param = mask.getParameter(paramName);
|
||||
valStr = param.Value;
|
||||
|
||||
% Проверяем, существует ли элемент defPrompt в структуре defines
|
||||
if isfield(defines, defPrompt)
|
||||
% Преобразуем строку в соответствующий тип
|
||||
if strcmpi(defines.(defPrompt).Type, 'checkbox')
|
||||
config.(periph).Defines.(defPrompt).Default = strcmpi(valStr, 'on');
|
||||
elseif strcmpi(defines.(defPrompt).Type, 'edit')
|
||||
valNum = str2double(valStr);
|
||||
if isnan(valNum)
|
||||
config.(periph).Defines.(defPrompt).Default = valStr;
|
||||
else
|
||||
config.(periph).Defines.(defPrompt).Default = valNum;
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function config = read(blockPath)
|
||||
mask = Simulink.Mask.get(blockPath);
|
||||
|
||||
pathparam = mask.getParameter('periphPath');
|
||||
config_path = pathparam.Value;
|
||||
|
||||
if ~isempty(config_path)
|
||||
jsonText = fileread(config_path);
|
||||
config = jsondecode(jsonText);
|
||||
else
|
||||
config = [];
|
||||
end
|
||||
end
|
||||
|
||||
function write(config)
|
||||
if isempty(config)
|
||||
return
|
||||
end
|
||||
|
||||
blockHandle = gcbh;
|
||||
mask = Simulink.Mask.get(blockHandle);
|
||||
|
||||
pathparam = mask.getParameter('periphPath');
|
||||
config_path = pathparam.Value;
|
||||
|
||||
jsonText = jsonencode(config, 'PrettyPrint', true);
|
||||
fid = fopen(config_path, 'w', 'n', 'UTF-8');
|
||||
if fid == -1
|
||||
error('Не удалось открыть файл periph_config.json для записи.');
|
||||
end
|
||||
fwrite(fid, jsonText, 'char');
|
||||
fclose(fid);
|
||||
end
|
||||
|
||||
|
||||
|
||||
function value = get_field(configStruct, targetConfig)
|
||||
% получить targetConfig структуру из конфига (для глубоко вложенных)
|
||||
value = [];
|
||||
fields = fieldnames(configStruct);
|
||||
|
||||
for i = 1:numel(fields)
|
||||
key = fields{i};
|
||||
if strcmp(key, targetConfig)
|
||||
value = configStruct.(key);
|
||||
return; % нашли и возвращаем
|
||||
elseif isstruct(configStruct.(key))
|
||||
value = configJs.get_field(configStruct.(key), targetConfig);
|
||||
if ~isempty(value)
|
||||
return; % нашли во вложенной структуре
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
% Если не нашли, можно выбросить ошибку или вернуть пустое
|
||||
if isempty(value)
|
||||
% error('Поле "%s" не найдено в структуре.', targetConfig);
|
||||
end
|
||||
end
|
||||
|
||||
function short = get_final_name_from_prefix(prefix)
|
||||
% Берёт последнее имя после "_" (читаемое имя)
|
||||
parts = strsplit(prefix, '_');
|
||||
short = parts{end};
|
||||
end
|
||||
|
||||
function value = convert_code_value(codeField)
|
||||
% Преобразует значение поля Options в строку
|
||||
if ischar(codeField)
|
||||
value = codeField;
|
||||
elseif isstring(codeField)
|
||||
value = char(codeField);
|
||||
elseif iscell(codeField)
|
||||
value = strjoin(codeField, newline);
|
||||
else
|
||||
% warning('Неподдерживаемый тип данных: сохранено как пустая строка');
|
||||
value = '';
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
methods(Static, Access=private)
|
||||
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user