function varargout = commblkerrormeterhelper(block, varargin)

% Copyright 1996-2000 The MathWorks, Inc.

%   $Revision: 1.7 $  $Date: 2000/05/19 14:29:39 $

% --- Modes of operation
%		varargin{1} = action =	'init' - Indicates that the block initialisation
%													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');

try
   Vis	= get_param(block, 'maskvisibilities');
   En  	= get_param(block, 'maskenables');
   DynDlg = 1;
catch
   Vis = [];
   En  = [];
   DynDlg = 0;
end;

% --- Field numbers

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


%*********************************************************************
% 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'))
   if(DynDlg)
   	feval(mfilename,block,'UpdateFields');
   end;

	feval(mfilename,block,'UpdateBlocks',Args);

	%[out1,out2]=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},'External'))
      AddReset(block);
      triggerType = lower(get_param([block '/Produce Ratio/Accumulate/Two Phase Enable/Trigger'],'TriggerType'));
      
      if(~strcmp(triggerType, lower(Vals{ExternalReset})))
         set_param([block '/Produce Ratio/Accumulate/Two Phase Enable/Trigger'],'TriggerType',Vals{ExternalReset});
      end;
      
   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);
	feval(mfilename,block,'ResetMode2',1);
	feval(mfilename,block,'ExternalReset',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)
				feval(mfilename,block,'UpdateBlocks');
			end;
		end;
	elseif(strcmp(Vis{WSName},'on'))
		Vis{WSName} = 'off';
		set_param(block,'MaskVisibilities',Vis);
		if(~Args)
			feval(mfilename,block,'UpdateBlocks');
		end;
	end

end;


%*********************************************************************
% Function Name:	'ResetMode2'
%
% Description:		Deal with the reset mode event
%
% Inputs:			current block
%
% Return Values:	none
%
%********************************************************************

if(strcmp(action,'ResetMode2'))

   if(strcmp(Vals{ResetMode2},'External'))
		if(strcmp(En{ExternalReset},'off'))
			En{ExternalReset} = 'on';
			set_param(block,'MaskEnables',En);
			if(~Args)
				feval(mfilename,block,'UpdateBlocks');
			end;
		end;
	elseif(strcmp(En{ExternalReset},'on'))
      En{ExternalReset} = 'off';
		set_param(block,'MaskEnables',En);
		if(~Args)
			feval(mfilename,block,'UpdateBlocks');
		end;
	end;

end;


%*********************************************************************
% Function Name:	'ExternalReset'
%
% Description:		Deal with the type of external reset mode
%
% Inputs:			current block
%
% Return Values:	none
%
%********************************************************************

if(strcmp(action,'ExternalReset'))
	if(~Args)
		feval(mfilename,block,'UpdateBlocks');
	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{ResetMode2}		= [mfilename '(gcb,''ResetMode2'');'];
	Cb{ExternalReset} = [mfilename '(gcb,''ExternalReset'');'];
	Cb{ODataMode}		= [mfilename '(gcb,''ODataMode'');'];
	Cb{WSName}			= '';

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

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

	% --- Set Callbacks, enable status and visibilities
   if(DynDlg)
   	set_param(block,'MaskCallbacks',Cb,'MaskEnables',En,'MaskVisibilities',Vis);
   end;

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

	Vals{SampleTime}		= '1';
	Vals{RecDelay}			= '0';
	Vals{ResetMode2}		= 'None';
	Vals{ExternalReset}	= 'Rising';
	Vals{ODataMode}		= 'Workspace';
	Vals{WSName}			= 'ErrorVec';
	
	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}), 'none'))
		RemoveReset(block);
	else
		AddReset(block);
	end;

	% --- External Reset trigger type
	set_param([block '/Produce Ratio/Accumulate/Two Phase Enable/Trigger'],'TriggerType',Vals{ExternalReset});

	% --- 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 '/Reset'],'BlockType'),'Inport'))
      
      % --- Delete the line
      delete_line(Parent,'Reset/1','Produce Ratio/3');
      
      % --- Get the position of the blocks
      blockPos = get_param([Parent '/Reset'],'Position');
      
      % --- Delete the block
      delete_block([Parent '/Reset']);
      
      % --- Add the new block
      add_block('built-in/Inport',[Parent '/Reset'],'Position',blockPos);
      
      % --- Add the new line
      add_line(Parent,'Reset/1','Produce Ratio/3');

		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 '/Reset'],'BlockType'),'Ground'))

      % --- Delete the line
      delete_line(Parent,'Reset/1','Produce Ratio/3');
      
      % --- Get the position of the blocks
      blockPos = get_param([Parent '/Reset'],'Position');
      
      % --- Delete the block
      delete_block([Parent '/Reset']);
      
      % --- Add the new block
      add_block('built-in/Ground',[Parent '/Reset'],'Position',blockPos);
      
      % --- Add the new line
      add_line(Parent,'Reset/1','Produce Ratio/3');
      
      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;




