chiark / gitweb /
Add "Reload platform" to refresh all the objects on the platform from their files
[cura.git] / Cura / avr_isp / intelHex.py
1 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
2 import io
3
4 def readHex(filename):
5         data = []
6         extraAddr = 0
7         f = io.open(filename, "r")
8         for line in f:
9                 line = line.strip()
10                 if line[0] != ':':
11                         raise Exception("Hex file has a line not starting with ':'")
12                 recLen = int(line[1:3], 16)
13                 addr = int(line[3:7], 16) + extraAddr
14                 recType = int(line[7:9], 16)
15                 if len(line) != recLen * 2 + 11:
16                         raise Exception("Error in hex file: " + line)
17                 checkSum = 0
18                 for i in xrange(0, recLen + 5):
19                         checkSum += int(line[i*2+1:i*2+3], 16)
20                 checkSum &= 0xFF
21                 if checkSum != 0:
22                         raise Exception("Checksum error in hex file: " + line)
23                 
24                 if recType == 0:#Data record
25                         while len(data) < addr + recLen:
26                                 data.append(0)
27                         for i in xrange(0, recLen):
28                                 data[addr + i] = int(line[i*2+9:i*2+11], 16)
29                 elif recType == 1:      #End Of File record
30                         pass
31                 elif recType == 2:      #Extended Segment Address Record
32                         extraAddr = int(line[9:13], 16) * 16
33                 else:
34                         print(recType, recLen, addr, checkSum, line)
35         f.close()
36         return data