matlab_stm_emulate/config_reader.asv

160 lines
4.4 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'; % Имя вкладки (Prompt)
allControls = mask.getDialogControls();
tabCtrl = find_tab_by_prompt(allControls, tabPrompt);
if isempty(tabCtrl)
error('Вкладка с названием "%s" не найдена в маске', tabPrompt);
end
% Удаляем все контролы внутри вкладки
children = tabCtrl.DialogControls;
while ~isempty(children)
tabCtrl.removeControl(children(1));
children = tabCtrl.DialogControls; % обновляем список после удаления
end
periphs = fieldnames(config);
for i = 1:numel(periphs)
periph = periphs{i};
defines = config.(periph).Defines;
defNames = fieldnames(defines);
for j = 1:numel(defNames)
defPrompt = defNames{j};
def = defines.(defPrompt);
% Обрабатываем только checkbox и edit
switch lower(def.Type)
case 'checkbox'
paramType = 'checkbox';
case 'edit'
paramType = 'edit';
otherwise
continue;
end
paramName = matlab.lang.makeValidName([periph '_' defPrompt]);
val = def.Default;
if islogical(val)
if val
valStr = 'on';
else
valStr = 'off';
end
elseif isnumeric(val)
valStr = num2str(val);
elseif ischar(val)
valStr = val;
else
error('Unsupported default value type for %s.%s', periph, defPrompt);
end
% Добавляем параметр в маску (без TabName)
mask.addParameter(...
'Type', paramType, ...
'Prompt', defPrompt, ...
'Name', paramName, ...
'Value', valStr, ...
'Container', tabPrompt);
disp(['paramType = ', paramType]);
disp(['paramName = ', paramName]);
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
%% поиск вкладки
function tab = find_tab_by_name(controls, targetName)
tab = [];
for i = 1:numel(controls)
ctrl = controls(i);
% Проверяем, вкладка ли это и совпадает ли имя
if isa(ctrl, 'Simulink.dialog.Tab') && strcmp(ctrl.Name, targetName)
tab = ctrl;
return;
end
% Если это контейнер — обходим его детей
children = get_children(ctrl);
if ~isempty(children)
tab = find_tab_by_name(children, targetName);
if ~isempty(tab)
return;
end
end
end
end
function children = get_children(ctrl)
if isprop(ctrl, 'DialogControls')
children = ctrl.DialogControls;
elseif isprop(ctrl, 'Controls')
children = ctrl.Controls;
elseif isprop(ctrl, 'Children')
children = ctrl.Children;
else
children = [];
end
end