44 return os.path.join(self.CTNPath(), "pyfile.xml") |
44 return os.path.join(self.CTNPath(), "pyfile.xml") |
45 |
45 |
46 def PythonFileName(self): |
46 def PythonFileName(self): |
47 return os.path.join(self.CTNPath(), "py_ext.xml") |
47 return os.path.join(self.CTNPath(), "py_ext.xml") |
48 |
48 |
49 def GetSectionsCode(self): |
49 PreSectionsTexts = {} |
|
50 PostSectionsTexts = {} |
|
51 def GetSection(self,section): |
|
52 return self.PreSectionsTexts.get(section,"") + "\n" + \ |
|
53 getattr(self.CodeFile, section).gettext() + "\n" + \ |
|
54 self.PostSectionsTexts.get(section,"") |
|
55 |
|
56 |
|
57 def CTNGenerate_C(self, buildpath, locations): |
|
58 current_location = self.GetCurrentLocation() |
|
59 # define a unique name for the generated C file |
|
60 location_str = "_".join(map(lambda x:str(x), current_location)) |
|
61 |
50 |
62 |
51 # Generate Beremiz python runtime variables code |
63 # Generate Beremiz python runtime variables code |
52 config = self.GetCTRoot().GetProjectConfigNames()[0] |
64 config = self.GetCTRoot().GetProjectConfigNames()[0] |
53 variables_str = "" |
65 variables_str = "" |
54 for variable in self.CodeFile.variables.variable: |
66 for variable in self.CodeFile.variables.variable: |
56 variables_str += "# global_var:%s python_var:%s type:%s initial:%s\n" % ( |
68 variables_str += "# global_var:%s python_var:%s type:%s initial:%s\n" % ( |
57 global_name, |
69 global_name, |
58 variable.getname(), |
70 variable.getname(), |
59 variable.gettype(), |
71 variable.gettype(), |
60 str(variable.getinitial())) |
72 str(variable.getinitial())) |
61 |
73 |
62 sections_code = { |
74 # Runtime calls (start, stop, init, and cleanup) |
63 "variables": variables_str, |
75 rtcalls = "" |
64 "globals": self.CodeFile.globals.gettext().strip() |
|
65 } |
|
66 |
|
67 # Generate Beremiz python runtime functions code |
|
68 for section in self.SECTIONS_NAMES: |
76 for section in self.SECTIONS_NAMES: |
69 if section != "globals": |
77 if section != "globals": |
70 code_object = getattr(self.CodeFile, section) |
78 rtcalls += "def _runtime_%s_%s():\n" % (location_str, section) |
71 section_str = "" |
79 sectiontext = self.GetSection(section).strip() |
72 lines = code_object.gettext().strip().splitlines() |
80 if sectiontext: |
73 if len(lines) > 0: |
81 rtcalls += ' ' + \ |
74 for line in lines: |
82 sectiontext.strip().replace('\n', '\n ')+"\n" |
75 section_str += " " + line + "\n" |
83 else: |
76 section_str += "\n" |
84 rtcalls += " pass\n\n" |
77 sections_code[section] = section_str |
85 |
78 |
86 text = """\ |
79 return sections_code |
87 #!/usr/bin/env python |
|
88 # -*- coding: utf-8 -*- |
|
89 ## Code generated by Beremiz python mixin confnode |
|
90 ## |
|
91 |
|
92 ## Code for PLC global variable access |
|
93 %s |
|
94 |
|
95 ## User code in "global" scope |
|
96 %s |
80 |
97 |
|
98 ## Beremiz python runtime calls |
|
99 %s |
|
100 |
|
101 """%( # variables |
|
102 variables_str, |
|
103 # globals |
|
104 self.GetSection("globals"), |
|
105 # Beremiz python runtime functions |
|
106 rtcalls) |
|
107 |
|
108 runtimefile_path = os.path.join(buildpath, |
|
109 "runtime_%s.py"%location_str) |
|
110 runtimefile = open(runtimefile_path, 'w') |
|
111 runtimefile.write(text.encode('utf-8')) |
|
112 runtimefile.close() |
|
113 |
|
114 text = """\ |
|
115 /* |
|
116 * Code generated by Beremiz py_ext confnode |
|
117 * for safe global variables access |
|
118 */ |
|
119 #include "iec_types_all.h" |
|
120 """ |
|
121 |
|
122 # Adding variables |
|
123 text += "/* User variables reference */\n" |
|
124 for variable in self.CodeFile.variables.variable: |
|
125 var_infos = { |
|
126 "name": variable.getname(), |
|
127 "global": "%s__%s" % (config.upper(), |
|
128 variable.getname().upper()), |
|
129 "type": "__IEC_%s_t" % variable.gettype()} |
|
130 text += "extern %(type)s %(global)s;\n" % var_infos |
|
131 text += "%(type)s __buffer_%(name)s;\n" % var_infos |
|
132 text += "\n" |
|
133 |
|
134 # Adding Beremiz confnode functions |
|
135 text += "/* Beremiz confnode functions */\n" |
|
136 text += "int __init_%s(int argc,char **argv)\n{\n"%location_str |
|
137 text += "/*TODO*/\n" |
|
138 text += " return 0;\n}\n\n" |
|
139 |
|
140 text += "void __cleanup_%s(void)\n{\n"%location_str |
|
141 text += "/*TODO*/\n" |
|
142 text += "\n}\n\n" |
|
143 |
|
144 text += "void __retrieve_%s(void)\n{\n"%location_str |
|
145 text += "/*TODO*/\n" |
|
146 text += "\n}\n\n" |
|
147 |
|
148 text += "void __publish_%s(void)\n{\n"%location_str |
|
149 text += "/*TODO*/\n" |
|
150 text += "\n}\n\n" |
|
151 |
|
152 Gen_PyCfile_path = os.path.join(buildpath, "PyCFile_%s.c"%location_str) |
|
153 pycfile = open(Gen_PyCfile_path,'w') |
|
154 pycfile.write(text) |
|
155 pycfile.close() |
|
156 |
|
157 matiec_flags = '"-I%s"'%os.path.abspath( |
|
158 self.GetCTRoot().GetIECLibPath()) |
|
159 |
|
160 return ([(Gen_PyCfile_path, matiec_flags)], |
|
161 "", |
|
162 False, |
|
163 ("runtime_%s.py"%location_str, file(runtimefile_path,"rb"))) |
|
164 |