/* * File : timestwo.c * Abstract: * An example C-file S-function for multiplying an input by 2, * y = 2*u * * Real-Time Workshop note: * This file can be used as is (noninlined) with the Real-Time Workshop * C rapid prototyping targets, or it can be inlined using the Target * Language Compiler technology and used with any target. See * matlabroot/toolbox/simulink/blocks/tlc_c/timestwo.tlc * matlabroot/toolbox/simulink/blocks/tlc_ada/timestwo.tlc * the C and Ada TLC code to inline the S-function. * * See simulink/src/sfuntmpl_doc.c * * Copyright 1990-2004 The MathWorks, Inc. * $Revision: 1.12.4.2 $ */ #define S_FUNCTION_NAME mysfunction #define S_FUNCTION_LEVEL 2 #include "simstruc.h" #include "tmwtypes.h " #include #include #include #include /*================* * Build checking * *================*/ /* Function: mdlInitializeSizes =============================================== * Abstract: * Setup sizes of the various vectors. */ static void mdlInitializeSizes(SimStruct *S) { ssSetNumSFcnParams(S, 0); if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) { return; /* Parameter mismatch will be reported by Simulink */ } if (!ssSetNumInputPorts(S, 3)) return; ssSetInputPortDimensionInfo(S, 0, DYNAMIC_DIMENSION); ssSetInputPortDimensionInfo(S, 1,DYNAMIC_DIMENSION); ssSetInputPortDimensionInfo(S, 2,DYNAMIC_DIMENSION); // DECL_AND_INIT_DIMSINFO(DataValid); // boolean_T Dvalid=1; // DataValid.numDims =1; // DataValid.dims = &Dvalid; // DataValid.width = SS_BOOLEAN; // ssSetInputPortDimensionInfo(S, 2, &DataValid); ssSetInputPortDirectFeedThrough(S, 0, 1); ssSetInputPortDirectFeedThrough(S, 1, 1); ssSetInputPortDirectFeedThrough(S, 2, 1); if (!ssSetNumOutputPorts(S,4)) return; ssSetOutputPortDimensionInfo(S, 0, DYNAMIC_DIMENSION); ssSetOutputPortDimensionInfo(S, 1, DYNAMIC_DIMENSION); ssSetOutputPortDimensionInfo(S, 2, DYNAMIC_DIMENSION); ssSetOutputPortDimensionInfo(S, 3, DYNAMIC_DIMENSION); // ssSetOutputPortWidth(S, 0, DYNAMICALLY_SIZED); // ssSetOutputPortWidth(S, 1, DYNAMICALLY_SIZED); // ssSetOutputPortWidth(S, 2, DYNAMICALLY_SIZED); // ssSetOutputPortWidth(S, 3,SS_BOOLEAN); // ssSetOutputPortDataType( S, 3, SS_BOOLEAN); // ssSetNumSampleTimes(S, 1); /* Take care when specifying exception free code - see sfuntmpl_doc.c */ ssSetOptions(S, SS_OPTION_WORKS_WITH_CODE_REUSE | SS_OPTION_EXCEPTION_FREE_CODE | SS_OPTION_USE_TLC_WITH_ACCELERATOR); } /* Function: mdlInitializeSampleTimes ========================================= * Abstract: * Specifiy that we inherit our sample time from the driving block. */ static void mdlInitializeSampleTimes(SimStruct *S) { ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME); ssSetOffsetTime(S, 0, 0.0); ssSetModelReferenceSampleTimeDefaultInheritance(S); } /* Function: mdlOutputs ======================================================= * Abstract: * y = 2*u */ static void mdlOutputs(SimStruct *S, int_T tid) { real_T ID,Temp; InputRealPtrsType IDPtrs = ssGetInputPortRealSignalPtrs(S,0); InputRealPtrsType TempPtrs = ssGetInputPortRealSignalPtrs(S,1); InputBooleanPtrsType DataValidPtrs =(InputBooleanPtrsType)ssGetInputPortSignalPtrs(S,3); boolean_T Dvalid=*DataValidPtrs[0]; double *r = (int *)ssGetOutputPortSignal(S,0); double *g = (int *) ssGetOutputPortSignal(S,1); double *b = (int *) ssGetOutputPortSignal(S,2); boolean_T *DataReady =(boolean_T *) ssGetOutputPortSignal(S,3); int_T width = ssGetOutputPortWidth(S,0); double Rt1; double Gt1; double Bt1; double Rt2; double Gt2; double Bt2; double Rt3; double Gt3; double Bt3; double gray; Rt1=(255.000-180.000)/65536; Gt1=(0.000-180.000)/65536; Bt1=(0.000-180.000)/65536; Rt2=(255.000-255.000)/65536; Gt2=(128.000-0.000)/65536; Bt2=(0.000-0.000)/65536; Rt3=((255.000-255.000)/65536); Gt3=(255.000-128.000)/65536; Bt3=(0.000-0.000)/65536; gray=(255.0/65536.0); ID=*IDPtrs[0]; Temp=*TempPtrs[0]; if(Dvalid=1) { if (Temp<=4096) { // *r=1; //*g=1; //*b=1; *r=(int)(gray*ID); *g=(int)(gray*ID); *b=(int)(gray*ID); *DataReady=0; } // if (Temp<=16000 && Temp> 14000) // { // *r=(int)(180+(Rt1*ID)); // *g=(int)(180+(Gt1*ID)); // *b=(int)(180+(Bt1*ID)); // *DataReady=0; // } // if (Temp<=20000 && Temp>16000) // { // //*r=(int)(255+(Rt3*ID)); // // *g=(int)(128+(Gt3*ID)); // //*b=(int)(0+(Bt3*ID)); // // *r=(int)(255+(Rt2*ID)); // *g=(int)(0+(Gt2*ID)); // *b=(int)(0+(Bt2*ID)); // *DataReady=0; // } // // if (Temp>20000) // { // // *r=4; // //*g=4; // //*b=4; // *r=(int)(255+(Rt3*ID)); // *g=(int)(128+(Gt3*ID)); // *b=(int)(0+(Bt3*ID)); // *DataReady=0; // } } else { *DataReady=1; } } /* Function: mdlTerminate ===================================================== * Abstract: * No termination needed, but we are required to have this routine. */ static void mdlTerminate(SimStruct *S) { } #ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */ #include "simulink.c" /* MEX-file interface mechanism */ #else #include "cg_sfun.h" /* Code generation registration function */ #endif