тест конфигов (пока не работает)
This commit is contained in:
parent
99ec69324d
commit
fffd725d91
121
config_reader.asv
Normal file
121
config_reader.asv
Normal file
@ -0,0 +1,121 @@
|
||||
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
|
||||
|
||||
147
config_reader.m
Normal file
147
config_reader.m
Normal file
@ -0,0 +1,147 @@
|
||||
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
|
||||
|
||||
BIN
matlab.mat
Normal file
BIN
matlab.mat
Normal file
Binary file not shown.
Binary file not shown.
14
mycode.c
14
mycode.c
@ -1,14 +0,0 @@
|
||||
void app_init() {
|
||||
// code of foo
|
||||
}
|
||||
|
||||
void app_step() {
|
||||
// code of foo
|
||||
}
|
||||
|
||||
void app_readInputs() {
|
||||
// code of foo
|
||||
}
|
||||
void app_writeOutputBuffer() {
|
||||
// code of foo
|
||||
}
|
||||
26
periph_config.asv
Normal file
26
periph_config.asv
Normal file
@ -0,0 +1,26 @@
|
||||
{
|
||||
"ADC1": {
|
||||
"Defines": {
|
||||
"ENABLE": {
|
||||
"Type": "checkbox",
|
||||
"Default": true
|
||||
},
|
||||
"SAMPLE_RATE": {
|
||||
"Type": "edit",
|
||||
"Default": 48000
|
||||
}
|
||||
}
|
||||
},
|
||||
"USART1": {
|
||||
"Defines": {
|
||||
"ENABLE": {
|
||||
"Type": "checkbox",
|
||||
"Default": false
|
||||
},
|
||||
"BAUDRATE": {
|
||||
"Type": "edit",
|
||||
"Default": 115200
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
26
periph_config.json
Normal file
26
periph_config.json
Normal file
@ -0,0 +1,26 @@
|
||||
{
|
||||
"ADC1": {
|
||||
"Defines": {
|
||||
"ENABLE": {
|
||||
"Type": "checkbox",
|
||||
"Default": true
|
||||
},
|
||||
"SAMPLE_RATE": {
|
||||
"Type": "edit",
|
||||
"Default": 48000
|
||||
}
|
||||
}
|
||||
},
|
||||
"USART1": {
|
||||
"Defines": {
|
||||
"ENABLE": {
|
||||
"Type": "checkbox",
|
||||
"Default": false
|
||||
},
|
||||
"BAUDRATE": {
|
||||
"Type": "edit",
|
||||
"Default": 115200
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user