]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blob - rpp/blocks/sfunction_ain.c
Change license to MIT
[pes-rpp/rpp-simulink.git] / rpp / blocks / sfunction_ain.c
1 /* Copyright (C) 2013, 2014, 2015 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_ain.c
28  * Abstract:
29  *     C-MEX S-function block for RPP analog input.
30  *
31  * References:
32  *     header.c
33  *     trailer.c
34  *
35  * Compile with:
36  *     <matlabroot>/bin/mex sfunction_ain.c
37  */
38
39 /*
40 TODO: Change Pin number range to use macro SFUNCTION_AIN_PIN_COUNT, expanded by C preprocesor
41 %YAML 1.2
42 ---
43 Name: Analog Input
44 Category: IO blocks
45 Header: rpp/adc.h
46 Mnemonic: ADC
47
48 Inputs:
49
50 Outputs:
51   - { name: "Analog Input Pin Number", type: "uint16" }
52   - { name: "ErrFlag",                 type: "bool"   }
53
54 Parameters:
55   - { name: "AD1IN pin number", type: "uint8", range: "[1–16]" }
56 # TODO: Use this for RPP: - { name: "Pin number", type: "uint8", range: "[1–12]" }
57
58 # Description and Help is in Markdown mark-up
59 Description: |
60
61   Gets the analog value of the specified analog input pin on the RPP
62   board.
63
64   The value is an unsigned 16 bit integer between 0–4095 (12 bit ADC).
65
66   If an error is detected while reading the value, the ErrFlag is set
67   high.
68
69 Help: |
70
71   This block allows to read the analog inputs on the RPP board. The
72   ErrFlag should if raise `rpp_adc_update()` or `rpp_adc_get()`
73   returns error. `rpp_adc_update()` is called just by the first DIN
74   block in the model and thus only the first block could raise the
75   flag because of this. In case an error occurs the return value will
76   always be 0. Because the ErrFlag should never set, once set the
77   following steps will never clear it back.
78
79 Status: Stable
80
81 RPP API functions used:
82   - rpp_adc_update()
83   - rpp_adc_get()
84
85 Relevant demos:
86   - analog_passthrough
87   - hbridge_analog_control
88   - log_analog_input
89 ...
90 */
91
92
93 #define S_FUNCTION_NAME sfunction_ain
94 #include "header.c"
95
96
97 static void mdlInitializeSizes(SimStruct *S)
98 {
99     /*
100      * Configure parameters: 1
101      *  - Pin number: [1-16]
102      */
103     if (!rppSetNumParams(S, 1)) {
104         return;
105     }
106
107     /*
108      * Configure input ports: 0
109      */
110     if (!ssSetNumInputPorts(S, 0)) {
111         return;
112     }
113
114     /*
115      * Configure output ports: 2
116      *  - Analog input.
117      *  - Error flag.
118      */
119     if (!ssSetNumOutputPorts(S, 2)) {
120         return;
121     }
122     rppAddOutputPort(S, 0, SS_UINT16);
123     rppAddOutputPort(S, 1, SS_BOOLEAN);
124
125     /* Set standard options for this block */
126     rppSetStandardOptions(S);
127 }
128
129
130 #ifdef MATLAB_MEX_FILE
131 #define MDL_CHECK_PARAMETERS
132 static void mdlCheckParameters(SimStruct *S)
133 {
134     /* Check the parameter 1 */
135     if (!rppValidParamRange(S, 0, 1, 16)) {
136         return;
137     }
138 }
139 #endif
140
141
142 #ifdef MATLAB_MEX_FILE
143 #define MDL_SET_WORK_WIDTHS
144 static void mdlSetWorkWidths(SimStruct *S)
145 {
146     /* Set number of run-time parameters */
147     if (!ssSetNumRunTimeParams(S, 1)) {
148         return;
149     }
150
151     /* Register the run-time parameter 1 */
152     ssRegDlgParamAsRunTimeParam(S, 0, 0, "p1", SS_UINT8);
153 }
154 #endif
155
156
157 #define COMMON_MDLINITIALIZESAMPLETIMES_INHERIT
158 #define UNUSED_MDLOUTPUTS
159 #define UNUSED_MDLTERMINATE
160 #include "trailer.c"