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