1 /* Copyright (C) 2013, 2014 Czech Technical University in Prague
4 * - Carlos Jenkins <carlos@jenkins.co.cr>
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
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
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.
27 * File : sfunction_aout.c
29 * C-MEX S-function block for RPP analog output.
36 * <matlabroot>/bin/mex sfunction_aout.c
48 - { name: "Analog Output", type: "uint16" }
51 - { name: "ErrFlag", type: "bool" }
54 - { name: "Pin number [1-4]", type: "uint8" }
55 - { name: "Input in millivolts", type: "bool" }
57 # Description and Help is in Markdown mark-up
60 Sets the analog value of the specified analog output pin on the RPP
63 If 'input is voltage' is set the value must be an unsigned 16 bit
64 integer between 0-12000 (millivolts to set the analog output). If
65 'input is voltage' is NOT set the value must be and unsigned 16 bit
66 integer between 0-4095 (DAC is 12 bit).
68 If an error is detected while setting the value, the ErrFlag is set
73 Sets the analog value of the specified analog output pin on the RPP board. If an error is detected
74 while setting the value, the ErrFlag is set high.
76 This block allows to write to the analog outputs on the RPP board. The UseVoltage flag allows the
77 user to configure if block inputs should be interpreted as raw DAC value or millivolts. The ErrFlag
78 should raise it `rpp_dac_update()` or `rpp_dac_set()` (or `rpp_dac_set_voltage()`
79 depending on block configuration) returns error. Because the ErrFlag should never set, once set the
80 following steps will never clear it back.
82 `rpp_dac_update()` is called on each block but the implementation provides this to be efficient.
84 There is a know bug on the RPP Library, check `rpp_dac_update()` on the RPP API for details.
85 Because of this, the outputs of the DACs are initialized on the first step of the model and not on
86 the model initialization.
90 RPP API functions used:
93 - rpp_dac_set_voltage()
103 #define S_FUNCTION_NAME sfunction_aout
107 static void mdlInitializeSizes(SimStruct *S)
110 * Configure parameters: 1
111 * - Pin number: [1-4]
114 if (!rppSetNumParams(S, 2)) {
119 * Configure input ports: 1
122 if (!ssSetNumInputPorts(S, 1)) {
125 rppAddInputPort(S, 0, SS_UINT16);
128 * Configure output ports: 1
131 if (!ssSetNumOutputPorts(S, 1)) {
134 rppAddOutputPort(S, 0, SS_BOOLEAN);
136 /* Set standard options for this block */
137 rppSetStandardOptions(S);
141 #ifdef MATLAB_MEX_FILE
142 #define MDL_CHECK_PARAMETERS
143 static void mdlCheckParameters(SimStruct *S)
145 /* Check the parameter 1 */
146 if (!rppValidParamRange(S, 0, 1, 4)) {
150 /* Check the parameter 2 */
151 if (!rppValidParamBoolean(S, 1)) {
158 #ifdef MATLAB_MEX_FILE
159 #define MDL_SET_WORK_WIDTHS
160 static void mdlSetWorkWidths(SimStruct *S)
162 /* Set number of run-time parameters */
163 if (!ssSetNumRunTimeParams(S, 2)) {
167 /* Register the run-time parameter 1 */
168 ssRegDlgParamAsRunTimeParam(S, 0, 0, "p1", SS_UINT8);
170 /* Register the run-time parameter 2 */
171 ssRegDlgParamAsRunTimeParam(S, 1, 1, "p2", SS_BOOLEAN);
176 #define COMMON_MDLINITIALIZESAMPLETIMES_INHERIT
177 #define UNUSED_MDLOUTPUTS
178 #define UNUSED_MDLTERMINATE