]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blob - rpp/blocks/sfunction_sdrw.c
Change license to MIT
[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  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use,
10  * copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * File : sfunction_sdrw.c
28  * Abstract:
29  *     C-MEX S-function block for RPP SD-RAM log write.
30  *
31  * References:
32  *     header.c
33  *     trailer.c
34  *
35  * Compile with:
36  *     <matlabroot>/bin/mex sfunction_sdrw.c
37  */
38
39 /*
40 %YAML 1.2
41 ---
42 Name: SD-RAM Log Data
43 Category: Logging
44 Header: rpp/sdc.h
45 Mnemonic: SDRW
46
47 Inputs:
48   - { name: "Data", type: "double" }
49
50 Outputs:
51   - { name: "ErrFlag", type: "bool" }
52
53 Parameters:
54   - { name: "Block ID",              type: "uint8", range: "[0-255]" }
55   - { name: "Printf format for logging", type: "string", range: "(include specifiers)" }
56
57 # Description and Help is in Markdown mark-up
58 Description: &desc |
59
60   This block allows to log a double value to the SD-RAM. User needs to provide a valid PrintFormat 
61   string to format and register the double value on the log. The PrintFormat string should include 
62   two specifiers:  
63     1. For the block ID. Any valid integer specifier.  
64     2. For the value to log. Any valid double specifier.  
65
66   Note that the value of PrintFormat is inserted raw between quotes on code generation and thus there 
67   is no validation on it. Error to provide a valid PrintFormat could generate compilation errors or 
68   even run-time errors (normally this generates a warning on compile time). Note that the function 
69   for logging used is `rpp_sdr_printf()`, which is a blocking call, and can potentially overrun the 
70   step. The ErrFlag will set if `rpp_sdr_printf()` returns an error (for example out of memory), 
71   but will clear back if the next step the call to this function is successful.
72
73 Help: *desc
74
75 Status: Beta
76
77 RPP API functions used:
78     - rpp_sdr_printf()
79
80 Relevant demos:
81     - log_analog_input
82 ...
83 */
84
85 #define S_FUNCTION_NAME sfunction_sdrw
86 #include "header.c"
87
88
89 static void mdlInitializeSizes(SimStruct *S)
90 {
91     /*
92      * Configure parameters: 2
93      *  - Block ID.
94      *  - Printf format [setting].
95      */
96     if (!rppSetNumParams(S, 2)) {
97         return;
98     }
99
100     /*
101      * Configure input ports: 1
102      *  - Data.
103      */
104     if (!ssSetNumInputPorts(S, 1)) {
105         return;
106     }
107     rppAddInputPort(S, 0, SS_DOUBLE);
108
109     /*
110      * Configure output ports: 1
111      *  - Error flag.
112      */
113     if (!ssSetNumOutputPorts(S, 1)) {
114         return;
115     }
116     rppAddOutputPort(S, 0, SS_BOOLEAN);
117
118     /* Set standard options for this block */
119     rppSetStandardOptions(S);
120 }
121
122
123 #ifdef MATLAB_MEX_FILE
124 #define MDL_CHECK_PARAMETERS
125 static void mdlCheckParameters(SimStruct *S)
126 {
127     /* Check the parameter 1 */
128     if (!rppValidParamRange(S, 0, 0, 255)) {
129         return;
130     }
131 }
132 #endif
133
134
135 #ifdef MATLAB_MEX_FILE
136 #define MDL_SET_WORK_WIDTHS
137 static void mdlSetWorkWidths(SimStruct *S)
138 {
139     /* Set number of run-time parameters */
140     if (!ssSetNumRunTimeParams(S, 1)) {
141         return;
142     }
143
144     /* Register the run-time parameter 1 */
145     ssRegDlgParamAsRunTimeParam(S, 0, 0, "p1", SS_UINT8);
146 }
147 #endif
148
149
150 #ifdef MATLAB_MEX_FILE
151 #define MDL_RTW
152 static void mdlRTW(SimStruct* S)
153 {
154     /* Register parameter 2 as a parameter setting */
155     static char_T str[128];
156     mxGetString(ssGetSFcnParam(S, 1), (char*)&str, sizeof(str)); /* Get string */
157     if (!ssWriteRTWParamSettings(S, 1,
158             SSWRITE_VALUE_QSTR, "PrintfFormat", (const char_T*)&str)) {
159         /* An error ocurred */
160         return;
161     }
162 }
163 #endif
164
165
166 #define COMMON_MDLINITIALIZESAMPLETIMES_INHERIT
167 #define UNUSED_MDLOUTPUTS
168 #define UNUSED_MDLTERMINATE
169 #include "trailer.c"