]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blobdiff - rpp/blocks/tlc_c/sfunction_din.tlc
Change license to MIT
[pes-rpp/rpp-simulink.git] / rpp / blocks / tlc_c / sfunction_din.tlc
index 489a91d2762cf84252b7ae7901919660e7979c4e..907393204dc4d6a2b8c97d4561a21840d3a6f4eb 100644 (file)
@@ -1,20 +1,28 @@
-%% Copyright (C) 2013 Czech Technical University in Prague
+%% Copyright (C) 2013, 2014 Czech Technical University in Prague
 %%
 %% Authors:
 %%     - Carlos Jenkins <carlos@jenkins.co.cr>
 %%
-%% This program is free software; you can redistribute it and/or modify
-%% it under the terms of the GNU General Public License as published by
-%% the Free Software Foundation; either version 2 of the License, or
-%% (at your option) any later version.
-%%
-%% This program is distributed in the hope that it will be useful,
-%% but WITHOUT ANY WARRANTY; without even the implied warranty of
-%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-%% GNU General Public License for more details.
-%%
-%% You should have received a copy of the GNU General Public License
-%% along with this program.  If not, see <http://www.gnu.org/licenses/>.
+%% Permission is hereby granted, free of charge, to any person
+%% obtaining a copy of this software and associated documentation
+%% files (the "Software"), to deal in the Software without
+%% restriction, including without limitation the rights to use,
+%% copy, modify, merge, publish, distribute, sublicense, and/or sell
+%% copies of the Software, and to permit persons to whom the
+%% Software is furnished to do so, subject to the following
+%% conditions:
+
+%% The above copyright notice and this permission notice shall be
+%% included in all copies or substantial portions of the Software.
+
+%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+%% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+%% OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+%% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+%% HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+%% WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+%% OTHER DEALINGS IN THE SOFTWARE.
 %%
 %% File : sfunction_din.tlc
 %% Abstract:
 %include "common.tlc"
 
 
+%% Function: BlockInstanceSetup ================================================
+%function BlockInstanceSetup(block, system) void
+    %assign ::rpp_din_present=1
+    %assign pin_num = LibBlockParameterValue(p1, 0)
+    
+    %switch (%<pin_num>)
+        %case 10
+        %case 11
+            %if EXISTS("rpp_irc_1_used") == 1
+                %<LibBlockReportError(block, "Either IRC1 or Digital Input pin 10 and 11 blocks are allowed in one model, not both.")>
+            %else
+                %assign ::rpp_din_10_11_used = 1
+            %endif
+            %break
+        %case 14
+        %case 15
+            %if EXISTS("rpp_irc_2_used") == 1
+                %<LibBlockReportError(block, "Either IRC2 or Digital Input pin 14 and 15 blocks are allowed in one model, not both.")>
+            %else
+                %assign ::rpp_din_14_15_used = 1
+            %endif
+            %break
+    %endswitch
+
+    %if %<pin_num> < 8
+        %% Create array if not exist
+        %if EXISTS("rpp_din_in_model_array") == 0
+            %assign ::rpp_din_in_model_array = [0, 0, 0, 0, 0, 0, 0, 0]
+        %endif
+
+        %if ::rpp_din_in_model_array[%<pin_num>] == 0
+            %assign ::rpp_din_in_model_array[%<pin_num>] = 1
+        %else
+            %assign err_msg = "Only one Digital Input block of pin %<pin_num> is allowed in the model."
+            %<LibBlockReportError(block, err_msg)>
+        %endif
+    %endif
+
+%endfunction
+
 %% Function: BlockTypeSetup ====================================================
 %function BlockTypeSetup(block, system) void
 
     %% Ensure required header files are included
     %<RppCommonBlockTypeSetup(block, system)>
+    %<LibAddToCommonIncludes("rpp/rpp.h")>
 
 %endfunction
 
 %function Start(block, system) Output
 
     %if !SLibCodeGenForSim()
-        %<RppCommonStart(block, system)>
+        %assign pin_num = LibBlockParameterValue(p1, 0)
+        %if pin_num < 8
+            %assign pull_up = LibBlockParameterValue(p3, 0) - 1
+            %assign active = LibBlockParameterValue(p4, 0) - 1
+            rpp_din_setup(%<pin_num>, %<pull_up>, %<active>, FALSE);
+        %endif
     %endif
 
 %endfunction
 
     %if !SLibCodeGenForSim()
 
+        %% Declare temporal variables
+        %if EXISTS("_RPP_DIN_TMP_VARS_") == 0
+            %assign ::_RPP_DIN_TMP_VARS_ = 1
+            int8_t din_tmp;
+        %endif
+
         %% Error flag
         %assign err_flag = LibBlockOutputSignal(1, "", "", 0)
 
         %% First executed block must update cached values
         %if EXISTS("_RPP_DIN_UPDATE_") == 0
-            %assign _RPP_DIN_UPDATE_ = 1
-            if(rpp_din_update() != SUCCESS) {
+            %assign ::_RPP_DIN_UPDATE_ = 1
+            if (rpp_din_update() != SUCCESS) {
                 %<err_flag> = TRUE;
             }
         %endif
 
         %% Get pin number and if to use variable threshold
-        %assign pin_num = LibBlockParameter(p1, "", "", 0)
-        %assign var_thr = LibBlockParameter(p2, "", "", 0)
+        %assign pin_num = LibBlockParameterValue(p1, 0)
+        %assign var_thr = LibBlockParameterValue(p2, 0)
 
         %% Get pin value
-        int8_t tmp = rpp_din_get(%<pin_num>, %<var_thr>);
-        if(tmp < 0) {
+       %if var_thr == 0
+         din_tmp = rpp_din_get(%<pin_num>);
+       %else
+         din_tmp = rpp_din_get_tr(%<pin_num>);
+       %endif
+        if (din_tmp < 0) {
             %<err_flag> = TRUE;
-            tmp = LOW;
+            din_tmp = LOW;
         }
 
         %% Return input
         %assign digital_in = LibBlockOutputSignal(0, "", "", 0)
-        %<digital_in> = tmp;
+        %<digital_in> = din_tmp;
 
     %endif