matlab_stm_emulate/config_reader.asv

122 lines
3.5 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

clear; clc; close all;
model = 'mcu_test_r2023';
block = [model '/MCU_UPP'];
load_system(model); % если модель ещё не загружена
update_mask_from_config(block);
disp('Маска обновлена по конфигу.');
function update_mask_from_config(blockPath)
config = load_periph_config();
mask = Simulink.Mask.get(blockPath);
tabPrompt = 'Config Peripheral'; % Название вкладки, как отображается в маске
controls = mask.getDialogControls();
% Выведем все контролы и их свойства для отладки
fprintf('Всего контролов: %d\n', numel(controls));
for k = 1:numel(controls)
ctrl = controls(k);
props = properties(ctrl);
fprintf('Контрол #%d: Name="%s", Prompt="%s", Style="%s"\n', ...
k, ctrl.Name, ctrl.Prompt, ctrl.Style);
end
% Ищем вкладку по Prompt
tabIdx = find(arrayfun(@(c) strcmp(c.Style,'tab') && strcmp(c.Prompt, tabPrompt), controls), 1);
if isempty(tabIdx)
error('Вкладка с названием "%s" не найдена.', tabPrompt);
end
tabName = controls(tabIdx).Name;
fprintf('Найдена вкладка: Name="%s"\n', tabName);
% Удаляем параметры из найденной вкладки
i = 1;
while i <= numel(mask.Parameters)
if strcmp(mask.Parameters(i).TabName, tabName)
mask.removeParameter(i);
else
i = i + 1;
end
end
% Добавляем параметры в эту вкладку
periphs = fieldnames(config);
for i = 1:numel(periphs)
periph = periphs{i};
defines = config.(periph).Defines;
defNames = fieldnames(defines);
for j = 1:numel(defNames)
defName = defNames{j};
def = defines.(defName);
val = def.Default;
if islogical(val)
valStr = mat2str(val);
elseif isnumeric(val)
valStr = num2str(val);
elseif ischar(val)
valStr = ['''' val ''''];
else
error('Unsupported default value type for %s.%s', periph, defName);
end
mask.addParameter( ...
'Type', def.Type, ...
'Prompt', [periph ' - ' defName], ...
'Name', [periph '_' defName], ...
'Value', valStr, ...
'TabName', tabName ...
);
end
end
end
function config = load_periph_config()
jsonText = fileread('periph_config.json');
config = jsondecode(jsonText);
end
function config = load_periph_config()
jsonText = fileread('periph_config.json');
config = jsondecode(jsonText);
end
function clear_params_from_tab(blockPath, tabPrompt)
mask = Simulink.Mask.get(blockPath);
controls = mask.getDialogControls;
tabs = controls(strcmp({controls.Type}, 'tab'));
tabName = '';
for i = 1:numel(tabs)
if strcmp(tabs(i).Prompt, tabPrompt)
tabName = tabs(i).Name; % внутреннее имя вкладки
break;
end
end
if isempty(tabName)
error('Вкладка с названием "%s" не найдена.', tabPrompt);
end
% Удаляем параметры с TabName == tabName
i = 1;
while i <= numel(mask.Parameters)
if strcmp(mask.Parameters(i).TabName, tabName)
mask.removeParameter(i);
else
i = i + 1;
end
end
end