* C-MEX S-function block for RPP digital input.
*
* References:
- * mdlCheckParameters() : sfunctions.pdf p. 421
- * mdlInitializeSizes() : sfunctions.pdf p. 441
- * mdlInitializeSampleTimes() : sfunctions.pdf p. 436
- * mdlSetWorkWidths() : sfunctions.pdf p. 489
- * mdlStart() : sfunctions.pdf p. 492
- * mdlOutputs() : sfunctions.pdf p. 447
- * mdlTerminate() : sfunctions.pdf p. 493
- * mdlRTW() : sfunctions.pdf p. 458
+ * common.c
*
* Compile with <matlabroot>/bin/mex -v -g sfunction_din.c
*/
* Must specify the S_FUNCTION_NAME as the name of the S-function.
*/
#define S_FUNCTION_NAME sfunction_din
-#define S_FUNCTION_LEVEL 2
/*
* Include common header and utilities
*/
-#include "utils.c"
+#include "common.c"
#define MDL_CHECK_PARAMETERS
#if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
*/
static void mdlCheckParameters(SimStruct *S)
{
- /*
- * Check the parameter 1
- */
- if EDIT_OK(S, 0) {
- int_T dimsArray[2] = { 1, 1 };
-
- /* Check the parameter attributes */
- ssCheckSFcnParamValueAttribs(S, 0, "P1", DYNAMICALLY_TYPED, 2, dimsArray, 0);
- }
-
- /*
- * Check the parameter 2 (sample time)
- */
- if EDIT_OK(S, 1) {
- const double * sampleTime = NULL;
- const size_t stArraySize = mxGetM(SAMPLE_TIME) * mxGetN(SAMPLE_TIME);
-
- /* Sample time must be a real scalar value or 2 element array. */
- if (IsRealMatrix(SAMPLE_TIME) &&
- (stArraySize == 1 || stArraySize == 2) ) {
- sampleTime = mxGetPr(SAMPLE_TIME);
- } else {
- ssSetErrorStatus(S,
- "Invalid sample time. Sample time must be a real scalar value or an array of two real values.");
- return;
- }
-
- if (sampleTime[0] < 0.0 && sampleTime[0] != -1.0) {
- ssSetErrorStatus(S,
- "Invalid sample time. Period must be non-negative or -1 (for inherited).");
- return;
- }
+ /*
+ * Check the parameter 1
+ */
+ if EDIT_OK(S, 0) {
+ int_T dimsArray[2] = { 1, 1 };
- if (stArraySize == 2 && sampleTime[0] > 0.0 &&
- sampleTime[1] >= sampleTime[0]) {
- ssSetErrorStatus(S,
- "Invalid sample time. Offset must be smaller than period.");
- return;
+ /* Check the parameter attributes */
+ ssCheckSFcnParamValueAttribs(S, 0, "P1", DYNAMICALLY_TYPED, 2, dimsArray, 0);
}
- if (stArraySize == 2 && sampleTime[0] == -1.0 && sampleTime[1] != 0.0) {
- ssSetErrorStatus(S,
- "Invalid sample time. When period is -1, offset must be 0.");
- return;
- }
-
- if (stArraySize == 2 && sampleTime[0] == 0.0 &&
- !(sampleTime[1] == 1.0)) {
- ssSetErrorStatus(S,
- "Invalid sample time. When period is 0, offset must be 1.");
- return;
+ /*
+ * Check the parameter 2 (sample time)
+ */
+ if(!checkSampleTime(S, 1)) {
+ return;
}
- }
}
#endif
}
-static void mdlInitializeSampleTimes(SimStruct *S)
-{
- const double* sampleTime = mxGetPr(SAMPLE_TIME);
- const size_t stArraySize = mxGetM(SAMPLE_TIME) * mxGetN(SAMPLE_TIME);
- ssSetSampleTime(S, 0, sampleTime[0]);
-
- if(stArraySize == 1) {
- ssSetOffsetTime(S, 0, (sampleTime[0] == CONTINUOUS_SAMPLE_TIME?
- FIXED_IN_MINOR_STEP_OFFSET: 0.0));
- } else {
- ssSetOffsetTime(S, 0, sampleTime[1]);
- }
-
- #if defined(ssSetModelReferenceSampleTimeDefaultInheritance)
- ssSetModelReferenceSampleTimeDefaultInheritance(S);
- #endif
-}
-
-
#ifdef MATLAB_MEX_FILE
static void mdlCheckParameters(SimStruct *S)
{
return;
}
}
+#endif
+#ifdef MATLAB_MEX_FILE
static void mdlSetWorkWidths(SimStruct *S)
{
// Set number of run-time parameters
#endif
+#define COMMON_MDLINITIALIZESAMPLETIMES_INHERIT
#define UNUSED_MDLOUTPUTS
#define UNUSED_MDLTERMINATE
#include "trailer.c"
#endif
+#ifdef COMMON_MDLINITIALIZESAMPLETIMES_INHERIT
+static void mdlInitializeSampleTimes(SimStruct *S)
+{
+ ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
+ ssSetOffsetTime(S, 0, 0.0);
+ #if defined(ssSetModelReferenceSampleTimeDefaultInheritance)
+ ssSetModelReferenceSampleTimeDefaultInheritance(S);
+ #endif
+}
+#endif
+
+
/* Function: mdlOutputs ========================================================
* Abstract:
* In this function, you compute the outputs of your S-function