plugins/canfestival/config_utils.py
author lbessard
Tue, 04 Sep 2007 17:16:42 +0200
changeset 19 73257cea38bd
parent 11 75ae893d5eed
child 22 9a0c535c3272
permissions -rw-r--r--
Adding Plugin params visualization with basic controls
11
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
     1
#!/usr/bin/env python
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
     3
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
     4
#This file is part of Beremiz, a Integrated Development Environment for
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
     5
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. 
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
     6
#
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
     8
#
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
     9
#See COPYING file for copyrights details.
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    10
#
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    15
#
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    19
#General Public License for more details.
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    20
#
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    24
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    25
from types import *
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    26
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    27
DicoTypes = {"BOOL":0x01, "SINT":0x02, "INT":0x03,"DINT":0x04,"LINT":0x10,
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    28
             "USINT":0x05,"UINT":0x06,"UDINT":0x07,"ULINT":0x1B,"REAL":0x08,
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    29
             "LREAL":0x11,"STRING":0x09,"BYTE":0x02,"WORD":0x03,"DWORD":0x04,
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    30
             "LWORD":0x1B,"WSTRING":0x0B}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    31
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    32
DictLocations = {}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    33
DictCobID = {}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    34
DictLocationsNotMapped = {}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    35
ListCobIDAvailable = []
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    36
SlavesPdoNumber = {}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    37
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    38
# Constants for PDO types 
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    39
RPDO = 1
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    40
TPDO = 2
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    41
SlavePDOType = {"I" : TPDO, "Q" : RPDO}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    42
InvertPDOType = {RPDO : TPDO, TPDO : RPDO}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    43
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    44
DefaultTransmitTypeMaster = 0x01
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    45
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    46
GenerateMasterMapping = lambda x:[None] + [(loc_infos["type"], name) for name, loc_infos in x]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    47
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    48
TrashVariableSizes = {1 : 0x01, 8 : 0x05, 16 : 0x06, 32 : 0x07, 64 : 0x1B}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    49
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    50
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    51
def GetSlavePDOIndexes(slave, type, parameters = False):
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    52
    indexes = []
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    53
    if type & RPDO:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    54
        indexes.extend([idx for idx in slave.GetIndexes() if 0x1400 <= idx <= 0x15FF])
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    55
    if type & TPDO:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    56
        indexes.extend([idx for idx in slave.GetIndexes() if 0x1800 <= idx <= 0x19FF])
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    57
    if not parameters:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    58
        return [idx + 0x200 for idx in indexes]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    59
    else:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    60
        return indexes
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    61
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    62
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    63
def LE_to_BE(value, size): # Convert Little Endian to Big Endian
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    64
    data = ("%" + str(size * 2) + "." + str(size * 2) + "X") % value
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    65
    list_car = [data[i:i+2] for i in xrange(0, len(data), 2)]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    66
    list_car.reverse()
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    67
    return "".join([chr(int(car, 16)) for car in list_car])
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    68
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    69
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    70
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    71
def SearchSlavePDOMapping(loc_infos, slave): # Search the TPDO or RPDO mapping where location is defined on the slave
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    72
    typeinfos = slave.GetEntryInfos(loc_infos["type"])
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    73
    model = (loc_infos["index"] << 16) + (loc_infos["subindex"] << 8) + typeinfos["size"]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    74
    slavePDOidxlist = GetSlavePDOIndexes(slave, loc_infos["pdotype"])
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    75
    
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    76
    for PDOidx in slavePDOidxlist:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    77
        values = slave.GetEntry(PDOidx)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    78
        if values != None:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    79
            for subindex, mapping in enumerate(values):
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    80
                if subindex != 0 and mapping == model:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    81
                    return PDOidx, subindex
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    82
    return None
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    83
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    84
def GenerateMappingDCF(cobid, idx, pdomapping, mapped): # Build concise DCF
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    85
    
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    86
    # Create entry for RPDO or TPDO parameters and Disable PDO
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    87
    dcfdata = LE_to_BE(idx, 2) + LE_to_BE(0x01, 1) + LE_to_BE(0x04, 4) + LE_to_BE((0x80000000 + cobid), 4)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    88
    # Set Transmit type synchrone
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    89
    dcfdata += LE_to_BE(idx, 2) + LE_to_BE(0x02, 1) + LE_to_BE(0x01, 4) + LE_to_BE(DefaultTransmitTypeSlave, 1)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    90
    # Re-Enable PDO
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    91
    #         ---- INDEX -----   --- SUBINDEX ----   ----- SIZE ------   ------ DATA ------
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    92
    dcfdata += LE_to_BE(idx, 2) + LE_to_BE(0x01, 1) + LE_to_BE(0x04, 4) + LE_to_BE(0x00000000 + cobid, 4)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    93
    nbparams = 3
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    94
    if mapped == False and pdomapping != None:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    95
    # Map Variables
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    96
        for subindex, (name, loc_infos) in enumerate(pdomapping):
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    97
            value = (loc_infos["index"] << 16) + (loc_infos["subindex"] << 8) + loc_infos["size"]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    98
            dcfdata += LE_to_BE(idx + 0x200, 2) + LE_to_BE(subindex + 1, 1) + LE_to_BE(0x04, 4) + LE_to_BE(value, 4)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
    99
            nbparams += 1
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   100
    return dcfdata, nbparams
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   101
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   102
def GetNewCobID(nodeid, type): # Return a cobid not used
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   103
    global ListCobIDAvailable, SlavesPdoNumber
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   104
    
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   105
    if len(ListCobIDAvailable) == 0:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   106
        return None
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   107
    
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   108
    nbSlavePDO = SlavesPdoNumber[nodeid][type]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   109
    if type == RPDO:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   110
        if nbSlavePDO < 4:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   111
            # For the fourth PDO -> cobid = 0x200 + ( numPdo parameters * 0x100) + nodeid
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   112
            newcobid = (0x200 + nbSlavePDO * 0x100 + nodeid)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   113
            if newcobid in ListCobIDAvailable:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   114
                ListCobIDAvailable.remove(newcobid)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   115
                return newcobid, 0x1400 + nbSlavePDO
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   116
        return ListCobIDAvailable.pop(0), 0x1400 + nbSlavePDO
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   117
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   118
    elif type == TPDO:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   119
        if nbSlavePDO < 4:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   120
            # For the fourth PDO -> cobid = 0x180 + (numPdo parameters * 0x100) + nodeid
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   121
            newcobid = (0x180 + nbSlavePDO * 0x100 + nodeid)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   122
            if newcobid in ListCobIDAvailable:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   123
                ListCobIDAvailable.remove(newcobid)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   124
                return newcobid, 0x1800 + nbSlavePDO
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   125
        return ListCobIDAvailable.pop(0), 0x1800 + nbSlavePDO
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   126
    
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   127
    for number in xrange(4):
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   128
        if type == RPDO:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   129
            # For the fourth PDO -> cobid = 0x200 + ( numPdo * 0x100) + nodeid
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   130
            newcobid = (0x200 + number * 0x100 + nodeid)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   131
        elif type == TPDO:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   132
            # For the fourth PDO -> cobid = 0x180 + (numPdo * 0x100) + nodeid
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   133
            newcobid = (0x180 + number * 0x100 + nodeid)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   134
        else:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   135
            return None
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   136
        if newcobid in ListCobIDAvailable:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   137
            ListCobIDAvailable.remove(newcobid)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   138
            return newcobid
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   139
    return ListCobIDAvailable.pop(0)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   140
        
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   141
        
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   142
def GenerateConciseDCF(locations, busname, nodelist):
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   143
    global DictLocations, DictCobID, DictLocationsNotMapped, ListCobIDAvailable, SlavesPdoNumber, DefaultTransmitTypeSlave
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   144
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   145
    DictLocations = {}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   146
    DictCobID = {}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   147
    DictLocationsNotMapped = {}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   148
    DictSDOparams = {}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   149
    ListCobIDAvailable = range(0x180, 0x580)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   150
    SlavesPdoNumber = {}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   151
    DictNameVariable = { "" : 1, "X": 2, "B": 3, "W": 4, "D": 5, "L": 6, "increment": 0x100, 1:("__I", 0x2000), 2:("__Q", 0x4000)}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   152
    DefaultTransmitTypeSlave = 0xFF
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   153
    # Master Node initialisation
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   154
    
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   155
    manager = nodelist.Manager
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   156
    masternode = manager.GetCurrentNodeCopy()
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   157
    if not masternode.IsEntry(0x1F22):
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   158
        masternode.AddEntry(0x1F22, 1, "")
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   159
    manager.AddSubentriesToCurrent(0x1F22, 127, masternode)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   160
    # Adding trash mappable variables for unused mapped datas
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   161
    idxTrashVariables = 0x2000 + masternode.GetNodeID()
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   162
    TrashVariableValue = {}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   163
    manager.AddMapVariableToCurrent(idxTrashVariables, "trashvariables", 3, len(TrashVariableSizes), masternode)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   164
    for subidx, (size, typeidx) in enumerate(TrashVariableSizes.items()):
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   165
        manager.SetCurrentEntry(idxTrashVariables, subidx + 1, "TRASH%d" % size, "name", None, masternode)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   166
        manager.SetCurrentEntry(idxTrashVariables, subidx + 1, typeidx, "type", None, masternode)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   167
        TrashVariableValue[size] = (idxTrashVariables << 16) + ((subidx + 1) << 8) + size
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   168
    
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   169
    
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   170
    # Extract Master Node current empty mapping index
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   171
    CurrentPDOParamsIdx = {RPDO : 0x1400 + len(GetSlavePDOIndexes(masternode, RPDO)),
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   172
                           TPDO : 0x1800 + len(GetSlavePDOIndexes(masternode, TPDO))}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   173
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   174
    # Get list of all Slave's CobID and Slave's default SDO server parameters
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   175
    for nodeid, nodeinfos in nodelist.SlaveNodes.items():
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   176
        node = nodeinfos["Node"]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   177
        node.SetNodeID(nodeid)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   178
        DictSDOparams[nodeid] = {"RSDO" : node.GetEntry(0x1200,0x01), "TSDO" : node.GetEntry(0x1200,0x02)}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   179
        slaveRpdoIndexes = GetSlavePDOIndexes(node, RPDO, True)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   180
        slaveTpdoIndexes = GetSlavePDOIndexes(node, TPDO, True)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   181
        SlavesPdoNumber[nodeid] = {RPDO : len(slaveRpdoIndexes), TPDO : len(slaveTpdoIndexes)}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   182
        for PdoIdx in slaveRpdoIndexes + slaveTpdoIndexes:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   183
            pdo_cobid = node.GetEntry(PdoIdx, 0x01)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   184
            if pdo_cobid > 0x600 :
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   185
                pdo_cobid -= 0x80000000
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   186
            if pdo_cobid in ListCobIDAvailable:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   187
                ListCobIDAvailable.remove(pdo_cobid)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   188
    
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   189
    # Get list of locations check if exists and mappables -> put them in DictLocations
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   190
    for locationtype, name in locations:    
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   191
        if name in DictLocations.keys():
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   192
            if DictLocations[name]["type"] != DicoTypes[locationtype]:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   193
                raise ValueError, "Conflict type for location \"%s\"" % name 
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   194
        else:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   195
            loc = [i for i in name.split("_") if len(i) > 0]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   196
            if len(loc) not in (4, 5):
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   197
                continue
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   198
            
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   199
            prefix = loc[0][0]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   200
            
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   201
            # Extract and check busname
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   202
            if loc[0][1].isdigit():
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   203
                sizelocation = ""
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   204
                busnamelocation = int(loc[0][1:])
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   205
            else:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   206
                sizelocation = loc[0][1]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   207
                busnamelocation = int(loc[0][2:])
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   208
            if busnamelocation != busname:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   209
                continue # A ne pas remplacer par un message d'erreur
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   210
            
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   211
            # Extract and check nodeid
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   212
            nodeid = int(loc[1])
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   213
            if nodeid not in nodelist.SlaveNodes.keys():
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   214
                continue
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   215
            node = nodelist.SlaveNodes[nodeid]["Node"]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   216
            
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   217
            # Extract and check index and subindex
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   218
            index = int(loc[2])
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   219
            subindex = int(loc[3])
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   220
            if not node.IsEntry(index, subindex):
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   221
                continue
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   222
            subentry_infos = node.GetSubentryInfos(index, subindex)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   223
            
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   224
            if subentry_infos and subentry_infos["pdo"]:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   225
                if sizelocation == "X" and len(loc) > 4:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   226
                    numbit = loc[4]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   227
                elif sizelocation != "X" and len(loc) > 4:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   228
                    continue
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   229
                else:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   230
                    numbit = None
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   231
                
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   232
                locationtype = DicoTypes[locationtype]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   233
                entryinfos = node.GetSubentryInfos(index, subindex)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   234
                if entryinfos["type"] != locationtype:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   235
                    raise ValueError, "Invalid type for location \"%s\"" % name
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   236
                
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   237
                typeinfos = node.GetEntryInfos(locationtype)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   238
                DictLocations[name] = {"type":locationtype, "pdotype":SlavePDOType[prefix],
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   239
                                       "nodeid": nodeid, "index": index,"subindex": subindex, 
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   240
                                       "bit": numbit, "size": typeinfos["size"], "busname": busname, "sizelocation": sizelocation}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   241
                  
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   242
    # Create DictCobID with variables already mapped and add them in DictValidLocations
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   243
    for name, locationinfos in DictLocations.items():
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   244
        node = nodelist.SlaveNodes[locationinfos["nodeid"]]["Node"]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   245
        result = SearchSlavePDOMapping(locationinfos, node)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   246
        if result != None:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   247
            index, subindex = result
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   248
            cobid = nodelist.GetSlaveNodeEntry(locationinfos["nodeid"], index - 0x200, 1)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   249
            if cobid not in DictCobID.keys():
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   250
                mapping = [None]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   251
                values = node.GetEntry(index)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   252
                for value in values[1:]:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   253
                    mapping.append(value % 0x100)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   254
                DictCobID[cobid] = {"type" : InvertPDOType[locationinfos["pdotype"]], "mapping" : mapping}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   255
        
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   256
            DictCobID[cobid]["mapping"][subindex] = (locationinfos["type"], name)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   257
            
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   258
        else:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   259
            if locationinfos["nodeid"] not in DictLocationsNotMapped.keys():
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   260
                DictLocationsNotMapped[locationinfos["nodeid"]] = {TPDO : [], RPDO : []}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   261
            DictLocationsNotMapped[locationinfos["nodeid"]][locationinfos["pdotype"]].append((name, locationinfos))
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   262
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   263
    # Check Master Pdo parameters for cobid already used and remove it in ListCobIDAvailable
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   264
    ListPdoParams = [idx for idx in masternode.GetIndexes() if 0x1400 <= idx <= 0x15FF or  0x1800 <= idx <= 0x19FF]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   265
    for idx in ListPdoParams:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   266
        cobid = masternode.GetEntry(idx, 0x01)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   267
        if cobid not in DictCobID.keys():
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   268
            ListCobIDAvailable.pop(cobid)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   269
    
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   270
    #-------------------------------------------------------------------------------
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   271
    #                         Build concise DCF for the others locations
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   272
    #-------------------------------------------------------------------------------
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   273
    
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   274
    for nodeid, locations in DictLocationsNotMapped.items():
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   275
        # Get current concise DCF
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   276
        node = nodelist.SlaveNodes[nodeid]["Node"]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   277
        nodeDCF = masternode.GetEntry(0x1F22, nodeid)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   278
        
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   279
        if nodeDCF != None and nodeDCF != '':
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   280
            tmpnbparams = [i for i in nodeDCF[:4]]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   281
            tmpnbparams.reverse()
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   282
            nbparams = int(''.join(["%2.2x"%ord(i) for i in tmpnbparams]), 16)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   283
            dataparams = nodeDCF[4:]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   284
        else:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   285
            nbparams = 0
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   286
            dataparams = ""
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   287
        
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   288
        for pdotype in (TPDO, RPDO):
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   289
            pdosize = 0
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   290
            pdomapping = []
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   291
            for name, loc_infos in locations[pdotype]:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   292
                pdosize += loc_infos["size"]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   293
                # If pdo's size > 64 bits
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   294
                if pdosize > 64:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   295
                    result = GetNewCobID(nodeid, pdotype)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   296
                    if result:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   297
                        SlavesPdoNumber[nodeid][pdotype] += 1
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   298
                        new_cobid, new_idx = result
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   299
                        data, nbaddedparams = GenerateMappingDCF(new_cobid, new_idx, pdomapping, False)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   300
                        dataparams += data
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   301
                        nbparams += nbaddedparams
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   302
                        DictCobID[new_cobid] = {"type" : InvertPDOType[pdotype], "mapping" : GenerateMasterMapping(pdomapping)}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   303
                    pdosize = loc_infos["size"]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   304
                    pdomapping = [(name, loc_infos)]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   305
                else:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   306
                    pdomapping.append((name, loc_infos))
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   307
            if len(pdomapping) > 0:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   308
                result = GetNewCobID(nodeid, pdotype)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   309
                if result:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   310
                    SlavesPdoNumber[nodeid][pdotype] += 1
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   311
                    new_cobid, new_idx = result
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   312
                    data, nbaddedparams = GenerateMappingDCF(new_cobid, new_idx, pdomapping, False)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   313
                    dataparams += data
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   314
                    nbparams += nbaddedparams
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   315
                    DictCobID[new_cobid] = {"type" : InvertPDOType[pdotype], "mapping" : GenerateMasterMapping(pdomapping)}
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   316
        
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   317
        dcf = LE_to_BE(nbparams, 0x04) + dataparams
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   318
        masternode.SetEntry(0x1F22, nodeid, dcf)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   319
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   320
        
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   321
    #-------------------------------------------------------------------------------
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   322
    #                         Master Node Configuration
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   323
    #-------------------------------------------------------------------------------
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   324
    
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   325
    # Configure Master's SDO parameters entries
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   326
    for nodeid, SDOparams in DictSDOparams.items():
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   327
        SdoClient_index = [0x1280 + nodeid]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   328
        manager.ManageEntriesOfCurrent(SdoClient_index,[], masternode)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   329
        if SDOparams["RSDO"] != None:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   330
            RSDO_cobid = SDOparams["RSDO"]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   331
        else:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   332
            RSDO_cobid = 0x600 + nodeid 
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   333
            
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   334
        if SDOparams["TSDO"] != None:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   335
            TSDO_cobid = SDOparams["TSDO"]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   336
        else:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   337
            TSDO_cobid = 0x580 + nodeid
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   338
            
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   339
        masternode.SetEntry(SdoClient_index[0], 0x01, RSDO_cobid)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   340
        masternode.SetEntry(SdoClient_index[0], 0x02, TSDO_cobid)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   341
        masternode.SetEntry(SdoClient_index[0], 0x03, nodeid)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   342
    
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   343
    # Configure Master's PDO parameters entries and set cobid, transmit type
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   344
    for cobid, pdo_infos in DictCobID.items():
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   345
        current_idx = CurrentPDOParamsIdx[pdo_infos["type"]]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   346
        addinglist = [current_idx, current_idx + 0x200]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   347
        manager.ManageEntriesOfCurrent(addinglist, [], masternode)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   348
        masternode.SetEntry(current_idx, 0x01, cobid)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   349
        masternode.SetEntry(current_idx, 0x02, DefaultTransmitTypeMaster)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   350
        if len(pdo_infos["mapping"]) > 2:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   351
            manager.AddSubentriesToCurrent(current_idx + 0x200, len(pdo_infos["mapping"]) - 2, masternode)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   352
        
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   353
        # Create Master's PDO mapping
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   354
        for subindex, variable in enumerate(pdo_infos["mapping"]):
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   355
            if subindex == 0:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   356
                continue
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   357
            new_index = False
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   358
            
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   359
            if type(variable) != IntType:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   360
                
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   361
                typeidx, varname = variable
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   362
                indexname = DictNameVariable[DictLocations[variable[1]]["pdotype"]][0] + DictLocations[variable[1]]["sizelocation"] + str(DictLocations[variable[1]]["busname"]) + "_" + str(DictLocations[variable[1]]["nodeid"])
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   363
                mapvariableidx = DictNameVariable[DictLocations[variable[1]]["pdotype"]][1] +  DictNameVariable[DictLocations[variable[1]]["sizelocation"]] * DictNameVariable["increment"]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   364
                
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   365
                if not masternode.IsEntry(mapvariableidx):
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   366
                    manager.AddMapVariableToCurrent(mapvariableidx, indexname, 3, 1, masternode)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   367
                    new_index = True
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   368
                    nbsubentries = masternode.GetEntry(mapvariableidx, 0x00)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   369
                else:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   370
                    nbsubentries = masternode.GetEntry(mapvariableidx, 0x00)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   371
                    mapvariableidxbase = mapvariableidx 
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   372
                    while mapvariableidx < (mapvariableidxbase + 0x1FF) and nbsubentries == 0xFF:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   373
                        mapvariableidx += 0x800
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   374
                        if not manager.IsCurrentEntry(mapvariableidx):
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   375
                            manager.AddMapVariableToCurrent(mapvariableidx, indexname, 3, 1, masternode)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   376
                            new_index = True
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   377
                        nbsubentries = masternode.GetEntry(mapvariableidx, 0x00)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   378
                
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   379
                if mapvariableidx < 0x6000:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   380
                    if DictLocations[variable[1]]["bit"] != None:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   381
                        subindexname = "_" + str(DictLocations[variable[1]]["index"]) + "_" + str(DictLocations[variable[1]]["subindex"]) + "_" + str(DictLocations[variable[1]]["bit"])
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   382
                    else:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   383
                        subindexname = "_" + str(DictLocations[variable[1]]["index"]) + "_" + str(DictLocations[variable[1]]["subindex"])
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   384
                    if not new_index:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   385
                        manager.AddSubentriesToCurrent(mapvariableidx, 1, masternode)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   386
                        nbsubentries += 1
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   387
                    masternode.SetMappingEntry(mapvariableidx, nbsubentries, values = {"name" : subindexname})
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   388
                    masternode.SetMappingEntry(mapvariableidx, nbsubentries, values = {"type" : typeidx})
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   389
                    
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   390
                    # Map Variable
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   391
                    typeinfos = manager.GetEntryInfos(typeidx)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   392
                    if typeinfos != None:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   393
                        value = (mapvariableidx << 16) + ((nbsubentries) << 8) + typeinfos["size"]
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   394
                        masternode.SetEntry(current_idx + 0x200, subindex, value)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   395
            else:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   396
                masternode.SetEntry(current_idx + 0x200, subindex, TrashVariableValue[variable])
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   397
        
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   398
        CurrentPDOParamsIdx[pdo_infos["type"]] += 1
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   399
    #masternode.Print()
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   400
    return masternode
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   401
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   402
if __name__ == "__main__":
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   403
    from nodemanager import *
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   404
    from nodelist import *
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   405
    import sys
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   406
    
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   407
    manager = NodeManager(sys.path[0])
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   408
    nodelist = NodeList(manager)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   409
    result = nodelist.LoadProject("/home/deobox/Desktop/TestMapping")
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   410
   
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   411
##    if result != None:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   412
##        print result
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   413
##    else:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   414
##        print "MasterNode :"
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   415
##        manager.CurrentNode.Print()
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   416
##        for nodeid, node in nodelist.SlaveNodes.items():
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   417
##            print "SlaveNode name=%s id=0x%2.2X :"%(node["Name"], nodeid)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   418
##            node["Node"].Print()
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   419
            
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   420
    #filepath = "/home/deobox/beremiz/test_nodelist/listlocations.txt"
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   421
    filepath = "/home/deobox/Desktop/TestMapping/listlocations.txt"
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   422
    
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   423
    file = open(filepath,'r')
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   424
    locations = [location.split(' ') for location in [line.strip() for line in file.readlines() if len(line) > 0]] 
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   425
    file.close()
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   426
    GenerateConciseDCF(locations, 32, nodelist)
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   427
    print "MasterNode :"
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   428
    manager.CurrentNode.Print()
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents:
diff changeset
   429
    #masternode.Print()