py_ext/PythonFileCTNMixin.py
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Tue, 15 Aug 2017 15:50:30 +0300
changeset 1740 b789b695b5c6
parent 1739 ec153828ded2
child 1742 92932cd370a4
permissions -rw-r--r--
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
1511
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
     1
#!/usr/bin/env python
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
     2
# -*- coding: utf-8 -*-
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
     3
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
     4
# This file is part of Beremiz, a Integrated Development Environment for
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
     5
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
     6
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
     7
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
1680
6db967480b7d make run Beremiz and PLCOpen Editor, if full path contain non-lating
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
     8
# Copyright (C) 2017: Andrey Skvortsov
1511
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
     9
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
    10
# See COPYING file for copyrights details.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
    11
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
    12
# This program is free software; you can redistribute it and/or
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
    13
# modify it under the terms of the GNU General Public License
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
    14
# as published by the Free Software Foundation; either version 2
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
    15
# of the License, or (at your option) any later version.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
    16
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
    17
# This program is distributed in the hope that it will be useful,
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
    18
# but WITHOUT ANY WARRANTY; without even the implied warranty of
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
    19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
    20
# GNU General Public License for more details.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
    21
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
    22
# You should have received a copy of the GNU General Public License
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
    23
# along with this program; if not, write to the Free Software
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
    24
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
    25
1732
94ffe74e6895 clean-up: fix PEP8 E401 multiple imports on one line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    26
import os
94ffe74e6895 clean-up: fix PEP8 E401 multiple imports on one line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    27
import re
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1160
diff changeset
    28
from lxml import etree
1680
6db967480b7d make run Beremiz and PLCOpen Editor, if full path contain non-lating
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    29
import util.paths as paths
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1160
diff changeset
    30
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1160
diff changeset
    31
from xmlclass import GenerateParserFromXSD
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1160
diff changeset
    32
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1160
diff changeset
    33
from CodeFileTreeNode import CodeFile
657
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    34
from PythonEditor import PythonEditor
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    35
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
    36
1097
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    37
class PythonFileCTNMixin(CodeFile):
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    38
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    39
    CODEFILE_NAME = "PyFile"
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    40
    SECTIONS_NAMES = [
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    41
        "globals",
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    42
        "init",
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    43
        "cleanup",
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    44
        "start",
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    45
        "stop"]
657
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    46
    EditorType = PythonEditor
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    47
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    48
    def __init__(self):
1097
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    49
        CodeFile.__init__(self)
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    50
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    51
        filepath = self.PythonFileName()
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    52
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    53
        if os.path.isfile(filepath):
1680
6db967480b7d make run Beremiz and PLCOpen Editor, if full path contain non-lating
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    54
            PythonParser = GenerateParserFromXSD(paths.AbsNeighbourFile(__file__, "py_ext_xsd.xsd"))
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    55
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    56
            xmlfile = open(filepath, 'r')
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1160
diff changeset
    57
            pythonfile_xml = xmlfile.read()
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    58
            xmlfile.close()
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    59
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1160
diff changeset
    60
            pythonfile_xml = pythonfile_xml.replace(
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    61
                'xmlns="http://www.w3.org/2001/XMLSchema"',
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1160
diff changeset
    62
                'xmlns:xhtml="http://www.w3.org/1999/xhtml"')
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1160
diff changeset
    63
            for cre, repl in [
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1160
diff changeset
    64
                (re.compile("(?<!<xhtml:p>)(?:<!\[CDATA\[)"), "<xhtml:p><![CDATA["),
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1160
diff changeset
    65
                (re.compile("(?:]]>)(?!</xhtml:p>)"), "]]></xhtml:p>")]:
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1160
diff changeset
    66
                pythonfile_xml = cre.sub(repl, pythonfile_xml)
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    67
1330
96b242e4c59d Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents: 1315
diff changeset
    68
            try:
96b242e4c59d Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents: 1315
diff changeset
    69
                python_code, error = PythonParser.LoadXMLString(pythonfile_xml)
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    70
                if error is None:
1330
96b242e4c59d Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents: 1315
diff changeset
    71
                    self.CodeFile.globals.setanyText(python_code.getanyText())
