%% $Revision: 1.1.6.5 $
%% 
%%
%% Copyright 1994-2003 The MathWorks, Inc.
%%
%% Abstract: Trigonometric Functions block target file

%implements Trigonometry "C"

%% Function: BlockInstanceSetup ==============================================
%% Abstract:
%%   Set expression folding compliance
%%
%function BlockInstanceSetup(block, system) void
  %<LibBlockSetIsExpressionCompliant(block)>
%endfunction


%% Function: Outputs ==========================================================
%% Abstract:
%% 	The trigonometric functions implement the following math functions:
%%      sin, cos, tan, atan, sinh, cosh, tanh, atan2, asin, acos
%%	
%function Outputs(block, system) Output
  %assign trigOperator = ParamSettings.Operator
  %%  
  %if (Accelerator && (trigOperator == "acosh" || ...
    trigOperator == "asinh" || ...
    trigOperator == "atanh") )
    /* Call into Simulink for %<Type> Block with %<trigOperator> */
    %<SLibCallBlockInSimulink(system, block, "SS_CALL_MDL_OUTPUTS")>
  %else
    %% The Trig Function block does not currently support complex signals.
    %foreach ip = NumDataInputPorts
      %if LibBlockInputSignalIsComplex(ip)
	%%START_ASSERT
	%assign errTxt = "The Trigonometric Functions block does not " ...
	  "currently support complex signals in Real-Time Workshop."
	%<LibBlockReportError(block, errTxt)>.
	%%END_ASSERT
      %endif
    %endforeach
    %assign outputIsComplex = LibBlockOutputSignalIsComplex(0)
    %assign ioBaseType      =  LibGetDataTypeIdAliasedThruToFromId(LibBlockOutputSignalDataTypeId(0))
    %assign dtMinusOne      = SLibGetFormattedValueFromId(ioBaseType,-1)
    %assign dtOne           = SLibGetFormattedValueFromId(ioBaseType,1)  
    %assign dtZero          = SLibGetFormattedValueFromId(ioBaseType,0)  
    %assign rollVars = ["U", "Y"]
    %roll sigIdx = RollRegions, lcv = RollThreshold, block, "Roller", rollVars
      %assign u = LibBlockInputSignal(0, "", lcv, sigIdx)
      %if NumDataInputPorts==2
	%assign u2 = LibBlockInputSignal(1, "", lcv, sigIdx)
      %endif
      %assign y = LibBlockOutputSignal(0, "", lcv, "%<tRealPart>%<sigIdx>")
      %if outputIsComplex
	%assign yIm = LibBlockOutputSignal(0, "", lcv, "%<tImagPart>%<sigIdx>")
      %endif
      %if (trigOperator == "acosh" || ...
	trigOperator == "asinh" || ...
	trigOperator == "atanh")
	%%START_ASSERT
	%assign warnTxt = "'%<trigOperator>' is not an " ...
	  " ANSI-C function and may not be supported by your compiler."
	%<LibBlockReportWarning(block, warnTxt)>
	%%END_ASSERT
      %endif
      %%
      %% Cases :
      %%    sin, cos, tan, atan, sinh, cosh, tanh, atan2, asin, acos
      %%
      %if NumDataInputPorts==2
	%% atan2: note that rt_atan2(..) generates code to takes care of 0/0
	%<y> = %<LibGenMathFcnCall(trigOperator,ioBaseType,"%<u>","%<u2>")>;
      %else
	%% sin, cos, tan, atan, sinh, cosh, tanh,  asin, acos
	%switch trigOperator
	  %case "asin"
	  %case "acos"
	    if (%<u> >  %<dtOne>) {
	      %<y> = %<LibGenMathFcnCall(trigOperator,ioBaseType,"%<dtOne>","")>;
	    } else if (%<u> < %<dtMinusOne>) {
	      %<y> = %<LibGenMathFcnCall(trigOperator,ioBaseType,"%<dtMinusOne>","")>;
	    } else {
	      %<y> = %<LibGenMathFcnCall(trigOperator,ioBaseType,"%<u>","")>;
	    }
	    %break
	  %default
	    %<y> = %<LibGenMathFcnCall(trigOperator,ioBaseType,"%<u>","")>;
	%endswitch
      %endif
      %if outputIsComplex
	%if !Accelerator 
	  %switch trigOperator
	    %case "acosh"
	      %assign warnTxt = "The Trigonometric Function block has been " ...
		"set to output complex for '%<trigOperator>' operator. "...
		"The simulation results may be different from the generated code " ...
	        "if the input is less then zero."
	      %<LibBlockReportWarning(block, warnTxt)>	      
	      %break	  
	    %case "atanh"
	      %assign warnTxt = "The Trigonometric Function block has been " ...
		"set to output complex for '%<trigOperator>' operator. "...
		"The simulation results may different from the generated code " ...
	        "if the absolute value of the input is greater then one."
	      %<LibBlockReportWarning(block, warnTxt)>	      
	      %break	  
	    %default
	   %endswitch 
	 %endif
	%<yIm> = %<dtZero>;
      %endif
    %endroll
  %endif
%endfunction

%% Function: BlockOutputSignal =================================================
%% Abstract:
%%      Return an output expression.  This function *may*
%%      be used by Simulink when optimizing the Block IO data structure.
%%
%function BlockOutputSignal(block,system,portIdx,ucv,lcv,idx,retType) void
  %switch retType
    %case "Signal"
      %assign u = LibBlockInputSignal(0, ucv, lcv, idx)
      %assign ioBaseType      =  LibGetDataTypeIdAliasedThruToFromId(LibBlockOutputSignalDataTypeId(0))
      %assign trigOperator = ParamSettings.Operator
      %if (trigOperator == "acosh" || ...
	trigOperator == "asinh" || ...
	trigOperator == "atanh")
        %%START_ASSERT
	%assign warnTxt = "'%<trigOperator>' is not an " ...
	  " ANSI-C function and may not be supported by your compiler."
	%<LibBlockReportWarning(block, warnTxt)>
        %%END_ASSERT
      %endif
      %%
      %% Cases :
      %%    sin, cos, tan, atan, sinh, cosh, tanh
      %%
      %return "(" + LibGenMathFcnCall(trigOperator,ioBaseType,"%<u>","") + ")"
    %default
      %%START_ASSERT
      %assign errTxt = "Unsupported return type: %<retType>"
      %<LibBlockReportError(block,errTxt)>
      %%END_ASSERT
  %endswitch
%endfunction  
%% [EOF] trigfcn.tlc
