function varargout = commblkmapdata(block, varargin)
% COMMBLKMAPDATA Mask dynamic dialog function for data mapping block

% Copyright 1996-2016 The MathWorks, Inc.

nvarargin = nargin - 1;

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

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

switch action
  case 'init'
    cbMapMode(block);    
    [varargout{1:2}] = updateWS(varargin{2},varargin{3},varargin{4});

  case 'MapMode'
    % dynamic dialog specific field case
    cbMapMode(block);
end


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

function varargout = updateWS(maskMapMode,maskM,maskMapVec)
%*********************************************************************
% Function:		UpdateWS
% Description:		Set the simulink blocks as required
% Inputs:		mapping mode, symbol set size, mapping vector
% Return Values:	values to be set in the workspace
%********************************************************************

% --- Common parameter checks
if(maskMapMode ~= 4)
    if (~isscalar(maskM))
        error(message('comm:commblkmapdata:NonScalarSymbolSetSize'));
    end

    if(maskM < 2)
        error(message('comm:commblkmapdata:SymbolSetSizeLessThan2'));
    end

    if(maskM-floor(maskM) > 0)
        error(message('comm:commblkmapdata:NonIntSymbolSetSize'));
    end

    if(abs(log2(maskM)-round(log2(maskM))) > 0)
        error(message('comm:commblkmapdata:InvalidSymbolSetSize'));
    end
end

% --- Set the lookup table as required
switch maskMapMode

  case 1      % Binary to Gray
    EnFlag   = 1;
    IPValues = (0:maskM-1);
    OPValues = bitxor(IPValues,floor(IPValues/2));
  case 2      % Gray to binary
    EnFlag   = 1;
    IPValues = (0:maskM-1);
    [y,i]    = sort(bitxor(IPValues,floor(IPValues/2)));
    OPValues = i-1;
  case 3		% User defined mapping
    if(maskM ~= length(maskMapVec))
        error(message('comm:commblkmapdata:MismatchSizes'));
    end
    EnFlag   = 1;
    OPValues = maskMapVec;
  case 4		% Straight through mapping
    EnFlag   = 0;
    OPValues = 0:1023;  % Needed to prevent lookup table from erroring
end

% --- Set the output variables
varargout{1} = EnFlag;
varargout{2} = OPValues;


%----------------------------------------------------------------------
%	Dynamic Dialog specific field functions
%----------------------------------------------------------------------

function cbMapMode(block)
%*********************************************************************
% Function Name:	'MapMode'
% Description:		Deal with the mapping mode
% Inputs:			current block
% Return Values:	none
%********************************************************************

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

[idxMapMode,idxM,idxMapVec] = dspGetMaskParamIdx(block,'MapMode',...
                                                 'M','MapVec');

if(strncmp('User',   Vals{idxMapMode}, length('User')) || ...
   strncmp('Gray',   Vals{idxMapMode}, length('Gray')) || ...
   strncmp('Binary', Vals{idxMapMode}, length('Binary')))

    if(strcmp(En{idxMapVec},'off'))
        En{idxM} = 'on'; Vis{idxM} = 'on';
        if(strncmp('User', Vals{idxMapMode}, length('User')))
            En{idxMapVec} = 'on'; Vis{idxMapVec} = 'on';
        end
        set_param(block,'MaskEnables',En, 'maskVisibilities', Vis);

    elseif(strncmp('Gray',   Vals{idxMapMode}, length('Gray')) || ...
           strncmp('Binary', Vals{idxMapMode}, length('Binary')))
        En{idxMapVec} = 'off';Vis{idxMapVec} = 'off';
        set_param(block,'MaskEnables',En, 'maskVisibilities', Vis);
    end

elseif(strcmp(En{idxM},'on') || strcmp(En{idxMapVec},'on'))
    En{idxM} = 'off'; Vis{idxM} = 'off';
    En{idxMapVec} = 'off'; Vis{idxMapVec} = 'off';
    set_param(block,'MaskEnables',En, 'maskVisibilities', Vis);
end

% EOF - commblkmapdata.m
