%% $Revision: 1.1.6.4 $
%% 
%%
%% Copyright 1994-2003 The MathWorks, Inc.
%%
%% Abstract: Relay block target file.

%implements Relay "C"

%% Function: GetSigChName ======================================================
%% Abstract:
%%   return the defined signal channel variable name. It is used as an
%%   index of parameter values in frame mode.
%%
%function GetSigChName() Output
  %assign signalChannelName = "rt_sigChIdx"
  %return signalChannelName
%endfunction

%% Function: BlockInstanceSetup ================================================
%% Abstract: 
%%   Pre-code generation work
%%
%function BlockInstanceSetup(block, system) void
  %<LibBlockSetIsExpressionCompliant(block)>
%endfunction

%% Function: NonFrameOutputs ==============================================
%% Abstract:
%%         This function is called when in non-frame mode
%%
%function NonFrameOutputs(block, system) Output
    %%
    %assign y0IsComplex   = LibBlockOutputSignalIsComplex(0)
    %%
    %% create header comment
    %%
    %openfile commentBuffer
    %%
    %% add general comments
    %%
    %<FixPt_GeneralComments()>\
    %%
    %% add comments about parameters
    %%
     * On  Points Value parameter uses the same data type and scaling as Input0
     * Off Points Value parameter uses the same data type and scaling as Input0
     * On  Output Value parameter uses the same data type and scaling as Output0
     * Off Output Value parameter uses the same data type and scaling as Output0
    %%
    %% END: header comment
    %%
    %closefile commentBuffer
    %%
    %<LibCacheBlockComment(block,commentBuffer)>\
    %%
    %% Roll around signal width
    %%
    %%
    %if LibIsContinuous(TID)
      if (%<RTMIs("MajorTimeStep")>) {
    %endif
    %%
    %assign rollVars = \
      ["U", "<dwork>/Mode", "<param>/OnSwitchValue", "<param>/OffSwitchValue"]
    %%
    %roll sigIdx = RollRegions, lcv = RollThreshold, block, "Roller", rollVars
      %%
      %% Get input 0
      %%
      %assign u0Label = LibBlockInputSignal(0, "", lcv, sigIdx)
      %%
      %% Get Threshold
      %%
      %assign On_PointsLabel = LibBlockParameter(OnSwitchValue, "", lcv, sigIdx)
      %assign OffPointsLabel = LibBlockParameter(OffSwitchValue, "", lcv, sigIdx)
      %%
      %% Update mode
      %%
      %assign mode = LibBlockDWork(Mode, "", lcv, sigIdx)
      %%
      if ( %<u0Label> >= %<On_PointsLabel> ) {
	%<mode> = true;
      } else if ( %<u0Label> <= %<OffPointsLabel> ) {
	%<mode> = false;
      }
      %%
    %endroll
    %%
    %if LibIsContinuous(TID)
      }
    %endif
    %%
    %assign rollVars = \
      ["Y", "<dwork>/Mode", "<param>/OnOutputValue", "<param>/OffOutputValue"]
    %%
    %assign rollRegions = [0:%<LibBlockOutputSignalWidth(0) - 1>]
    %%
    %roll sigIdx = rollRegions, lcv = RollThreshold, block, "Roller", rollVars
      %%
      %if y0IsComplex
	%%
	%assign reSigIdx = tRealPart + STRING(sigIdx)
	%assign imSigIdx = tImagPart + STRING(sigIdx)
	%%
	%assign On_OutValReLabel = LibBlockParameter(OnOutputValue, "", lcv, reSigIdx)
	%assign OffOutValReLabel = LibBlockParameter(OffOutputValue, "", lcv, reSigIdx)
	%%
	%assign On_OutValImLabel = LibBlockParameter(OnOutputValue, "", lcv, imSigIdx)
	%assign OffOutValImLabel = LibBlockParameter(OffOutputValue, "", lcv, imSigIdx)
	%%
	%assign y0ReLabel = LibBlockOutputSignal(0, "", lcv, reSigIdx)
	%assign y0ImLabel = LibBlockOutputSignal(0, "", lcv, imSigIdx)
	%%
      %else
	%assign On_OutValReLabel = LibBlockParameter(OnOutputValue, "", lcv, sigIdx)
	%assign OffOutValReLabel = LibBlockParameter(OffOutputValue, "", lcv, sigIdx)
	%%
	%assign y0ReLabel = LibBlockOutputSignal(0, "", lcv, sigIdx)
	%%
      %endif
      %%
      %assign mode = LibBlockDWork(Mode, "", lcv, sigIdx)
      %%
      %if y0IsComplex
	if (%<mode>) {
	  %<y0ReLabel> =  %<On_OutValReLabel>;
	  %<y0ImLabel> =  %<On_OutValImLabel>;
	} else {
	  %<y0ReLabel> =  %<OffOutValReLabel>;
	  %<y0ImLabel> =  %<OffOutValImLabel>;
	}
      %else
	%<y0ReLabel> =  %<mode> ? %<On_OutValReLabel> : %<OffOutValReLabel>;
      %endif
      %%
    %endroll
    %%
    %% blank line for formating

