Skip to content

Commit

Permalink
rename findNotUsedBits in get_frame_layout (#254)
Browse files Browse the repository at this point in the history
* rename findNotUsedBits in get_frame_layout
let get_frame_layout create array for each bit containing signals using this bit
fix createDummySignals to use  get_frame_layout
rename createDummySignals to create_dummy_signals
* store signal (object references) instead of signal names
  • Loading branch information
ebroecker authored Dec 4, 2018
1 parent e556461 commit 4bf63de
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 56 deletions.
Binary file modified examples/cmTemplate.xlsx
Binary file not shown.
86 changes: 38 additions & 48 deletions src/canmatrix/canmatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,70 +809,60 @@ def calcDLC(self):
maxBit = sig.getStartbit() + int(sig.size)
self.size = max(self.size, int(math.ceil(maxBit / 8)))

def findNotUsedBits(self):
def get_frame_layout(self):
"""
Find unused bits in frame.
get layout of frame.
Represents the bit usage in the frame by means of a list with 64 items.
Every item represents one bit and contains unique number for each signal, occupying that bit.
Numbering starts from one.
Represents the bit usage in the frame by means of a list with n items (n bits of frame length).
Every item represents one bit and contains a list of signals (object refs) with each signal, occupying that bit.
Bits with empty list are unused.
Bits with "zero" index are unused.
Example: [2, 2, 2, 1, 1, 0, 0, 3, 3, 3, 3, 0, 0, ...]
:return: list with signal "index plus one" in every bit. Zeros mean 'unused'.
:rtype: list of int
Example: [[], [], [], [sig1], [sig1], [sig1, sig5], [sig2, sig5], [sig2], []]
:return: list of lists with signalnames
:rtype: list of lists
"""
bitfield = []
bitfieldLe = []
bitfieldBe = []

for i in range(0,64):
bitfieldBe.append(0)
bitfieldLe.append(0)
bitfield.append(0)
i = 0

for sig in self.signals:
i += 1
for bit in range(sig.getStartbit(), sig.getStartbit() + int(sig.size)):
if sig.is_little_endian:
bitfieldLe[bit] = i
else:
bitfieldBe[bit] = i
little_bits = [[] for _dummy in range((self.size * 8))]
big_bits = [[] for _dummy in range((self.size * 8))]
for signal in self.signals:
if signal.is_little_endian:
least = len(little_bits) - signal.startBit
most = least - signal.size
for little_bit_signals in little_bits[most:least]:
little_bit_signals.append(signal)

for i in range(0,8):
for j in range(0,8):
bitfield[i*8+j] = bitfieldLe[i*8+(7-j)]
else:
most = signal.startBit
least = most + signal.size
for big_bit_signals in big_bits[most:least]:
big_bit_signals.append(signal)

for i in range(0,8):
for j in range(0,8):
if bitfield[i*8+j] == 0:
bitfield[i*8+j] = bitfieldBe[i*8+j]
little_bits = reversed(tuple(grouper(little_bits, 8)))
little_bits = tuple(chain(*little_bits))

returnList = [
little + big
for little, big in zip(little_bits, big_bits)
]

return bitfield
return returnList

def createDummySignals(self):
def create_dummy_signals(self):
"""Create big-endian dummy signals for unused bits.
Names of dummy signals are *_Dummy_<frame.name>_<index>*
"""
bitfield = self.findNotUsedBits()
# for i in range(0,8):
# print (bitfield[(i)*8:(i+1)*8])
bitfield = self.get_frame_layout()
startBit = -1
sigCount = 0
for i in range(0,64):
if bitfield[i] == 0 and startBit == -1:
startBit = i
if (i == 63 or bitfield[i] != 0) and startBit != -1:
if i == 63:
i = 64
self.addSignal(Signal("_Dummy_%s_%d" % (self.name,sigCount),size=i-startBit, startBit=startBit, is_little_endian = False))
for index, bit_signals in enumerate(bitfield):
if bit_signals == [] and startBit == -1:
startBit = index
if (index == (len(bitfield)-1) or bit_signals != []) and startBit != -1:
if index == (len(bitfield)-1):
index = len(bitfield)
self.addSignal(Signal("_Dummy_%s_%d" % (self.name,sigCount),size=index-startBit, startBit=startBit, is_little_endian = False))
startBit = -1
sigCount +=1
sigCount += 1



Expand Down
18 changes: 10 additions & 8 deletions src/canmatrix/tests/test_canmatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,20 +439,22 @@ def test_frame_compute_dlc():

def test_frame_find_unused_bits():
frame = canmatrix.canmatrix.Frame(size=1)
frame.addSignal(canmatrix.canmatrix.Signal(startBit=0, size=3))
frame.addSignal(canmatrix.canmatrix.Signal(startBit=4, size=2))
bit_usage = frame.findNotUsedBits()
assert bit_usage.count(0) == 64 - 3 - 2
assert bit_usage[:8] == [0, 0, 2, 2, 0, 1, 1, 1]
frame.addSignal(canmatrix.canmatrix.Signal(name="sig1",startBit=0, size=3))
frame.addSignal(canmatrix.canmatrix.Signal(name="sig2",startBit=4, size=2))
bit_usage = frame.get_frame_layout()
assert bit_usage.count([]) == frame.size*8 - 3 - 2
sig1 = frame.signalByName("sig1")
sig2 = frame.signalByName("sig2")
assert bit_usage == [[], [], [sig2], [sig2], [], [sig1], [sig1], [sig1]]


def test_frame_create_dummy_signals_covers_all_bits():
frame = canmatrix.canmatrix.Frame(size=1)
frame.addSignal(canmatrix.canmatrix.Signal(startBit=0, size=3))
frame.addSignal(canmatrix.canmatrix.Signal(startBit=4, size=2))
frame.createDummySignals()
assert len(frame.signals) == 2 + 3
assert frame.findNotUsedBits().count(0) == 0
frame.create_dummy_signals()
assert len(frame.signals) == 2 + 2
assert frame.get_frame_layout().count([]) == 0


def test_frame_update_receivers():
Expand Down

0 comments on commit 4bf63de

Please sign in to comment.