/*
* TIMESTWO An example C-file S-function for multiplying an input by 2
*
* y = 2*x
*
* See sfuntmpl.c for a general S-function template.
*
* See also VSFUNC, DSFUNC, SFUNTMPL.
*
* Copyright (c) 1990-96 by MathWorks, Inc.
* All Rights Reserved
* $Revision: 1.1 $
*/
#ifdef __cplusplus
extern "C" {
#endif
#define S_FUNCTION_NAME timestwo_level1_cpp
/* The following three lines are necessary for the WATCOM compiler
* only. You need to remove these lines for the MSVC compiler.
*/
#ifndef COMPILER_SUPPORTS_BOOL
typedef int bool;
#endif
#include "simstruc.h"
/*
* mdlInitializeSizes - initialize the sizes array
*/
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumContStates( S, 0); /* number of continuous states */
ssSetNumDiscStates( S, 0); /* number of discrete states */
ssSetNumInputs( S, DYNAMICALLY_SIZED); /* number of inputs */
ssSetNumOutputs( S, DYNAMICALLY_SIZED); /* number of outputs */
ssSetDirectFeedThrough(S, 1); /* direct feedthrough flag */
ssSetNumSampleTimes( S, 1); /* number of sample times */
ssSetNumSFcnParams( S, 0); /* number of input arguments */
ssSetNumRWork( S, 0); /* number of real work vector elements */
ssSetNumIWork( S, 0); /* number of integer work vector elements */
ssSetNumPWork( S, 0); /* number of pointer work vector elements */
}
/*
* mdlInitializeSampleTimes - initialize the sample times array
*/
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
}
/*
* mdlInitializeConditions - initialize the states
*/
static void mdlInitializeConditions(double *x0, SimStruct *S)
{
}
/*
* mdlOutputs - compute the outputs
*/
static void mdlOutputs(double *y, double *x, double *u, SimStruct *S, int tid)
{
class mul2 {
double *u;
double *y;
int width;
public:
mul2(double *u_i, double *y_i, int width_i)
{
u = u_i;
y = y_i;
width = width_i;
}
void output(void)
{
for (int i=0;i<width;i++)
{
*y++=2.0*(*u++);
}
}
};
class mul2 m(u,y,ssGetNumOutputs(S));
m.output();
}
/*
* mdlUpdate - perform action at major integration time step
*/
static void mdlUpdate(double *x, double *u, SimStruct *S, int tid)
{
}
/*
* mdlDerivatives - compute the derivatives
*/
static void mdlDerivatives(double *dx, double *x,
double *u, SimStruct *S, int tid)
{
}
/*
* mdlTerminate - called when the simulation is terminated.
*/
static void mdlTerminate(SimStruct *S)
{
}
#ifdefMATLAB_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
#ifdef __cplusplus
}
#endif
Best Answer