1 |
|
2 |
|
3 | import sys
|
4 | import argparse
|
5 | import csv
|
6 | import array
|
7 |
|
8 |
|
9 | def 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 |
|
23 | def 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 |
|
37 | if (beginRecording == False and command.recordType == 0x00 and (command.address == int("1000", 16))):
|
38 |
|
39 |
|
40 | beginRecording = True;
|
41 |
|
42 |
|
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 |
|
51 | def 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 |
|
67 | startIndex = 9;
|
68 |
|
69 |
|
70 | if (command.byteCount):
|
71 |
|
72 |
|
73 | for i in range(command.byteCount):
|
74 |
|
75 |
|
76 | dataIndex = startIndex + (i * 2)
|
77 |
|
78 |
|
79 | command.data.append(int(hexLine[dataIndex : dataIndex + 2], 16))
|
80 |
|
81 |
|
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 |
|
92 | class 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 |
|
103 | def 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 |
|
109 | def main():
|
110 |
|
111 | args = parseCLArguments()
|
112 |
|
113 |
|
114 | buf = extractHexData(args.input)
|
115 |
|
116 |
|
117 | generateNodeFile(args.output, buf)
|
118 |
|
119 | main() |
\ | No newline at end of file |