UNPKG

2.8 kBPlain TextView Raw
1#!/usr/bin/python
2
3import sys
4import argparse
5import csv
6
7
8def 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
22def 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 # The first data starts at memory location 0x1000
36 if (beginRecording == False and command.recordType == 0x00 and (command.address == int("1000", 16))):
37
38 # Tell the loop to start recording
39 beginRecording = True;
40
41 # If we have a data command and are ready to start recording
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
50def 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 # Start reading at index 9
66 startIndex = 9;
67
68 # If this command has data
69 if (command.byteCount):
70
71 # Iterate through the data
72 for i in range(command.byteCount):
73
74 # We want to put each two digits together
75 dataIndex = startIndex + (i * 2)
76
77 # Append the two digits converted to a base 16 int
78 command.data.append(int(hexLine[dataIndex : dataIndex + 2], 16))
79
80 # Add the checksum
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
91class 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
102def 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
108def main():
109 # Pull out the command line arguments to get input and output file
110 args = parseCLArguments()
111
112 # Extract data bytes from input file
113 buf = extractHexData(args.input)
114
115 # Open output file, write module with buffer
116 generateNodeFile(args.output, buf)
117
118main()
\No newline at end of file