lpcmanager

Ripped LPCBus class appart from LPCManager.py

2014-12-08, Edouard Tisserant
acee64d7456d
Parents 17e71d6ad597
Children dad562d1bab4
Ripped LPCBus class appart from LPCManager.py
  • +443 -0
    LPCBus.py
  • +13 -456
    LPCManager.py
  • --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/LPCBus.py Mon Dec 08 17:02:35 2014 +0100
    @@ -0,0 +1,443 @@
    +bus_template_code = {"MC8":"""
    +/* Code generated by LPCBus confnode */
    +
    +/* LPCBus confnode includes */
    +#include "app_glue.h"
    +#ifdef _WINDOWS_H
    + #include "iec_types.h"
    +#else
    + #include "iec_std_lib.h"
    +#endif
    +
    +%(declare_code)s
    +
    +/* LPCBus confnode user variables definition */
    +%(var_decl)s
    +
    +/* LPCBus confnode functions */
    +int __init_%(location_str)s(int argc,char **argv)
    +{
    +%(init_code)s
    + return 0;
    +}
    +
    +void __cleanup_%(location_str)s(void)
    +{
    +}
    +
    +void __retrieve_%(location_str)s(void)
    +{
    +%(retrieve_code)s
    +}
    +
    +void __publish_%(location_str)s(void)
    +{
    +%(publish_code)s
    +}
    +""",
    +#############################
    +"MC9":"""
    +/* Code generated by LPCBus confnode */
    +
    +#ifdef _WINDOWS_H
    + #include "iec_types.h"
    +#else
    + #include "iec_std_lib.h"
    +#endif
    +
    +static inline int16_t getWord(char *buffer)
    +{
    + return *((int16_t*)buffer);
    +}
    +
    +static inline void setWord(char *buffer, int16_t word)
    +{
    + *((int16_t*)buffer) = word;
    +}
    +
    +static inline int8_t getBit(char *buffer, unsigned char bitOffset)
    +{
    + return (*buffer & (0x01 << bitOffset)) && 1;
    +}
    +
    +static inline void setBit(char *buffer, unsigned char bitOffset, int8_t bit)
    +{
    + unsigned char msk = (0x01 << bitOffset);
    + if(bit)
    + *buffer |= msk;
    + else
    + *buffer &= ~msk;
    +}
    +
    +%(bus_decl)s
    +
    +%(declare_code)s
    +
    +/* LPCBus confnode user variables definition */
    +%(var_decl)s
    +
    +/* LPCBus confnode functions */
    +int __init_%(location_str)s(int argc,char **argv)
    +{
    +%(init_code)s
    + %(bus_init_code)s
    + return 0;
    +}
    +
    +void __cleanup_%(location_str)s(void)
    +{
    + %(bus_cleanup_code)s
    +}
    +
    +void __retrieve_%(location_str)s(void)
    +{
    +%(bus_retrieve_code)s
    +%(retrieve_code)s
    +}
    +
    +void __publish_%(location_str)s(void)
    +{
    +%(publish_code)s
    +%(bus_publish_code)s
    +}
    +"""}
    +
    +bus_code = {
    + "MC9:Right": {
    + "decl" : """
    +#include <rtdm/rtdm.h>
    +
    +/* LPCBus confnode includes */
    +#define RIGHT_READ_BUFSIZE 30 /**< Right bus read buffer size */
    +#define RIGHT_WRITE_BUFSIZE 30 /**< Right bus write buffer size */
    +
    +#define MAX_RIGHT_MODULES 8
    +
    +/**< Buffers for reading data from right bus modules */
    +typedef char rightReadBuf_t[MAX_RIGHT_MODULES][RIGHT_READ_BUFSIZE];
    +
    +/**< Buffers for writing data to right bus modules */
    +typedef char rightWriteBuf_t[MAX_RIGHT_MODULES][RIGHT_WRITE_BUFSIZE];
    +
    +/**< Tables containing information about connected modules
    + on right bus (initialized by Composer) */
    +typedef char rightI2CMod_t[MAX_RIGHT_MODULES][2];
    +
    +#define RTIOC_TYPE_SMT RTDM_CLASS_EXPERIMENTAL
    +
    +#define RTSMT_RTIOC_INIT _IOR(RTIOC_TYPE_SMT, 0x00, rightI2CMod_t)
    +//#define RTSMT_RTIOC_CLEAN _IOR(RTIOC_TYPE_SMT, 0x01, int)
    +#define RTSMT_RTIOC_READ _IOR(RTIOC_TYPE_SMT, 0x02, rightReadBuf_t)
    +#define RTSMT_RTIOC_WRITE _IOR(RTIOC_TYPE_SMT, 0x03, rightWriteBuf_t)
    +
    +static rightReadBuf_t rightReadBuf;
    +static rightWriteBuf_t rightWriteBuf;
    +static rightI2CMod_t rightI2CMod;
    +
    +static int rightbusfd = -1;
    +
    +/* XXX TODO #include "smarteh.h" */
    +""",
    + "init":"""
    +#define DEVICEFILENAME "rightbus"
    +
    +int err;
    +rightbusfd = rt_dev_open( DEVICEFILENAME, 0);
    +if (rightbusfd < 0) {
    + printf("can't open %s rtdm device, %s\\n", DEVICEFILENAME,
    + strerror(-rightbusfd));
    + return rightbusfd;
    +}
    +
    +err = rt_dev_ioctl(rightbusfd, RTSMT_RTIOC_INIT, &rightI2CMod);
    +if (err) {
    + printf("error while RTSMT_RTIOC_INIT, %s\\n",
    + strerror(-err));
    + rt_dev_close(rightbusfd);
    + return err;
    +}
    +""",
    + "retrieve":"""
    +rt_dev_ioctl(rightbusfd, RTSMT_RTIOC_READ, &rightReadBuf);
    +""",
    + "publish":"""
    +rt_dev_ioctl(rightbusfd, RTSMT_RTIOC_WRITE, &rightWriteBuf);
    +""",
    + "cleanup":"""
    +rt_dev_close(rightbusfd);
    +""",
    + },
    + "MC9:On Board": {
    + "decl" : """
    +#define MAX_ONBOARD_DEVICES 2
    +unsigned char onBoardDev[MAX_ONBOARD_DEVICES][2]; /**< Tables containing information about enabled on-board devices (initialized by Composer) */
    +""",
    + "init":"""
    +""",
    + "retrieve":"""
    +""",
    + "publish":"""
    +""",
    + "cleanup":"""
    +""",
    + },
    +}
    +
    +from plcopen.structures import LOCATIONDATATYPES
    +from PLCControler import LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP,\
    + LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY
    +LOCATION_TYPES = {"I": LOCATION_VAR_INPUT,
    + "Q": LOCATION_VAR_OUTPUT,
    + "M": LOCATION_VAR_MEMORY}
    +
    +LOCATION_DIRS = dict([(dir, size) for size, dir in LOCATION_TYPES.iteritems()])
    +
    +LOCATION_SIZES = {}
    +for size, types in LOCATIONDATATYPES.iteritems():
    + for _type in types:
    + LOCATION_SIZES[_type] = size
    +
    +def GetModuleChildren(module):
    + children = []
    + for child in module["children"]:
    + if child["type"] == LOCATION_GROUP:
    + children.extend(child["children"])
    + else:
    + children.append(child)
    + return children
    +
    +def _GetVariables(module):
    + variables = []
    + for child in module["children"]:
    + if child["type"] in [LOCATION_GROUP, LOCATION_MODULE]:
    + variables.extend(_GetVariables(child))
    + else:
    + variables.append(child)
    + return variables
    +
    +def GetLastModuleGroup(module):
    + group = module
    + for child in module["children"]:
    + if child["type"] == LOCATION_GROUP:
    + group = child
    + return group["children"]
    +
    +def GetModuleBySomething(module, something, toks):
    + for child in GetModuleChildren(module):
    + if child.get(something) == toks[0]:
    + if len(toks) > 1:
    + return GetModuleBySomething(child, something, toks[1:])
    + return child
    + return None
    +
    +def GetModuleVariable(module, location, direction):
    + for child in GetModuleChildren(module):
    + if child["location"] == location and child["type"] == LOCATION_TYPES[direction]:
    + return child
    + return None
    +
    +def RemoveModuleChild(module, child):
    + if child in module["children"]:
    + module["children"].remove(child)
    + else:
    + for group in module["children"]:
    + if group["type"] == LOCATION_GROUP and child in group["children"]:
    + group["children"].remove(child)
    +
    +class LPCBus(object):
    +
    + def __init__(self):
    + self.VariableLocationTree = []
    + self.ResetUsedLocations()
    + self.Icon = None
    +
    + def __getitem__(self, key):
    + if key == "children":
    + return self.VariableLocationTree
    + raise KeyError, "Only 'children' key is available"
    +
    + def CTNEnabled(self):
    + return None
    +
    + def SetIcon(self, icon):
    + self.Icon = icon
    +
    + def _GetChildBySomething(self, something, toks):
    + return GetModuleBySomething({"children" : self.VariableLocationTree}, something, toks)
    +
    + def GetBaseTypes(self):
    + return self.GetCTRoot().GetBaseTypes()
    +
    + def GetSizeOfType(self, type):
    + return LOCATION_SIZES[self.GetCTRoot().GetBaseType(type)]
    +
    + def _GetVariableLocationTree(self, current_location, infos):
    + if infos["type"] == LOCATION_MODULE:
    + location = current_location + (infos["IEC_Channel"],)
    + return {"name": infos["name"],
    + "type": infos["type"],
    + "location": ".".join(map(str, location + ("x",))),
    + "icon": infos["icon"],
    + "children": [self._GetVariableLocationTree(location, child) for child in infos["children"]]}
    + elif infos["type"] == LOCATION_GROUP:
    + return {"name": infos["name"],
    + "type": infos["type"],
    + "location": "",
    + "icon": infos["icon"],
    + "children": [self._GetVariableLocationTree(current_location, child) for child in infos["children"]]}
    + else:
    + size = self.GetSizeOfType(infos["IEC_type"])
    + location = "%" + LOCATION_DIRS[infos["type"]] + size + ".".join(map(str, current_location + infos["location"]))
    + return {"name": infos["name"],
    + "type": infos["type"],
    + "size": size,
    + "IEC_type": infos["IEC_type"],
    + "var_name": infos["name"],
    + "location": location,
    + "description": infos["description"],
    + "children": []}
    +
    + def GetVariableLocationTree(self):
    + return {"name": self.BaseParams.getName(),
    + "type": LOCATION_CONFNODE,
    + "location": self.GetFullIEC_Channel(),
    + "icon": self.Icon,
    + "children": [self._GetVariableLocationTree(self.GetCurrentLocation(), child)
    + for child in self.VariableLocationTree]}
    +
    + def CTNTestModified(self):
    + return False
    +
    + def CTNMakeDir(self):
    + pass
    +
    + def CTNRequestSave(self, from_project_path=None):
    + return None
    +
    + def ResetUsedLocations(self):
    + self.UsedLocations = {}
    +
    + def _AddUsedLocation(self, parent, location):
    + num = location.pop(0)
    + if not parent.has_key(num):
    + parent[num] = {"used": False, "children": {}}
    + if len(location) > 0:
    + self._AddUsedLocation(parent[num]["children"], location)
    + else:
    + parent[num]["used"] = True
    +
    + def AddUsedLocation(self, location):
    + if len(location) > 0:
    + self._AddUsedLocation(self.UsedLocations, list(location))
    +
    + def _CheckLocationConflicts(self, parent, location):
    + num = location.pop(0)
    + if not parent.has_key(num):
    + return False
    + if len(location) > 0:
    + if parent[num]["used"]:
    + return True
    + return self._CheckLocationConflicts(parent[num]["children"], location)
    + elif len(parent[num]["children"]) > 0:
    + return True
    + return False
    +
    + def CheckLocationConflicts(self, location):
    + if len(location) > 0:
    + return self._CheckLocationConflicts(self.UsedLocations, list(location))
    + return False
    +
    + def CTNGenerate_C(self, buildpath, locations):
    + """
    + Generate C code
    + @param current_location: Tupple containing confnode IEC location : %I0.0.4.5 => (0,0,4,5)
    + @param locations: List of complete variables locations \
    + [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...)
    + "NAME" : name of the variable (generally "__IW0_1_2" style)
    + "DIR" : direction "Q","I" or "M"
    + "SIZE" : size "X", "B", "W", "D", "L"
    + "LOC" : tuple of interger for IEC location (0,1,2,...)
    + }, ...]
    + @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
    + """
    + current_location = self.GetCurrentLocation()
    + # define a unique name for the generated C file
    + location_str = "_".join(map(str, current_location))
    +
    + BusName = arch + ":" + self.BaseParams.getName()
    +
    + bcode = bus_code.get(BusName,{"decl":"",
    + "init":"",
    + "retrieve":"",
    + "publish":"",
    + "cleanup":"",
    + })
    + code_str = {"location_str": location_str,
    + "var_decl": "",
    + "declare_code": "",
    + "init_code": "",
    + "retrieve_code": "",
    + "publish_code": "",
    + "bus_decl":bcode["decl"],
    + "bus_init_code": bcode["init"],
    + "bus_cleanup_code": bcode["cleanup"],
    + "bus_retrieve_code": bcode["retrieve"],
    + "bus_publish_code": bcode["publish"],
    + }
    +
    + for module in GetModuleChildren(self):
    + if module["init"] != "":
    + code_str["init_code"] += " %s\n" % module["init"]
    +
    + # Adding variables
    + vars = []
    + self.ResetUsedLocations()
    + for location in locations:
    + loc = location["LOC"][len(current_location):]
    + group = next = self
    + i = 0
    + while next is not None and i < len(loc):
    + next = self._GetChildBySomething("IEC_Channel", loc[:i + 1])
    + if next is not None:
    + i += 1
    + group = next
    + var_loc = loc[i:]
    + for variable in GetModuleChildren(group):
    + if variable["location"] == var_loc and location["DIR"] == LOCATION_DIRS[variable["type"]]:
    +# if location["DIR"] != LOCATION_DIRS[variable["type"]]:
    +# raise Exception, "Direction conflict in variable definition"
    +# if location["IEC_TYPE"] != variable["IEC_type"]:
    +# raise Exception, "Type conflict in variable definition"
    + if location["DIR"] == "Q":
    + if self.CheckLocationConflicts(location["LOC"]):
    + raise Exception, "BYTE and BIT from the same BYTE can't be used together"
    + self.AddUsedLocation(location["LOC"])
    + vars.append({"location": location["NAME"],
    + "Type": variable["IEC_type"],
    + "Retrieve": variable["retrieve"],
    + "Publish": variable["publish"],
    + })
    + break
    + base_types = self.GetCTRoot().GetBaseTypes()
    + for var in vars:
    + prefix = ""
    + if var["Type"] in base_types:
    + prefix = "IEC_"
    + code_str["var_decl"] += "%s%s beremiz%s;\n"%(prefix, var["Type"], var["location"])
    + code_str["var_decl"] += "%s%s *%s = &beremiz%s;\n"%(prefix, var["Type"], var["location"], var["location"])
    + if var["Retrieve"] != "":
    + code_str["retrieve_code"] += " " + var["Retrieve"] % ("*" + var["location"]) + "\n"
    + if var["Publish"] != "":
    + code_str["publish_code"] += " " + var["Publish"] % ("*" + var["location"]) + "\n"
    +
    + if arch not in bus_template_code:
    + raise Exception, "Unknown arch %s. Please use %s"%(
    + arch,repr(bus_template_code.keys()))
    + Gen_Module_path = os.path.join(buildpath, "Bus_%s.c"%location_str)
    + module = open(Gen_Module_path,'w')
    + module.write(bus_template_code[arch] % code_str)
    + module.close()
    +
    + matiec_flags = '"-I%s" -Wno-unused-function'%os.path.abspath(self.GetCTRoot().GetIECLibPath())
    + return [(Gen_Module_path, matiec_flags)],"",True
    +
    --- a/LPCManager.py Fri Nov 21 20:16:03 2014 +0100
    +++ b/LPCManager.py Mon Dec 08 17:02:35 2014 +0100
    @@ -89,8 +89,7 @@
    from ConfigTreeNode import ConfigTreeNode
    from editors.ProjectNodeEditor import ProjectNodeEditor
    -from plcopen.structures import LOCATIONDATATYPES
    -from PLCControler import PLCControler, LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP,\
    +from PLCControler import PLCControler, LOCATION_MODULE, LOCATION_GROUP,\
    LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY
    from IDEFrame import IDEFrame
    from dialogs import ProjectDialog
    @@ -141,450 +140,8 @@
    # return ""
    #
    # canfestival.local_canfestival_config = LPC_canfestival_config()
    -#-------------------------------------------------------------------------------
    -# LPCModule Class
    -#-------------------------------------------------------------------------------
    -LOCATION_TYPES = {"I": LOCATION_VAR_INPUT,
    - "Q": LOCATION_VAR_OUTPUT,
    - "M": LOCATION_VAR_MEMORY}
    -
    -LOCATION_DIRS = dict([(dir, size) for size, dir in LOCATION_TYPES.iteritems()])
    -
    -LOCATION_SIZES = {}
    -for size, types in LOCATIONDATATYPES.iteritems():
    - for _type in types:
    - LOCATION_SIZES[_type] = size
    -
    -def _GetModuleChildren(module):
    - children = []
    - for child in module["children"]:
    - if child["type"] == LOCATION_GROUP:
    - children.extend(child["children"])
    - else:
    - children.append(child)
    - return children
    -
    -def _GetVariables(module):
    - variables = []
    - for child in module["children"]:
    - if child["type"] in [LOCATION_GROUP, LOCATION_MODULE]:
    - variables.extend(_GetVariables(child))
    - else:
    - variables.append(child)
    - return variables
    -
    -def _GetLastModuleGroup(module):
    - group = module
    - for child in module["children"]:
    - if child["type"] == LOCATION_GROUP:
    - group = child
    - return group["children"]
    -
    -def _GetModuleBySomething(module, something, toks):
    - for child in _GetModuleChildren(module):
    - if child.get(something) == toks[0]:
    - if len(toks) > 1:
    - return _GetModuleBySomething(child, something, toks[1:])
    - return child
    - return None
    -
    -def _GetModuleVariable(module, location, direction):
    - for child in _GetModuleChildren(module):
    - if child["location"] == location and child["type"] == LOCATION_TYPES[direction]:
    - return child
    - return None
    -
    -def _RemoveModuleChild(module, child):
    - if child in module["children"]:
    - module["children"].remove(child)
    - else:
    - for group in module["children"]:
    - if group["type"] == LOCATION_GROUP and child in group["children"]:
    - group["children"].remove(child)
    -
    -bus_template_code = {"MC8":"""
    -/* Code generated by LPCBus confnode */
    -
    -/* LPCBus confnode includes */
    -#include "app_glue.h"
    -#ifdef _WINDOWS_H
    - #include "iec_types.h"
    -#else
    - #include "iec_std_lib.h"
    -#endif
    -
    -%(declare_code)s
    -
    -/* LPCBus confnode user variables definition */
    -%(var_decl)s
    -
    -/* LPCBus confnode functions */
    -int __init_%(location_str)s(int argc,char **argv)
    -{
    -%(init_code)s
    - return 0;
    -}
    -
    -void __cleanup_%(location_str)s(void)
    -{
    -}
    -
    -void __retrieve_%(location_str)s(void)
    -{
    -%(retrieve_code)s
    -}
    -
    -void __publish_%(location_str)s(void)
    -{
    -%(publish_code)s
    -}
    -""",
    -#############################
    -"MC9":"""
    -/* Code generated by LPCBus confnode */
    -
    -#ifdef _WINDOWS_H
    - #include "iec_types.h"
    -#else
    - #include "iec_std_lib.h"
    -#endif
    -
    -static inline int16_t getWord(char *buffer)
    -{
    - return *((int16_t*)buffer);
    -}
    -
    -static inline void setWord(char *buffer, int16_t word)
    -{
    - *((int16_t*)buffer) = word;
    -}
    -
    -static inline int8_t getBit(char *buffer, unsigned char bitOffset)
    -{
    - return (*buffer & (0x01 << bitOffset)) && 1;
    -}
    -
    -static inline void setBit(char *buffer, unsigned char bitOffset, int8_t bit)
    -{
    - unsigned char msk = (0x01 << bitOffset);
    - if(bit)
    - *buffer |= msk;
    - else
    - *buffer &= ~msk;
    -}
    -
    -%(bus_decl)s
    -
    -%(declare_code)s
    -
    -/* LPCBus confnode user variables definition */
    -%(var_decl)s
    -
    -/* LPCBus confnode functions */
    -int __init_%(location_str)s(int argc,char **argv)
    -{
    -%(init_code)s
    - %(bus_init_code)s
    - return 0;
    -}
    -
    -void __cleanup_%(location_str)s(void)
    -{
    - %(bus_cleanup_code)s
    -}
    -
    -void __retrieve_%(location_str)s(void)
    -{
    -%(bus_retrieve_code)s
    -%(retrieve_code)s
    -}
    -
    -void __publish_%(location_str)s(void)
    -{
    -%(publish_code)s
    -%(bus_publish_code)s
    -}
    -"""}
    -
    -bus_code = {
    - "MC9:Right": {
    - "decl" : """
    -#include <rtdm/rtdm.h>
    -
    -/* LPCBus confnode includes */
    -#define RIGHT_READ_BUFSIZE 30 /**< Right bus read buffer size */
    -#define RIGHT_WRITE_BUFSIZE 30 /**< Right bus write buffer size */
    -
    -#define MAX_RIGHT_MODULES 8
    -
    -/**< Buffers for reading data from right bus modules */
    -typedef char rightReadBuf_t[MAX_RIGHT_MODULES][RIGHT_READ_BUFSIZE];
    -
    -/**< Buffers for writing data to right bus modules */
    -typedef char rightWriteBuf_t[MAX_RIGHT_MODULES][RIGHT_WRITE_BUFSIZE];
    -
    -/**< Tables containing information about connected modules
    - on right bus (initialized by Composer) */
    -typedef char rightI2CMod_t[MAX_RIGHT_MODULES][2];
    -
    -#define RTIOC_TYPE_SMT RTDM_CLASS_EXPERIMENTAL
    -
    -#define RTSMT_RTIOC_INIT _IOR(RTIOC_TYPE_SMT, 0x00, rightI2CMod_t)
    -//#define RTSMT_RTIOC_CLEAN _IOR(RTIOC_TYPE_SMT, 0x01, int)
    -#define RTSMT_RTIOC_READ _IOR(RTIOC_TYPE_SMT, 0x02, rightReadBuf_t)
    -#define RTSMT_RTIOC_WRITE _IOR(RTIOC_TYPE_SMT, 0x03, rightWriteBuf_t)
    -
    -static rightReadBuf_t rightReadBuf;
    -static rightWriteBuf_t rightWriteBuf;
    -static rightI2CMod_t rightI2CMod;
    -
    -static int rightbusfd = -1;
    -
    -/* XXX TODO #include "smarteh.h" */
    -""",
    - "init":"""
    -#define DEVICEFILENAME "rightbus"
    -
    -int err;
    -rightbusfd = rt_dev_open( DEVICEFILENAME, 0);
    -if (rightbusfd < 0) {
    - printf("can't open %s rtdm device, %s\\n", DEVICEFILENAME,
    - strerror(-rightbusfd));
    - return rightbusfd;
    -}
    -
    -err = rt_dev_ioctl(rightbusfd, RTSMT_RTIOC_INIT, &rightI2CMod);
    -if (err) {
    - printf("error while RTSMT_RTIOC_INIT, %s\\n",
    - strerror(-err));
    - rt_dev_close(rightbusfd);
    - return err;
    -}
    -""",
    - "retrieve":"""
    -rt_dev_ioctl(rightbusfd, RTSMT_RTIOC_READ, &rightReadBuf);
    -""",
    - "publish":"""
    -rt_dev_ioctl(rightbusfd, RTSMT_RTIOC_WRITE, &rightWriteBuf);
    -""",
    - "cleanup":"""
    -rt_dev_close(rightbusfd);
    -""",
    - },
    - "MC9:On Board": {
    - "decl" : """
    -#define MAX_ONBOARD_DEVICES 2
    -unsigned char onBoardDev[MAX_ONBOARD_DEVICES][2]; /**< Tables containing information about enabled on-board devices (initialized by Composer) */
    -""",
    - "init":"""
    -""",
    - "retrieve":"""
    -""",
    - "publish":"""
    -""",
    - "cleanup":"""
    -""",
    - },
    -}
    -
    -
    -class LPCBus(object):
    -
    - def __init__(self):
    - self.VariableLocationTree = []
    - self.ResetUsedLocations()
    - self.Icon = None
    -
    - def __getitem__(self, key):
    - if key == "children":
    - return self.VariableLocationTree
    - raise KeyError, "Only 'children' key is available"
    -
    - def CTNEnabled(self):
    - return None
    -
    - def SetIcon(self, icon):
    - self.Icon = icon
    -
    - def _GetChildBySomething(self, something, toks):
    - return _GetModuleBySomething({"children" : self.VariableLocationTree}, something, toks)
    -
    - def GetBaseTypes(self):
    - return self.GetCTRoot().GetBaseTypes()
    -
    - def GetSizeOfType(self, type):
    - return LOCATION_SIZES[self.GetCTRoot().GetBaseType(type)]
    -
    - def _GetVariableLocationTree(self, current_location, infos):
    - if infos["type"] == LOCATION_MODULE:
    - location = current_location + (infos["IEC_Channel"],)
    - return {"name": infos["name"],
    - "type": infos["type"],
    - "location": ".".join(map(str, location + ("x",))),
    - "icon": infos["icon"],
    - "children": [self._GetVariableLocationTree(location, child) for child in infos["children"]]}
    - elif infos["type"] == LOCATION_GROUP:
    - return {"name": infos["name"],
    - "type": infos["type"],
    - "location": "",
    - "icon": infos["icon"],
    - "children": [self._GetVariableLocationTree(current_location, child) for child in infos["children"]]}
    - else:
    - size = self.GetSizeOfType(infos["IEC_type"])
    - location = "%" + LOCATION_DIRS[infos["type"]] + size + ".".join(map(str, current_location + infos["location"]))
    - return {"name": infos["name"],
    - "type": infos["type"],
    - "size": size,
    - "IEC_type": infos["IEC_type"],
    - "var_name": infos["name"],
    - "location": location,
    - "description": infos["description"],
    - "children": []}
    -
    - def GetVariableLocationTree(self):
    - return {"name": self.BaseParams.getName(),
    - "type": LOCATION_CONFNODE,
    - "location": self.GetFullIEC_Channel(),
    - "icon": self.Icon,
    - "children": [self._GetVariableLocationTree(self.GetCurrentLocation(), child)
    - for child in self.VariableLocationTree]}
    -
    - def CTNTestModified(self):
    - return False
    -
    - def CTNMakeDir(self):
    - pass
    -
    - def CTNRequestSave(self, from_project_path=None):
    - return None
    -
    - def ResetUsedLocations(self):
    - self.UsedLocations = {}
    -
    - def _AddUsedLocation(self, parent, location):
    - num = location.pop(0)
    - if not parent.has_key(num):
    - parent[num] = {"used": False, "children": {}}
    - if len(location) > 0:
    - self._AddUsedLocation(parent[num]["children"], location)
    - else:
    - parent[num]["used"] = True
    -
    - def AddUsedLocation(self, location):
    - if len(location) > 0:
    - self._AddUsedLocation(self.UsedLocations, list(location))
    -
    - def _CheckLocationConflicts(self, parent, location):
    - num = location.pop(0)
    - if not parent.has_key(num):
    - return False
    - if len(location) > 0:
    - if parent[num]["used"]:
    - return True
    - return self._CheckLocationConflicts(parent[num]["children"], location)
    - elif len(parent[num]["children"]) > 0:
    - return True
    - return False
    -
    - def CheckLocationConflicts(self, location):
    - if len(location) > 0:
    - return self._CheckLocationConflicts(self.UsedLocations, list(location))
    - return False
    -
    - def CTNGenerate_C(self, buildpath, locations):
    - """
    - Generate C code
    - @param current_location: Tupple containing confnode IEC location : %I0.0.4.5 => (0,0,4,5)
    - @param locations: List of complete variables locations \
    - [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...)
    - "NAME" : name of the variable (generally "__IW0_1_2" style)
    - "DIR" : direction "Q","I" or "M"
    - "SIZE" : size "X", "B", "W", "D", "L"
    - "LOC" : tuple of interger for IEC location (0,1,2,...)
    - }, ...]
    - @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
    - """
    - current_location = self.GetCurrentLocation()
    - # define a unique name for the generated C file
    - location_str = "_".join(map(str, current_location))
    -
    - BusName = arch + ":" + self.BaseParams.getName()
    -
    - bcode = bus_code.get(BusName,{"decl":"",
    - "init":"",
    - "retrieve":"",
    - "publish":"",
    - "cleanup":"",
    - })
    - code_str = {"location_str": location_str,
    - "var_decl": "",
    - "declare_code": "",
    - "init_code": "",
    - "retrieve_code": "",
    - "publish_code": "",
    - "bus_decl":bcode["decl"],
    - "bus_init_code": bcode["init"],
    - "bus_cleanup_code": bcode["cleanup"],
    - "bus_retrieve_code": bcode["retrieve"],
    - "bus_publish_code": bcode["publish"],
    - }
    -
    - for module in _GetModuleChildren(self):
    - if module["init"] != "":
    - code_str["init_code"] += " %s\n" % module["init"]
    -
    - # Adding variables
    - vars = []
    - self.ResetUsedLocations()
    - for location in locations:
    - loc = location["LOC"][len(current_location):]
    - group = next = self
    - i = 0
    - while next is not None and i < len(loc):
    - next = self._GetChildBySomething("IEC_Channel", loc[:i + 1])
    - if next is not None:
    - i += 1
    - group = next
    - var_loc = loc[i:]
    - for variable in _GetModuleChildren(group):
    - if variable["location"] == var_loc and location["DIR"] == LOCATION_DIRS[variable["type"]]:
    -# if location["DIR"] != LOCATION_DIRS[variable["type"]]:
    -# raise Exception, "Direction conflict in variable definition"
    -# if location["IEC_TYPE"] != variable["IEC_type"]:
    -# raise Exception, "Type conflict in variable definition"
    - if location["DIR"] == "Q":
    - if self.CheckLocationConflicts(location["LOC"]):
    - raise Exception, "BYTE and BIT from the same BYTE can't be used together"
    - self.AddUsedLocation(location["LOC"])
    - vars.append({"location": location["NAME"],
    - "Type": variable["IEC_type"],
    - "Retrieve": variable["retrieve"],
    - "Publish": variable["publish"],
    - })
    - break
    - base_types = self.GetCTRoot().GetBaseTypes()
    - for var in vars:
    - prefix = ""
    - if var["Type"] in base_types:
    - prefix = "IEC_"
    - code_str["var_decl"] += "%s%s beremiz%s;\n"%(prefix, var["Type"], var["location"])
    - code_str["var_decl"] += "%s%s *%s = &beremiz%s;\n"%(prefix, var["Type"], var["location"], var["location"])
    - if var["Retrieve"] != "":
    - code_str["retrieve_code"] += " " + var["Retrieve"] % ("*" + var["location"]) + "\n"
    - if var["Publish"] != "":
    - code_str["publish_code"] += " " + var["Publish"] % ("*" + var["location"]) + "\n"
    -
    - if arch not in bus_template_code:
    - raise Exception, "Unknown arch %s. Please use %s"%(
    - arch,repr(bus_template_code.keys()))
    - Gen_Module_path = os.path.join(buildpath, "Bus_%s.c"%location_str)
    - module = open(Gen_Module_path,'w')
    - module.write(bus_template_code[arch] % code_str)
    - module.close()
    -
    - matiec_flags = '"-I%s" -Wno-unused-function'%os.path.abspath(self.GetCTRoot().GetIECLibPath())
    - return [(Gen_Module_path, matiec_flags)],"",True
    +from LPCBus import *
    #-------------------------------------------------------------------------------
    # LPC CanFestival ConfNode Class
    @@ -2132,12 +1689,12 @@
    module = self.CTR.GetChildByIECLocation(parent)
    if module is None:
    return "Error: No parent found\n"
    - for child in _GetModuleChildren(module):
    + for child in GetModuleChildren(module):
    if child["name"] == name:
    return "Error: A module named %s already exists\n" % name
    elif child["IEC_Channel"] == iec_channel:
    return "Error: A module with IEC_channel %d already exists\n" % iec_channel
    - _GetLastModuleGroup(module).append({"name": name,
    + GetLastModuleGroup(module).append({"name": name,
    "type": LOCATION_MODULE,
    "IEC_Channel": iec_channel,
    "icon": icon,
    @@ -2153,7 +1710,7 @@
    if parent is self.CTR:
    return "Error: No module found\n"
    if module["name"] != name:
    - for child in _GetModuleChildren(parent):
    + for child in GetModuleChildren(parent):
    if child["name"] == name:
    return "Error: A module named %s already exists\n" % name
    module["name"] = name
    @@ -2167,7 +1724,7 @@
    if parent is self.CTR:
    return "Error: No module found\n"
    if module["IEC_Channel"] != new_iec_channel:
    - for child in _GetModuleChildren(parent):
    + for child in GetModuleChildren(parent):
    if child["IEC_Channel"] == new_iec_channel:
    return "Error: A module with IEC_channel %d already exists\n" % new_iec_channel
    self.CTR.UpdateProjectVariableLocation(".".join(map(str, old_iec_location)), ".".join(map(str, old_iec_location[:1] + (new_iec_channel,))))
    @@ -2184,11 +1741,11 @@
    module = self.CTR.GetChildByIECLocation(parent)
    if module is None:
    return "Error: No parent found\n"
    - child = _GetModuleBySomething(module, "IEC_Channel", (iec_channel,))
    + child = GetModuleBySomething(module, "IEC_Channel", (iec_channel,))
    if child is None:
    return "Error: No module found\n"
    self.CTR.RemoveProjectVariableByFilter(".".join(map(str, parent + (iec_channel,))))
    - _RemoveModuleChild(module, child)
    + RemoveModuleChild(module, child)
    self.RestartTimer()
    def StartGroup(self, parent, name, icon=None):
    @@ -2208,12 +1765,12 @@
    module = self.CTR.GetChildByIECLocation(parent)
    if module is None:
    return "Error: No parent found\n"
    - for child in _GetModuleChildren(module):
    + for child in GetModuleChildren(module):
    if child["name"] == name:
    return "Error: A variable named %s already exists\n" % name
    if child["location"] == location and child["type"] == LOCATION_TYPES[direction]:
    return "Error: A variable with location %s already exists\n" % ".".join(map(str, location))
    - _GetLastModuleGroup(module).append({"name": name,
    + GetLastModuleGroup(module).append({"name": name,
    "location": location,
    "type": LOCATION_TYPES[direction],
    "IEC_type": type,
    @@ -2227,7 +1784,7 @@
    if module is None:
    return "Error: No parent found\n"
    variable = None
    - for child in _GetModuleChildren(module):
    + for child in GetModuleChildren(module):
    if child["location"] == location and child["type"] == LOCATION_TYPES[new_direction]:
    variable = child
    elif child["name"] == new_name:
    @@ -2249,13 +1806,13 @@
    module = self.CTR.GetChildByIECLocation(parent)
    if module is None:
    return "Error: No parent found\n"
    - child = _GetModuleVariable(module, location, direction)
    + child = GetModuleVariable(module, location, direction)
    if child is None:
    return "Error: No variable found\n"
    size = LOCATION_SIZES[self.CTR.GetBaseType(child["IEC_type"])]
    address = "%" + LOCATION_DIRS[child["type"]] + size + ".".join(map(str, parent + location))
    self.CTR.RemoveProjectVariableByAddress(address)
    - _RemoveModuleChild(module, child)
    + RemoveModuleChild(module, child)
    self.RestartTimer()
    def location(loc):