* C-MEX S-function block for RPP digital input.
*
* References:
- * ????
- */
-
-/*
- * FIXME: Adapt, then implement.
+ * 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
+ *
+ * Compile with <matlabroot>/bin/mex -v -g sfunction_din.c
*/
#define S_FUNCTION_LEVEL 2
/*
- * Need to include simstruc.h for the definition of the SimStruct and
- * its associated macro definitions.
+ * Include common header and utilities
*/
-#include "simstruc.h"
-#define EDIT_OK(S, P_IDX) \
- (!((ssGetSimMode(S)==SS_SIMMODE_SIZES_CALL_ONLY) && mxIsEmpty(ssGetSFcnParam(S, P_IDX))))
-#define SAMPLE_TIME (ssGetSFcnParam(S, 1))
-
-/*
- * Utility function prototypes.
- */
-static bool IsRealMatrix(const mxArray * const m);
+#include "utils.c"
#define MDL_CHECK_PARAMETERS
#if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
-/* Function: mdlCheckParameters ===========================================
+/* Function: mdlCheckParameters ================================================
* Abstract:
* mdlCheckParameters verifies new parameter settings whenever parameter
* change or are re-evaluated during a simulation. When a simulation is
#endif
-/* Function: mdlInitializeSizes ===========================================
+/* Function: mdlInitializeSizes ================================================
* Abstract:
* The sizes information is used by Simulink to determine the S-function
* block's characteristics (number of inputs, outputs, states, etc.).
SS_OPTION_DISALLOW_CONSTANT_SAMPLE_TIME);
}
-/* Function: mdlInitializeSampleTimes =====================================
+/* Function: mdlInitializeSampleTimes ==========================================
* Abstract:
* This function is used to specify the sample time(s) for your
* S-function. You must register the same number of sample times as
#define MDL_SET_WORK_WIDTHS
#if defined(MDL_SET_WORK_WIDTHS) && defined(MATLAB_MEX_FILE)
-/* Function: mdlSetWorkWidths =============================================
+/* Function: mdlSetWorkWidths ==================================================
* Abstract:
* The optional method, mdlSetWorkWidths is called after input port
* width, output port width, and sample times of the S-function have
#define MDL_START
#if defined(MDL_START)
-/* Function: mdlStart =====================================================
+/* Function: mdlStart ==========================================================
* Abstract:
* This function is called once at start of model execution. If you
* have states that should be initialized once, this is the place
#endif
-/* Function: mdlOutputs ===================================================
+/* Function: mdlOutputs ========================================================
* Abstract:
* In this function, you compute the outputs of your S-function
* block. Generally outputs are placed in the output vector(s),
UNUSED_PARAMETER(tid);
}
-/* Function: mdlTerminate =================================================
+/* Function: mdlTerminate ======================================================
* Abstract:
* In this function, you should perform any actions that are necessary
* at the termination of a simulation.
#define MDL_RTW
#if defined(MATLAB_MEX_FILE) && defined(MDL_RTW)
-/* Function: mdlRTW =======================================================
+/* Function: mdlRTW ============================================================
* Abstract:
* This function is called when the Real-Time Workshop is generating
* the model.rtw file.
#endif
-/* Function: IsRealMatrix =================================================
- * Abstract:
- * Verify that the mxArray is a real (double) finite matrix
- */
-static bool IsRealMatrix(const mxArray * const m)
-{
- if (mxIsNumeric(m) &&
- mxIsDouble(m) &&
- !mxIsLogical(m) &&
- !mxIsComplex(m) &&
- !mxIsSparse(m) &&
- !mxIsEmpty(m) &&
- mxGetNumberOfDimensions(m) == 2) {
- const double * const data = mxGetPr(m);
- const size_t numEl = mxGetNumberOfElements(m);
- size_t i;
- for (i = 0; i < numEl; i++) {
- if (!mxIsFinite(data[i])) {
- return(false);
- }
- }
-
- return(true);
- } else {
- return(false);
- }
-}
/*
* Required S-function trailer
* C-MEX S-function block for RPP digital output.
*
* 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
+ *
+ * Compile with <matlabroot>/bin/mex -v -g sfunction_lout.c
*/
#define S_FUNCTION_LEVEL 2
/*
- * Need to include simstruc.h for the definition of the SimStruct and
- * its associated macro definitions.
- */
-#include "simstruc.h"
-#define EDIT_OK(S, P_IDX) \
- (!((ssGetSimMode(S)==SS_SIMMODE_SIZES_CALL_ONLY) && mxIsEmpty(ssGetSFcnParam(S, P_IDX))))
-#define SAMPLE_TIME (ssGetSFcnParam(S, 1))
-
-/*
- * Utility function prototypes.
+ * Include common header and utilities
*/
-static bool IsRealMatrix(const mxArray * const m);
+#include "utils.c"
#define MDL_CHECK_PARAMETERS
#if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
-/* Function: mdlCheckParameters ===========================================
+/* Function: mdlCheckParameters ================================================
* Abstract:
* mdlCheckParameters verifies new parameter settings whenever parameter
* change or are re-evaluated during a simulation. When a simulation is
#endif
-/* Function: mdlInitializeSizes ===========================================
+/* Function: mdlInitializeSizes ================================================
* Abstract:
* The sizes information is used by Simulink to determine the S-function
* block's characteristics (number of inputs, outputs, states, etc.).
SS_OPTION_DISALLOW_CONSTANT_SAMPLE_TIME);
}
-/* Function: mdlInitializeSampleTimes =====================================
+/* Function: mdlInitializeSampleTimes ==========================================
* Abstract:
* This function is used to specify the sample time(s) for your
* S-function. You must register the same number of sample times as
#define MDL_SET_WORK_WIDTHS
#if defined(MDL_SET_WORK_WIDTHS) && defined(MATLAB_MEX_FILE)
-/* Function: mdlSetWorkWidths =============================================
+/* Function: mdlSetWorkWidths ==================================================
* Abstract:
* The optional method, mdlSetWorkWidths is called after input port
* width, output port width, and sample times of the S-function have
#define MDL_START
#if defined(MDL_START)
-/* Function: mdlStart =====================================================
+/* Function: mdlStart ==========================================================
* Abstract:
* This function is called once at start of model execution. If you
* have states that should be initialized once, this is the place
#endif
-/* Function: mdlOutputs ===================================================
+/* Function: mdlOutputs ========================================================
* Abstract:
* In this function, you compute the outputs of your S-function
* block. Generally outputs are placed in the output vector(s),
UNUSED_PARAMETER(tid);
}
-/* Function: mdlTerminate =================================================
+/* Function: mdlTerminate ======================================================
* Abstract:
* In this function, you should perform any actions that are necessary
* at the termination of a simulation.
#define MDL_RTW
#if defined(MATLAB_MEX_FILE) && defined(MDL_RTW)
-/* Function: mdlRTW =======================================================
+/* Function: mdlRTW ============================================================
* Abstract:
* This function is called when the Real-Time Workshop is generating
* the model.rtw file.
#endif
-/* Function: IsRealMatrix =================================================
- * Abstract:
- * Verify that the mxArray is a real (double) finite matrix
- */
-static bool IsRealMatrix(const mxArray * const m)
-{
- if (mxIsNumeric(m) &&
- mxIsDouble(m) &&
- !mxIsLogical(m) &&
- !mxIsComplex(m) &&
- !mxIsSparse(m) &&
- !mxIsEmpty(m) &&
- mxGetNumberOfDimensions(m) == 2) {
- const double * const data = mxGetPr(m);
- const size_t numEl = mxGetNumberOfElements(m);
- size_t i;
- for (i = 0; i < numEl; i++) {
- if (!mxIsFinite(data[i])) {
- return(false);
- }
- }
-
- return(true);
- } else {
- return(false);
- }
-}
/*
* Required S-function trailer
%if !SLibCodeGenForSim()
%<RppCommonStart(block, system)>
+
+ %% Get pin number and pull type
+ %assign p1_val = LibBlockParameter(p1, "", "", 0)
+ %assign p2_val = LibBlockParameter(p2, "", "", 0)
+
+ rpp_din_setup(%<p1_val>, %<p2_val>);
+
%endif
%endfunction
%function Outputs(block, system) Output
%if !SLibCodeGenForSim()
+
+ %% First executed block must update cached values
+ %if EXISTS("_RPP_DIN_UPDATE_") == 0
+ %assign _RPP_DIN_UPDATE_ = 1
+ rpp_din_update();
+ %endif
+
+ %% Get pin number and if to use variable threshold
%assign p1_val = LibBlockParameter(p1, "", "", 0)
- %assign u1_val = LibBlockInputSignal(0, "", "", 0)
+ %assign p3_val = LibBlockParameter(p3, "", "", 0)
+
+ %assign y1_val = LibBlockOutputSignal(0, "", "", 0)
+ %<y1_val> = rpp_din_get(%<p1_val>, %<p3_val>);
- //rpp_din_get(%<p1_val>, %<u1_val>);
%endif
%endfunction
%function Outputs(block, system) Output
%if !SLibCodeGenForSim()
+
+ %% Get pin number
%assign p1_val = LibBlockParameter(p1, "", "", 0)
+
%assign u1_val = LibBlockInputSignal(0, "", "", 0)
+ rpp_lout_set(%<p1_val>, %<u1_val>);
+
+ %% FIXME: Improve to call this function only once per step
+ %% But... what happens if the next block want to check it's diagnostic
+ %% value and is not the last LOUT block?
+ rpp_lout_update();
+
+ %% TODO: Get diagnostic value, if mismatch, set error output flag.
- //rpp_lout_set(%<p1_val>, %<u1_val>);
%endif
%endfunction