]> rtime.felk.cvut.cz Git - jenkicar/rpp-simulink.git/blob - rpp/blocks/sfunction_gio_in.c
Change license to MIT
[jenkicar/rpp-simulink.git] / rpp / blocks / sfunction_gio_in.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_gio_in.c
28  * Abstract:
29  *     C-MEX S-function block for reading from RPP GPIO ports.
30  *
31  * References:
32  *     header.c
33  *     trailer.c
34  *
35  * Compile with:
36  *     <matlabroot>/bin/mex sfunction_gio_in.c
37  */
38
39 /*
40 %YAML 1.2
41 ---
42 Name: General Purpose Digital Input
43 Category: IO blocks
44 Header: rpp/gio.h
45 Mnemonic: GIOIN
46
47 Inputs:
48
49 Outputs:
50   - { name: "Digital Input value", type: "bool" }
51 Parameters:
52   - { name: "Pin", type: "Choice", range: "Target dependent pin names or '---'" }
53   - { name: "Pin name", type: "String", note: "Pin name as defined in gio_def.h. This parameter is only visible if *Pin* is '---'." }
54   - { name: "Input Type", type: "Choice", range: "Tri-state, Pull Up, Pull Down" }
55
56 # Description is in Markdown mark-up
57 Description: &desc |
58   Reads value from a GIO pin. Which pins are supported depends on the
59   target board. The selected pin can be configured as tri-state, pull
60   up or pull down.
61
62   When the pin is used in multiple GIOIN or GIOOUT blocks, an error is
63   signaled.
64
65 Help: *desc
66
67 Status: Stable
68
69 RPP API functions used:
70   - rpp_gio_setup
71   - rpp_gio_get
72
73 Relevant demos:
74   - gio_demo
75 ...
76 */
77
78 /*
79  * Block mask documentation
80  * ------------------------
81  *
82  * This S-function has only the PIN_NAME parameter, but the block mask
83  * has two parameters for pin selection: PIN and PIN_NAME. PIN
84  * parameter is shown as a popup list with pin names available on the
85  * given board. When a pin is selected from this list, the callback
86  * function copies the value from PIN to PIN_NAME parameter. By
87  * default, PIN_NAME is not shown in the dialog.
88  *
89  * It might happen that a model designed for one board is opened for
90  * another board (i.e. rpp_setup was called when the RPP C library was
91  * compiled for another board). If we had only the PIN popup and the
92  * other board had different pin names, than the value of the PIN
93  * parameter would be lost (Simulink would set the parameter to the
94  * default value). With our two parameters, PIN_NAME is not changed
95  * (it's an edit box) and PIN is set to the default value '---', which
96  * results in PIN_NAME to be shown to the user. This way, the user
97  * sees the original pin name and can select the appropriate pin on
98  * the new board from the PIN list.
99  */
100
101
102 #define S_FUNCTION_NAME sfunction_gio_in
103 #include "header.c"
104
105 /** Identifiers of the block parameters */
106 enum params{
107         PARAM_PIN_NAME,
108         PARAM_INPUT_TYPE,
109         PARAMS_COUNT
110 };
111
112 enum outputs{
113         OUT_PIN_VALUE,
114         OUTPUTS_COUNT
115 };
116
117 static void mdlInitializeSizes(SimStruct *S)
118 {
119         if (!rppSetNumParams(S, PARAMS_COUNT)) {
120                 return;
121         }
122
123         /*
124         * Configure input ports: 0
125         */
126         if (!ssSetNumInputPorts(S, 0)) {
127                 return;
128         }
129
130         /*
131         * Configure output ports: 1
132         */
133         if (!ssSetNumOutputPorts(S, OUTPUTS_COUNT)) {
134                 return;
135         }
136         rppAddOutputPort(S, OUT_PIN_VALUE, SS_BOOLEAN);
137
138         /* Set standard options for this block */
139         rppSetStandardOptions(S);
140 }
141
142
143 #ifdef MATLAB_MEX_FILE
144 #define MDL_CHECK_PARAMETERS
145 static void mdlCheckParameters(SimStruct *S)
146 {
147     if (!mxIsChar(ssGetSFcnParam(S, PARAM_PIN_NAME))) {
148         ssSetErrorStatus(S, "Parameter to S-function must be a string.");
149     }
150 }
151 #endif
152
153
154 #if defined(MATLAB_MEX_FILE)
155 #define MDL_RTW
156 static void mdlRTW(SimStruct *S)
157 {
158     char_T pin_name[80];
159     int8_T pull_type = mxGetPr(ssGetSFcnParam(S, PARAM_INPUT_TYPE))[0];
160
161     if (mxGetString(ssGetSFcnParam(S, PARAM_PIN_NAME), pin_name, sizeof(pin_name)) != 0) {
162         ssSetErrorStatus(S,"mxGetString error in mdlRTW");
163         return;
164     }
165
166     if (!ssWriteRTWParamSettings(S, 2,
167                                  SSWRITE_VALUE_STR, "PinName", pin_name,
168                                  SSWRITE_VALUE_DTYPE_NUM, "PullType", &pull_type, DTINFO(SS_INT8, COMPLEX_NO))) {
169         ssSetErrorStatus(S, "ssWriteRTWParamSettings failed");
170     }
171 }
172 #endif /* MDL_RTW */
173
174
175
176 #define COMMON_MDLINITIALIZESAMPLETIMES_INHERIT
177 #define UNUSED_MDLOUTPUTS
178 #define UNUSED_MDLTERMINATE
179 #include "trailer.c"