function varargout = commblkerrmeter(block, varargin)
% COMMBLKERRMETER Mask dynamic dialog function for error meter

% Author: Trefor Delve
% Copyright 1996-2000 The MathWorks, Inc.


% --- Modes of operation
%		varargin{1} = action =	'init' - Indicates that the block initialization
%													function is calling the code.
%													The code should call the functions to update
%													visibilities/enables etc. and based on these
%													settings, make any changes to the
%													simulink block.
%
%						= CallbackName		-	Indicates that a dialog field callback
%													is executing.
%
%		varargin{2} = 0					- Dynamic Dialog initiated callback
%						= 1					- Callback required to update fields only
%


nvarargin = nargin - 1;

switch nvarargin
	case 0
		action = '';
	case 1
		action = varargin{1};
		Args = 0;
	otherwise
		action = varargin{1};
		Args = varargin{2:end};
end;

% --- Field data

Vals	= get_param(block, 'maskvalues');
Vis	= get_param(block, 'maskvisibilities');
En  	= get_param(block, 'maskenables');

% --- Field numbers

SampleTime  	= 1;
RecDelay			= 2;
ODataMode		= 3;
WSName			= 4;
ResetMode2		= 5;


%*********************************************************************
% Function:			initialize
%
% Description:		Set the dialogs up based on the parameter values.
%						MaskEnables/MaskVisibles are not save in reference 
%						blocks.
%
% Inputs:			current block
%
% Return Values:	none
%
%********************************************************************

if(strcmp(action,'init'))
   feval(mfilename,block,'UpdateFields');
	feval(mfilename,block,'UpdateBlocks',Args);

   % --- No workspace arguments required
   %[varargout{1:3}] = feval(mfilename,block,'UpdateWs',Args);

end;



%----------------------------------------------------------------------
%
%	Block specific update functions
%
%----------------------------------------------------------------------

%*********************************************************************
% Function:			UpDateBlocks
%
% Description:		Set the simulink blocks as required
%
% Inputs:			current block
%
% Return Values:	none
%
%********************************************************************

if(strcmp(action,'UpdateBlocks'))

   if(strcmp(Vals{ResetMode2},'on'))
      AddReset(block);
   else
      RemoveReset(block);
   end
   
   if(strcmp(Vals{ODataMode},'Workspace'))
      if(AddWorkspace(block))
         set_param([block '/Error Vec'],'VariableName',Vals{WSName},'Buffer','1','Decimation','1','SampleTime','Ts');
      end;
      
      currentWS = get_param([block '/Error Vec'],'VariableName');
      
      if(~strcmp(currentWS,Vals{WSName}))
         set_param([block '/Error Vec'],'VariableName',Vals{WSName});
      end;
   else
      RemoveWorkspace(block);
   end;
end;

%*********************************************************************
% Function:			UpdateWs
%
% Description:		Set the simulink blocks as required
%
% Inputs:			current block
%
% Return Values:	none
%
%********************************************************************

if(strcmp(action,'UpdateWs'))
end;


%*********************************************************************
% Function:			UpdateFields
%
% Description:		Update the dialog fields.
%						Indicate that block updates are not required
%
% Inputs:			current block
%
% Return Values:	none
%
%********************************************************************

if(strcmp(action,'UpdateFields'))
	feval(mfilename,block,'ODataMode',1);
end;

%----------------------------------------------------------------------
%
%	Dynamic dialog specific field functions
%
%----------------------------------------------------------------------

%*********************************************************************
% Function Name:	'ODataMode'
%
% Description:		Deal with the output data mode
%
% Inputs:			current block
%
% Return Values:	none
%
%********************************************************************

if(strcmp(action,'ODataMode'))

   if(strcmp(Vals{ODataMode},'Workspace'))
		if(strcmp(Vis{WSName},'off'))
			Vis{WSName} = 'on';
			set_param(block,'MaskVisibilities',Vis);
			if(~Args)
            % --- Un-comment this if blocks are required to change
            %     when the dynamic dialog event occurs
				%feval(mfilename,block,'UpdateBlocks');
			end;
		end;
	elseif(strcmp(Vis{WSName},'on'))
		Vis{WSName} = 'off';
		set_param(block,'MaskVisibilities',Vis);
		if(~Args)
         % --- Un-comment this if blocks are required to change
         %     when the dynamic dialog event occurs
			%feval(mfilename,block,'UpdateBlocks');
		end;
	end

end;

%----------------------------------------------------------------------
%
%	Setup/Utility functions
%
%----------------------------------------------------------------------

%*********************************************************************
% Function Name:	'default'
%
% Description:		Set the block defaults (development use only)
%
% Inputs:			current block
%
% Return Values:	none
%
%********************************************************************