96b242e4c59d Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents: 1315
diff changeset
    72
                    os.remove(filepath)
96b242e4c59d Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents: 1315
diff changeset
    73
                    self.CreateCodeFileBuffer(False)
96b242e4c59d Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents: 1315
diff changeset
    74
                    self.OnCTNSave()
96b242e4c59d Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents: 1315
diff changeset
    75
            except Exception, exc:
96b242e4c59d Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents: 1315
diff changeset
    76
                error = unicode(exc)
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    77
1330
96b242e4c59d Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents: 1315
diff changeset
    78
            if error is not None:
96b242e4c59d Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents: 1315
diff changeset
    79
                self.GetCTRoot().logger.write_error(
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    80
                    _("Couldn't import old %s file.") % self.CTNName())
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    81
1097
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    82
    def CodeFileName(self):
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    83
        return os.path.join(self.CTNPath(), "pyfile.xml")
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    84
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    85
    def PythonFileName(self):
721
ecf4d203c4d4 refactoring
Edouard Tisserant
parents: 718
diff changeset
    86
        return os.path.join(self.CTNPath(), "py_ext.xml")
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    87
1132
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
    88
    PreSectionsTexts = {}
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
    89
    PostSectionsTexts = {}
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    90
    def GetSection(self, section):
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    91
        return self.PreSectionsTexts.get(section, "") + "\n" + \
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1160
diff changeset
    92
               getattr(self.CodeFile, section).getanyText() + "\n" + \
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    93
               self.PostSectionsTexts.get(section, "")
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    94
1132
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
    95
    def CTNGenerate_C(self, buildpath, locations):
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    96
        # location string for that CTN
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    97
        location_str = "_".join(map(lambda x: str(x),
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
    98
                                self.GetCurrentLocation()))
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
    99
        configname = self.GetCTRoot().GetProjectConfigNames()[0]
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   100
1447
d6b878525ceb Fixed systematically loading PLC binary at startup even without -a parameter. Extended py_ext extensions instances variable description (PLCGlobalsDesc). Now contains list of variables organizd by extension, with extension name
Edouard Tisserant
parents: 1436
diff changeset
   101
        pyextname = self.CTNName()
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   102
        varinfos = map(lambda variable: {
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   103
                    "name": variable.getname(),
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   104
                    "desc": repr(variable.getdesc()),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   105
                    "onchangecode": '"'+variable.getonchange()+\
1449
5f09fa31d7b0 Added __ext_name__ variable accessible from user python code, reflecting extension name. Fixed contend of OnChange field in PLCGlobalsDesc
Edouard Tisserant
parents: 1448
diff changeset
   106
                                         "('"+variable.getname()+"')\"" \
5f09fa31d7b0 Added __ext_name__ variable accessible from user python code, reflecting extension name. Fixed contend of OnChange field in PLCGlobalsDesc
Edouard Tisserant
parents: 1448
diff changeset
   107
                                     if variable.getonchange() else '""',
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   108
                    "onchange": repr(variable.getonchange()) \
1449
5f09fa31d7b0 Added __ext_name__ variable accessible from user python code, reflecting extension name. Fixed contend of OnChange field in PLCGlobalsDesc
Edouard Tisserant
parents: 1448
diff changeset
   109
                                 if variable.getonchange() else None,
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   110
                    "opts": repr(variable.getopts()),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   111
                    "configname": configname.upper(),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   112
                    "uppername": variable.getname().upper(),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   113
                    "IECtype": variable.gettype(),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   114
                    "pyextname": pyextname},
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   115
                    self.CodeFile.variables.variable)
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   116
        # python side PLC global variables access stub
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   117
        globalstubs = "\n".join(["""\
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   118
_%(name)s_ctype, _%(name)s_unpack, _%(name)s_pack = \\
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   119
    TypeTranslator["%(IECtype)s"]
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   120
_PySafeGetPLCGlob_%(name)s = PLCBinary.__SafeGetPLCGlob_%(name)s
1145
203f4eff3313 Fixed PLC global var access from python. Added test in tests/python
Edouard Tisserant
parents: 1144
diff changeset
   121
_PySafeGetPLCGlob_%(name)s.restype = None
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   122
_PySafeGetPLCGlob_%(name)s.argtypes = [ctypes.POINTER(_%(name)s_ctype)]
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   123
_PySafeSetPLCGlob_%(name)s = PLCBinary.__SafeSetPLCGlob_%(name)s
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   124
_PySafeSetPLCGlob_%(name)s.restype = None
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   125
_PySafeSetPLCGlob_%(name)s.argtypes = [ctypes.POINTER(_%(name)s_ctype)]
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   126
_%(pyextname)sGlobalsDesc.append((
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   127
    "%(name)s",
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   128
    "%(IECtype)s",
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   129
    %(desc)s,
1449
5f09fa31d7b0 Added __ext_name__ variable accessible from user python code, reflecting extension name. Fixed contend of OnChange field in PLCGlobalsDesc
Edouard Tisserant
parents: 1448
diff changeset
   130
    %(onchange)s,
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   131
    %(opts)s))
1452
7a2b344de8cf Fixed build of tests/python.
Edouard Tisserant
parents: 1449
diff changeset
   132
""" % varinfo
1449
5f09fa31d7b0 Added __ext_name__ variable accessible from user python code, reflecting extension name. Fixed contend of OnChange field in PLCGlobalsDesc
Edouard Tisserant
parents: 1448
diff changeset
   133
      for varinfo in varinfos])