%endfunction

%% Function: FrameOutputs =================================================
%% Abstract:
%%         This function is called when in frame mode
%%         
%function FrameOutputs(block, system) Output
    %%
    %assign y0IsComplex   = LibBlockOutputSignalIsComplex(0)
    %assign frameDims     = LibBlockInputSignalDimensions(0)
    %%
    %% create header comment
    %%
    %openfile commentBuffer
    %%
    %% add general comments
    %%
    %<FixPt_GeneralComments()>\
    %%
    %% add comments about parameters
    %%
     * On  Points Value parameter uses the same data type and scaling as Input0
     * Off Points Value parameter uses the same data type and scaling as Input0
     * On  Output Value parameter uses the same data type and scaling as Output0
     * Off Output Value parameter uses the same data type and scaling as Output0
    %%
    %% END: header comment
    %%
    %closefile commentBuffer
    %%
    %<LibCacheBlockComment(block,commentBuffer)>\
    {
    %%
    %% Roll around signal width
    %%
    %assign rollVars = ["U", "Y"]
    %%
    %roll sigIdx = RollRegions, lcv = RollThreshold, block, "Roller", rollVars
        %%
        %assign inputWidth = LibBlockInputSignalWidth(0)
        %%
        %% Get input 0
        %%
        %assign u0Label = LibBlockInputSignal(0, "", lcv, sigIdx)
	%%
        %% Get output
        %%
	%if y0IsComplex
            %assign reSigIdx = tRealPart + STRING(sigIdx)
	    %assign imSigIdx = tImagPart + STRING(sigIdx)
	    %%
	    %assign y0ReLabel = LibBlockOutputSignal(0, "", lcv, reSigIdx)
	    %assign y0ImLabel = LibBlockOutputSignal(0, "", lcv, imSigIdx)
	%else
	    %assign y0ReLabel = LibBlockOutputSignal(0, "", lcv, sigIdx)
        %endif
	%%
	%if inputWidth >= RollThreshold
	  %assign sigChName = GetSigChName()
	  int_T %<sigChName> = %<lcv> / %<frameDims[0]>; /* signal channel index */
	  %assign mode = LibBlockDWork(Mode, sigChName, "","")
	  %%
	  %assign On_PointsLabel = LibBlockParameter(OnSwitchValue, sigChName,"", "")
	  %assign OffPointsLabel = LibBlockParameter(OffSwitchValue,sigChName,"", "")
	  %assign On_OutValLabel = LibBlockParameter(OnOutputValue, sigChName, "", "")
	  %assign OffOutValLabel = LibBlockParameter(OffOutputValue,sigChName, "", "")
	  %%
	  %% Update mode
	  %%
	  if ( %<u0Label> >= %<On_PointsLabel> ) {
	    %<mode> = true;
	  } else if ( %<u0Label> <= %<OffPointsLabel> ) {
	    %<mode> = false;
	  }

	  %if y0IsComplex
	    if (%<mode>) {
	      %<y0ReLabel> =  %<On_OutValLabel>.re;
	      %<y0ImLabel> =  %<On_OutValLabel>.im;
	    } else {
	      %<y0ReLabel> =  %<OffOutValLabel>.re;
	      %<y0ImLabel> =  %<OffOutValLabel>.im;
	    }
	  %else
	    %<y0ReLabel> =  %<mode> ? %<On_OutValLabel> : %<OffOutValLabel>;
	  %endif
	  %%	
	%else
	  %assign sigChVal = sigIdx / frameDims[0]
	  %%
	  %assign mode = LibBlockDWork(Mode, "", "", sigChVal)
	  %%
	  %assign On_PointsLabel = LibBlockParameter(OnSwitchValue, "", "", sigChVal)
	  %assign OffPointsLabel = LibBlockParameter(OffSwitchValue, "", "", sigChVal)
	  %%
	  %if y0IsComplex
	    %assign reSigCh = tRealPart + STRING(sigChVal)
	    %assign imSigCh = tImagPart + STRING(sigChVal)
	    %%
	    %assign On_OutValReLabel = LibBlockParameter(OnOutputValue, "", "", reSigCh)
	    %assign OffOutValReLabel = LibBlockParameter(OffOutputValue, "", "", reSigCh)
	    %%
	    %assign On_OutValImLabel = LibBlockParameter(OnOutputValue, "", "", imSigCh)
	    %assign OffOutValImLabel = LibBlockParameter(OffOutputValue, "", "", imSigCh)
	  %else
	    %assign On_OutValReLabel = LibBlockParameter(OnOutputValue, "", "", sigChVal)
	    %assign OffOutValReLabel = LibBlockParameter(OffOutputValue, "", "", sigChVal)
	  %endif
	  %%
	  %% Implement relay code
	  %%
	  if ( %<u0Label> >= %<On_PointsLabel> ) {
	    %<mode> = true;
	  } else if ( %<u0Label> <= %<OffPointsLabel> ) {
	    %<mode> = false;
	  }
      
	  %if y0IsComplex
	    if (%<mode>) {
	      %<y0ReLabel> =  %<On_OutValReLabel>;
	      %<y0ImLabel> =  %<On_OutValImLabel>;
	    } else {
	      %<y0ReLabel> =  %<OffOutValReLabel>;
	      %<y0ImLabel> =  %<OffOutValImLabel>;
	    }
	  %else
	    %<y0ReLabel> =  %<mode> ? %<On_OutValReLabel> : %<OffOutValReLabel>;
	  %endif
	  %%		  
	%endif
      %endroll
    }
    %%
    %% blank line for formating
    
