plcopen/structures.py
author Laurent Bessard
Wed, 05 Sep 2012 12:39:50 +0200
changeset 761 996515c4b394
parent 754 48966b6ceedc
permissions -rw-r--r--
Fix bug when drag'n dropping location with undefined direction on Windows
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     1
#!/usr/bin/env python
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     3
14
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
     4
import string, os, sys
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
     5
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     6
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     7
#based on the plcopen standard. 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     8
#
58
39cd981ff242 Changing file headers
lbessard
parents: 57
diff changeset
     9
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    10
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    11
#See COPYING file for copyrights details.
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    12
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    13
#This library is free software; you can redistribute it and/or
5
f8652b073e84 GPL->LGPL
etisserant
parents: 0
diff changeset
    14
#modify it under the terms of the GNU General Public
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    15
#License as published by the Free Software Foundation; either
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    16
#version 2.1 of the License, or (at your option) any later version.
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    17
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    18
#This library is distributed in the hope that it will be useful,
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    19
#but WITHOUT ANY WARRANTY; without even the implied warranty of
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    20
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
58
39cd981ff242 Changing file headers
lbessard
parents: 57
diff changeset
    21
#General Public License for more details.
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    22
#
5
f8652b073e84 GPL->LGPL
etisserant
parents: 0
diff changeset
    23
#You should have received a copy of the GNU General Public
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    24
#License along with this library; if not, write to the Free Software
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    25
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    26
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    27
9
b29105e29081 Adding test on step names in SFC Editor
lbessard
parents: 5
diff changeset
    28
LANGUAGES = ["IL","ST","FBD","LD","SFC"]
b29105e29081 Adding test on step names in SFC Editor
lbessard
parents: 5
diff changeset
    29
104
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 99
diff changeset
    30
LOCATIONDATATYPES = {"X" : ["BOOL"],
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 99
diff changeset
    31
                     "B" : ["SINT", "USINT", "BYTE", "STRING"],
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 99
diff changeset
    32
                     "W" : ["INT", "UINT", "WORD", "WSTRING"],
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 99
diff changeset
    33
                     "D" : ["DINT", "UDINT", "REAL", "DWORD"],
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 99
diff changeset
    34
                     "L" : ["LINT", "ULINT", "LREAL", "LWORD"]} 
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 99
diff changeset
    35
391
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
    36
_ = lambda x:x
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
    37
230
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
    38
# Helper for emulate join on element list
305
0ed2b61de43e Fixed bug structures.py crashing with non-connected blocks.
etisserant
parents: 284
diff changeset
    39
def JoinList(separator, mylist):
0ed2b61de43e Fixed bug structures.py crashing with non-connected blocks.
etisserant
parents: 284
diff changeset
    40
    if len(mylist) > 0 :
0ed2b61de43e Fixed bug structures.py crashing with non-connected blocks.
etisserant
parents: 284
diff changeset
    41
        return reduce(lambda x, y: x + separator + y, mylist)
0ed2b61de43e Fixed bug structures.py crashing with non-connected blocks.
etisserant
parents: 284
diff changeset
    42
    else :
0ed2b61de43e Fixed bug structures.py crashing with non-connected blocks.
etisserant
parents: 284
diff changeset
    43
        return mylist
230
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
    44