if(strcmp(action,'default'))

	Cb{SampleTime} 	= '';
	Cb{RecDelay} 		= '';
	Cb{ODataMode}		= [mfilename '(gcb,''ODataMode'');'];
	Cb{WSName}			= '';
	Cb{ResetMode2}		= '';

	En{SampleTime}		= 'on';
	En{RecDelay}		= 'on';
	En{ODataMode}		= 'on';
	En{WSName}			= 'on';
	En{ResetMode2}		= 'on';

	Vis{SampleTime}	= 'on';
	Vis{RecDelay}		= 'on';
	Vis{ODataMode}		= 'on';
	Vis{WSName}			= 'on';
	Vis{ResetMode2}	= 'on';

   % --- Get the MaskTunableValues 
   Tunable = get_param(block,'MaskTunableValues');
	Tunable{SampleTime}       = 'off';
	Tunable{RecDelay}         = 'off';
   Tunable{ODataMode}        = 'off';
	Tunable{WSName}           = 'off';
	Tunable{ResetMode2}       = 'off';

	% --- Set Callbacks, enable status, visibilities and tunable values
	set_param(block,'MaskCallbacks',Cb,'MaskEnables',En,'MaskVisibilities',Vis, 'MaskTunableValues', Tunable);

	% --- Set the startup values.  '' Indicates that the default saved will be used

	Vals{SampleTime}		= '1';
	Vals{RecDelay}			= '0';
	Vals{ODataMode}		= 'Workspace';
	Vals{WSName}			= 'ErrorVec';
	Vals{ResetMode2}		= 'off';
	
	MN = get_param(gcb,'MaskNames');
	for n=1:length(Vals)
		if(~isempty(Vals{n}))
			set_param(block,MN{n},Vals{n});
		end;
	end;

	% --- Update the Vals field with the actual values

	Vals	= get_param(block, 'maskvalues');

	% --- Set the default blocks and triggers etc.

	% --- External Reset
	if(strcmp(lower(Vals{ResetMode2}), 'off'))
		RemoveReset(block);
	else
		AddReset(block);
	end;

	% --- Set the default output mode

	if(strcmp(lower(Vals{ODataMode}), 'port'))
		RemoveWorkspace(block);
	else
		AddWorkspace(block);
		set_param([block '/Error Vec'],'VariableName',Vals{WSName},'Buffer','1','Decimation','1','SampleTime','Ts');
	end;

	% --- Ensure that the block operates correctly from a library
	set_param(block,'MaskSelfModifiable','on');

end;


%*********************************************************************
% Function Name:	show all
%
% Description:		Show all of the widgets
%
% Inputs:			current block
%
% Return Values:	none
%
% Notes:				This function is for development use only and allows
%						All fields to be displayed
%
%********************************************************************

if(strcmp(action,'showall'))

	Vis	= get_param(block, 'maskvisibilities');
	En  	= get_param(block, 'maskenables');

	Cb = {};
	for n=1:length(Vis)
		Vis{n} = 'on';
		En{n} = 'on';
		Cb{n} = '';
	end;

   set_param(block,'MaskVisibilities',Vis,'MaskEnables',En,'MaskCallbacks',Cb);

end;




%----------------------------------------------------------------------
%
%	Subfunctions
%
%----------------------------------------------------------------------

%*********************************************************************
% Function Name:	AddReset
%
% Description:		Remove the ground block and replace with a port
%
% Inputs:			current block
%
%*********************************************************************

function ECode = AddReset(Parent)

	if(~strcmp(get_param([Parent '/Rst'],'BlockType'),'Inport'))
      
      % --- Delete the line
      delete_line(Parent,'Rst/1','Compute ratio/2');
      
      % --- Get the position of the blocks
      blockPos = get_param([Parent '/Rst'],'Position');
      
      % --- Delete the block
      delete_block([Parent '/Rst']);
      
      % --- Add the new block
      add_block('built-in/Inport',[Parent '/Rst'],'Position',blockPos);
      
      % --- Add the new line
      add_line(Parent,'Rst/1','Compute ratio/2');

		ECode = 1;
      
   else
		ECode = 0;
	end;

return;

%*********************************************************************
% Function Name:	RemoveReset
%
% Description:		Remove the reset port and replace with a ground
%
% Inputs:			current block
%
%*********************************************************************

function ECode = RemoveReset(Parent)

	if(~strcmp(get_param([Parent '/Rst'],'BlockType'),'Ground'))

      % --- Delete the line
      delete_line(Parent,'Rst/1','Compute ratio/2');
      
      % --- Get the position of the blocks
      blockPos = get_param([Parent '/Rst'],'Position');
      
      % --- Delete the block
      delete_block([Parent '/Rst']);
      
      % --- Add the new block
      add_block('built-in/Ground',[Parent '/Rst'],'Position',blockPos);
      
      % --- Add the new line
      add_line(Parent,'Rst/1','Compute ratio/2');
      
      ECode = 1;
	else
		ECode = 0;
	end;


return;

%*********************************************************************
% Function Name:	AddWorkspace
%
% Description:		Remove the output port block and replace with 
%						to workspace
%
% Inputs:			current block
%
%*********************************************************************

function ECode = AddWorkspace(Parent)

	if(~strcmp(get_param([Parent '/Error Vec'],'BlockType'),'ToWorkspace'))
      
      % --- Delete the line
      delete_line(Parent,'Mux/1','Error Vec/1');
      
      % --- Get the position of the blocks
      blockPos = get_param([Parent '/Error Vec'],'Position');
      
      % --- Delete the block
      delete_block([Parent '/Error Vec']);
      
      % --- Add the new block
      add_block('built-in/ToWorkspace',[Parent '/Error Vec'],'Position',blockPos);
      
      % --- Add the new line
      add_line(Parent,'Mux/1','Error Vec/1');
      
		ECode = 1;
	else
		ECode = 0;
	end;
return;

%*********************************************************************
% Function Name:	RemoveWorkspace
%
% Description:		Remove the to workspace block and replace with 
%						a port
%
% Inputs:			current block
%
%*********************************************************************

function ECode = RemoveWorkspace(Parent)

	if(~strcmp(get_param([Parent '/Error Vec'],'BlockType'),'Outport'))

      % --- Delete the line
      delete_line(Parent,'Mux/1','Error Vec/1');
      
      % --- Get the position of the blocks
      blockPos = get_param([Parent '/Error Vec'],'Position');
      
      % --- Delete the block
      delete_block([Parent '/Error Vec']);
      
      % --- Add the new block
      add_block('built-in/Outport',[Parent '/Error Vec'],'Position',blockPos);
      
      % --- Add the new line
      add_line(Parent,'Mux/1','Error Vec/1');

		ECode = 1;      
   else
		ECode = 0;
	end;
return;




