Commit e96a3b7c authored by Matthieu Cattin's avatar Matthieu Cattin

Add signed_short in typelen generation. Remove trailing whitespaces.

parent a8db7cd1
......@@ -24,12 +24,12 @@ default_inputFile = './eeprom_input.xml'
########################################
# Define New Type
# Define New Type
########################################
type_list =["yes_no","calib_data"]
type_list_description = [" Takes an input of the form 'yes' or 'no', returns an associated number","This is the type conversation for calibration data,\n should not be used in the XML file"]
type_list_description = [" Takes an input of the form 'yes' or 'no', returns an associated number","This is the type conversation for calibration data,\n should not be used in the XML file"]
......@@ -56,8 +56,8 @@ def generate_addresses(data_size):
address_list =[]
for i in range(data_size):
address_bytes = convert_to_byte(i,2)
combined_addr = address_bytes[0]+address_bytes[1]
combined_addr = address_bytes[0]+address_bytes[1]
address_list.append([combined_addr])
return address_list
......@@ -69,9 +69,9 @@ def print_area_nums():
areas=["internal use" ,"chassis info" ,"board info","product info","multi area"]
print ""
for i in range(len(areas)):
print "%d : %s"%((i+1),areas[i])
print "%d : %s"%((i+1),areas[i])
print ""
def print_type_options():
print "type options"
......@@ -81,23 +81,23 @@ def print_type_options():
print "BCD : Enter an integer\n"
print "Signed_short : Enter in the form +0.00 or -0.00"
print " To be given in 0.01 incriments\n"
if len(type_list)!=0:
for i in range(len(type_list)):
print type_list[i] + " :" + type_list_description[i]+"\n"
def convert_to_byte(number,num=0):
converted=[]
separate_bytes=[]
character = bin(number)[2:]
if num == 0:
required_length = (len(character)/9 +1)*8
else:
required_length = num*8
converted.append(('0' * (required_length-len(character))) + character)
(required_length/8)
for i in range(required_length/8):
......@@ -106,8 +106,8 @@ def convert_to_byte(number,num=0):
def generate_multirecord_header(mh_type,last_multirecord, length):
header=[]
def generate_multirecord_header(mh_type,last_multirecord, length):
header=[]
if mh_type==multi_record_options[0] :
type_identifier="00000000"
elif mh_type==multi_record_options[1]:
......@@ -121,22 +121,22 @@ def generate_multirecord_header(mh_type,last_multirecord, length):
elif mh_type==multi_record_options[5]:
type_identifier="00000101"
elif mh_type==multi_record_options[6]:
type_identifier="11111111"
type_identifier="11111111"
else:
print "unidentified record type"
type_identifier = "WRONG mh type"
if last_multirecord =='yes':
record_field = convert_to_byte(130)
record_field = convert_to_byte(130)
else:
record_field = convert_to_byte(2)
header.append([type_identifier])
header.append(record_field)
header.append([str(convert_to_byte(length)[0])])
header.append(record_field)
header.append([str(convert_to_byte(length)[0])])
mrchecksum = return_checksum(length)
header.append([str(mrchecksum)])
header.append(["11111011"])
header.append([str(mrchecksum)])
header.append(["11111011"])
return header
......@@ -150,11 +150,11 @@ def append_sections(master_data,common_header):
## for i in range(len(master_data)):
## for j in range(len(master_data[i])):
## #print master_data[i]
##
##
## if len(master_data[i][j])>1:
## for k in range(len(master_data[i][j])):
## master_binary.append([master_data[i][j][k]])
##
##
## else:
## master_binary.append(master_data[i][j])
......@@ -183,7 +183,7 @@ def append_common_header(areas_found,area_lengths):
common_header.append(convert_to_byte(0))
else:
common_header.append(convert_to_byte(sum(array[0:i])+1))
common_header.append(["00000000"])
common_header.append(["11110000"]) # CHECK THIS
return common_header
......@@ -201,32 +201,32 @@ def gen_common_header_tags():
tags.append(["Padding for Common Header"])
tags.append(["Common header checksum"])
return tags
def write_to_file(file_name,data_list,addr_list,tag_bytes,debug):
def write_to_file(file_name,data_list,addr_list,tag_bytes,debug):
if file_name ==None:
writing_to_file=open("eeprom_formatted_data.txt","w")
else:
writing_to_file=open(file_name,"w")
if len(data_list)!=len(addr_list):
print "problem with data and addr length"
if debug:
for h in range(len(data_list)):
writing_to_file.write("%s %s %s\n"%(addr_list[h][0],data_list[h][0],tag_bytes[h][0]))
# print ("%s %s %s \n"%(addr_list[h][0],data_list[h][0],tag_bytes[h][0]))
else:
for h in range(len(data_list)):
writing_to_file.write("%s %s \n"%(addr_list[h][0],data_list[h][0]))
#print ("%s %s %s \n"%(addr_list[h][0],data_list[h][0],tag_bytes[h][0]))
if file_name ==None:
print "Formatted data written to 'EEPROM_FORMATTED_DATA.txt' : in current directory"
else:
......@@ -242,9 +242,9 @@ def return_checksum(byte):
if h=='0':
inv_binary_value.append('1')
elif h=='1':
inv_binary_value.append('0')
inv_binary_value.append('0')
inverted="".join(element for element in inv_binary_value)
twos_comp_checksum = (bin(int(inverted,2)+1)[2:])
twos_comp_checksum = (bin(int(inverted,2)+1)[2:])
return twos_comp_checksum
......@@ -255,10 +255,10 @@ def convert_to_ascii(text):
character=bin(ord(char))[2:]
converted_line.append(('0' * (8-len(character))) + character)
# print "ascii converted",converted_line
return converted_line
return converted_line
def convert_to_bcd(text):
def convert_to_bcd(text):
bcd_conv=[]
if text.isdigit()!=True:
print "Error : BCD value contains a character, ensure the content contains only numbers"
......@@ -281,7 +281,7 @@ def print_calib_info():
print "E.g. 0xABCD,yes"
print "The hex number must start with 0x"
print "If a typelen byte is desired for the data, write yes, else no"
def print_multi_record_options():
print "\nPlease use one of the following MultiRecord names:"
......@@ -297,7 +297,7 @@ def convert_to_signed_short(content):
return "error"
sign = content[0]
fp_value =float(content[1:])
converted_ss=[]
converted_ss=[]
value=int(fp_value*100)
if sign=="+":
binary_value=bin(value)[2:]
......@@ -309,7 +309,7 @@ def convert_to_signed_short(content):
converted_ss.append(sixteen_bits[8:16])
#return converted_ss
elif sign=="-":
converted_ss=[]
converted_ss=[]
inv_binary_value=[]
binary_value=bin(value)[2:]
sixteen_bits=str(('0' * (16- len(binary_value))) + binary_value)
......@@ -317,9 +317,9 @@ def convert_to_signed_short(content):
if h=='0':
inv_binary_value.append('1')
elif h=='1':
inv_binary_value.append('0')
inv_binary_value.append('0')
inverted="".join(element for element in inv_binary_value)
twos_comp=bin(int(inverted,2)+1)[2:]
twos_comp=bin(int(inverted,2)+1)[2:]
converted_ss.append(twos_comp[0:8])
converted_ss.append(twos_comp[8:16])
#print converted_ss,"converted SS"
......@@ -349,22 +349,22 @@ def check_byte_size(text):
else:
array.append(bytes_split[i])
return array
def type_length_byte(type_enc,length): # allows the generation of a type length byte Where is signed short?!
#if type_enc=="ascii":
array =[]
field_size = []
type_len_byte=['0','0','0','0','0','0','0','0']
binary_len=bin(length)[2:]
field_size.append(('0' * (6-len(binary_len))) + binary_len)
binary_len=bin(length)[2:]
field_size.append(('0' * (6-len(binary_len))) + binary_len)
if type_enc== "ascii":
type_len_byte[7]='1'
type_len_byte[6]='1'
elif type_enc=="bcd":
type_len_byte[7]='0'
type_len_byte[6]='1'
elif type_enc in type_list or type_enc== "binary":
elif type_enc in type_list or type_enc== "binary" or type_enc == "signed_short":
type_len_byte[7]='0'
type_len_byte[6]='0'
else:
......@@ -373,36 +373,36 @@ def type_length_byte(type_enc,length): # allows the generation of a ty
return "error"
type_len_byte[7]='0'
type_len_byte[6]='0'
test_type_len = str(type_len_byte[7]) + str(type_len_byte[6]) + str(field_size[0])
return [test_type_len]
def generate_tags(converted_line, data_type, area_num):
tag_array=[]
for fields in converted_line:
for fields in converted_line:
#print fields
#print data_type
tag_array.append("Area Number :" + area_num + " Data Type :" + data_type )
#print tag_array
return tag_array
#print converted_line
def gen_multi_record_header_tags():
tags = []
tags.append(["Multirecord Header : Record ID Type"])
tags.append(["Multirecord Header : Format Version (Bit 7: Last MultiRecord Flag)"])
tags.append(["Multirecord Header : Record Length"])
tags.append(["Multirecord Header : Record Checksum"])
tags.append(["Multirecord Header : Header Checksum"])
tags.append(["Multirecord Header : Record ID Type"])
tags.append(["Multirecord Header : Format Version (Bit 7: Last MultiRecord Flag)"])
tags.append(["Multirecord Header : Record Length"])
tags.append(["Multirecord Header : Record Checksum"])
tags.append(["Multirecord Header : Header Checksum"])
return tags
def format_calib_data(hex_calib_data):
#print int(hex_calib_data,16)
......@@ -417,52 +417,52 @@ def calibration_data(calibration_file):
area_1_tags=[]
area_1_data.append(["00000001"])
area_1_tags.append(["The start of area 2"])
f = open(calibration_file[0])
for line in f:
split_line = (line.split(','))
converted_line = typeConvFunctionsMap["calib_data"](split_line[0])
tags = generate_tags(converted_line,"calib_data", "2")
has_typelen = split_line[1].strip("\n")
if has_typelen.lower() =="yes":
type_len=type_length_byte("calib_data",len(converted_line))
type_len=type_length_byte("calib_data",len(converted_line))
area_1_data.append(type_len)
area_1_tags.append(["type_len byte"])
area_1_data.append(converted_line)
area_1_tags.append(tags)
byte_count = []
for fields in area_1_data:
for fields in area_1_data:
for byte in fields:
byte_count.append(byte)
area_len = len(byte_count)
#print (len(byte_count)%8)
while area_len%8!=7:
area_1_data.append(["00000000"])
area_1_data.append(["00000000"])
area_1_tags.append(["Padding - in calibration area"])
area_len = area_len +1
area_1_data.append(["11000001"])
area_1_tags.append(["End of fields identifier in calibration data area"])
byte_count.append("anything")
#print "byte count calib area", len(byte_count)
length_calced = (area_len+1)/8
return_area = [0,0,0]
return_area[0] = area_1_data
return_area[1] = area_1_tags
return_area[2] = length_calced
return return_area
#########################################
# Funtions map
#########################################
......@@ -484,21 +484,21 @@ typeConvFunctionsMap = {
def main(file_name,input_name,debug,calibration_file):
if input_name == None:
doc = libxml2.parseFile(default_inputFile)
elif len(input_name.split())==1:
doc = libxml2.parseFile('./' + str(input_name.split()[0]))
else:
print "\n\nunexpected input for input file name"
print "expected : filename.xml\n\n"
error_count = 0
master_data = []
master_tags = []
pos_of_area_len = 2
pos_of_area_len = 2
data=[]
addr=[]
root = doc.getRootElement()
......@@ -509,22 +509,22 @@ def main(file_name,input_name,debug,calibration_file):
concat=[]
########################################################################################################################
########################################################################################################################
# Loop through XML FILE
########################################################################################################################
while area is not None:
if area.type == "element":
if area.type == "element":
areas_found.append(area.prop('number'))
area_data =[]
area_tags =[]
byte_count=[]
pos_of_len_field = area.prop('pos_of_len_field')
areaContent = area.children
areaContent = area.children
while areaContent is not None:
if areaContent.type == "element":
areaContentName = areaContent.name ########################################
if (areaContentName!='multirecord'): # if fields, then process
if (areaContentName!='multirecord'): # if fields, then process
field_name = areaContent.prop('name') ########################################
has_typelen = areaContent.prop('has_typelen')
field_content = areaContent.prop('content')
......@@ -536,58 +536,58 @@ def main(file_name,input_name,debug,calibration_file):
converted_line = typeConvFunctionsMap[type](field_content)
tags = generate_tags(converted_line,type, area.prop('number'))
# add descripter
else:
converted_line = "error"
print "Type not contained in function map..."
print "If a new type is required, see \"fmc_eeprom_gen\" for details of how to add a new type"
if converted_line =="error":
print "ERROR Position : Area :",area.prop('number')," \'",areaContent.prop('name'),"\'"
error_count = error_count +1
if has_typelen == "yes":
type_len=type_length_byte(type,len(converted_line))
if has_typelen == "yes":
type_len=type_length_byte(type,len(converted_line))
if type_len =="error":
print "Type len error"
print "ERROR Position : Area :",area.prop('number')," \'",areaContent.prop('name'),"\'"
error_count = error_count +1
area_data.append(type_len)
area_tags.append(["type_len byte"])
area_data.append(type_len)
area_tags.append(["type_len byte"])
area_data.append(converted_line)
area_tags.append(tags)
########################################################### Here
########################################
else: # else, go further to multirecord
else: # else, go further to multirecord
# nested loop within areas loop
######################################## ########################################
# Next Child element - MultiRecord
########################################
field = areaContent.children
########################################
field = areaContent.children
multi_area_data =[]
multi_area_tags =[]
while field is not None:
while field is not None:
if field.type=="element":
fieldContentName = field.name ########################################
if fieldContentName!='byteField': # if fields, then process
if fieldContentName!='byteField': # if fields, then process
field_name = field.prop('name') ########################################
has_typelen = field.prop('has_typelen')
field_content = field.prop('content')
type = field.prop('type')
if typeConvFunctionsMap.has_key(type):
converted_line = typeConvFunctionsMap[type](field_content)
##################################
tags = generate_tags(converted_line,type,area.prop('number'))
##################################
else:
......@@ -595,39 +595,39 @@ def main(file_name,input_name,debug,calibration_file):
print "Type not contained in function map..."
print "If a new type is required, see \"fmc_eeprom_gen\" for details"
print "on how to add a new type"
if converted_line == "error":
print "ERROR Position :", areaContent.prop('name'),", Multirecord number: ",areaContent.prop('multi_record_num'), " \'",field.prop('name'),"\'"
error_count = error_count +1
if has_typelen == "yes":
type_len=type_length_byte(type,len(converted_line))
if has_typelen == "yes":
type_len=type_length_byte(type,len(converted_line))
if type_len =="error":
print "Type len error"
print "ERROR Position :", areaContent.prop('name'),", Multirecord number: ",areaContent.prop('multi_record_num'), " \'",field.prop('name'),"\'"
error_count = error_count +1
multi_area_data.append(type_len)
multi_area_tags.append(["type_len"])
multi_area_data.append(converted_line)
multi_area_tags.append(tags)
########################################################### Here
########################################
# Next Child Element - ByteField Data
# Process ByteField
# Process ByteField
# Append to MultiRecord Data
######################################## ########################################
else: # else, go further to bytefield
else: # else, go further to bytefield
bytefieldData =[] # nested loop within multirecord loop
bytefield = field.children ########################################
bytefield = field.children ########################################
while bytefield is not None:
if bytefield.type=="element":
bytefieldData.append(bytefield.prop('content'))
bytefieldData.append(bytefield.prop('content'))
bytefield = bytefield.next
if len("".join(bytefieldData))!=8:
print "ERROR : fields in byteField \'",field.prop('name'),"\' do not add to 8 bits"
......@@ -641,17 +641,17 @@ def main(file_name,input_name,debug,calibration_file):
# Process MultiRecord data
# Append to Area Data
########################################
for fields in multi_area_data:
for fields in multi_area_data:
for byte in fields:
byte_count.append(byte)
multi_record_header = generate_multirecord_header(areaContent.prop('name'),areaContent.prop('last_multirecord'),len(byte_count))
multi_record_header_tags = gen_multi_record_header_tags()
multi_record_header_tags = gen_multi_record_header_tags()
#master_data.append(multi_record_header)
#master_tags.append(multi_record_header_tags)
#area_data.append(multi_record_header)
#master_data.append(multi_area_data)
#area_data.append(multi_area_data)
#master_tags.append(multi_area_tags)
......@@ -661,7 +661,7 @@ def main(file_name,input_name,debug,calibration_file):
area_tags.append(multi_record_header_tags + multi_area_tags)
byte_count=[]
########################################
......@@ -672,12 +672,12 @@ def main(file_name,input_name,debug,calibration_file):
if (areaContentName!='multirecord'):
area_data.append([convert_to_byte(int("C1",16))[0] ])
area_tags.append(["END OF FIELDS IDENTIFIER"])
for fields in area_data:
for fields in area_data:
for byte in fields:
byte_count.append(byte)
area_size = len(byte_count)
if int(pos_of_len_field) == 0:
pad_size = 7
length_calc_addition = 1
......@@ -685,34 +685,34 @@ def main(file_name,input_name,debug,calibration_file):
length_calc_addition = 2
pad_size = 6
#while len(byte_count)%8!=pad_size:
while area_size%8!=pad_size:
area_size = area_size +1
#while len(byte_count)%8!=pad_size:
while area_size%8!=pad_size:
area_size = area_size +1
area_data.append(["00000000"])
area_tags.append(["Padding to round to multiple of 8 byes"])
# byte_count.append(["00000000"])
# print "pos_of_len_field :",pos_of_len_field
#length_calced = (len(byte_count)+length_calc_addition)/8
length_calced = (area_size+length_calc_addition)/8
if int(pos_of_len_field) !=0:
if int(pos_of_len_field) !=0:
area_data.insert(int(pos_of_len_field)-1,[convert_to_byte(length_calced)[0]])
area_tags.insert(int(pos_of_len_field)-1,["length of area " + area.prop('number')+" (multiples of 8bytes)"])
area_length.append(length_calced)
checksum = return_checksum(int(length_calced))
string = checksum
area_data.append([string])
area_data.append([string])
area_tags.append(["checksum"])
#byte_count.append("doesnt matter")
#byte_count.append("doesnt matter")
master_data.append(area_data)
master_tags.append(area_tags)
########################################
# If multirecod, just give length as 1
# Not important as offset is to start of
......@@ -722,9 +722,9 @@ def main(file_name,input_name,debug,calibration_file):
master_data.append(area_data)
master_tags.append(area_tags)
area_length.append(1)
area = area.next
area = area.next
### Process calibration data.
if len(calibration_file)!=0:
......@@ -733,39 +733,39 @@ def main(file_name,input_name,debug,calibration_file):
area_1_tags = area_1_info[1]
area_1_length = area_1_info[2]
# need to change this to insert calibration data at offset 0
print areas_found
print area_length
print area_1_info[2]
# need to change this to insert calibration data at offset 0
#print areas_found
#print area_length
#print area_1_info[2]
areas_found.insert(0,"1")
area_length.insert(0,area_1_length)
master_data.insert(0,area_1_data)
master_tags.insert(0,area_1_tags)
print areas_found
print area_length
#print areas_found
#print area_length
#areas_found.insert(1,"1")
#area_length.insert(1,area_1_info[2])
#master_data.insert(1,area_1_data)
#master_tags.insert(1,area_1_tags)
common_header = append_common_header(areas_found,area_length)
common_header_tags = gen_common_header_tags()
tag_bytes = append_sections(master_tags,common_header_tags)
tag_bytes = append_sections(master_tags,common_header_tags)
data_bytes = append_sections(master_data,common_header)
if len(data_bytes)>memory_size:
print "Converted data size exceeds size of EEPROM"
error_count=error_count+1
print "\nSize of EEPROM according to XML file is : %d bytes"%memory_size
print "Size of converted data : %d bytes"%len(data_bytes)
if error_count==0:
write_to_file(file_name,data_bytes,generate_addresses(len(data_bytes)),tag_bytes,debug)
else:
......@@ -787,7 +787,7 @@ if __name__ == "__main__":
#arser.add_option("-h", "--help", help =("Help Options for GEN_FMC_EEPROM"))
parser.usage = "\n\nusage: python %prog [options] calibration_data_filename.txt \n\n Help Options for fmc_eeprom.py"
parser.add_option("-i", "--file_in", dest="file_in", type="str",
help =("Filename for output file [default:eeprom_formatted_data.txt]"))
help =("Filename for output file [default:eeprom_formatted_data.txt]"))
parser.add_option("-o", "--file_out", dest="file_out", type="str",
help =("Filename for output file [default:eeprom_data.xml]"))
parser.add_option("-t", "--types", action="store_true", dest="types",
......@@ -797,13 +797,13 @@ if __name__ == "__main__":
parser.add_option("-a", "--areas", action="store_true", dest="areas",
help =("Prints the available Areas and their associated numbers"))
parser.add_option("-c", "--calib", action="store_true", dest="calib_info",
help =("Prints the required format for calibration data"))
help =("Prints the required format for calibration data"))
parser.add_option("-d", "--debug", action="store_true", dest="debug",
help =("Add this option with -r to produce labels on output data"))
help =("Add this option with -r to produce labels on output data"))
opts, args = parser.parse_args()
########################################
# Options process
########################################
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment