]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blob - rpp/blocks/tlc_c/sfunction_din.tlc
Change license to MIT
[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 %% Permission is hereby granted, free of charge, to any person
7 %% obtaining a copy of this software and associated documentation
8 %% files (the "Software"), to deal in the Software without
9 %% restriction, including without limitation the rights to use,
10 %% copy, modify, merge, publish, distribute, sublicense, and/or sell
11 %% copies of the Software, and to permit persons to whom the
12 %% Software is furnished to do so, subject to the following
13 %% conditions:
14
15 %% The above copyright notice and this permission notice shall be
16 %% included in all copies or substantial portions of the Software.
17
18 %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 %% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 %% OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 %% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 %% HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 %% WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 %% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 %% OTHER DEALINGS IN THE SOFTWARE.
26 %%
27 %% File : sfunction_din.tlc
28 %% Abstract:
29 %%     TLC file for inlining RPP digital input block.
30 %%
31 %% References:
32 %%     BlockTypeSetup() : rtw_tlc.pdf p. 277
33 %%     Start()          : rtw_tlc.pdf p. 279
34 %%     Outputs()        : rtw_tlc.pdf p. 281
35
36
37 %implements sfunction_din "C"
38
39 %include "common.tlc"
40
41
42 %% Function: BlockInstanceSetup ================================================
43 %function BlockInstanceSetup(block, system) void
44     %assign ::rpp_din_present=1
45     %assign pin_num = LibBlockParameterValue(p1, 0)
46     
47     %switch (%<pin_num>)
48         %case 10
49         %case 11
50             %if EXISTS("rpp_irc_1_used") == 1
51                 %<LibBlockReportError(block, "Either IRC1 or Digital Input pin 10 and 11 blocks are allowed in one model, not both.")>
52             %else
53                 %assign ::rpp_din_10_11_used = 1
54             %endif
55             %break
56         %case 14
57         %case 15
58             %if EXISTS("rpp_irc_2_used") == 1
59                 %<LibBlockReportError(block, "Either IRC2 or Digital Input pin 14 and 15 blocks are allowed in one model, not both.")>
60             %else
61                 %assign ::rpp_din_14_15_used = 1
62             %endif
63             %break
64     %endswitch
65
66     %if %<pin_num> < 8
67         %% Create array if not exist
68         %if EXISTS("rpp_din_in_model_array") == 0
69             %assign ::rpp_din_in_model_array = [0, 0, 0, 0, 0, 0, 0, 0]
70         %endif
71
72         %if ::rpp_din_in_model_array[%<pin_num>] == 0
73             %assign ::rpp_din_in_model_array[%<pin_num>] = 1
74         %else
75             %assign err_msg = "Only one Digital Input block of pin %<pin_num> is allowed in the model."
76             %<LibBlockReportError(block, err_msg)>
77         %endif
78     %endif
79
80 %endfunction
81
82 %% Function: BlockTypeSetup ====================================================
83 %function BlockTypeSetup(block, system) void
84
85     %% Ensure required header files are included
86     %<RppCommonBlockTypeSetup(block, system)>
87     %<LibAddToCommonIncludes("rpp/rpp.h")>
88
89 %endfunction
90
91
92 %% Function: Start =============================================================
93 %function Start(block, system) Output
94
95     %if !SLibCodeGenForSim()
96         %assign pin_num = LibBlockParameterValue(p1, 0)
97         %if pin_num < 8
98             %assign pull_up = LibBlockParameterValue(p3, 0) - 1
99             %assign active = LibBlockParameterValue(p4, 0) - 1
100             rpp_din_setup(%<pin_num>, %<pull_up>, %<active>, FALSE);
101         %endif
102     %endif
103
104 %endfunction
105
106
107 %% Function: Outputs ===========================================================
108 %function Outputs(block, system) Output
109
110     %if !SLibCodeGenForSim()
111
112         %% Declare temporal variables
113         %if EXISTS("_RPP_DIN_TMP_VARS_") == 0
114             %assign ::_RPP_DIN_TMP_VARS_ = 1
115             int8_t din_tmp;
116         %endif
117
118         %% Error flag
119         %assign err_flag = LibBlockOutputSignal(1, "", "", 0)
120
121         %% First executed block must update cached values
122         %if EXISTS("_RPP_DIN_UPDATE_") == 0
123             %assign ::_RPP_DIN_UPDATE_ = 1
124             if (rpp_din_update() != SUCCESS) {
125                 %<err_flag> = TRUE;
126             }
127         %endif
128
129         %% Get pin number and if to use variable threshold
130         %assign pin_num = LibBlockParameterValue(p1, 0)
131         %assign var_thr = LibBlockParameterValue(p2, 0)
132
133         %% Get pin value
134         %if var_thr == 0
135           din_tmp = rpp_din_get(%<pin_num>);
136         %else
137           din_tmp = rpp_din_get_tr(%<pin_num>);
138         %endif
139         if (din_tmp < 0) {
140             %<err_flag> = TRUE;
141             din_tmp = LOW;
142         }
143
144         %% Return input
145         %assign digital_in = LibBlockOutputSignal(0, "", "", 0)
146         %<digital_in> = din_tmp;
147
148     %endif
149
150 %endfunction
151
152 %% [EOF]