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