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