]> rtime.felk.cvut.cz Git - jenkicar/rpp-simulink.git/blob - rpp/blocks/sfunction_din.c
Change license to MIT
[jenkicar/rpp-simulink.git] / rpp / blocks / sfunction_din.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_din.c
28  * Abstract:
29  *     C-MEX S-function block for RPP digital input.
30  *
31  * References:
32  *     header.c
33  *     trailer.c
34  *
35  * Compile with:
36  *     <matlabroot>/bin/mex sfunction_din.c
37  */
38
39 /*
40 %YAML 1.2
41 ---
42 Name: Digital Input
43 Category: IO blocks
44 Header: rpp/din.h
45 Mnemonic: DIN
46
47 Inputs:
48
49 Outputs:
50   - { name: "Digital Input", type: "bool" }
51   - { name: "ErrFlag",       type: "bool" }
52
53 Parameters:
54   - { name: "Pin number",             type: "uint8", range: "[0-15]"           }
55   - { name: "Use variable threshold", type: "bool"                             }
56   - { name: "Pull",                   type: "Choice from Pull-down or Pull-up" }
57   - { name: "Active",                 type: "Choice from Active or Tri-stated" }
58
59 # Description and Help is in Markdown mark-up
60 Description: |
61
62   Gets the digital value of the specified digital input pin on the RPP board.
63
64   If pin is high the output is 1, 0 if the pin is low.
65
66   If an error is detected while getting the pin, the ErrFlag is set high.
67
68 Help: |
69
70   This block allows to read the digital inputs on the RPP board. For pins number 0-7 is not
71   relevant and not visible parameter 'Use variable threshold' and only one block for each pin
72   can be in model. And oppositely for other pins (8-15) are not relevant and visible parameters
73   of names 'Pull' and 'Active'. The variable threshold check change the read mode of the pin. For
74   setting it see Digital Input Configure block.
75
76   The ErrFlag should raise if `rpp_din_update()` or `rpp_din_get()` returns error. `rpp_din_update()`
77   is called just by the first DIN block in the model and thus only the first block could raise the
78   flag because of this. In case an errors occurs the return value will always be LOW (0). Because the
79   ErrFlag should never set, once set the following steps will never clear it back.
80
81 Status: Stable
82
83 RPP API functions used:
84     - rpp_din_setup()
85     - rpp_din_update()
86     - rpp_din_get()
87
88 Relevant demos:
89     - digital_passthrough
90     - hbridge_digital_control
91 ...
92 */
93
94 #define S_FUNCTION_NAME sfunction_din
95 #include "header.c"
96
97
98 static void mdlInitializeSizes(SimStruct *S)
99 {
100     /*
101      * Configure parameters: 1
102      *  - Pin number: [1-16]
103      *  - Use variable threshold: [true|false]
104      *  - Pull: [Pull-up|Pull-down]
105      *  - Active: [Active|Tristate]
106      */
107     if (!rppSetNumParams(S, 4)) {
108         return;
109     }
110
111     /*
112      * Configure input ports: 0
113      */
114     if (!ssSetNumInputPorts(S, 0)) {
115         return;
116     }
117
118     /*
119      * Configure output ports: 2
120      *  - Digital input.
121      *  - Error flag.
122      */
123     if (!ssSetNumOutputPorts(S, 2)) {
124         return;
125     }
126     rppAddOutputPort(S, 0, SS_BOOLEAN);
127     rppAddOutputPort(S, 1, SS_BOOLEAN);
128
129     /* Set standard options for this block */
130     rppSetStandardOptions(S);
131 }
132
133
134 #ifdef MATLAB_MEX_FILE
135 #define MDL_CHECK_PARAMETERS
136 static void mdlCheckParameters(SimStruct *S)
137 {
138     /* Check the parameter 1 */
139     if (!rppValidParamRange(S, 0, 0, 15)) {
140         return;
141     }
142
143     /* Check the parameter 2 */
144     if (!rppValidParamBoolean(S, 1)) {
145         return;
146     }
147
148     /* Check the parameter 3 */
149     if (!rppValidParamRange(S, 2, 0, 2)) {
150         return;
151     }
152
153     /* Check the parameter 4 */
154     if (!rppValidParamRange(S, 3, 0, 2)) {
155         return;
156     }
157 }
158 #endif
159
160
161 #ifdef MATLAB_MEX_FILE
162 #define MDL_SET_WORK_WIDTHS
163 static void mdlSetWorkWidths(SimStruct *S)
164 {
165     /* Set number of run-time parameters */
166     if (!ssSetNumRunTimeParams(S, 4)) {
167         return;
168     }
169
170     /* Register the run-time parameter 1 */
171     ssRegDlgParamAsRunTimeParam(S, 0, 0, "p1", SS_INT8);
172
173     /* Register the run-time parameter 2 */
174     ssRegDlgParamAsRunTimeParam(S, 1, 1, "p2", SS_BOOLEAN);
175
176     /* Register the run-time parameter 3 */
177     ssRegDlgParamAsRunTimeParam(S, 2, 2, "p3", SS_UINT8);
178
179     /* Register the run-time parameter 4 */
180     ssRegDlgParamAsRunTimeParam(S, 3, 3, "p4", SS_UINT8);
181 }
182 #endif
183
184
185 #define COMMON_MDLINITIALIZESAMPLETIMES_INHERIT
186 #define UNUSED_MDLOUTPUTS
187 #define UNUSED_MDLTERMINATE
188 #include "trailer.c"