%endfunction


%% Function: ZeroCrossings =====================================================
%% Abstract:
%%      NSZC[i] = U[i] - threshold[i];  use threshold opposite of current mode
%%
%function ZeroCrossings(block, system) Output
  /* %<Type> Block: %<Name> */
  %if ParamSettings.InputContiguous == "yes"
    %% Input is also contiguous, do run-time loop
    %% ZC indices are always contiguous for this case
    %assign rollVars = ["U", "<dwork>/Mode", "NSZC", "<param>/OnSwitchValue", ...
      "<param>/OffSwitchValue"]
    %roll sigIdx = RollRegions, lcv = RollThreshold, block, "Roller", rollVars
      %assign mode = LibBlockDWork(Mode, "", lcv, sigIdx)
      %assign zc = LibBlockNonSampledZC("",lcv,sigIdx)
      %assign u  = LibBlockInputSignal(0, "", lcv, sigIdx)
      %assign switchOn = LibBlockParameter(OnSwitchValue, "", lcv, sigIdx)
      %assign switchOff = LibBlockParameter(OffSwitchValue, "", lcv, sigIdx)
      %%
      %<zc> = (%<mode>) ? (%<u> - %<switchOff>) : (%<u> - %<switchOn>);
      %%
    %endroll
  %else
    %% Input is not contiguous, do each element separately
    %foreach idx = NumNonsampledZCs
      %assign mode = LibBlockDWork(Mode, "", "", NonsampledZC[idx].MapIdx)
      %assign zc = LibBlockNonSampledZC("","",idx)
      %assign u  = LibBlockInputSignal(0, "", "", NonsampledZC[idx].MapIdx)
      %assign switchOn = ...
	LibBlockParameter(OnSwitchValue, "", "", NonsampledZC[idx].MapIdx)
      %assign switchOff = ...
	LibBlockParameter(OffSwitchValue, "", "", NonsampledZC[idx].MapIdx)
      %%
      %<zc> = (%<mode>) ? (%<u> - %<switchOff>) : (%<u> - %<switchOn>);
      %%
    %endforeach
  %endif
    
%endfunction


%% Function: Outputs ===========================================================
%% Abstract:
%%   The relay block allows the output to switch between two specified
%% values.  When the relay is on, it remains on until the input signal 
%% drops below the value of switchOff parameter.  When the relay is off, 
%% it remains off until the input exceeds the value of the switchOn 
%% parameter.
%%
%function Outputs(block, system) Output
  %if LibBlockInputSignalIsFrameData(0)           
      %<FrameOutputs(block, system)>
  %else 
      %<NonFrameOutputs(block, system)>
  %endif
%endfunction


%% [EOF] relay.tlc
