function varargout= dspblkdspconst(action,varargin)
% DSPBLKDSPCONST DSP Blockset DSP Constant block helper function.

% Copyright 1995-2000 The MathWorks, Inc.
% $Revision: 1.5 $ $Date: 2000/06/19 22:06:29 $
      
blk     = gcbh;
fullblk = getfullname(blk);

if nargin==0, action = 'dynamic'; end

% Commenting these out because Simulink now greys out non-tunable parameters:
%SimStatus    = get_param(bdroot(gcs),'SimulationStatus');
%isSimStopped = strcmp(SimStatus,'stopped');

%if isSimStopped
    % We only want to check these values if the simulation is stopped.
    % Everything besides the Value parameter is non-tunable
    
	interpretAs1D  = strcmp(get_param(blk,'InterpretAs1D'),'on');
	framebased     = strcmp(get_param(blk,'FramebasedOutput'),'on');
	Mode           = get_param(blk,'SampleMode');	
	isDiscrete     = strncmp(Mode,'Discrete',8);

	const_blk   = [fullblk '/Constant'];
	frame_blk   = [fullblk '/Frame Status Conversion'];
    
	frameStr    = get_param(frame_blk, 'outframe');
    enables     = get_param(blk,'MaskEnables');
    maskPrompts = get_param(blk,'MaskPrompts');

    % Set the Simulink Constant block to mirror what is set in the
    % Interpret vectors as 1-D parameter in the DSP Constant block.
    % Also, turn the frame-based checkbox on/off accordingly.
    
  	switch action
    case 'init'
        % Update the blocks underneath only when Apply or OK are clicked on
        % Update the Frame Status Conversion block:
        if framebased,
    		if ~strcmp(frameStr,'Frame-based')
        		set_param(frame_blk, 'outframe', 'Frame-based');  
    		end
		else 
        	% Sample-based:
 	   		if ~strcmp(frameStr,'Sample-based')
  	      		set_param(frame_blk, 'outframe', 'Sample-based');  
    		end
		end      
        % Update the Simulink Constant block:
        if interpretAs1D,
 	   		if ~strcmp(get_param(const_blk, 'VectorParams1D'),'on')
      		 	set_param(const_blk, 'VectorParams1D', 'on');
			end
        else
 	   		if ~strcmp(get_param(const_blk, 'VectorParams1D'),'off')
                set_param(const_blk, 'VectorParams1D', 'off');
			end
        end

	case 'dynamic'
   	% Update enable-status of: sample time and frame status:
    	if isDiscrete,
        	ena = 'on';
    	else
        	ena = 'off';
    	end
    
        % Make sure that both the 1-D and frame-based checkboxes
        % are not checked at the same time:
        if interpretAs1D,
         	if ~strcmp(enables{5},'off')
            	enables(5) = {'off'};
         		set_param(blk,'MaskEnables',enables);
         	end
		else
        	if ~strcmp(enables{5},'on') & isDiscrete
            	enables(5) = {'on'};
         		set_param(blk,'MaskEnables',enables);
        	end
		end

        if framebased
            % Disable the 1-D paramter:
        	if ~strcmp(enables{2},'off')
         		enables(2) = {'off'};
         		set_param(blk,'MaskEnables',enables);
        	end
            
        	% Change "Sample time" to "Frame period" in mask prompts:
        	if ~strcmp(maskPrompts(4),'Frame period:')
            	maskPrompts(4) = {'Frame period:'};
        		set_param(blk,'MaskPrompts',maskPrompts);
	    	end
        else
            % Sample-based:
        	% Make sure 1-D option is enabled for sample-based mode:
        	if ~strcmp(enables{2},'on')
            	enables(2) = {'on'};
         		set_param(blk,'MaskEnables',enables);
        	end
            
        	% Make sure frame-based checkbox is enabled for discrete, 2-D outputs:
        	if isDiscrete & ~interpretAs1D & ~strcmp(enables{5},'on')
        		enables(5) = 'on';
	        	set_param(blk,'MaskEnables',enables);
 	    	end               

        	% Change "Frame period" to "Sample time" in mask prompts:
    		if ~strcmp(maskPrompts(4),'Sample time:')
            	maskPrompts(4) = {'Sample time:'};
        		set_param(blk,'MaskPrompts',maskPrompts);
    		end
        end

        % Make sure 1-D is enabled for Continuous, sample-based mode:
      	if ~isDiscrete & ~strcmp(enables{2},'on'),
	     	enables(2) = {'on'};
         	set_param(blk,'MaskEnables',enables);
     	end
                                      
        % Set the frame-based and 1-D options:
      	if ~strcmp(enables{4},ena),
            % Set both Sample time and frame checkbox
	     	enables(4:5) = {ena};
            if interpretAs1D & isDiscrete
                if framebased
                    % Frames take precedence if both 
                    % are checked so, disable 1-D option.
                	enables(2) = {'off'};
            	else
                    % Disable frame checkbox and verify 1-D
                    % option is enabled.
                	enables(2) = {'on'};
                	enables(5) = {'off'};
            	end
            end
         	set_param(blk,'MaskEnables',enables);
     	end
     
 	case 'update'    
      	%%%%% Insert/Remove Zero-Order Hold block %%%%
      	if isDiscrete
         	insert_zoh_block(blk);       
      	else
         	remove_zoh_block(blk);
      	end     
 	end
    %end   
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function insert_zoh_block(blk)
% If the block doesn't exist, then insert it

fullblk  = getfullname(blk);
zoh_blk  = [fullblk '/zoh'];

if ~exist_block(blk, 'zoh')
   delete_line(fullblk,'Constant/1','Frame Status Conversion/1');
   add_block('built-in/Zero-Order Hold',zoh_blk);
   
   set_param(zoh_blk,'Position',[90 22 120 48]);                        

   add_line(fullblk,'Constant/1','zoh/1');  
   add_line(fullblk,'zoh/1','Frame Status Conversion/1');    

   set_param(zoh_blk,'SampleTime','Ts');
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function remove_zoh_block(blk)
%If the block exists, then remove it

fullblk   = getfullname(blk);
zoh_blk  = [fullblk '/zoh'];

if exist_block(blk, 'zoh')
   delete_line(fullblk,'Constant/1','zoh/1');
   delete_line(fullblk,'zoh/1','Frame Status Conversion/1');

   delete_block(zoh_blk);
   add_line(fullblk,'Constant/1','Frame Status Conversion/1')
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function present = exist_block(sys, name)

present = ~isempty(find_system(sys,'searchdepth',1,...
         'followlinks','on','lookundermasks','on','name',name));


% [EOF] dspblkdspconst.m