pre-release 1.02
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
classdef mcuMask
|
||||
|
||||
%% CALLBACKS
|
||||
methods(Static)
|
||||
% Following properties of 'maskInitContext' are avalaible to use:
|
||||
% - BlockHandle
|
||||
@@ -8,10 +8,10 @@ classdef mcuMask
|
||||
function MaskInitialization(maskInitContext)
|
||||
% Получаем хэндл текущего блока
|
||||
blk = gcbh;
|
||||
set_param(blk,"MaskSelfModifiable","on")
|
||||
set_param(blk, 'LinkStatus', 'none');
|
||||
% Получаем объект маски текущего блока
|
||||
mask = Simulink.Mask.get(gcb);
|
||||
set_param(blk,"MaskSelfModifiable","on")
|
||||
set_param(blk, 'LinkStatus', 'none');
|
||||
% mcuMask.disp(1,'');
|
||||
try
|
||||
% Проверка наличия findjobj
|
||||
@@ -19,8 +19,6 @@ classdef mcuMask
|
||||
catch
|
||||
findjobjAvailable = false;
|
||||
end
|
||||
% Получаем объект маски текущего блока
|
||||
mask = Simulink.Mask.get(gcb);
|
||||
% Имя checkbox-параметра (укажите точное имя из маски)
|
||||
checkboxParamName = 'extConsol'; % пример
|
||||
findjobjLinkName = 'findjobj_link'; % пример
|
||||
@@ -60,156 +58,37 @@ classdef mcuMask
|
||||
end
|
||||
|
||||
%% WRAPPER PARAMS
|
||||
function wrapperPath_add(callbackContext)
|
||||
mcuPath.addPath('wrapperPath');
|
||||
end
|
||||
|
||||
function enableThreading(callbackContext)
|
||||
block = gcb;
|
||||
maskNames = get_param(block, 'MaskNames');
|
||||
maskValues = get_param(block, 'MaskValues');
|
||||
maskEnables = get_param(block, 'MaskEnables');
|
||||
idxEnable = find(strcmp(maskNames, 'enableThreading'));
|
||||
idxEdit = find(strcmp(maskNames, 'threadCycles'));
|
||||
if isempty(idxEnable) || isempty(idxEdit)
|
||||
error('Параметры enableThreading или threadCycles не найдены в маске');
|
||||
end
|
||||
val = maskValues{idxEnable};
|
||||
if strcmp(val, 'on')
|
||||
maskEnables{idxEdit} = 'on';
|
||||
else
|
||||
maskEnables{idxEdit} = 'off';
|
||||
end
|
||||
set_param(block, 'MaskEnables', maskEnables);
|
||||
mainWrap.enableThreading();
|
||||
end
|
||||
|
||||
function enableDeinit(callbackContext)
|
||||
block = gcb;
|
||||
maskNames = get_param(block, 'MaskNames');
|
||||
maskValues = get_param(block, 'MaskValues');
|
||||
maskEnables = get_param(block, 'MaskEnables');
|
||||
idxEnable = find(strcmp(maskNames, 'enableThreading'));
|
||||
idxEdit = find(strcmp(maskNames, 'threadCycles'));
|
||||
if isempty(idxEnable) || isempty(idxEdit)
|
||||
error('Параметры enableThreading или threadCycles не найдены в маске');
|
||||
end
|
||||
val = maskValues{idxEnable};
|
||||
if strcmp(val, 'on')
|
||||
maskEnables{idxEdit} = 'on';
|
||||
else
|
||||
maskEnables{idxEdit} = 'off';
|
||||
end
|
||||
set_param(block, 'MaskEnables', maskEnables);
|
||||
mainWrap.enableDeinit();
|
||||
end
|
||||
|
||||
function extConsol(callbackContext)
|
||||
block = gcb;
|
||||
mask = Simulink.Mask.get(block);
|
||||
fullOut = mask.getParameter('fullOutput');
|
||||
extCons = mask.getParameter('extConsol');
|
||||
if isempty(extCons) || isempty(fullOut)
|
||||
error('Параметры fullOutput или extConsol не найдены в маске');
|
||||
end
|
||||
|
||||
if(strcmp(extCons.Enabled, 'on'))
|
||||
if strcmp(extCons.Value, 'on')
|
||||
fullOut.Enabled = 'off';
|
||||
fullOut.Value = 'on';
|
||||
else
|
||||
fullOut.Enabled = 'on';
|
||||
end
|
||||
else
|
||||
fullOut.Enabled = 'on';
|
||||
end
|
||||
|
||||
mainWrap.extConsol();
|
||||
end
|
||||
|
||||
function wrapperPath_add(callbackContext)
|
||||
block = gcb;
|
||||
mask = Simulink.Mask.get(block);
|
||||
% Открываем окно выбора папки
|
||||
folderPath = uigetdir('', 'Выберите папку');
|
||||
% Проверка на отмену
|
||||
if isequal(folderPath, 0)
|
||||
return;
|
||||
end
|
||||
% Установка значения параметра маски
|
||||
rel = mcuMask.absoluteToRelativePath(folderPath);
|
||||
param = mask.getParameter('wrapperPath');
|
||||
param.Value = rel;
|
||||
|
||||
end
|
||||
%% USER WRAPPER CODE
|
||||
|
||||
function wrapperFunc(callbackContext)
|
||||
block = gcb;
|
||||
% Получаем имя функции и путь к файлам
|
||||
[filename, section, tool, example]= mcuMask.getWrapperUserFile(block);
|
||||
mcuMask.tool(tool, example);
|
||||
|
||||
% Загружаем содержимое файла
|
||||
set_param(block, 'wrapperCode', '');
|
||||
try
|
||||
code = fileread(filename);
|
||||
code = regexprep(code, '\r\n?', '\n'); % нормализуем окончания строк
|
||||
|
||||
includesText = editCode.extractSection(code, section);
|
||||
set_param(block, 'wrapperCode', includesText);
|
||||
catch
|
||||
end
|
||||
% % Поиск тела обычной функции
|
||||
% expr = sprintf('void %s()', sel);
|
||||
% funcBody = editCode.extractSection(code, expr);
|
||||
% set_param(block, 'wrapperCode', funcBody);
|
||||
end
|
||||
|
||||
function saveWrapperCode(callbackContext)
|
||||
block = gcb;
|
||||
|
||||
% Получаем имя функции и путь к файлам
|
||||
[filename, section] = mcuMask.getWrapperUserFile(block);
|
||||
if ~isfile(filename)
|
||||
errordlg(['Файл не найден: ', filename]);
|
||||
return;
|
||||
end
|
||||
|
||||
sel = get_param(block, 'wrapperFunc');
|
||||
basePath = get_param(block, 'wrapperPath');
|
||||
if isempty(basePath)
|
||||
errordlg('Не указан путь к файлам обёртки (wrapperPath).');
|
||||
return;
|
||||
end
|
||||
newBody = get_param(block, 'wrapperCode');
|
||||
code = fileread(filename);
|
||||
code = regexprep(code, '\r\n?', '\n');
|
||||
newBody = strrep(newBody, '\', '\\');
|
||||
code = editCode.insertSection(code, section, newBody);
|
||||
% else
|
||||
% % Обновляем тело функции
|
||||
% expr = sprintf('void %s()', sel);
|
||||
% code = editCode.insertSection(code, expr, newBody);
|
||||
% end
|
||||
fid = fopen(filename, 'w', 'n', 'UTF-8');
|
||||
if fid == -1
|
||||
errordlg('Не удалось открыть файл для записи');
|
||||
return;
|
||||
end
|
||||
fwrite(fid, code);
|
||||
fclose(fid);
|
||||
mcuMask.disp(1, ['Обновлено: ' sel]);
|
||||
function appWrapperPath_add(callbackContext)
|
||||
mcuPath.addPath('appWrapperPath');
|
||||
end
|
||||
|
||||
function openWrapperCode(callbackContext)
|
||||
block = gcb;
|
||||
function appWrapperFunc(callbackContext)
|
||||
appWrap.appWrapperFunc();
|
||||
end
|
||||
|
||||
function saveAppWrapperCode(callbackContext)
|
||||
appWrap.saveAppWrapperCode();
|
||||
end
|
||||
|
||||
% Получаем имя функции и путь к файлам
|
||||
filename = mcuMask.getAbsolutePath(mcuMask.getWrapperUserFile(block));
|
||||
if exist(filename, 'file') == 2
|
||||
% Формируем команду без кавычек
|
||||
cmd = sprintf('rundll32.exe shell32.dll,OpenAs_RunDLL %s', filename);
|
||||
status = system(cmd);
|
||||
if status ~= 0
|
||||
errordlg('Не удалось открыть окно выбора приложения.');
|
||||
end
|
||||
else
|
||||
errordlg('Файл не найден');
|
||||
end
|
||||
function openAppWrapperCode(callbackContext)
|
||||
appWrap.openAppWrapperCode();
|
||||
end
|
||||
|
||||
%% USER CODE
|
||||
@@ -222,106 +101,37 @@ classdef mcuMask
|
||||
end
|
||||
|
||||
function btnAddSrc(callbackContext)
|
||||
blockHandle = gcb;
|
||||
% Открываем проводник для выбора файлов
|
||||
[files, pathstr] = uigetfile({ ...
|
||||
'*.c;*.cpp', 'Исходные файлы (*.c, *.cpp)'; ...
|
||||
'*.obj;*.lib', 'Библиотеки (*.obj, *.lib)'; ...
|
||||
'*.*', 'Все файлы (*.*)'}, ...
|
||||
'Выберите файлы', ...
|
||||
'MultiSelect', 'on');
|
||||
|
||||
if isequal(files, 0)
|
||||
return; % Отмена выбора
|
||||
end
|
||||
if ischar(files)
|
||||
files = {files}; % Один файл — в cell
|
||||
end
|
||||
% Парсим строку в cell-массив
|
||||
oldTable = customtable.parse('srcTable');
|
||||
|
||||
% Добавляем новые пути, проверяя уникальность
|
||||
for i = 1:numel(files)
|
||||
fullpath = fullfile(pathstr, files{i});
|
||||
rel = mcuMask.absoluteToRelativePath(fullpath);
|
||||
if ~any(strcmp(rel, oldTable))
|
||||
oldTable{end+1, 1} = rel;
|
||||
end
|
||||
end
|
||||
|
||||
% Парсим строку в cell-массив
|
||||
customtable.collect('srcTable', oldTable);
|
||||
|
||||
mcuPath.addSourceFileTable('srcTable', 'Выберите исходные файлы');
|
||||
end
|
||||
|
||||
function btnAddInc(callbackContext)
|
||||
blockHandle = gcb;
|
||||
% Открываем проводник для выбора папок
|
||||
pathstr = uigetdir(pwd, 'Выберите папку с заголовочными файлами');
|
||||
if isequal(pathstr, 0)
|
||||
return; % Отмена выбора
|
||||
end
|
||||
% Парсим таблицу
|
||||
oldTable = customtable.parse('incTable');
|
||||
|
||||
rel = mcuMask.absoluteToRelativePath(pathstr);
|
||||
|
||||
% Проверяем наличие пути
|
||||
if ~any(strcmp(rel, oldTable))
|
||||
oldTable{end+1, 1} = rel;
|
||||
end
|
||||
|
||||
% Собираем таблицу
|
||||
customtable.collect('incTable', oldTable);
|
||||
mcuPath.addPathTable('incTable', 'Выберите папку с заголовочными файлами');
|
||||
end
|
||||
|
||||
%% PERIPH CONFIG
|
||||
|
||||
function periphPath_add(callbackContext)
|
||||
block = gcbh;
|
||||
mask = Simulink.Mask.get(block);
|
||||
[file, path] = uigetfile({'*.*','Все файлы (*.*)'}, 'Выберите файл');
|
||||
if isequal(file, 0) || isequal(path, 0)
|
||||
% Отмена выбора — ничего не делаем
|
||||
return;
|
||||
end
|
||||
fullFilePath = fullfile(path, file);
|
||||
rel = mcuMask.absoluteToRelativePath(fullFilePath);
|
||||
param = mask.getParameter('periphPath');
|
||||
param.Value = rel;
|
||||
mcuPath.addAnyFile('periphPath');
|
||||
end
|
||||
|
||||
function compile(callbackContext)
|
||||
addpath('MCU_Wrapper');
|
||||
mexing(1);
|
||||
end
|
||||
|
||||
|
||||
function updateModel(callbackContext)
|
||||
addpath('MCU_Wrapper');
|
||||
res = mexing(1);
|
||||
if res ~= 0
|
||||
return;
|
||||
end
|
||||
|
||||
function periphUpdate(callbackContext)
|
||||
modelName = bdroot(gcb); % получить имя верхнего уровня модели
|
||||
blockName = gcb;
|
||||
mgr = asynchManage(modelName, blockName); % создать объект класса
|
||||
mgr.saveAndUpdateModel(); % запустить сохранение и обновление
|
||||
mgr.updateGUIfromConfig(); % запустить сохранение и обновление
|
||||
end
|
||||
|
||||
|
||||
function findjobj_link(callbackContext)
|
||||
web('https://www.mathworks.com/matlabcentral/fileexchange/14317-findjobj-find-java-handles-of-matlab-graphic-objects');
|
||||
%% COMPILE
|
||||
function compile(callbackContext)
|
||||
compiler.compile();
|
||||
end
|
||||
|
||||
function set_name()
|
||||
function setSFuncName(callbackContext)
|
||||
block = gcb;
|
||||
% Получаем параметр имени S-Function из маски блока
|
||||
newName = mcuMask.get_name();
|
||||
|
||||
% Путь к файлу, в котором надо заменить строку
|
||||
cFilePath = fullfile(pwd, './MCU_Wrapper/MCU.c'); % <-- укажи правильный путь
|
||||
cFilePath = fullfile(pwd, mcuPath.get('wrapperPath'), 'MCU.c'); % <-- укажи правильный путь
|
||||
|
||||
% Считаем файл в память
|
||||
try
|
||||
@@ -349,93 +159,29 @@ classdef mcuMask
|
||||
fclose(fid);
|
||||
end
|
||||
|
||||
function name = get_name()
|
||||
block = gcb;
|
||||
% Получаем параметр имени S-Function из маски блока
|
||||
name = get_param(block, 'sfuncName');
|
||||
%% LINK TO EXTERNAL CONSOLE
|
||||
function findjobj_link(callbackContext)
|
||||
web https://www.mathworks.com/matlabcentral/fileexchange/14317-findjobj-find-java-handles-of-matlab-graphic-objects;
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
%% SPECIFIC TOOLS
|
||||
methods(Static, Access = private)
|
||||
|
||||
function [filename, section, tool, example] = getWrapperUserFile(block)
|
||||
sel = get_param(block, 'wrapperFunc');
|
||||
basePath = get_param(block, 'wrapperPath');
|
||||
if isempty(basePath)
|
||||
errordlg('Не указан путь к файлам обёртки (wrapperPath).');
|
||||
return;
|
||||
end
|
||||
% Формируем путь к файлу в зависимости от типа запроса
|
||||
if strcmp(sel, 'Includes')
|
||||
filename = fullfile(basePath, 'app_includes.h');
|
||||
section = '// INCLUDES';
|
||||
tool = 'Инклюды для доступа к коду МК в коде оболочке';
|
||||
example = '#include "main.h"';
|
||||
elseif strcmp(sel, 'Dummy')
|
||||
filename = fullfile(basePath, 'app_wrapper.c');
|
||||
section = '// DUMMY';
|
||||
tool = 'Заглушки для различных функций и переменных';
|
||||
example = ['CAN_HandleTypeDef hcan = {0};' newline...
|
||||
'void hardware_func(handle *huart) {}' newline...
|
||||
'int wait_for_hardware_flag(int *flag) {' newline...
|
||||
' return 1;' newline...
|
||||
'}' newline...
|
||||
''];
|
||||
elseif strcmp(sel, 'App Init')
|
||||
filename = fullfile(basePath, 'app_init.c');
|
||||
section = '// USER APP INIT';
|
||||
tool = ['Код для инициализации приложения МК.' newline newline...
|
||||
'Вызов функций инициализации, если не используется отдельный поток для main().'];
|
||||
example = 'init_func();';
|
||||
elseif strcmp(sel, 'App Step')
|
||||
filename = fullfile(basePath, 'app_wrapper.c');
|
||||
section = '// USER APP STEP';
|
||||
tool = ['Код приложения МК для вызова в шаге симуляции.' newline newline ...
|
||||
'Вызов функций программы МК, если не используется отдельный поток для main().'];
|
||||
example = 'step_func();';
|
||||
elseif strcmp(sel, 'App Inputs')
|
||||
filename = fullfile(basePath, 'app_io.c');
|
||||
section = '// USER APP INPUT';
|
||||
tool = ['Работа с буффером для портов S-Function' newline newline ...
|
||||
'Буфер в начале хранит входные порты S-Function, далее идут выходные порты:' newline ...
|
||||
'Buffer[0:15] - входной порт, Buffer[16:31] - входной 1 порт, ' newline ...
|
||||
'Buffer[32:47] - выходной 1 порт, Buffer[48:63] - выходной 2 порт'];
|
||||
example = ['// чтение 1-го элемента 0-го входного массива' newline...
|
||||
'app_variable_2 = ReadInputArray(0, 1);' newline newline...
|
||||
'// запись в буфер выходов' newline ...
|
||||
'app_variable_2 = Buffer[10];'];
|
||||
elseif strcmp(sel, 'App Outputs')
|
||||
filename = fullfile(basePath, 'app_io.c');
|
||||
section = '// USER APP OUTPUT';
|
||||
tool = ['Работа с буффером для портов S-Function' newline newline ...
|
||||
'Буфер в начале хранит входные порты S-Function, далее идут выходные порты:' newline ...
|
||||
'Buffer[0:15] - входной порт, Buffer[16:31] - входной 1 порт, ' newline ...
|
||||
'Buffer[32:47] - выходной 1 порт, Buffer[48:63] - выходной 2 порт'];
|
||||
example = ['// запись в 1-й элемент 0-го выходного массива' newline...
|
||||
'WriteOutputArray(app_variable, 0, 1);' newline newline ...
|
||||
'// запись в буфер выходов' newline ...
|
||||
'Buffer[XD_OUTPUT_START + 10] = app_variable_2;'];
|
||||
elseif strcmp(sel, 'App Deinit')
|
||||
filename = fullfile(basePath, 'app_init.c');
|
||||
section = '// USER APP DEINIT';
|
||||
tool = ['Код для деинициализации приложения МК.' newline newline ...
|
||||
'Можно деинициализировать приложение МК, для повторного запуска.'];
|
||||
example = 'memset(&htim1, sizeof(htim1), 0;';
|
||||
else
|
||||
tool = '';
|
||||
mcuMask.disp(0, '\nОшибка выбора типа секции кода: неизвестное значение');
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
%% GENERAL TOOLS
|
||||
methods(Static, Access = public)
|
||||
|
||||
function saveAndClose(blockPath)
|
||||
function updateModel()
|
||||
addpath(mcuPath.get('wrapperPath'));
|
||||
res = mexing(1);
|
||||
if res ~= 0
|
||||
return;
|
||||
end
|
||||
|
||||
% modelName = bdroot(gcb); % получить имя верхнего уровня модели
|
||||
% blockName = gcb;
|
||||
% mgr = asynchManage(modelName, blockName); % создать объект класса
|
||||
% mgr.saveAndUpdateModel(); % запустить сохранение и обновление
|
||||
end
|
||||
|
||||
function close(blockPath)
|
||||
try
|
||||
% Считываем текущее имя модели
|
||||
modelName = bdroot(blockPath);
|
||||
@@ -459,81 +205,13 @@ classdef mcuMask
|
||||
mcuMask.disp(clear_flag, '');
|
||||
end
|
||||
|
||||
|
||||
function absPath = getAbsolutePath(relPath)
|
||||
% relativeToAbsolutePath — преобразует относительный путь в абсолютный.
|
||||
%
|
||||
% Если путь уже абсолютный — возвращается он же, приведённый к канонической форме.
|
||||
% Если путь относительный — преобразуется относительно текущей директории.
|
||||
|
||||
% Проверка: абсолютный ли путь
|
||||
if ispc
|
||||
isAbsolute = ~isempty(regexp(relPath, '^[a-zA-Z]:[\\/]', 'once')) || startsWith(relPath, '\\');
|
||||
else
|
||||
isAbsolute = startsWith(relPath, '/');
|
||||
end
|
||||
|
||||
if isAbsolute
|
||||
% Канонизируем абсолютный путь (убираем ./, ../ и т.п.)
|
||||
absPath = char(java.io.File(relPath).getCanonicalPath());
|
||||
else
|
||||
% Строим абсолютный путь от текущей директории
|
||||
cwd = pwd;
|
||||
combined = fullfile(cwd, relPath);
|
||||
absPath = char(java.io.File(combined).getCanonicalPath());
|
||||
end
|
||||
function name = get_name()
|
||||
block = gcb;
|
||||
% Получаем параметр имени S-Function из маски блока
|
||||
name = get_param(block, 'sfuncName');
|
||||
end
|
||||
|
||||
|
||||
|
||||
function rel = absoluteToRelativePath(pathstr)
|
||||
% absoluteToRelativePath — преобразует абсолютный путь в относительный от текущей директории.
|
||||
%
|
||||
% Если путь находится в текущей директории или вложенной в неё — добавляется префикс './'
|
||||
% Если выше — формируются переходы '..'
|
||||
% Если путь совпадает с текущей директорией — возвращается '.'
|
||||
|
||||
% Получаем текущую рабочую директорию
|
||||
cwd = pwd;
|
||||
|
||||
% Преобразуем пути в канонические абсолютные пути
|
||||
fullpath = char(java.io.File(pathstr).getCanonicalPath());
|
||||
cwd = char(java.io.File(cwd).getCanonicalPath());
|
||||
|
||||
% Разбиваем пути на части
|
||||
targetParts = strsplit(fullpath, filesep);
|
||||
baseParts = strsplit(cwd, filesep);
|
||||
|
||||
% Находим длину общего префикса
|
||||
j = 1;
|
||||
while j <= min(length(targetParts), length(baseParts)) && strcmpi(targetParts{j}, baseParts{j})
|
||||
j = j + 1;
|
||||
end
|
||||
|
||||
% Формируем количество подъемов ".." из cwd
|
||||
numUps = length(baseParts) - (j - 1);
|
||||
ups = repmat({'..'}, 1, numUps);
|
||||
|
||||
% Оставшаяся часть пути после общего префикса
|
||||
rest = targetParts(j:end);
|
||||
|
||||
% Объединяем для получения относительного пути
|
||||
relParts = [ups, rest];
|
||||
rel = fullfile(relParts{:});
|
||||
|
||||
% Если путь пустой — это текущая директория
|
||||
if isempty(rel)
|
||||
rel = '.';
|
||||
end
|
||||
|
||||
% Если путь не содержит ".." и начинается внутри текущей директории — добавим './'
|
||||
if ~isempty(rest) && isempty(ups)
|
||||
rel = fullfile('.', rel);
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
function checkbox_state = read_checkbox(checkboxName)
|
||||
maskValues = get_param(gcbh, 'MaskValues');
|
||||
paramNames = get_param(gcbh, 'MaskNames');
|
||||
@@ -593,15 +271,7 @@ classdef mcuMask
|
||||
end
|
||||
end
|
||||
|
||||
function res = ternary(cond, valTrue, valFalse)
|
||||
if cond
|
||||
res = valTrue;
|
||||
else
|
||||
res = valFalse;
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
function tool(text, example)
|
||||
% Устанавливает заданный текст в параметр Text Area 'toolText' через объект маски
|
||||
|
||||
@@ -630,9 +300,13 @@ classdef mcuMask
|
||||
out = sprintf(varargin{:});
|
||||
end
|
||||
|
||||
out_now = get_param(gcb, 'consoleOutput');
|
||||
out_now = get_param(gcb, 'consoleOutput');
|
||||
if ~strcmp(out, '') && ~strcmp(out_now, '')
|
||||
set_param(gcb, 'consoleOutput', [out_now newline out]);
|
||||
else
|
||||
set_param(gcb, 'consoleOutput', [out_now out]);
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function updateModelAsync()
|
||||
|
||||
Reference in New Issue
Block a user