Skip to content

Commit

Permalink
Don't use the db's GenSigStartValue to set (#795)
Browse files Browse the repository at this point in the history
individual signal's start values.  That could
make them be set outside of the min / max
values they have.
  • Loading branch information
jmailloux authored Jun 5, 2024
1 parent c1a7eb5 commit db8298d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
24 changes: 12 additions & 12 deletions src/canmatrix/canmatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def raw2phys(self, value, decode_to_str=False):
"""
if self.is_float:
value = self.float_factory(value)

if decode_to_str:
for value_key, value_string in self.values.items():
if value_key == value:
Expand Down Expand Up @@ -920,7 +920,7 @@ class Frame(object):
attributes = attr.ib(factory=dict) # type: typing.MutableMapping[str, typing.Any]
receivers = attr.ib(factory=list) # type: typing.MutableSequence[str]
signalGroups = attr.ib(factory=list) # type: typing.MutableSequence[SignalGroup]

cycle_time = attr.ib(default=0) # type: int

is_j1939 = attr.ib(default=False) # type: bool
Expand All @@ -932,7 +932,7 @@ class Frame(object):

header_id = attr.ib(default=None) # type: int
endpoints = attr.ib(default=None) # type: typing.MutableSequence[Endpoint]

secOC_properties = attr.ib(default=None) # type: Optional[AutosarSecOCProperties]

@property
Expand Down Expand Up @@ -1439,7 +1439,7 @@ def bitstring_to_signal_list(signals, big, little, size):

return unpacked

def unpack(self, data: bytes,
def unpack(self, data: bytes,
allow_truncated: bool = False,
allow_exceeded: bool = False,
) -> typing.Mapping[str, DecodedSignal]:
Expand All @@ -1466,7 +1466,7 @@ def unpack(self, data: bytes,
if allow_exceeded:
# trim the payload data to match the expected size
data = data[:self.size]

if len(data) != self.size:
# return None
raise DecodingFrameLength(
Expand Down Expand Up @@ -1614,7 +1614,7 @@ def decode(self, data):

else:
return decoded

def _compress_little(self):
for signal in self.signals:
if not signal.is_little_endian:
Expand All @@ -1635,7 +1635,7 @@ def _compress_little(self):
gap_len += 1
else:
if gap_len is not None:
signal = layout[bit_nr][0]
signal = layout[bit_nr][0]
signal.start_bit -= gap_len
gap_found = True
break
Expand All @@ -1657,7 +1657,7 @@ def compress(self):
free_start = bit_nr
else:
if free_start is not None:
signal = layout[bit_nr][0]
signal = layout[bit_nr][0]
signal.start_bit = free_start
gap_found = True
break
Expand Down Expand Up @@ -1970,7 +1970,7 @@ def frame_by_id(self, arbitration_id): # type: (ArbitrationId) -> typing.Union[
# found ID while ignoring extended or standard
return test
return None

def frame_by_header_id(self, header_id): # type: (HeaderId) -> typing.Union[Frame, None]
"""Get Frame by its Header id.
Expand Down Expand Up @@ -2016,7 +2016,7 @@ def frame_by_name(self, name): # type: (str) -> typing.Union[Frame, None]
if test.name == name:
return test
return None

def get_frame_by_name(self, name): # type: (str) -> typing.Union[Frame, None]
"""Get Frame by name.
Expand Down Expand Up @@ -2360,8 +2360,8 @@ def set_fd_type(self) -> None:
if frame.size > 8:
frame.is_fd = True

def encode(self,
frame_id: ArbitrationId,
def encode(self,
frame_id: ArbitrationId,
data: typing.Mapping[str, typing.Any]
) -> bytes:
"""Return a byte string containing the values from data packed
Expand Down
13 changes: 2 additions & 11 deletions src/canmatrix/formats/dbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,7 @@ def dump(in_db, f, **options):
if signal.cycle_time != 0:
signal.add_attribute("GenSigCycleTime", signal.cycle_time)
if "GenSigStartValue" in db.signal_defines:
if signal.phys2raw(None) != 0:
if db.signal_defines["GenSigStartValue"].defaultValue is not None and \
float(signal.initial_value) != float(db.signal_defines["GenSigStartValue"].defaultValue):
signal.add_attribute("GenSigStartValue", signal.phys2raw(float(db.signal_defines["GenSigStartValue"].defaultValue)))
elif db.signal_defines["GenSigStartValue"].defaultValue is None:
signal.add_attribute("GenSigStartValue", signal.phys2raw(None))
signal.add_attribute("GenSigStartValue", signal.phys2raw(None))

name = normalized_names[signal]
if compatibility:
Expand Down Expand Up @@ -955,11 +950,7 @@ def add_frame_by_id(new_frame): # type: (canmatrix.Frame) -> None
# frame.extended = 1

for signal in frame.signals:
if "GenSigStartValue" in db.signal_defines \
and db.signal_defines["GenSigStartValue"].defaultValue is not None:
default_value = signal.phys2raw(float_factory(db.signal_defines["GenSigStartValue"].defaultValue))
else:
default_value = signal.phys2raw(None)
default_value = signal.phys2raw(None)
gen_sig_start_value = float_factory(signal.attributes.get("GenSigStartValue", default_value))
signal.initial_value = (gen_sig_start_value * signal.factor) + signal.offset
signal.cycle_time = int(signal.attributes.get("GenSigCycleTime", 0))
Expand Down

0 comments on commit db8298d

Please sign in to comment.