Skip to content

Commit

Permalink
Merge pull request #30 from r-downing/master
Browse files Browse the repository at this point in the history
Added min_gap param for segments().
  • Loading branch information
bialix committed Jul 9, 2020
2 parents fa310ae + c491c20 commit 9769d3f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
11 changes: 6 additions & 5 deletions intelhex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,23 +890,24 @@ def merge(self, other, overlap='error'):
elif overlap == 'replace':
self.start_addr = other.start_addr

def segments(self):
def segments(self, min_gap=1):
"""Return a list of ordered tuple objects, representing contiguous occupied data addresses.
Each tuple has a length of two and follows the semantics of the range and xrange objects.
The second entry of the tuple is always an integer greater than the first entry.
@param min_gap the minimum gap size between data in order to separate the segments
"""
addresses = self.addresses()
if not addresses:
return []
elif len(addresses) == 1:
return([(addresses[0], addresses[0]+1)])
adjacent_differences = [(b - a) for (a, b) in zip(addresses[:-1], addresses[1:])]
breaks = [i for (i, x) in enumerate(adjacent_differences) if x > 1]
breaks = [i for (i, x) in enumerate(adjacent_differences) if x > min_gap]
endings = [addresses[b] for b in breaks]
endings.append(addresses[-1])
beginings = [addresses[b+1] for b in breaks]
beginings.insert(0, addresses[0])
return [(a, b+1) for (a, b) in zip(beginings, endings)]
beginnings = [addresses[b+1] for b in breaks]
beginnings.insert(0, addresses[0])
return [(a, b+1) for (a, b) in zip(beginnings, endings)]

def get_memory_size(self):
"""Returns the approximate memory footprint for data."""
Expand Down
7 changes: 7 additions & 0 deletions intelhex/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,13 @@ def test_segments(self):
self.assertEqual(max(sg[0]), 0x102)
self.assertEqual(min(sg[1]), 0x200)
self.assertEqual(max(sg[1]), 0x203)
ih[0x204] = 5
sg = ih.segments()
self.assertEqual(len(sg), 3)
sg = ih.segments(min_gap=2)
self.assertEqual(len(sg), 2)
self.assertEqual(min(sg[1]), 0x200)
self.assertEqual(max(sg[1]), 0x205)
pass

class TestIntelHexLoadBin(TestIntelHexBase):
Expand Down

0 comments on commit 9769d3f

Please sign in to comment.