diff --git a/config_reader.asv b/config_reader.asv new file mode 100644 index 0000000..feeb816 --- /dev/null +++ b/config_reader.asv @@ -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 + diff --git a/config_reader.m b/config_reader.m new file mode 100644 index 0000000..08bc06c --- /dev/null +++ b/config_reader.m @@ -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 + 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 + 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 + diff --git a/matlab.mat b/matlab.mat new file mode 100644 index 0000000..cadce09 Binary files /dev/null and b/matlab.mat differ diff --git a/mcu_test_r2023.slx b/mcu_test_r2023.slx index 1a15a62..da4b025 100644 Binary files a/mcu_test_r2023.slx and b/mcu_test_r2023.slx differ diff --git a/mycode.c b/mycode.c deleted file mode 100644 index 9c9ece4..0000000 --- a/mycode.c +++ /dev/null @@ -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 -} \ No newline at end of file diff --git a/periph_config.asv b/periph_config.asv new file mode 100644 index 0000000..5ec730c --- /dev/null +++ b/periph_config.asv @@ -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 + } + } + } +} diff --git a/periph_config.json b/periph_config.json new file mode 100644 index 0000000..5ec730c --- /dev/null +++ b/periph_config.json @@ -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 + } + } + } +}