]> rtime.felk.cvut.cz Git - jenkicar/rpp-simulink.git/blob - rpp/blocks/tlc_c/sfunction_din.tlc
Add DIN setup parameters for pins 0-7 and buildtime prevent of multiple blocks for...
[jenkicar/rpp-simulink.git] / rpp / blocks / tlc_c / sfunction_din.tlc
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_din.tlc
12 %% Abstract:
13 %%     TLC file for inlining RPP digital input block.
14 %%
15 %% References:
16 %%     BlockTypeSetup() : rtw_tlc.pdf p. 277
17 %%     Start()          : rtw_tlc.pdf p. 279
18 %%     Outputs()        : rtw_tlc.pdf p. 281
19
20
21 %implements sfunction_din "C"
22
23 %include "common.tlc"
24
25
26 %% Function: BlockInstanceSetup ================================================
27 %function BlockInstanceSetup(block, system) void
28     %assign pin_num = LibBlockParameterValue(p1, 0)
29     
30     %switch (%<pin_num>)
31         %case 10
32         %case 11
33             %if EXISTS("rpp_irc_1_used") == 1
34                 %<LibBlockReportError(block, "Either IRC1 or Digital Input pin 10 and 11 blocks are allowed in one model, not both.")>
35             %else
36                 %assign ::rpp_din_10_11_used = 1
37             %endif
38             %break
39         %case 14
40         %case 15
41             %if EXISTS("rpp_irc_2_used") == 1
42                 %<LibBlockReportError(block, "Either IRC2 or Digital Input pin 14 and 15 blocks are allowed in one model, not both.")>
43             %else
44                 %assign ::rpp_din_14_15_used = 1
45             %endif
46             %break
47     %endswitch
48
49     %if %<pin_num> < 8
50         %% Create array if not exist
51         %if EXISTS("rpp_din_in_model_array") == 0
52             %assign ::rpp_din_in_model_array = [0, 0, 0, 0, 0, 0, 0, 0]
53         %endif
54
55         %if ::rpp_din_in_model_array[%<pin_num>] == 0
56             %assign ::rpp_din_in_model_array[%<pin_num>] = 1
57         %else
58             %assign err_msg = "Only one Digital Input block of pin %<pin_num> is allowed in the model."
59             %<LibBlockReportError(block, err_msg)>
60         %endif
61     %endif
62
63 %endfunction
64
65 %% Function: BlockTypeSetup ====================================================
66 %function BlockTypeSetup(block, system) void
67
68     %% Ensure required header files are included
69     %<RppCommonBlockTypeSetup(block, system)>
70
71 %endfunction
72
73
74 %% Function: Start =============================================================
75 %function Start(block, system) Output
76
77     %if !SLibCodeGenForSim()
78         %assign pin_num = LibBlockParameterValue(p1, 0)
79         %if pin_num < 8
80             %assign pull_up = LibBlockParameterValue(p3, 0) - 1
81             %assign active = LibBlockParameterValue(p4, 0) - 1
82             rpp_din_setup(%<pin_num>, %<pull_up>, %<active>, FALSE);
83         %endif
84     %endif
85
86 %endfunction
87
88
89 %% Function: Outputs ===========================================================
90 %function Outputs(block, system) Output
91
92     %if !SLibCodeGenForSim()
93
94         %% Declare temporal variables
95         %if EXISTS("_RPP_DIN_TMP_VARS_") == 0
96             %assign ::_RPP_DIN_TMP_VARS_ = 1
97             int8_t din_tmp;
98         %endif
99
100         %% Error flag
101         %assign err_flag = LibBlockOutputSignal(1, "", "", 0)
102
103         %% First executed block must update cached values
104         %if EXISTS("_RPP_DIN_UPDATE_") == 0
105             %assign ::_RPP_DIN_UPDATE_ = 1
106             if (rpp_din_update() != SUCCESS) {
107                 %<err_flag> = TRUE;
108             }
109         %endif
110
111         %% Get pin number and if to use variable threshold
112         %assign pin_num = LibBlockParameterValue(p1, 0)
113         %assign var_thr = LibBlockParameterValue(p2, 0)
114
115         %% Get pin value
116         %if var_thr == 0
117           din_tmp = rpp_din_get(%<pin_num>);
118         %else
119           din_tmp = rpp_din_get_tr(%<pin_num>);
120         %endif
121         if (din_tmp < 0) {
122             %<err_flag> = TRUE;
123             din_tmp = LOW;
124         }
125
126         %% Return input
127         %assign digital_in = LibBlockOutputSignal(0, "", "", 0)
128         %<digital_in> = din_tmp;
129
130     %endif
131
132 %endfunction
133
134 %% [EOF]