1132
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   134
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   135
        # Runtime calls (start, stop, init, and cleanup)
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   136
        rtcalls = ""
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
   137
        for section in self.SECTIONS_NAMES:
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
   138
            if section != "globals":
1132
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   139
                rtcalls += "def _runtime_%s_%s():\n" % (location_str, section)
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   140
                sectiontext = self.GetSection(section).strip()
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   141
                if sectiontext:
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   142
                    rtcalls += '    ' + \
1154
da9ccfceff31 Fixed build error with python <-> PLCglobals variables declared with no initial value
Edouard Tisserant
parents: 1148
diff changeset
   143
                        sectiontext.replace('\n', '\n    ')+"\n\n"
1132
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   144
                else:
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   145
                    rtcalls += "    pass\n\n"
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   146
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   147
        globalsection = self.GetSection("globals")
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   148
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   149
        PyFileContent = """\
1132
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   150
#!/usr/bin/env python
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   151
# -*- coding: utf-8 -*-
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   152
## Code generated by Beremiz python mixin confnode
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   153
##
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   154
1132
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   155
## Code for PLC global variable access
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   156
from targets.typemapping import TypeTranslator
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   157
import ctypes
1447
d6b878525ceb Fixed systematically loading PLC binary at startup even without -a parameter. Extended py_ext extensions instances variable description (PLCGlobalsDesc). Now contains list of variables organizd by extension, with extension name
Edouard Tisserant
parents: 1436
diff changeset
   158
_%(pyextname)sGlobalsDesc = []
1449
5f09fa31d7b0 Added __ext_name__ variable accessible from user python code, reflecting extension name. Fixed contend of OnChange field in PLCGlobalsDesc
Edouard Tisserant
parents: 1448
diff changeset
   159
__ext_name__ = "%(pyextname)s"
1452
7a2b344de8cf Fixed build of tests/python.
Edouard Tisserant
parents: 1449
diff changeset
   160
PLCGlobalsDesc.append(( "%(pyextname)s" , _%(pyextname)sGlobalsDesc ))
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   161
%(globalstubs)s
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   162
1132
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   163
## User code in "global" scope
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   164
%(globalsection)s
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   165
1132
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   166
## Beremiz python runtime calls
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   167
%(rtcalls)s
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   168
1449
5f09fa31d7b0 Added __ext_name__ variable accessible from user python code, reflecting extension name. Fixed contend of OnChange field in PLCGlobalsDesc
Edouard Tisserant
parents: 1448
diff changeset
   169
del __ext_name__
5f09fa31d7b0 Added __ext_name__ variable accessible from user python code, reflecting extension name. Fixed contend of OnChange field in PLCGlobalsDesc
Edouard Tisserant
parents: 1448
diff changeset
   170
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   171
""" % locals()
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   172
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   173
        # write generated content to python file
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   174
        runtimefile_path = os.path.join(buildpath,
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1732
diff changeset
   175
            "runtime_%s.py" % location_str)
