Go to the documentation of this file.00001
00002
00003 import sys, getopt
00004 import csv
00005 import re
00006
00007 def getSize(arg):
00008 if("uint16_t" in arg or "int16_t" in arg):
00009 return 16
00010 elif("uint32_t" in arg or "int32_t" in arg):
00011 return 32
00012 elif("float" in arg):
00013 return 64
00014
00015 def parseRegisterDescription(desc):
00016
00017 regex = re.compile("([0-9a-zA-Z_]+)\(([0-9]+)\:([0-9]+)\)")
00018 registers = desc.split(' ')
00019
00020 output = []
00021 for reg in registers:
00022
00023 try:
00024 match = regex.match(reg)
00025 mydict = {"id": match.group(1),
00026 "size": int(match.group(2)) - int(match.group(3)) +1,
00027 "index": int(match.group(3))}
00028 output.append(mydict)
00029 except AttributeError:
00030
00031 pass
00032 return output
00033
00034 def main(argv):
00035 csvFile = 'BMS_MLVDS_register_map_oct28.csv'
00036 xmlFile = 'BmsRegisters.xml'
00037 try:
00038 opts, args = getopt.getopt(argv, "hi:o:",["input=", "output="])
00039 except getopt.GetoptError:
00040 print("BMS_MLVDS_register_map_to_registers.py -i <inputfile>")
00041 sys.exit(2)
00042 for opt, arg in opts:
00043 if opt == '-h':
00044 print 'BMS_MLVDS_register_map_to_registers.py -i <inputfile>'
00045 sys.exit()
00046 elif opt in ("-i", "--input"):
00047 csvFile = arg
00048 elif opt in ("-o", "--output"):
00049 xmlFile = arg
00050
00051 print("Opening " + csvFile)
00052 data = csv.reader(open(csvFile))
00053
00054 print("Writing to " + xmlFile)
00055 xmlData = open(xmlFile, 'w')
00056
00057 xmlData.write('<?xml version="1.0" encoding="ISO-8859-1"?>' + "\n")
00058 xmlData.write('<RegisterData> ' + "\n")
00059 xmlData.write(' <Properties>' + "\n")
00060 xmlData.write(' <NodeType>BMS</NodeType>' + "\n")
00061 xmlData.write(' </Properties>' + "\n")
00062 xmlData.write(' <Registers>' + "\n")
00063 rowNum = 0
00064 for row in data:
00065 if rowNum == 0:
00066 tags = row
00067
00068 for i in range(len(tags)):
00069 tags[i] = tags[i].strip().replace(' ', '_')
00070 if("name" in tags):
00071 iName = tags.index("name")
00072 print("found name tag at index: " + str(iName))
00073 else:
00074 print("can not find name tag")
00075 break
00076 if("data_type" in tags):
00077 iDataType = tags.index("data_type")
00078 print("found datatype tag at index: " + str(iDataType))
00079 else:
00080 print("can not find datatype tag")
00081 break
00082 if("ROBONET_type" in tags):
00083 iDirection = tags.index("ROBONET_type")
00084 print("found ROBONET_type tag at index: " + str(iDirection))
00085 else:
00086 print("can not find ROBONET_type tag")
00087 break
00088 if("registers" in tags):
00089 iRegisters = tags.index("registers")
00090 print("found registers tag at index: " + str(iRegisters))
00091 else:
00092 print("can not find registers tag")
00093 break
00094 if("description" in tags):
00095 iDescription = tags.index("description")
00096 print("found description tag at index: " + str(iDescription))
00097 else:
00098 print("can not find description tag")
00099 iDescription = -1
00100 else:
00101 if(row[iName].strip().replace(' ', '_')):
00102 registers = parseRegisterDescription(row[iRegisters])
00103 size = getSize(row[iDataType])
00104
00105 if(len(registers)>0):
00106 xmlData.write(' <Register size="' + str(getSize(row[iDataType])) + '" type="' + str(row[iDirection]) + '" id="' + str(row[iName].strip().replace(' ', '_')) + '">\n')
00107 xmlData.write(' <Certificate mask="0x0" value="0x0"/> \n')
00108 xmlData.write(' <Entries> \n')
00109 for reg in registers:
00110
00111 xmlData.write(' <Entry size="' + str(reg['size']) + '" id="' + str(reg['id']) + '" index="' + str(reg['index']) + '">')
00112 if(iDescription == -1):
00113 xmlData.write('<Description>' + row[iDescription] + '</Description></Entry> \n')
00114 else:
00115 xmlData.write('<Description>' + row[iDescription] + '</Description></Entry> \n')
00116 xmlData.write(' </Entries> \n')
00117 xmlData.write(' </Register> \n')
00118 rowNum +=1
00119 xmlData.write(' </Registers>' + "\n")
00120 xmlData.write('</RegisterData>')
00121
00122 xmlData.close()
00123
00124 if __name__ == "__main__":
00125 main(sys.argv[1:])