754
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    45
def generate_block(generator, block, block_infos, body, link, order=False, to_inout=False):
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    46
    body_type = body.getcontent()["name"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    47
    name = block.getinstanceName()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    48
    type = block.gettypeName()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    49
    executionOrderId = block.getexecutionOrderId()
754
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    50
    inout_variables = {}
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    51
    for input_variable in block.inputVariables.getvariable():
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    52
        for output_variable in block.outputVariables.getvariable():
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    53
            if input_variable.getformalParameter() == output_variable.getformalParameter():
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    54
                inout_variables[input_variable.getformalParameter()] = ""
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 109
diff changeset
    55
    if block_infos["type"] == "function":
270
351d134babd7 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 250
diff changeset
    56
        output_variables = block.outputVariables.getvariable()
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 109
diff changeset
    57
        if not generator.ComputedBlocks.get(block, False) and not order:
208
c70aefcadf66 Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents: 194
diff changeset
    58
            generator.ComputedBlocks[block] = True
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 109
diff changeset
    59
            vars = []
307
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 305
diff changeset
    60
            one_input_connected = False
230
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
    61
            for i, variable in enumerate(block.inputVariables.getvariable()):
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
    62
                input_info = (generator.TagName, "block", block.getlocalId(), "input", i)
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    63
                connections = variable.connectionPointIn.getconnections()
250
e6b58eafef1a Adding support for Debugging in PLCOpenEditor
lbessard
parents: 247
diff changeset
    64
                if connections is not None:
307
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 305
diff changeset
    65
                    parameter = variable.getformalParameter()
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 305
diff changeset
    66
                    if parameter != "EN":
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 305
diff changeset
    67
                        one_input_connected = True
754
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    68
                    if inout_variables.has_key(parameter):
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    69
                        value = generator.ComputeExpression(body, connections, executionOrderId > 0, True)
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    70
                        inout_variables[parameter] = value
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    71
                    else:
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    72
                        value = generator.ComputeExpression(body, connections, executionOrderId > 0)
270
351d134babd7 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 250
diff changeset
    73
                    if len(output_variables) > 1:
351d134babd7 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 250
diff changeset
    74
                        vars.append([(parameter, input_info),
351d134babd7 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 250
diff changeset
    75
                                     (" := ", ())] + generator.ExtractModifier(variable, value, input_info))
351d134babd7 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 250
diff changeset
    76
                    else:
351d134babd7 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 250
diff changeset
    77
                        vars.append(generator.ExtractModifier(variable, value, input_info))
307
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 305
diff changeset
    78
            if one_input_connected:
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 305
diff changeset
    79
                for i, variable in enumerate(output_variables):
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 305
diff changeset
    80
                    parameter = variable.getformalParameter()
754
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    81
                    if not inout_variables.has_key(parameter):
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    82
                        if variable.getformalParameter() == "":
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    83
                            variable_name = "%s%d"%(type, block.getlocalId())
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    84
                        else:
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    85
                            variable_name = "%s%d_%s"%(type, block.getlocalId(), parameter)
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    86
                        if generator.Interface[-1][0] != "VAR" or generator.Interface[-1][1] is not None or generator.Interface[-1][2]:
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    87
                            generator.Interface.append(("VAR", None, False, []))
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    88
                        if variable.connectionPointOut in generator.ConnectionTypes:
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    89
                            generator.Interface[-1][3].append((generator.ConnectionTypes[variable.connectionPointOut], variable_name, None, None))
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    90
                        else:
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    91
                            generator.Interface[-1][3].append(("ANY", variable_name, None, None))
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    92
                        if len(output_variables) > 1 and parameter not in ["", "OUT"]:
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    93
                            vars.append([(parameter, (generator.TagName, "block", block.getlocalId(), "output", i)), 
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    94
                                         (" => %s"%variable_name, ())])
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    95
                        else:
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    96
                            output_info = (generator.TagName, "block", block.getlocalId(), "output", i)
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
    97
                            output_name = variable_name
307
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 305
diff changeset
    98
                generator.Program += [(generator.CurrentIndent, ()),
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 305
diff changeset
    99
                                      (output_name, output_info),
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 305
diff changeset
   100
                                      (" := ", ()),
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 305
diff changeset
   101
                                      (type, (generator.TagName, "block", block.getlocalId(), "type")),
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 305
diff changeset
   102
                                      ("(", ())]
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 305
diff changeset
   103
                generator.Program += JoinList([(", ", ())], vars)
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 305
diff changeset
   104
                generator.Program += [(");\n", ())]
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 305
diff changeset
   105
            else:
391
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   106
                generator.Warnings.append(_("\"%s\" function cancelled in \"%s\" POU: No input connected")%(type, generator.TagName.split("::")[-1]))
270
351d134babd7 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 250
diff changeset
   107
        if link:
351d134babd7 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 250
diff changeset
   108
            connectionPoint = link.getposition()[-1]
351d134babd7 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 250
diff changeset
   109
        else:
351d134babd7 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 250
diff changeset
   110
            connectionPoint = None
351d134babd7 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 250
diff changeset
   111
        for i, variable in enumerate(output_variables):
351d134babd7 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 250
diff changeset
   112
            blockPointx, blockPointy = variable.connectionPointOut.getrelPositionXY()
351d134babd7 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 250
diff changeset
   113
            if not connectionPoint or block.getx() + blockPointx == connectionPoint.getx() and block.gety() + blockPointy == connectionPoint.gety():
351d134babd7 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 250
diff changeset
   114
                output_info = (generator.TagName, "block", block.getlocalId(), "output", i)
754
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   115
                parameter = variable.getformalParameter()
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   116
                if inout_variables.has_key(parameter):
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   117
                    output_value = inout_variables[parameter]
270
351d134babd7 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 250
diff changeset
   118
                else:
754
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   119
                    if parameter == "":
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   120
                        output_name = "%s%d"%(type, block.getlocalId())
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   121
                    else:
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   122
                        output_name = "%s%d_%s"%(type, block.getlocalId(), parameter)
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   123
                    output_value = [(output_name, output_info)]
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   124
                return generator.ExtractModifier(variable, output_value, output_info)
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 58
diff changeset
   125
    elif block_infos["type"] == "functionBlock":
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 109
diff changeset
   126
        if not generator.ComputedBlocks.get(block, False) and not order:
208
c70aefcadf66 Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents: 194
diff changeset
   127
            generator.ComputedBlocks[block] = True
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 58
diff changeset
   128
            vars = []
230
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   129
            for i, variable in enumerate(block.inputVariables.getvariable()):
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   130
                input_info = (generator.TagName, "block", block.getlocalId(), "input", i)
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   131
                connections = variable.connectionPointIn.getconnections()
250
e6b58eafef1a Adding support for Debugging in PLCOpenEditor
lbessard
parents: 247
diff changeset
   132
                if connections is not None:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   133
                    parameter = variable.getformalParameter()
754
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   134
                    value = generator.ComputeExpression(body, connections, executionOrderId > 0, inout_variables.has_key(parameter))
230
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   135
                    vars.append([(parameter, input_info),
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   136
                                 (" := ", ())] + generator.ExtractModifier(variable, value, input_info))
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   137
            generator.Program += [(generator.CurrentIndent, ()), 
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   138
                                  (name, (generator.TagName, "block", block.getlocalId(), "name")),
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   139
                                  ("(", ())]
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   140
            generator.Program += JoinList([(", ", ())], vars)
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   141
            generator.Program += [(");\n", ())]
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 58
diff changeset
   142
        if link:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   143
            connectionPoint = link.getposition()[-1]
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 58
diff changeset
   144
        else:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 58
diff changeset
   145
            connectionPoint = None
230
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   146
        for i, variable in enumerate(block.outputVariables.getvariable()):
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   147
            blockPointx, blockPointy = variable.connectionPointOut.getrelPositionXY()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   148
            if not connectionPoint or block.getx() + blockPointx == connectionPoint.getx() and block.gety() + blockPointy == connectionPoint.gety():
270
351d134babd7 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 250
diff changeset
   149
                output_info = (generator.TagName, "block", block.getlocalId(), "output", i)
754
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   150
                output_name = generator.ExtractModifier(variable, [("%s.%s"%(name, variable.getformalParameter()), output_info)], output_info)
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   151
                if to_inout:
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   152
                    variable_name = "%s_%s"%(name, variable.getformalParameter())
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   153
                    if not generator.IsAlreadyDefined(variable_name):
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   154
                        if generator.Interface[-1][0] != "VAR" or generator.Interface[-1][1] is not None or generator.Interface[-1][2]:
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   155
                            generator.Interface.append(("VAR", None, False, []))
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   156
                        if variable.connectionPointOut in generator.ConnectionTypes:
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   157
                            generator.Interface[-1][3].append((generator.ConnectionTypes[variable.connectionPointOut], variable_name, None, None))
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   158
                        else:
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   159
                            generator.Interface[-1][3].append(("ANY", variable_name, None, None))
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   160
                        generator.Program += [(generator.CurrentIndent, ()),
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   161
                                              ("%s := "%variable_name, ())]
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   162
                        generator.Program += output_name
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   163
                        generator.Program += [(";\n", ())]
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   164
                    return [(variable_name, ())]
48966b6ceedc Fix bug in ST code generated for in-out variables in Function and FunctionBlock interface
Laurent Bessard
parents: 649
diff changeset
   165
                return output_name 
354
20ccf1e5266e Allow usage of FunctionBlock without output
greg
parents: 307
diff changeset
   166
    if link is not None:
391
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   167
        raise ValueError, _("No output variable found")
21
e619d7bea692 Add support for AND, OR, XOR for booleans
lbessard
parents: 19
diff changeset
   168
194
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 170
diff changeset
   169
def initialise_block(type, name, block = None):
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 92
diff changeset
   170
    return [(type, name, None, None)]
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 92
diff changeset
   171
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   172
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   173
#                        Function Block Types definitions
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   174
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   175
21
e619d7bea692 Add support for AND, OR, XOR for booleans
lbessard
parents: 19
diff changeset
   176
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   177
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   178
Ordored list of common Function Blocks defined in the IEC 61131-3
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   179
Each block have this attributes:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   180
    - "name" : The block name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   181
    - "type" : The block type. It can be "function", "functionBlock" or "program"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   182
    - "extensible" : Boolean that define if the block is extensible
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   183
    - "inputs" : List of the block inputs
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   184
    - "outputs" : List of the block outputs
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   185
    - "comment" : Comment that will be displayed in the block popup
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 58
diff changeset
   186
    - "generate" : Method that generator will call for generating ST block code
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   187
Inputs and outputs are a tuple of characteristics that are in order:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   188
    - The name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   189
    - The data type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   190
    - The default modifier which can be "none", "negated", "rising" or "falling"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   191
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   192
391
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   193
BlockTypes = [{"name" : _("Standard function blocks"), "list":
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   194
               [{"name" : "SR", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   195
                    "inputs" : [("S1","BOOL","none"),("R","BOOL","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   196
                    "outputs" : [("Q1","BOOL","none")],
391
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   197
                    "comment" : _("SR bistable\nThe SR bistable is a latch where the Set dominates."),
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 92
diff changeset
   198
                    "generate" : generate_block, "initialise" : initialise_block},
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   199
                {"name" : "RS", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   200
                    "inputs" : [("S","BOOL","none"),("R1","BOOL","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   201
                    "outputs" : [("Q1","BOOL","none")],
391
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   202
                    "comment" : _("RS bistable\nThe RS bistable is a latch where the Reset dominates."),
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 92
diff changeset
   203
                    "generate" : generate_block, "initialise" : initialise_block},
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   204
                {"name" : "SEMA", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   205
                    "inputs" : [("CLAIM","BOOL","none"),("RELEASE","BOOL","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   206
                    "outputs" : [("BUSY","BOOL","none")],
391
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   207
                    "comment" : _("Semaphore\nThe semaphore provides a mechanism to allow software elements mutually exclusive access to certain ressources."),
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 92
diff changeset
   208
                    "generate" : generate_block, "initialise" : initialise_block},
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   209
                {"name" : "R_TRIG", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   210
                    "inputs" : [("CLK","BOOL","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   211
                    "outputs" : [("Q","BOOL","none")],
391
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   212
                    "comment" : _("Rising edge detector\nThe output produces a single pulse when a rising edge is detected."),
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 92
diff changeset
   213
                    "generate" : generate_block, "initialise" : initialise_block},
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   214
                {"name" : "F_TRIG", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   215
                    "inputs" : [("CLK","BOOL","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   216
                    "outputs" : [("Q","BOOL","none")],
391
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   217
                    "comment" : _("Falling edge detector\nThe output produces a single pulse when a falling edge is detected."),
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 92
diff changeset
   218
                    "generate" : generate_block, "initialise" : initialise_block},
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   219
                {"name" : "CTU", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   220
                    "inputs" : [("CU","BOOL","rising"),("R","BOOL","none"),("PV","INT","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   221
                    "outputs" : [("Q","BOOL","none"),("CV","INT","none")],
391
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   222
                    "comment" : _("Up-counter\nThe up-counter can be used to signal when a count has reached a maximum value."),
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 92
diff changeset
   223
                    "generate" : generate_block, "initialise" : initialise_block},
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   224
                {"name" : "CTD", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   225
                    "inputs" : [("CD","BOOL","rising"),("LD","BOOL","none"),("PV","INT","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   226
                    "outputs" : [("Q","BOOL","none"),("CV","INT","none")],
391
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   227
                    "comment" : _("Down-counter\nThe down-counter can be used to signal when a count has reached zero, on counting down from a preset value."),
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 92
diff changeset
   228
                    "generate" : generate_block, "initialise" : initialise_block},
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   229
                {"name" : "CTUD", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   230
                    "inputs" : [("CU","BOOL","rising"),("CD","BOOL","rising"),("R","BOOL","none"),("LD","BOOL","none"),("PV","INT","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   231
                    "outputs" : [("QU","BOOL","none"),("QD","BOOL","none"),("CV","INT","none")],
391
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   232
                    "comment" : _("Up-down counter\nThe up-down counter has two inputs CU and CD. It can be used to both count up on one input and down on the other."),
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 92
diff changeset
   233
                    "generate" : generate_block, "initialise" : initialise_block},
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   234
                {"name" : "TP", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   235
                    "inputs" : [("IN","BOOL","none"),("PT","TIME","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   236
                    "outputs" : [("Q","BOOL","none"),("ET","TIME","none")],
391
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   237
                    "comment" : _("Pulse timer\nThe pulse timer can be used to generate output pulses of a given time duration."),
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 92
diff changeset
   238
                    "generate" : generate_block, "initialise" : initialise_block},
247
4a47ccafbef7 Inversion between TON and TOF comments and errors in IL keywords fixed
lbessard
parents: 242
diff changeset
   239
                {"name" : "TON", "type" : "functionBlock", "extensible" : False, 
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   240
                    "inputs" : [("IN","BOOL","none"),("PT","TIME","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   241
                    "outputs" : [("Q","BOOL","none"),("ET","TIME","none")],
391
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   242
                    "comment" : _("On-delay timer\nThe on-delay timer can be used to delay setting an output true, for fixed period after an input becomes true."),
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 92
diff changeset
   243
                    "generate" : generate_block, "initialise" : initialise_block},
247
4a47ccafbef7 Inversion between TON and TOF comments and errors in IL keywords fixed
lbessard
parents: 242
diff changeset
   244
                {"name" : "TOF", "type" : "functionBlock", "extensible" : False, 
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   245
                    "inputs" : [("IN","BOOL","none"),("PT","TIME","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   246
                    "outputs" : [("Q","BOOL","none"),("ET","TIME","none")],
391
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   247
                    "comment" : _("Off-delay timer\nThe off-delay timer can be used to delay setting an output false, for fixed period after input goes false."),
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 92
diff changeset
   248
                    "generate" : generate_block, "initialise" : initialise_block},
373
db5f282946b0 Removing undefined standard function blocks
lbessard
parents: 371
diff changeset
   249
                ]},
427
22d16c457d87 improved English spelling & grammar
b.taylor@willowglen.ca
parents: 404
diff changeset
   250
              {"name" : _("Additional function blocks"), "list":
649
ea237e1431ec Enable RTC in standard FB lib
Edouard Tisserant
parents: 638
diff changeset
   251
               [{"name" : "RTC", "type" : "functionBlock", "extensible" : False, 
ea237e1431ec Enable RTC in standard FB lib
Edouard Tisserant
parents: 638
diff changeset
   252
                    "inputs" : [("IN","BOOL","none"),("PDT","DATE_AND_TIME","none")], 
ea237e1431ec Enable RTC in standard FB lib
Edouard Tisserant
parents: 638
diff changeset
   253
                    "outputs" : [("Q","BOOL","none"),("CDT","DATE_AND_TIME","none")],
ea237e1431ec Enable RTC in standard FB lib
Edouard Tisserant
parents: 638
diff changeset
   254
                    "comment" : _("Real time clock\nThe real time clock has many uses including time stamping, setting dates and times of day in batch reports, in alarm messages and so on."),
ea237e1431ec Enable RTC in standard FB lib
Edouard Tisserant
parents: 638
diff changeset
   255
                    "generate" : generate_block, "initialise" : initialise_block},
ea237e1431ec Enable RTC in standard FB lib
Edouard Tisserant
parents: 638
diff changeset
   256
                {"name" : "INTEGRAL", "type" : "functionBlock", "extensible" : False, 
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   257
                    "inputs" : [("RUN","BOOL","none"),("R1","BOOL","none"),("XIN","REAL","none"),("X0","REAL","none"),("CYCLE","TIME","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   258
                    "outputs" : [("Q","BOOL","none"),("XOUT","REAL","none")],
391
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   259
                    "comment" : _("Integral\nThe integral function block integrates the value of input XIN over time."),
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 92
diff changeset
   260
                    "generate" : generate_block, "initialise" : initialise_block},
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   261
                {"name" : "DERIVATIVE", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   262
                    "inputs" : [("RUN","BOOL","none"),("XIN","REAL","none"),("CYCLE","TIME","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   263
                    "outputs" : [("XOUT","REAL","none")],
391
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   264
                    "comment" : _("Derivative\nThe derivative function block produces an output XOUT proportional to the rate of change of the input XIN."),
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 92
diff changeset
   265
                    "generate" : generate_block, "initialise" : initialise_block},
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   266
                {"name" : "PID", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   267
                    "inputs" : [("AUTO","BOOL","none"),("PV","REAL","none"),("SP","REAL","none"),("X0","REAL","none"),("KP","REAL","none"),("TR","REAL","none"),("TD","REAL","none"),("CYCLE","TIME","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   268
                    "outputs" : [("XOUT","REAL","none")],
391
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   269
                    "comment" : _("PID\nThe PID (proportional, Integral, Derivative) function block provides the classical three term controller for closed loop control."),
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 92
diff changeset
   270
                    "generate" : generate_block, "initialise" : initialise_block},
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   271
                {"name" : "RAMP", "type" : "functionBlock", "extensible" : False, 
638
2b9ed21d965c Fix issues with standard library definition
laurent
parents: 586
diff changeset
   272
                    "inputs" : [("RUN","BOOL","none"),("X0","REAL","none"),("X1","REAL","none"),("TR","TIME","none"),("CYCLE","TIME","none")], 
2b9ed21d965c Fix issues with standard library definition
laurent
parents: 586
diff changeset
   273
                    "outputs" : [("BUSY","BOOL","none"),("XOUT","REAL","none")],
2b9ed21d965c Fix issues with standard library definition
laurent
parents: 586
diff changeset
   274
                    "comment" : _("Ramp\nThe RAMP function block is modelled on example given in the standard."),
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 92
diff changeset
   275
                    "generate" : generate_block, "initialise" : initialise_block},
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   276
                {"name" : "HYSTERESIS", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   277
                    "inputs" : [("XIN1","REAL","none"),("XIN2","REAL","none"),("EPS","REAL","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   278
                    "outputs" : [("Q","BOOL","none")],
391
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   279
                    "comment" : _("Hysteresis\nThe hysteresis function block provides a hysteresis boolean output driven by the difference of two floating point (REAL) inputs XIN1 and XIN2."),
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 92
diff changeset
   280
                    "generate" : generate_block, "initialise" : initialise_block},
371
a69a5a72daa1 Removing undefined standard function blocks
lbessard
parents: 354
diff changeset
   281
##                {"name" : "RATIO_MONITOR", "type" : "functionBlock", "extensible" : False, 
a69a5a72daa1 Removing undefined standard function blocks
lbessard
parents: 354
diff changeset
   282
##                    "inputs" : [("PV1","REAL","none"),("PV2","REAL","none"),("RATIO","REAL","none"),("TIMON","TIME","none"),("TIMOFF","TIME","none"),("TOLERANCE","BOOL","none"),("RESET","BOOL","none"),("CYCLE","TIME","none")], 
a69a5a72daa1 Removing undefined standard function blocks
lbessard
parents: 354
diff changeset
   283
##                    "outputs" : [("ALARM","BOOL","none"),("TOTAL_ERR","BOOL","none")],
391
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   284
##                    "comment" : _("Ratio monitor\nThe ratio_monitor function block checks that one process value PV1 is always a given ratio (defined by input RATIO) of a second process value PV2."),
371
a69a5a72daa1 Removing undefined standard function blocks
lbessard
parents: 354
diff changeset
   285
##                    "generate" : generate_block, "initialise" : initialise_block}
57
9bf197698af0 SVGUI Functions Blocks added
jon
parents: 47
diff changeset
   286
                ]},
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   287
             ]
230
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   288
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   289
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   290
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   291
#                           Data Types definitions
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   292
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   293
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   294
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   295
Ordored list of common data types defined in the IEC 61131-3
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   296
Each type is associated to his direct parent type. It defines then a hierarchy
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   297
between type that permits to make a comparison of two types
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   298
"""
18
ee18a387e80a Enhanced standard funcrtion declaration from .csv file.
etisserant
parents: 15
diff changeset
   299
TypeHierarchy_list = [
22
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   300
    ("ANY", None),
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   301
    ("ANY_DERIVED", "ANY"),
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   302
    ("ANY_ELEMENTARY", "ANY"),
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   303
    ("ANY_MAGNITUDE", "ANY_ELEMENTARY"),
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   304
    ("ANY_BIT", "ANY_ELEMENTARY"),
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   305
    ("ANY_NBIT", "ANY_BIT"),
22
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   306
    ("ANY_STRING", "ANY_ELEMENTARY"),
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   307
    ("ANY_DATE", "ANY_ELEMENTARY"),
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   308
    ("ANY_NUM", "ANY_MAGNITUDE"),
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   309
    ("ANY_REAL", "ANY_NUM"),
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   310
    ("ANY_INT", "ANY_NUM"),
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   311
    ("ANY_SINT", "ANY_INT"),
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   312
    ("ANY_UINT", "ANY_INT"),
230
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   313
    ("BOOL", "ANY_BIT"),
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   314
    ("SINT", "ANY_SINT"),
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   315
    ("INT", "ANY_SINT"),
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   316
    ("DINT", "ANY_SINT"),
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   317
    ("LINT", "ANY_SINT"),
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   318
    ("USINT", "ANY_UINT"),
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   319
    ("UINT", "ANY_UINT"),
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   320
    ("UDINT", "ANY_UINT"),
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   321
    ("ULINT", "ANY_UINT"),
27
dae55dd9ee14 Current developping version
lbessard
parents: 25
diff changeset
   322
    ("REAL", "ANY_REAL"),
dae55dd9ee14 Current developping version
lbessard
parents: 25
diff changeset
   323
    ("LREAL", "ANY_REAL"),
22
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   324
    ("TIME", "ANY_MAGNITUDE"),
27
dae55dd9ee14 Current developping version
lbessard
parents: 25
diff changeset
   325
    ("DATE", "ANY_DATE"),
dae55dd9ee14 Current developping version
lbessard
parents: 25
diff changeset
   326
    ("TOD", "ANY_DATE"),
dae55dd9ee14 Current developping version
lbessard
parents: 25
diff changeset
   327
    ("DT", "ANY_DATE"),
dae55dd9ee14 Current developping version
lbessard
parents: 25
diff changeset
   328
    ("STRING", "ANY_STRING"),
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   329
    ("BYTE", "ANY_NBIT"),
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   330
    ("WORD", "ANY_NBIT"),
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   331
    ("DWORD", "ANY_NBIT"),
27
dae55dd9ee14 Current developping version
lbessard
parents: 25
diff changeset
   332
    ("LWORD", "ANY_NBIT")
dae55dd9ee14 Current developping version
lbessard
parents: 25
diff changeset
   333
    #("WSTRING", "ANY_STRING") # TODO
dae55dd9ee14 Current developping version
lbessard
parents: 25
diff changeset
   334
]
18
ee18a387e80a Enhanced standard funcrtion declaration from .csv file.
etisserant
parents: 15
diff changeset
   335
ee18a387e80a Enhanced standard funcrtion declaration from .csv file.
etisserant
parents: 15
diff changeset
   336
TypeHierarchy = dict(TypeHierarchy_list)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   337
230
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   338
"""
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   339
returns true if the given data type is the same that "reference" meta-type or one of its types.
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   340
"""
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   341
def IsOfType(type, reference):
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   342
    if reference is None:
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   343
        return True
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   344
    elif type == reference:
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   345
        return True
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   346
    else:
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   347
        parent_type = TypeHierarchy[type]
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   348
        if parent_type is not None:
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   349
            return IsOfType(parent_type, reference)
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   350
    return False
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   351
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   352
"""
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   353
returns list of all types that correspont to the ANY* meta type
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   354
"""
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   355
def GetSubTypes(type):
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   356
    return [typename for typename, parenttype in TypeHierarchy.items() if not typename.startswith("ANY") and IsOfType(typename, type)]
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   357
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 109
diff changeset
   358
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 109
diff changeset
   359
DataTypeRange_list = [
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 109
diff changeset
   360
    ("SINT", (-2**7, 2**7 - 1)),
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 109
diff changeset
   361
    ("INT", (-2**15, 2**15 - 1)),
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 109
diff changeset
   362
    ("DINT", (-2**31, 2**31 - 1)),
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 109
diff changeset
   363
    ("LINT", (-2**31, 2**31 - 1)),
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 109
diff changeset
   364
    ("USINT", (0, 2**8 - 1)),
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 109
diff changeset
   365
    ("UINT", (0, 2**16 - 1)),
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 109
diff changeset
   366
    ("UDINT", (0, 2**31 - 1)),
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 109
diff changeset
   367
    ("ULINT", (0, 2**31 - 1))
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 109
diff changeset
   368
]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 109
diff changeset
   369
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 109
diff changeset
   370
DataTypeRange = dict(DataTypeRange_list)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 109
diff changeset
   371
230
45d70748e45a Moving Data types and POU types informations into project model
lbessard
parents: 210
diff changeset
   372
18
ee18a387e80a Enhanced standard funcrtion declaration from .csv file.
etisserant
parents: 15
diff changeset
   373
21
e619d7bea692 Add support for AND, OR, XOR for booleans
lbessard
parents: 19
diff changeset
   374
#-------------------------------------------------------------------------------
e619d7bea692 Add support for AND, OR, XOR for booleans
lbessard
parents: 19
diff changeset
   375
#                             Test identifier
e619d7bea692 Add support for AND, OR, XOR for booleans
lbessard
parents: 19
diff changeset
   376
#-------------------------------------------------------------------------------
e619d7bea692 Add support for AND, OR, XOR for booleans
lbessard
parents: 19
diff changeset
   377
e619d7bea692 Add support for AND, OR, XOR for booleans
lbessard
parents: 19
diff changeset
   378
e619d7bea692 Add support for AND, OR, XOR for booleans
lbessard
parents: 19
diff changeset
   379
e619d7bea692 Add support for AND, OR, XOR for booleans
lbessard
parents: 19
diff changeset
   380
# Test if identifier is valid
e619d7bea692 Add support for AND, OR, XOR for booleans
lbessard
parents: 19
diff changeset
   381
def TestIdentifier(identifier):
e619d7bea692 Add support for AND, OR, XOR for booleans
lbessard
parents: 19
diff changeset
   382
     if identifier[0].isdigit():
e619d7bea692 Add support for AND, OR, XOR for booleans
lbessard
parents: 19
diff changeset
   383
        return False
e619d7bea692 Add support for AND, OR, XOR for booleans
lbessard
parents: 19
diff changeset
   384
     words = identifier.split('_')
e619d7bea692 Add support for AND, OR, XOR for booleans
lbessard
parents: 19
diff changeset
   385
     for i, word in enumerate(words):
e619d7bea692 Add support for AND, OR, XOR for booleans
lbessard
parents: 19
diff changeset
   386
         if len(word) == 0 and i != 0:
e619d7bea692 Add support for AND, OR, XOR for booleans
lbessard
parents: 19
diff changeset
   387
             return False
e619d7bea692 Add support for AND, OR, XOR for booleans
lbessard
parents: 19
diff changeset
   388
         if len(word) != 0 and not word.isalnum():
e619d7bea692 Add support for AND, OR, XOR for booleans
lbessard
parents: 19
diff changeset
   389
             return False
e619d7bea692 Add support for AND, OR, XOR for booleans
lbessard
parents: 19
diff changeset
   390
     return True
e619d7bea692 Add support for AND, OR, XOR for booleans
lbessard
parents: 19
diff changeset
   391
e619d7bea692 Add support for AND, OR, XOR for booleans
lbessard
parents: 19
diff changeset
   392
e619d7bea692 Add support for AND, OR, XOR for booleans
lbessard
parents: 19
diff changeset
   393
#-------------------------------------------------------------------------------
109
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   394
#                        Standard functions list generation
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   395
#-------------------------------------------------------------------------------
18
ee18a387e80a Enhanced standard funcrtion declaration from .csv file.
etisserant
parents: 15
diff changeset
   396
ee18a387e80a Enhanced standard funcrtion declaration from .csv file.
etisserant
parents: 15
diff changeset
   397
15
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   398
"""
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   399
take a .csv file and translate it it a "csv_table"
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   400
"""            
14
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   401
def csv_file_to_table(file):
22
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   402
    return [ map(string.strip,line.split(';')) for line in file.xreadlines()]
14
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   403
15
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   404
"""
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   405
seek into the csv table to a section ( section_name match 1st field )
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   406
return the matching row without first field
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   407
"""
14
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   408
def find_section(section_name, table):
22
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   409
    fields = [None]
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   410
    while(fields[0] != section_name):
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   411
        fields = table.pop(0)
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   412
    return fields[1:]
14
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   413
15
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   414
"""
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   415
extract the standard functions standard parameter names and types...
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   416
return a { ParameterName: Type, ...}
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   417
"""
14
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   418
def get_standard_funtions_input_variables(table):
22
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   419
    variables = find_section("Standard_functions_variables_types", table)
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   420
    standard_funtions_input_variables = {}
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   421
    fields = [True,True]
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   422
    while(fields[1]):
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   423
        fields = table.pop(0)
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   424
        variable_from_csv = dict([(champ, val) for champ, val in zip(variables, fields[1:]) if champ!=''])
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   425
        standard_funtions_input_variables[variable_from_csv['name']] = variable_from_csv['type']
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   426
    return standard_funtions_input_variables
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   427
    
15
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   428
"""
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   429
translate .csv file input declaration into PLCOpenEditor interessting values
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   430
in : "(ANY_NUM, ANY_NUM)" and { ParameterName: Type, ...}
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   431
return [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")] 
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   432
"""
14
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   433
def csv_input_translate(str_decl, variables, base):
22
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   434
    decl = str_decl.replace('(','').replace(')','').replace(' ','').split(',')
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   435
    params = []
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   436
    
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   437
    len_of_not_predifined_variable = len([True for param_type in decl if param_type not in variables])
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   438
    
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   439
    for param_type in decl:
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   440
        if param_type in variables.keys():
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   441
            param_name = param_type
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   442
            param_type = variables[param_type]
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   443
        elif len_of_not_predifined_variable > 1:
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   444
            param_name = "IN%d"%base
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   445
            base += 1
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   446
        else:
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   447
            param_name = "IN"
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   448
        params.append((param_name, param_type, "none"))
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   449
    return params
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   450
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   451
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   452
ANY_TO_ANY_LIST=[
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   453
        # simple type conv are let as C cast
541
24f111835805 Fixing REAL_TO_INT conversion (behaved like a trunc and not like a round)
laurent
parents: 526
diff changeset
   454
        (("ANY_INT","ANY_BIT"),("ANY_NUM","ANY_BIT"), ("return_type", "__move_", "IN_type")),
24f111835805 Fixing REAL_TO_INT conversion (behaved like a trunc and not like a round)
laurent
parents: 526
diff changeset
   455
        (("ANY_REAL",),("ANY_REAL",), ("return_type", "__move_", "IN_type")),
24f111835805 Fixing REAL_TO_INT conversion (behaved like a trunc and not like a round)
laurent
parents: 526
diff changeset
   456
        # REAL_TO_INT
24f111835805 Fixing REAL_TO_INT conversion (behaved like a trunc and not like a round)
laurent
parents: 526
diff changeset
   457
        (("ANY_REAL",),("ANY_SINT",), ("return_type", "__real_to_sint", None)),
24f111835805 Fixing REAL_TO_INT conversion (behaved like a trunc and not like a round)
laurent
parents: 526
diff changeset
   458
        (("ANY_REAL",),("ANY_UINT",), ("return_type", "__real_to_uint", None)),
24f111835805 Fixing REAL_TO_INT conversion (behaved like a trunc and not like a round)
laurent
parents: 526
diff changeset
   459
        (("ANY_REAL",),("ANY_BIT",), ("return_type", "__real_to_bit", None)),
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   460
        # TO_TIME
284
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   461
        (("ANY_INT","ANY_BIT"),("ANY_DATE","TIME"), ("return_type", "__int_to_time", None)),
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   462
        (("ANY_REAL",),("ANY_DATE","TIME"), ("return_type", "__real_to_time", None)),
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   463
        (("ANY_STRING",), ("ANY_DATE","TIME"), ("return_type", "__string_to_time", None)),
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   464
        # FROM_TIME
284
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   465
        (("ANY_DATE","TIME"), ("ANY_REAL",), ("return_type", "__time_to_real", None)),
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   466
        (("ANY_DATE","TIME"), ("ANY_INT","ANY_NBIT"), ("return_type", "__time_to_int", None)),
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   467
        (("TIME",), ("ANY_STRING",), ("return_type", "__time_to_string", None)),
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   468
        (("DATE",), ("ANY_STRING",), ("return_type", "__date_to_string", None)),
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   469
        (("TOD",), ("ANY_STRING",), ("return_type", "__tod_to_string", None)),
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   470
        (("DT",), ("ANY_STRING",), ("return_type", "__dt_to_string", None)),
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   471
        # TO_STRING
284
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   472
        (("BOOL",), ("ANY_STRING",), ("return_type", "__bool_to_string", None)),
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   473
        (("ANY_BIT",), ("ANY_STRING",), ("return_type", "__bit_to_string", None)),
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   474
        (("ANY_REAL",), ("ANY_STRING",), ("return_type", "__real_to_string", None)),
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   475
        (("ANY_SINT",), ("ANY_STRING",), ("return_type", "__sint_to_string", None)),
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   476
        (("ANY_UINT",), ("ANY_STRING",), ("return_type", "__uint_to_string", None)),
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   477
        # FROM_STRING
284
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   478
        (("ANY_STRING",), ("BOOL",), ("return_type", "__string_to_bool", None)),
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   479
        (("ANY_STRING",), ("ANY_BIT",), ("return_type", "__string_to_bit", None)),
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   480
        (("ANY_STRING",), ("ANY_SINT",), ("return_type", "__string_to_sint", None)),
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   481
        (("ANY_STRING",), ("ANY_UINT",), ("return_type", "__string_to_uint", None)),
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   482
        (("ANY_STRING",), ("ANY_REAL",), ("return_type", "__string_to_real", None))]
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   483
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   484
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   485
BCD_TO_ANY_LIST=[
284
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   486
        (("BYTE",),("USINT",), ("return_type", "__bcd_to_uint", None)),
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   487
        (("WORD",),("UINT",), ("return_type", "__bcd_to_uint", None)),
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   488
        (("DWORD",),("UDINT",), ("return_type", "__bcd_to_uint", None)),
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   489
        (("LWORD",),("ULINT",), ("return_type", "__bcd_to_uint", None))]
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   490
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   491
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   492
ANY_TO_BCD_LIST=[
284
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   493
        (("USINT",),("BYTE",), ("return_type", "__uint_to_bcd", None)),
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   494
        (("UINT",),("WORD",), ("return_type", "__uint_to_bcd", None)),
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   495
        (("UDINT",),("DWORD",), ("return_type", "__uint_to_bcd", None)),
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 279
diff changeset
   496
        (("ULINT",),("LWORD",), ("return_type", "__uint_to_bcd", None))]
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   497
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   498
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   499
def ANY_TO_ANY_FORMAT_GEN(any_to_any_list, fdecl):
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   500
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   501
    for (InTypes, OutTypes, Format) in any_to_any_list:
22
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   502
        outs = reduce(lambda a,b: a or b, map(lambda testtype : IsOfType(fdecl["outputs"][0][1],testtype), OutTypes))
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   503
        inps = reduce(lambda a,b: a or b, map(lambda testtype : IsOfType(fdecl["inputs"][0][1],testtype), InTypes))
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   504
        if inps and outs and fdecl["outputs"][0][1] != fdecl["inputs"][0][1]:
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   505
             return Format
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   506
    
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   507
    return None
18
ee18a387e80a Enhanced standard funcrtion declaration from .csv file.
etisserant
parents: 15
diff changeset
   508
ee18a387e80a Enhanced standard funcrtion declaration from .csv file.
etisserant
parents: 15
diff changeset
   509
15
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   510
"""
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   511
Returns this kind of declaration for all standard functions
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   512
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   513
            [{"name" : "Numerical", 'list': [   {   
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   514
                'baseinputnumber': 1,
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   515
                'comment': 'Addition',
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   516
                'extensible': True,
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   517
                'inputs': [   ('IN1', 'ANY_NUM', 'none'),
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   518
                              ('IN2', 'ANY_NUM', 'none')],
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   519
                'name': 'ADD',
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   520
                'outputs': [('OUT', 'ANY_NUM', 'none')],
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   521
                'type': 'function'}, ...... ] },.....]
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   522
"""
14
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   523
def get_standard_funtions(table):
22
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   524
    
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   525
    variables = get_standard_funtions_input_variables(table)
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   526
    
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   527
    fonctions = find_section("Standard_functions_type",table)
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   528
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   529
    Standard_Functions_Decl = []
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   530
    Current_section = None
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   531
    
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   532
    translate = {
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   533
            "extensible" : lambda x: {"yes":True, "no":False}[x],
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   534
            "inputs" : lambda x:csv_input_translate(x,variables,baseinputnumber),
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   535
            "outputs":lambda x:[("OUT",x,"none")]}
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   536
    
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   537
    for fields in table:
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   538
        if fields[1]:
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   539
            # If function section name given
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   540
            if fields[0]:
391
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   541
                words = fields[0].split('"')
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   542
                if len(words) > 1:
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   543
                    section_name = words[1]
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   544
                else:
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   545
                    section_name = fields[0]
07447ee3538e Adding support for internationalization
laurent
parents: 373
diff changeset
   546
                Current_section = {"name" : section_name, "list" : []}
22
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   547
                Standard_Functions_Decl.append(Current_section)
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   548
                Function_decl_list = []
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   549
            if Current_section:
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   550
                Function_decl = dict([(champ, val) for champ, val in zip(fonctions, fields[1:]) if champ])
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 58
diff changeset
   551
                Function_decl["generate"] = generate_block
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 92
diff changeset
   552
                Function_decl["initialise"] = lambda x,y:[]
22
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   553
                baseinputnumber = int(Function_decl.get("baseinputnumber",1))
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   554
                Function_decl["baseinputnumber"] = baseinputnumber
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   555
                for param, value in Function_decl.iteritems():
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   556
                    if param in translate:
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   557
                        Function_decl[param] = translate[param](value)
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   558
                Function_decl["type"] = "function"
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   559
                
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   560
                if Function_decl["name"].startswith('*') or Function_decl["name"].endswith('*') :
22
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   561
                    input_ovrloading_types = GetSubTypes(Function_decl["inputs"][0][1])
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   562
                    output_types = GetSubTypes(Function_decl["outputs"][0][1])
22
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   563
                else:
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   564
                    input_ovrloading_types = [None]
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   565
                    output_types = [None]
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 109
diff changeset
   566
                
22
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   567
                funcdeclname_orig = Function_decl["name"]
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   568
                funcdeclname = Function_decl["name"].strip('*_')
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   569
                fdc = Function_decl["inputs"][:]
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   570
                for intype in input_ovrloading_types:
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   571
                    if intype != None:
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   572
                        Function_decl["inputs"] = []
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   573
                        for decl_tpl in fdc:
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   574
                            if IsOfType(intype, decl_tpl[1]):
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   575
                                Function_decl["inputs"] += [(decl_tpl[0], intype, decl_tpl[2])]
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   576
                            else:
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   577
                                Function_decl["inputs"] += [(decl_tpl)]
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   578
                            
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   579
                            if funcdeclname_orig.startswith('*'):
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   580
                                funcdeclin = intype + '_' + funcdeclname 
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   581
                            else:
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   582
                                funcdeclin = funcdeclname
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   583
                    else:
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   584
                        funcdeclin = funcdeclname
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   585
                        
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   586
                    for outype in output_types:
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   587
                        if outype != None:
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   588
                            decl_tpl = Function_decl["outputs"][0]
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   589
                            Function_decl["outputs"] = [ (decl_tpl[0] , outype,  decl_tpl[2])]
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   590
                            if funcdeclname_orig.endswith('*'):
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   591
                                funcdeclout =  funcdeclin + '_' + outype
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   592
                            else:
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   593
                                funcdeclout =  funcdeclin
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   594
                        else:
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   595
                            funcdeclout =  funcdeclin
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   596
                        Function_decl["name"] = funcdeclout
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   597
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   598
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   599
                        fdecl = Function_decl
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   600
                        res = eval(Function_decl["python_eval_c_code_format"])
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   601
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   602
                        if res != None :
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   603
                            # create the copy of decl dict to be appended to section
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   604
                            Function_decl_copy = Function_decl.copy()
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   605
                            Current_section["list"].append(Function_decl_copy)
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   606
            else:
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   607
                raise "First function must be in a category"
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   608
    
a765fae3b361 Modifications on structure.py
lbessard
parents: 21
diff changeset
   609
    return Standard_Functions_Decl
14
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   610
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   611
std_decl = get_standard_funtions(csv_file_to_table(open(os.path.join(os.path.split(__file__)[0],"iec_std.csv"))))#, True)
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   612
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   613
BlockTypes.extend(std_decl)
8dc68e669d99 Early implementation of STD library.
etisserant
parents: 23
diff changeset
   614
517
026ef159e8a2 Added POU interface description in library help text for standard function blocks too, not only in standard functions
Edouard Tisserant
parents: 483
diff changeset
   615
for section in BlockTypes: 
026ef159e8a2 Added POU interface description in library help text for standard function blocks too, not only in standard functions
Edouard Tisserant
parents: 483
diff changeset
   616
    for desc in section["list"]:
026ef159e8a2 Added POU interface description in library help text for standard function blocks too, not only in standard functions
Edouard Tisserant
parents: 483
diff changeset
   617
        words = desc["comment"].split('"')
026ef159e8a2 Added POU interface description in library help text for standard function blocks too, not only in standard functions
Edouard Tisserant
parents: 483
diff changeset
   618
        if len(words) > 1:
026ef159e8a2 Added POU interface description in library help text for standard function blocks too, not only in standard functions
Edouard Tisserant
parents: 483
diff changeset
   619
            desc["comment"] = words[1]
026ef159e8a2 Added POU interface description in library help text for standard function blocks too, not only in standard functions
Edouard Tisserant
parents: 483
diff changeset
   620
        desc["usage"] = (
026ef159e8a2 Added POU interface description in library help text for standard function blocks too, not only in standard functions
Edouard Tisserant
parents: 483
diff changeset
   621
            "\n (" +
026ef159e8a2 Added POU interface description in library help text for standard function blocks too, not only in standard functions
Edouard Tisserant
parents: 483
diff changeset
   622
            str([ " " + fctdecl[1]+":"+fctdecl[0] for fctdecl in desc["inputs"]]).strip("[]").replace("'",'') +
026ef159e8a2 Added POU interface description in library help text for standard function blocks too, not only in standard functions
Edouard Tisserant
parents: 483
diff changeset
   623
            " ) => (" +
026ef159e8a2 Added POU interface description in library help text for standard function blocks too, not only in standard functions
Edouard Tisserant
parents: 483
diff changeset
   624
            str([ " " + fctdecl[1]+":"+fctdecl[0] for fctdecl in desc["outputs"]]).strip("[]").replace("'",'') +
026ef159e8a2 Added POU interface description in library help text for standard function blocks too, not only in standard functions
Edouard Tisserant
parents: 483
diff changeset
   625
            " )")
026ef159e8a2 Added POU interface description in library help text for standard function blocks too, not only in standard functions
Edouard Tisserant
parents: 483
diff changeset
   626
109
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   627
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   628
#-------------------------------------------------------------------------------
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   629
#                            Languages Keywords
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   630
#-------------------------------------------------------------------------------
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   631
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   632
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   633
# Keywords for Pou Declaration
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   634
POU_BLOCK_START_KEYWORDS = ["FUNCTION", "FUNCTION_BLOCK", "PROGRAM"]
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   635
POU_BLOCK_END_KEYWORDS = ["END_FUNCTION", "END_FUNCTION_BLOCK", "END_PROGRAM"]
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   636
POU_KEYWORDS = ["EN", "ENO", "F_EDGE", "R_EDGE"] + POU_BLOCK_START_KEYWORDS + POU_BLOCK_END_KEYWORDS
109
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   637
for category in BlockTypes:
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   638
    for block in category["list"]:
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   639
        if block["name"] not in POU_KEYWORDS:
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   640
            POU_KEYWORDS.append(block["name"])
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   641
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   642
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   643
# Keywords for Type Declaration
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   644
TYPE_BLOCK_START_KEYWORDS = ["TYPE", "STRUCT"]
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   645
TYPE_BLOCK_END_KEYWORDS = ["END_TYPE", "END_STRUCT"]
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   646
TYPE_KEYWORDS = ["ARRAY", "OF", "T", "D", "TIME_OF_DAY", "DATE_AND_TIME"] + TYPE_BLOCK_START_KEYWORDS + TYPE_BLOCK_END_KEYWORDS
109
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   647
TYPE_KEYWORDS.extend([keyword for keyword in TypeHierarchy.keys() if keyword not in TYPE_KEYWORDS])
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   648
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   649
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   650
# Keywords for Variable Declaration
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   651
VAR_BLOCK_START_KEYWORDS = ["VAR", "VAR_INPUT", "VAR_OUTPUT", "VAR_IN_OUT", "VAR_TEMP", "VAR_EXTERNAL"]
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   652
VAR_BLOCK_END_KEYWORDS = ["END_VAR"]
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   653
VAR_KEYWORDS = ["AT", "CONSTANT", "RETAIN", "NON_RETAIN"] + VAR_BLOCK_START_KEYWORDS + VAR_BLOCK_END_KEYWORDS
109
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   654
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   655
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   656
# Keywords for Configuration Declaration
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   657
CONFIG_BLOCK_START_KEYWORDS = ["CONFIGURATION", "RESOURCE", "VAR_ACCESS", "VAR_CONFIG", "VAR_GLOBAL"]
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   658
CONFIG_BLOCK_END_KEYWORDS = ["END_CONFIGURATION", "END_RESOURCE", "END_VAR"]
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   659
CONFIG_KEYWORDS = ["ON", "PROGRAM", "WITH", "READ_ONLY", "READ_WRITE", "TASK"] + CONFIG_BLOCK_START_KEYWORDS + CONFIG_BLOCK_END_KEYWORDS
109
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   660
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   661
# Keywords for Structured Function Chart
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   662
SFC_BLOCK_START_KEYWORDS = ["ACTION", "INITIAL_STEP", "STEP", "TRANSITION"]
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   663
SFC_BLOCK_END_KEYWORDS = ["END_ACTION", "END_STEP", "END_TRANSITION"]
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   664
SFC_KEYWORDS = ["FROM", "TO"] + SFC_BLOCK_START_KEYWORDS + SFC_BLOCK_START_KEYWORDS
109
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   665
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   666
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   667
# Keywords for Instruction List
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   668
IL_KEYWORDS = ["TRUE", "FALSE", "LD", "LDN", "ST", "STN", "S", "R", "AND", "ANDN", "OR", "ORN",
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   669
 "XOR", "XORN", "NOT", "ADD", "SUB", "MUL", "DIV", "MOD", "GT", "GE", "EQ", "NE",
247
4a47ccafbef7 Inversion between TON and TOF comments and errors in IL keywords fixed
lbessard
parents: 242
diff changeset
   670
 "LE", "LT", "JMP", "JMPC", "JMPCN", "CAL", "CALC", "CALCN", "RET", "RETC", "RETCN"]
109
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   671
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   672
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   673
# Keywords for Structured Text
582
aa41547baa2a Adding support for folding/unfolding and auto-indentation in ST code editor
laurent
parents: 541
diff changeset
   674
ST_BLOCK_START_KEYWORDS = ["IF", "ELSIF", "ELSE", "CASE", "FOR", "WHILE", "REPEAT"]
aa41547baa2a Adding support for folding/unfolding and auto-indentation in ST code editor
laurent
parents: 541
diff changeset
   675
ST_BLOCK_END_KEYWORDS = ["END_IF", "END_CASE", "END_FOR", "END_WHILE", "END_REPEAT"]
aa41547baa2a Adding support for folding/unfolding and auto-indentation in ST code editor
laurent
parents: 541
diff changeset
   676
ST_KEYWORDS = ["TRUE", "FALSE", "THEN", "OF", "TO", "BY", "DO", "DO", "UNTIL", "EXIT", 
aa41547baa2a Adding support for folding/unfolding and auto-indentation in ST code editor
laurent
parents: 541
diff changeset
   677
 "RETURN", "NOT", "MOD", "AND", "XOR", "OR"] + ST_BLOCK_START_KEYWORDS + ST_BLOCK_END_KEYWORDS
109
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   678
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   679
# All the keywords of IEC
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   680
IEC_BLOCK_START_KEYWORDS = []
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   681
IEC_BLOCK_END_KEYWORDS = []
109
734e02ab4018 Bug that didn't affect standard function names as keywords fixed
lbessard
parents: 108
diff changeset
   682
IEC_KEYWORDS = ["E", "TRUE", "FALSE"]
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   683
for all_keywords, keywords_list in [(IEC_BLOCK_START_KEYWORDS, [POU_BLOCK_START_KEYWORDS, TYPE_BLOCK_START_KEYWORDS,
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   684
                                                                VAR_BLOCK_START_KEYWORDS, CONFIG_BLOCK_START_KEYWORDS,
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   685
                                                                SFC_BLOCK_START_KEYWORDS, ST_BLOCK_START_KEYWORDS]),
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   686
                                    (IEC_BLOCK_END_KEYWORDS, [POU_BLOCK_END_KEYWORDS, TYPE_BLOCK_END_KEYWORDS,
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   687
                                                              VAR_BLOCK_END_KEYWORDS, CONFIG_BLOCK_END_KEYWORDS,
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   688
                                                              SFC_BLOCK_END_KEYWORDS, ST_BLOCK_END_KEYWORDS]),
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   689
                                    (IEC_KEYWORDS, [POU_KEYWORDS, TYPE_KEYWORDS, VAR_KEYWORDS, CONFIG_KEYWORDS,
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   690
                                                    SFC_KEYWORDS, IL_KEYWORDS, ST_KEYWORDS])]:
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   691
    for keywords in keywords_list:
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   692
        all_keywords.extend([keyword for keyword in keywords if keyword not in all_keywords])
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents: 582
diff changeset
   693