]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blob - rpp/blocks/sfunction_sdrw.c
Merge branch 'master' of rtime.felk.cvut.cz:jenkicar/rpp-simulink
[pes-rpp/rpp-simulink.git] / rpp / blocks / sfunction_sdrw.c
1 /* Copyright (C) 2013, 2014 Czech Technical University in Prague
2  *
3  * Authors:
4  *     - Carlos Jenkins <carlos@jenkins.co.cr>
5  *
6  * This document contains proprietary information belonging to Czech
7  * Technical University in Prague. Passing on and copying of this
8  * document, and communication of its contents is not permitted
9  * without prior written authorization.
10  *
11  * File : sfunction_sdrw.c
12  * Abstract:
13  *     C-MEX S-function block for RPP SD-RAM log write.
14  *
15  * References:
16  *     header.c
17  *     trailer.c
18  *
19  * Compile with:
20  *     <matlabroot>/bin/mex sfunction_sdrw.c
21  */
22
23 /*
24 %YAML 1.2
25 ---
26 Name: SD-RAM Write
27 Category: Logging
28 Header: rpp/sdc.h
29 Mnemonic: SDRW
30
31 Inputs:
32   - { name: "Data", type: "double" }
33
34 Outputs:
35   - { name: "ErrFlag", type: "bool" }
36
37 Parameters:
38   - { name: "Block ID",              type: "uint8"  }
39   - { name: "PrintFormat [SETTING]", type: "string" }
40
41 # Description is in Markdown mark-up
42 Description: >
43
44   This block allows to log a double value to the SD-RAM. User needs to provide a valid PrintFormat 
45   string to format and register the double value on the log. The PrintFormat string should include 
46   two specifiers:  
47     1. For the block ID. Any valid integer specifier.  
48     2. For the value to log. Any valid double specifier.  
49
50   Note that the value of PrintFormat is inserted raw between quotes on code generation and thus there 
51   is no validation on it. Error to provide a valid PrintFormat could generate compilation errors or 
52   even run-time errors (normally this generates a warning on compile time). Note that the function 
53   for logging used is `rpp_sdr_printf()`, which is a blocking call, and can potentially overrun the 
54   step. The ErrFlag will set if `rpp_sdr_printf()` returns an error (for example out of memory), 
55   but will clear back if the next step the call to this function is successful.
56
57 Status:
58   Tested:
59     - Logging data.
60     - Compilation and general use.
61   Untested:
62     - Faulty situation for the ErrFlag to set.
63   Not working:
64
65 RPP API functions used:
66     - rpp_sdr_printf()
67
68 Relevant demos:
69     - log_analog_input
70 ...
71 */
72
73 #define S_FUNCTION_NAME sfunction_sdrw
74 #include "header.c"
75
76
77 static void mdlInitializeSizes(SimStruct *S)
78 {
79     /*
80      * Configure parameters: 2
81      *  - Block ID.
82      *  - Printf format [setting].
83      */
84     if (!rppSetNumParams(S, 2)) {
85         return;
86     }
87
88     /*
89      * Configure input ports: 1
90      *  - Data.
91      */
92     if (!ssSetNumInputPorts(S, 1)) {
93         return;
94     }
95     rppAddInputPort(S, 0, SS_DOUBLE);
96
97     /*
98      * Configure output ports: 1
99      *  - Error flag.
100      */
101     if (!ssSetNumOutputPorts(S, 1)) {
102         return;
103     }
104     rppAddOutputPort(S, 0, SS_BOOLEAN);
105
106     /* Set standard options for this block */
107     rppSetStandardOptions(S);
108 }
109
110
111 #ifdef MATLAB_MEX_FILE
112 #define MDL_CHECK_PARAMETERS
113 static void mdlCheckParameters(SimStruct *S)
114 {
115     /* Check the parameter 1 */
116     if (!rppValidParamRange(S, 0, 0, 255)) {
117         return;
118     }
119 }
120 #endif
121
122
123 #ifdef MATLAB_MEX_FILE
124 #define MDL_SET_WORK_WIDTHS
125 static void mdlSetWorkWidths(SimStruct *S)
126 {
127     /* Set number of run-time parameters */
128     if (!ssSetNumRunTimeParams(S, 1)) {
129         return;
130     }
131
132     /* Register the run-time parameter 1 */
133     ssRegDlgParamAsRunTimeParam(S, 0, 0, "p1", SS_UINT8);
134 }
135 #endif
136
137
138 #ifdef MATLAB_MEX_FILE
139 #define MDL_RTW
140 static void mdlRTW(SimStruct* S)
141 {
142     /* Register parameter 2 as a parameter setting */
143     static char_T str[128];
144     mxGetString(ssGetSFcnParam(S, 1), (char*)&str, sizeof(str)); /* Get string */
145     if (!ssWriteRTWParamSettings(S, 1,
146             SSWRITE_VALUE_QSTR, "PrintfFormat", (const char_T*)&str)) {
147         /* An error ocurred */
148         return;
149     }
150 }
151 #endif
152
153
154 #define COMMON_MDLINITIALIZESAMPLETIMES_INHERIT
155 #define UNUSED_MDLOUTPUTS
156 #define UNUSED_MDLTERMINATE
157 #include "trailer.c"