1132
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   176
        runtimefile = open(runtimefile_path, 'w')
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   177
        runtimefile.write(PyFileContent.encode('utf-8'))
1132
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   178
        runtimefile.close()
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   179
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   180
        # C code for safe global variables access
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   181
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   182
        vardecfmt = """\
1154
da9ccfceff31 Fixed build error with python <-> PLCglobals variables declared with no initial value
Edouard Tisserant
parents: 1148
diff changeset
   183
extern  __IEC_%(IECtype)s_t %(configname)s__%(uppername)s;
da9ccfceff31 Fixed build error with python <-> PLCglobals variables declared with no initial value
Edouard Tisserant
parents: 1148
diff changeset
   184
IEC_%(IECtype)s __%(name)s_rbuffer = __INIT_%(IECtype)s;
da9ccfceff31 Fixed build error with python <-> PLCglobals variables declared with no initial value
Edouard Tisserant
parents: 1148
diff changeset
   185
IEC_%(IECtype)s __%(name)s_wbuffer;
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   186
long __%(name)s_rlock = 0;
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   187
long __%(name)s_wlock = 0;
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   188
int __%(name)s_wbuffer_written = 0;
1154
da9ccfceff31 Fixed build error with python <-> PLCglobals variables declared with no initial value
Edouard Tisserant
parents: 1148
diff changeset
   189
void __SafeGetPLCGlob_%(name)s(IEC_%(IECtype)s *pvalue){
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   190
    while(AtomicCompareExchange(&__%(name)s_rlock, 0, 1));
1145
203f4eff3313 Fixed PLC global var access from python. Added test in tests/python
Edouard Tisserant
parents: 1144
diff changeset
   191
    *pvalue = __%(name)s_rbuffer;
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   192
    AtomicCompareExchange((long*)&__%(name)s_rlock, 1, 0);
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   193
}
1160
e9e9d3193894 Fixed GCC warnings when building stub code for python PLC globals access
Edouard Tisserant
parents: 1154
diff changeset
   194
void __SafeSetPLCGlob_%(name)s(IEC_%(IECtype)s *value){
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   195
    while(AtomicCompareExchange(&__%(name)s_wlock, 0, 1));
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   196
    __%(name)s_wbuffer = *value;
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   197
    __%(name)s_wbuffer_written = 1;
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   198
    AtomicCompareExchange((long*)&__%(name)s_wlock, 1, 0);
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   199
}
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   200
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   201
"""
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   202
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   203
        vardeconchangefmt = """\
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   204
PYTHON_POLL* __%(name)s_notifier;
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   205
"""
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   206
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   207
        varretfmt = """\
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   208
    if(!AtomicCompareExchange(&__%(name)s_wlock, 0, 1)){
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   209
        if(__%(name)s_wbuffer_written == 1){
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   210
            %(configname)s__%(uppername)s.value = __%(name)s_wbuffer;
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   211
            __%(name)s_wbuffer_written = 0;
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   212
        }
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   213
        AtomicCompareExchange((long*)&__%(name)s_wlock, 1, 0);
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   214
    }
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   215
"""
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   216
        varpubfmt = """\
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   217
    if(!AtomicCompareExchange(&__%(name)s_rlock, 0, 1)){
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   218
        __%(name)s_rbuffer = __GET_VAR(%(configname)s__%(uppername)s);
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   219
        AtomicCompareExchange((long*)&__%(name)s_rlock, 1, 0);
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   220
    }
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   221
"""
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   222
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   223
        varpubonchangefmt = """\
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   224
    if(!AtomicCompareExchange(&__%(name)s_rlock, 0, 1)){
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   225
        IEC_%(IECtype)s tmp = __GET_VAR(%(configname)s__%(uppername)s);
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   226
        if(__%(name)s_rbuffer != tmp){
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   227
            __%(name)s_rbuffer = %(configname)s__%(uppername)s.value;
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   228
            PYTHON_POLL_body__(__%(name)s_notifier);
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   229
        }
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   230
        AtomicCompareExchange((long*)&__%(name)s_rlock, 1, 0);
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   231
    }
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   232
"""
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   233
        varinitonchangefmt = """\
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   234
    __%(name)s_notifier = __GET_GLOBAL_ON%(uppername)sCHANGE();
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   235
    __SET_VAR(__%(name)s_notifier->,TRIG,,__BOOL_LITERAL(TRUE));
1449
5f09fa31d7b0 Added __ext_name__ variable accessible from user python code, reflecting extension name. Fixed contend of OnChange field in PLCGlobalsDesc
Edouard Tisserant
parents: 1448
diff changeset
   236
    __SET_VAR(__%(name)s_notifier->,CODE,,__STRING_LITERAL(%(onchangelen)d,%(onchangecode)s));
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   237
"""
1452
7a2b344de8cf Fixed build of tests/python.
Edouard Tisserant
parents: 1449
diff changeset
   238
        vardec = "\n".join([(vardecfmt + vardeconchangefmt
7a2b344de8cf Fixed build of tests/python.
Edouard Tisserant
parents: 1449
diff changeset
   239
                             if varinfo["onchange"] else vardecfmt)% varinfo
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   240
                            for varinfo in varinfos])
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   241
        varret = "\n".join([varretfmt % varinfo for varinfo in varinfos])
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   242
        varpub = "\n".join([(varpubonchangefmt if varinfo["onchange"] else
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   243
                             varpubfmt) % varinfo
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   244
                            for varinfo in varinfos])
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   245
        varinit = "\n".join([varinitonchangefmt % dict(
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   246
                                onchangelen = len(varinfo["onchangecode"]), **varinfo)
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   247
                            for varinfo in varinfos if varinfo["onchange"]])
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   248
1452
7a2b344de8cf Fixed build of tests/python.
Edouard Tisserant
parents: 1449
diff changeset
   249
        # TODO : use config name obtained from model instead of default
