matlab_stm_emulate/config_reader.m

148 lines
4.5 KiB
Matlab
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();
allTabs = find_all_tabs(mask);
fprintf('Найдено вкладок: %d\n', numel(allTabs));
for i = 1:numel(allTabs)
fprintf('Tab %d: Name="%s", Prompt="%s"\n', i, allTabs(i).Name, allTabs(i).Prompt);
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 tabs = find_all_tabs(mask)
controls = mask.getDialogControls();
tabs = find_tabs_recursive(controls);
end
function tabs = find_tabs_recursive(controls)
tabs = [];
for i = 1:numel(controls)
ctrl = controls(i);
% Проверяем тип контролла (у разных версий API может отличаться)
% Чтобы не упасть, проверяем, есть ли поле Type
if isprop(ctrl, 'Type')
if strcmp(ctrl.Type, 'tab')
tabs = [tabs, ctrl]; %#ok<AGROW>
elseif any(strcmp(ctrl.Type, {'tabcontainer', 'group', 'panel'}))
% В этих контролах могут быть свои Controls или Children
if isprop(ctrl, 'Controls')
subtabs = find_tabs_recursive(ctrl.Controls);
elseif isprop(ctrl, 'Children')
subtabs = find_tabs_recursive(ctrl.Children);
else
subtabs = [];
end
tabs = [tabs, subtabs]; %#ok<AGROW>
end
else
% Если у контролла нет свойства Type - игнорируем
end
end
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