function varargout = commblkbcgen(block, action, varargin)
%COMMBLKBCGEN Mask dialog function for Barker Code Generator block.
%
%   Syntax:
%    [bCode, sampPerFrame, eStr] = commblkbcgen(gcb,'init',length,ts,sampPerFrame)
%            - callback for initialization 
%    commblkbcgen(gcb,'cbFrameBased') 
%            - callback for frame based outputs

%   Copyright 1996-2011 The MathWorks, Inc.

% Exit code and error message definitions
eStr.ecode = 0;
eStr.eID   = '';
eStr.emsg  = '';

switch(action)
  case {'init'}
    %*********************************************************************
    % Function:         initialize
    % Description:      Set the dialogs based on the parameter values.
    % Inputs:           current block
    % Return Values:    bCode, sampPerFrame, eStr
    %*********************************************************************
    % read input values
    maskLength = varargin{1};
    maskTs = varargin{2};
    maskSampPerFrame = varargin{3};

    Vals  = get_param(block, 'maskvalues');
    idxFrameBased = dspGetMaskParamIdx(block,'frameBased');

    % Initial value 
    bCode = -1;

    cbFrameBased(block);
    if(strcmp(Vals{idxFrameBased},'off')) % Sample-based
        sampPerFrame = 1;
    else % Frame-based
        sampPerFrame = maskSampPerFrame;
    end

    % Return values in case of early exit
    varargout = {bCode, sampPerFrame, eStr};
    outLen = length(varargout);
    
    % Check the sample time parameter - scalar real
    if ( ~isscalar(maskTs) || ~isreal(maskTs) )
        eStr.eID   = 'comm:commblkbcgen:InvalidSampleTime';
        eStr.emsg  = 'The sample time parameter must be a real scalar.';
        eStr.ecode = 1; varargout{outLen} = eStr; return;
    end

    % Check the sample time parameter - no continuous sample times
    if ( maskTs <= 0  && maskTs ~= -1)
        eStr.eID   = 'comm:commblkbcgen:ContSampleTime';
        eStr.emsg  = 'Continuous sample times are not allowed.';
        eStr.ecode = 1; varargout{outLen} = eStr; return;
    end

    % Check the samples per frame parameter - scalar integer > 0 
    if ( ~isscalar(sampPerFrame) || ~is(sampPerFrame, 'integer') || ...
         sampPerFrame <= 0 )
        eStr.eID   = 'comm:commblkbcgen:InvalidSampPerFrame';
        eStr.emsg  = 'The samples per frame parameter must be a scalar integer greater than 0.';
        eStr.ecode = 1; varargout{outLen} = eStr; return;
    end

    % Set up the codes
    switch(str2double(maskLength))
      case 1
        bCode = -1;
      case 2
        bCode = [-1 1]; % or can be [-1 -1]
      case 3
        bCode = [-1 -1 1];
      case 4
        bCode = [-1 -1 1 -1]; % or can be [-1 -1 -1 1]
      case 5
        bCode = [-1 -1 -1 1 -1];
      case 7
        bCode = [-1 -1 -1 1 1 -1 1];
      case 11
        bCode = [-1 -1 -1 1 1 1 -1 1 1 -1 1];
      case 13
        bCode = [-1 -1 -1 -1 -1 1 1 -1 -1 1 -1 1 -1];
      otherwise
        eStr.eID   = 'comm:commblkbcgen:InvalidCodeLength';
        eStr.emsg  = 'Invalid code length parameter.';
        eStr.ecode = 1; varargout{outLen} = eStr; return;
    end

    bCode = feval(get_param(block,'outDataType'), bCode);

    % Set outputs with validated parameters            
    varargout = {bCode, sampPerFrame, eStr};

  case {'cbFrameBased'}
    cbFrameBased(block);
end

%----------------------------------------------------------------------
%               Dynamic-dialog specific field functions
%----------------------------------------------------------------------
%----------------------------------------------------------------------
% Function Name:    'cbFrameBased'
%       Callback function for 'frameBased' checkbox parameter
%----------------------------------------------------------------------
function cbFrameBased(block)

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

[idxFrameBased,idxSampPerFrame] = ...
    dspGetMaskParamIdx(block,'frameBased','sampPerFrame');
oldVisSampPerFrames   = Vis{idxSampPerFrame};

if(strcmp(Vals{idxFrameBased},'off')) % Sample-based
    En{idxSampPerFrame}  = 'off';
    Vis{idxSampPerFrame}  = 'off';

    set_param([block '/Frame Conversion'], 'outframe', 'Sample-based');
    set_param([block '/Reshape'], 'OutputDimensionality', '1-D array');
else % Frame-based
    En{idxSampPerFrame}  = 'on';
    Vis{idxSampPerFrame} = 'on';

    set_param([block '/Frame Conversion'],'outframe','Frame-based');
    set_param([block '/Reshape'], 'OutputDimensionality', 'Column vector');
end

if ( ~strcmp(oldVisSampPerFrames, Vis{idxSampPerFrame}) ) 
    set_param(block,'MaskEnables',En, 'MaskVisibilities', Vis);
end

% [EOF]