7a2b344de8cf Fixed build of tests/python.
Edouard Tisserant
parents: 1449
diff changeset
   250
        # "config.h". User cannot change config name, but project imported
7a2b344de8cf Fixed build of tests/python.
Edouard Tisserant
parents: 1449
diff changeset
   251
        # or created in older beremiz vesion could use different name.
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   252
        PyCFileContent = """\
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   253
/*
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   254
 * Code generated by Beremiz py_ext confnode
1132
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   255
 * for safe global variables access
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   256
 */
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   257
#include "iec_types_all.h"
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   258
#include "POUS.h"
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   259
#include "config.h"
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   260
#include "beremiz.h"
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   261
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   262
/* User variables reference */
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   263
%(vardec)s
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   264
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   265
/* Beremiz confnode functions */
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   266
int __init_%(location_str)s(int argc,char **argv){
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   267
%(varinit)s
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   268
    return 0;
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   269
}
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   270
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   271
void __cleanup_%(location_str)s(void){
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   272
}
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   273
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   274
void __retrieve_%(location_str)s(void){
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   275
%(varret)s
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   276
}
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   277
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   278
void __publish_%(location_str)s(void){
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   279
%(varpub)s
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   280
}
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   281
""" % locals()
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   282
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1732
diff changeset
   283
        Gen_PyCfile_path = os.path.join(buildpath, "PyCFile_%s.c" % location_str)
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   284
        pycfile = open(Gen_PyCfile_path, 'w')
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   285
        pycfile.write(PyCFileContent)
1132
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   286
        pycfile.close()
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   287
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1732
diff changeset
   288
        matiec_CFLAGS = '"-I%s"' % os.path.abspath(
1132
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   289
            self.GetCTRoot().GetIECLibPath())
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   290
1503
3a238c0c5993 fix regression introduced by changeset: 1431:28e9d479aa65 [Use '-p' command line option when running matiec (iec2c)]
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1475
diff changeset
   291
        return ([(Gen_PyCfile_path, matiec_CFLAGS)],
1132
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   292
                "",
1145
203f4eff3313 Fixed PLC global var access from python. Added test in tests/python
Edouard Tisserant
parents: 1144
diff changeset
   293
                True,
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   294
                ("runtime_%s.py" % location_str, file(runtimefile_path, "rb")))
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   295