function varargout = commblkpnseq2(block, varargin)
% COMMBLKPNSEQ2 Mask dynamic dialog function for PN Sequence Generator block.

% Copyright 1996-2000 The MathWorks, Inc.
%   $Revision: 1.1 $  $Date: 2000/06/06 22:39:37 $

% - 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
%

% --- Field numbers
Poly         = 1;
Ini_sta      = 2;
Ts           = 3;
FrameBased   = 4;
SampPerFrame = 5;

nvarargin = nargin - 1;

switch nvarargin
case 0
    action = '';
case 1
    action = varargin{1};
    Args = {};
otherwise
    action = varargin{1};
    Args = varargin{2:end};
    
    if(~strcmp(lower(action),'backprop'))
        % --- Breakout the arguments if required
        if((nvarargin > 1) & (isa(Args,'cell'))) end;
    end;
end;

% --- Exit code and error message definitions
eStr.ecode = 0;
eStr.emsg1  = '';
eStr.emsg2  = '';

% --- Field data
Vals  = get_param(block, 'maskvalues');
Vis   = get_param(block, 'maskvisibilities');
En    = get_param(block, 'maskenables');


%*********************************************************************
% 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'))
    cbFrameBased(block);
    
    poly = str2num(Vals{Poly});
    ini_sta = str2num(Vals{Ini_sta});
    
    % Check the initial state parameter
    if ~( ndims(ini_sta)==2 & min(size(ini_sta))==1 )
        error('The initial states must be a scalar or a vector of length equal to the order of the generator polynomial.');
    end
    if any(ini_sta~=1 & ini_sta~=0)
        error('The initial state values must be binary.');
    end
    
    % Check the polynomial parameter values
    if ~( ndims(poly)==2 & min(size(poly))==1 )
        error('Invalid generator polynomial parameter values.');
    end
    if max(poly)>=2
        error('Invalid generator polynomial parameter values.');
    elseif min(poly)<0
        if any(poly==1)
            error('Invalid generator polynomial parameter values.');
        end
        tmp = zeros(1, abs(min(poly))+1);
        for i = 1:length(poly), tmp(abs(poly(i))+1) = 1; end
        poly = tmp;
    end
    % Now poly is in the binary vector representation
    if ((poly(1) == 0) | (poly(end) == 0)),
        error('Invalid generator polynomial parameter values.');
    end
    s = length(poly)-1;
    if ~( (length(ini_sta) == s) | (length(ini_sta) == 1) )
        error('The initial states must be a scalar or a vector of length equal to the order of the generator polynomial.');
    end
    varargout{1} = poly;
    
end;

if(strcmp(action,'cbFrameBased'))
   cbFrameBased(block);
end

%----------------------------------------------------------------------
%
%	Dynamic dialog specific field functions
%
%----------------------------------------------------------------------
%----------------------------------------------------------------------
%
%	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{Poly}          = '';
	Cb{Ini_sta}       = '';
	Cb{Ts}            = '';
	Cb{FrameBased}    = 'commblkpnseq2(gcb,''cbFrameBased'');';
	Cb{SampPerFrame}  = '';

	En{Poly}          = 'on';
	En{Ini_sta}       = 'on';
	En{Ts}            = 'on';
    En{FrameBased}    = 'on';
	En{SampPerFrame}  = 'off';

	Vis{Poly}         = 'on';
	Vis{Ini_sta}      = 'on';
	Vis{Ts}           = 'on';
    Vis{FrameBased}   = 'on';
	Vis{SampPerFrame} = 'on';

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

	% --- Set the startup values.  '' Indicates that the default saved will be used
	Vals{Poly}         = '[1 1 0 1 1]';
	Vals{Ini_sta}      = '[1 1 0 1]';
	Vals{Ts}           = '1';
    Vals{FrameBased}   = 'off';
	Vals{SampPerFrame} = '1';

	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 copy function
	set_param(block,'CopyFcn','');

	% --- 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:	'cbFrameBased'
%
%  - Callback function for 'frameBased' parameter
%
%********************************************************************

function cbFrameBased(block)

Vals  = get_param(block, 'maskvalues');
En    = get_param(block, 'maskenables');
oldEnSamp   = En{5};

if(strcmp(Vals{4},'off'))
    En{5}  = 'off';
else % 'Frame-based'
    En{5}  = 'on';
end;

if ( strcmp(oldEnSamp, En{5}) == 0 ) 
    set_param(block,'MaskEnables',En);
end

%*******************************************
%
% Helper functions for attribute checking
%
%*******************************************

% --- ISINTEGER
%     Are all members of the vector/matrix integers
function ecode = isinteger(Vec)
    Vec   = abs(Vec(:));
    ecode = all((Vec - floor(Vec)) == 0);
return;

% --- ISSCALAR
function ecode = isscalar(in)

ecode = ndims(in)==2 & prod(size(in))==1;

% --- ISVECTOR
function ecode = isvector(in)

ecode = ndims(in)==2 & min(size(in))==1;

% --- ISMATRIX
function ecode = ismatrix(in)

ecode = ndims(in)==2;

% [EOF]
