UNPKG

2.93 kBPlain TextView Raw
1#!/usr/bin/python
2
3import sys
4import argparse
5import csv
6import array
7
8
9def parseCLArguments():
10
11 parser = argparse.ArgumentParser(description='Convert a BlueGiga .hex file into Node.js Module file.')
12
13 parser.add_argument('input', metavar='input file', type=argparse.FileType('r'), nargs='?',
14 help='The input .hex file')
15
16 parser.add_argument('-output', metavar='output file', type=argparse.FileType('wb'), nargs='?',
17 help='The optional output Node.js file name. Default is ble-firmware.js', default="ble-firmware.image")
18
19 args = parser.parse_args()
20
21 return args
22
23def extractHexData(inputFile):
24
25 commands = []
26
27 data = []
28
29 beginRecording = False;
30
31 for line in csv.reader(inputFile, delimiter=' '):
32 if (line[0]):
33 commands.append(makeFlashCommand(line[0]))
34
35 for command in commands:
36 # The first data starts at memory location 0x1000
37 if (beginRecording == False and command.recordType == 0x00 and (command.address == int("1000", 16))):
38
39 # Tell the loop to start recording
40 beginRecording = True;
41
42 # If we have a data command and are ready to start recording
43 if (beginRecording and command.recordType == 0x0):
44 for i in range(command.byteCount) :
45 data.append(command.data[i])
46
47 inputFile.close()
48
49 return data
50
51def makeFlashCommand(hexLine):
52
53 command = FlashCommand();
54 exceptionText = "Invalid Hex Format!"
55
56
57 if (hexLine[0] != ':'):
58 raise Exception(exceptionText)
59
60 try:
61 command.raw = hexLine
62 command.byteCount = int(hexLine[1:3], 16)
63 command.address = int(hexLine[3:7], 16);
64 command.recordType = int(hexLine[7:9], 16)
65
66 # Start reading at index 9
67 startIndex = 9;
68
69 # If this command has data
70 if (command.byteCount):
71
72 # Iterate through the data
73 for i in range(command.byteCount):
74
75 # We want to put each two digits together
76 dataIndex = startIndex + (i * 2)
77
78 # Append the two digits converted to a base 16 int
79 command.data.append(int(hexLine[dataIndex : dataIndex + 2], 16))
80
81 # Add the checksum
82 endIndex = startIndex + (command.byteCount*2)
83 command.checkSum = hexLine[endIndex:endIndex+2]
84
85 except IndexError:
86 raise Exception(exceptionText)
87
88
89 return command
90
91
92class FlashCommand():
93 def __init__(self):
94 self.data = []
95 self.byteCount = 0;
96 self.recordType = 0;
97 self.address = [];
98 self.checkSum = 0;
99 self.raw = ""
100
101
102
103def generateNodeFile(outputFile, buffer):
104 print ("Generating File with buffer length: " + str(len(buffer)))
105 outputFile.write(array.array('B', buffer).tostring())
106 outputFile.close();
107 print ("All Finished!")
108
109def main():
110 # Pull out the command line arguments to get input and output file
111 args = parseCLArguments()
112
113 # Extract data bytes from input file
114 buf = extractHexData(args.input)
115
116 # Open output file, write module with buffer
117 generateNodeFile(args.output, buf)
118
119main()
\No newline at end of file