Code covered by the BSD License  

Highlights from
MPC555 Motor Control Function Blockset

MPC555 Motor Control Function Blockset

by

 

20 Mar 2006 (Updated )

Additional I/O blocks for MPC555 target - specifically targetting TPU functionality

mpc555_dptram_wizard_macros(varargin)
function [varargout] = mpc555_dptram_wizard_macros(varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FUNCTION [VARARGOUT] = MPC555_DPTRAM_WIZARD_MACROS(VARARGIN)
%
% Used within the mpc555_dptram_wizard
%
% Example:
%      mpc555_dptram_wizard_macros('DRAW', macro_filename, h)
%      mpc555_dptram_wizard_macros('EXTRACT', h_data, macro_filename)
%      mpc555_dptram_wizard_macros('DECODE', value_string, macro_filename)

macro_replacement = mpc555_dptram_macro_parameters();

switch varargin{1}
    case 'DRAW'
        varargout{1} = i_draw_macros(macro_replacement, varargin{2}, varargin{3});
    case 'EXTRACT'
        varargout{1} = i_extract_data(macro_replacement, varargin{2}, varargin{3});
    case 'DECODE'
        varargout{1} = i_decode_data(macro_replacement, varargin{2}, varargin{3});
    otherwise
        error ('Unsupported function');
end

end

function h_data = i_draw_macros(macro_replacement, macro_filename, h)
 
%% Work out if this is a macro file that requires configuration
macrofilenumber = 0;
for current_entry = 1:length(macro_replacement)
    [macropath, macroname] = fileparts(macro_filename);
    if strcmpi(macro_replacement(current_entry).Filename, macroname)
        macrofilenumber = current_entry;
    end
end

% Return if this is not a supported macro file
if macrofilenumber == 0
    macro_values = {};
    h_data = [];
    return;
end

% Take a copy of the number of modifiable parameters
numparams = length(macro_replacement(macrofilenumber).Parameter);

% A cell array of strings containing the available channel numbers
channels_available = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15'};

% Make sure the correct figure is selected
figure(h);

% Open the macro file for which the parameters need to be changed
fid = fopen(macro_filename, 'r');
asifiledata = fread(fid, '*char')';
fclose(fid);

% Fixed parts of the regular expression used
expression_beginning = '\%macro[ \t]+';
expression_spaces = '[ \t]+';
expression_data = '''\$([A-F0-9]+)''.';

for param_num = 1:numparams
    %% See if we can reread defaults from the text file first
    if ~strcmp(macro_replacement(macrofilenumber).Parameter(param_num).Name, '');
        expression_command = macro_replacement(macrofilenumber).Parameter(param_num).Name;
        usingname = true;
    else
        expression_command = macro_replacement(macrofilenumber).Parameter(param_num).Name0;
        usingname = false;
    end

    full_expression = [expression_beginning, expression_command, expression_spaces, expression_data];
    tok = regexp(asifiledata, full_expression, 'tokens');

    if usingname
        defaultvalue = num2str(hex2dec(tok{:}));
    else
        defaultvalue = num2str(hex2dec(tok{:})/16);
    end

    % Note figure position
    figureposition = get(h,'Position');
    figurebackground = get(h,'Color');
    
    % Create the menu items

    wizardtext = ['Configuring macro: ', macro_replacement(macrofilenumber).Filename];
    wizardtext = [wizardtext, sprintf('\n'), 'Please choose channels for each function.'];
    wizard_label_position = [20 figureposition(4)-40 figureposition(3)-40 30];
    
    h_data.wizardlabel = uicontrol('Style', 'text', ...
    'String', wizardtext, ...
    'BackgroundColor', figurebackground, ...
    'Position', wizard_label_position );

    h_data.h_text(param_num) = uicontrol('Style', 'text', ...
        'String', macro_replacement(macrofilenumber).Parameter(param_num).Description, ...
        'BackgroundColor', figurebackground, ...
        'Position', [20 figureposition(4)-(43 + 25 * param_num), figureposition(4)-(110), 20]);
    h_data.h_pulldown(param_num) = uicontrol('Style', 'popupmenu', ...
        'String', channels_available, ...
        'Value', strmatch(defaultvalue, channels_available,'exact'), ...
        'Position', [figureposition(3)-70 figureposition(4) - (40 + 25 * param_num), 50, 20]);
end

end


function macro_values = i_extract_data(macro_replacement, h_data, macro_filename)

%% Work out if this is a macro file that requires configuration
macrofilenumber = 0;
for current_entry = 1:length(macro_replacement)
    [macropath, macroname] = fileparts(macro_filename);
    if strcmpi(macro_replacement(current_entry).Filename, macroname)
        macrofilenumber = current_entry;
    end
end

% Return if this is not a supported macro file
if macrofilenumber == 0
    macro_values = {};
    return;
end

% A cell array of strings containing the available channel numbers
channels_available = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15'};

% Now build up the selection of values
macropos = 1;
% Take a copy of the number of modifiable parameters
numparams = length(macro_replacement(macrofilenumber).Parameter);
for param_num = 1:numparams
    % First use the standard name
    if ~strcmp(macro_replacement(macrofilenumber).Parameter(param_num).Name, '');
        macro_values{macropos,1} =  macro_replacement(macrofilenumber).Parameter(param_num).Name;
        macro_values{macropos,2} =  ['\$',dec2hex(str2num(channels_available{get(h_data.h_pulldown(param_num), 'Value')}))];
        macropos = macropos+1;
    end
    % Now if necessary use the Name0 parameter too
    if ~strcmp(macro_replacement(macrofilenumber).Parameter(param_num).Name0, '');
        macro_values{macropos,1} =  macro_replacement(macrofilenumber).Parameter(param_num).Name0;
        macro_values{macropos,2} =  ['\$',dec2hex(str2num(channels_available{get(h_data.h_pulldown(param_num), 'Value')})), '0'];
        macropos = macropos+1;
    end
end
end

function macro_values = i_decode_data(macro_replacement, valuestring, macro_filename)

%% Work out if this is a macro file that requires configuration
macrofilenumber = 0;
for current_entry = 1:length(macro_replacement)
    [macropath, macroname] = fileparts(macro_filename);
    if strcmpi(macro_replacement(current_entry).Filename, macroname)
        macrofilenumber = current_entry;
    end
end

% Return if this is not a supported macro file
if macrofilenumber == 0
    macro_values = {};
    return;
end

% Now build up the selection of values
macropos = 1;
% Take a copy of the number of modifiable parameters
numparams = length(macro_replacement(macrofilenumber).Parameter);

if length(valuestring) ~= numparams
    error(['Invalid number of macro parameters specified - ', num2str(numparams),' are required.']);
end

for param_num = 1:numparams
    % First use the standard name
    if ~strcmp(macro_replacement(macrofilenumber).Parameter(param_num).Name, '');
        macro_values{macropos,1} =  macro_replacement(macrofilenumber).Parameter(param_num).Name;
        macro_values{macropos,2} =  ['\$',dec2hex(hex2dec(valuestring(param_num)))];
        macropos = macropos+1;
    end
    % Now if necessary use the Name0 parameter too
    if ~strcmp(macro_replacement(macrofilenumber).Parameter(param_num).Name0, '');
        macro_values{macropos,1} =  macro_replacement(macrofilenumber).Parameter(param_num).Name0;
        macro_values{macropos,2} =  ['\$',dec2hex(hex2dec(valuestring(param_num))), '0'];
        macropos = macropos+1;
    end
end
end

Contact us