1 |
|
2 |
|
3 | import sys
|
4 | import argparse
|
5 | import csv
|
6 |
|
7 |
|
8 | def parseCLArguments():
|
9 |
|
10 | parser = argparse.ArgumentParser(description='Convert a BlueGiga .hex file into Node.js Module file.')
|
11 |
|
12 | parser.add_argument('input', metavar='input file', type=argparse.FileType('r'), nargs='?',
|
13 | help='The input .hex file')
|
14 |
|
15 | parser.add_argument('-output', metavar='output file', type=argparse.FileType('w'), nargs='?',
|
16 | help='The optional output Node.js file name. Default is ble-firmware.js', default="ble-firmware.js")
|
17 |
|
18 | args = parser.parse_args()
|
19 |
|
20 | return args
|
21 |
|
22 | def extractHexData(inputFile):
|
23 |
|
24 | commands = []
|
25 |
|
26 | data = []
|
27 |
|
28 | beginRecording = False;
|
29 |
|
30 | for line in csv.reader(inputFile, delimiter=' '):
|
31 | if (line[0]):
|
32 | commands.append(makeFlashCommand(line[0]))
|
33 |
|
34 | for command in commands:
|
35 |
|
36 | if (beginRecording == False and command.recordType == 0x00 and (command.address == int("1000", 16))):
|
37 |
|
38 |
|
39 | beginRecording = True;
|
40 |
|
41 |
|
42 | if (beginRecording and command.recordType == 0x0):
|
43 | for i in range(command.byteCount) :
|
44 | data.append(command.data[i])
|
45 |
|
46 | inputFile.close()
|
47 |
|
48 | return data
|
49 |
|
50 | def makeFlashCommand(hexLine):
|
51 |
|
52 | command = FlashCommand();
|
53 | exceptionText = "Invalid Hex Format!"
|
54 |
|
55 |
|
56 | if (hexLine[0] != ':'):
|
57 | raise Exception(exceptionText)
|
58 |
|
59 | try:
|
60 | command.raw = hexLine
|
61 | command.byteCount = int(hexLine[1:3], 16)
|
62 | command.address = int(hexLine[3:7], 16);
|
63 | command.recordType = int(hexLine[7:9], 16)
|
64 |
|
65 |
|
66 | startIndex = 9;
|
67 |
|
68 |
|
69 | if (command.byteCount):
|
70 |
|
71 |
|
72 | for i in range(command.byteCount):
|
73 |
|
74 |
|
75 | dataIndex = startIndex + (i * 2)
|
76 |
|
77 |
|
78 | command.data.append(int(hexLine[dataIndex : dataIndex + 2], 16))
|
79 |
|
80 |
|
81 | endIndex = startIndex + (command.byteCount*2)
|
82 | command.checkSum = hexLine[endIndex:endIndex+2]
|
83 |
|
84 | except IndexError:
|
85 | raise Exception(exceptionText)
|
86 |
|
87 |
|
88 | return command
|
89 |
|
90 |
|
91 | class FlashCommand():
|
92 | def __init__(self):
|
93 | self.data = []
|
94 | self.byteCount = 0;
|
95 | self.recordType = 0;
|
96 | self.address = [];
|
97 | self.checkSum = 0;
|
98 | self.raw = ""
|
99 |
|
100 |
|
101 |
|
102 | def generateNodeFile(outputFile, buffer):
|
103 | print ("Generating File with buffer length: " + str(len(buffer)))
|
104 | outputFile.write("module.exports=" + str(buffer) + ";\n")
|
105 | outputFile.close();
|
106 | print ("All Finished!")
|
107 |
|
108 | def main():
|
109 |
|
110 | args = parseCLArguments()
|
111 |
|
112 |
|
113 | buf = extractHexData(args.input)
|
114 |
|
115 |
|
116 | generateNodeFile(args.output, buf)
|
117 |
|
118 | main() |
\ | No newline at end of file |