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