Skip to content

Commit 1a0e393

Browse files
committed
move j1939 (pgn, prio, source) handling direct to arbitration-id class
(#238)
1 parent 034d0c3 commit 1a0e393

File tree

2 files changed

+40
-40
lines changed

2 files changed

+40
-40
lines changed

src/canmatrix/canmatrix.py

+31-21
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,20 @@ def pgn(self):
613613
_pgn += ps
614614
return _pgn
615615

616+
@pgn.setter
617+
def pgn(self, value): # type: (int) -> None
618+
self.extended = True
619+
ps = value & 0xff
620+
pf = (value >> 8) & 0xFF
621+
_pgn = pf << 8
622+
if pf >= 240:
623+
_pgn += ps
624+
625+
self.id &= 0xff0000ff
626+
self.id |= (_pgn & 0xffff) << 8 # default pgn is None -> mypy reports error
627+
628+
629+
616630
@property
617631
def j1939_tuple(self): # type: () -> typing.Tuple[int, int, int]
618632
"""Get tuple (destination, PGN, source)
@@ -637,6 +651,11 @@ def j1939_source(self):
637651
raise J1939needsExtendedIdetifier
638652
return self.id & 0xFF
639653

654+
@j1939_source.setter
655+
def j1939_source(self, value): # type: (int) -> None
656+
self.extended = True
657+
self.id = (self.id & 0xffffff00) | (value & 0xff)
658+
640659
@property
641660
def j1939_ps(self):
642661
if not self.extended:
@@ -659,7 +678,12 @@ def j1939_edp(self):
659678
def j1939_priority(self):
660679
if not self.extended:
661680
raise J1939needsExtendedIdetifier
662-
return (self.id >> 25) & 0x7
681+
return (self.id >> 26) & 0x7
682+
683+
@j1939_priority.setter
684+
def j1939_priority(self, value): # type: (int) -> None
685+
self.extended = True
686+
self.id = (self.id & 0x2ffffff) | ((value & 0x7) << 26)
663687

664688
@property
665689
def j1939_str(self): # type: () -> str
@@ -733,9 +757,6 @@ class Frame(object):
733757
receivers = attr.ib(factory=list) # type: typing.MutableSequence[str]
734758
signalGroups = attr.ib(factory=list) # type: typing.MutableSequence[SignalGroup]
735759

736-
j1939_pgn = attr.ib(default=0) # type: typing.Optional[int]
737-
j1939_source = attr.ib(default=0) # type: int
738-
j1939_prio = attr.ib(default=0) # type: int
739760
is_j1939 = attr.ib(default=False) # type: bool
740761
# ('cycleTime', '_cycleTime', int, None),
741762
# ('sendType', '_sendType', str, None),
@@ -787,39 +808,28 @@ def pgn(self): # type: () -> int
787808

788809
@pgn.setter
789810
def pgn(self, value): # type: (int) -> None
790-
self.j1939_pgn = value
791-
self.recalc_J1939_id()
792-
self.j1939_pgn = self.arbitration_id.pgn
811+
self.arbitration_id.pgn = value
793812

794813
@property
795814
def priority(self): # type: () -> int
796815
"""Get J1939 priority."""
797-
return self.j1939_prio
816+
return self.arbitration_id.j1939_prio
798817

799818
@priority.setter
800819
def priority(self, value): # type: (int) -> None
801820
"""Set J1939 priority."""
802-
self.j1939_prio = value
803-
self.recalc_J1939_id()
821+
self.arbitration_id.j1939_priority = value
804822

805823
@property
806824
def source(self): # type: () -> int
807825
"""Get J1939 source."""
808-
return self.j1939_source
826+
return self.arbitration_id.j1939_source
809827

810828
@source.setter
811829
def source(self, value): # type: (int) -> None
812830
"""Set J1939 source."""
813-
self.j1939_source = value
814-
self.recalc_J1939_id()
815-
816-
def recalc_J1939_id(self): # type: () -> None
817-
"""Recompute J1939 ID"""
818-
self.arbitration_id.id = self.j1939_source & 0xff
819-
self.arbitration_id.id += (self.j1939_pgn & 0xffff) << 8 # default pgn is None -> mypy reports error
820-
self.arbitration_id.id += (self.j1939_prio & 0x7) << 26
821-
self.arbitration_id.extended = True
822-
self.is_j1939 = True
831+
self.arbitration_id.j1939_source = value
832+
823833

824834
# @property
825835
# def cycleTime(self):

src/canmatrix/tests/test_canmatrix.py

+9-19
Original file line numberDiff line numberDiff line change
@@ -557,32 +557,15 @@ def test_frame_not_multiplexed():
557557
frame.add_signal(canmatrix.canmatrix.Signal(name="some"))
558558
assert not frame.is_multiplexed
559559

560-
def test_j1939_frame_construction():
561-
frame = canmatrix.canmatrix.Frame()
562-
frame.source = 0x22
563-
frame.pgn = 0xAAAA
564-
frame.priority = 3
565-
assert frame.pgn == frame.j1939_pgn
566-
assert frame.source == frame.j1939_source
567-
assert frame.priority == frame.j1939_prio
568-
569560
def test_frame_calc_j1939_id():
570561
# we have to set all j1939 properties in the __init__ otherwise the setters crash
571-
frame = canmatrix.canmatrix.Frame(j1939_source=0x11, j1939_pgn=0xFFFF, j1939_prio=0)
562+
frame = canmatrix.canmatrix.Frame()
572563
frame.source = 0x22
573564
frame.pgn = 0xAAAA
574565
frame.priority = 3
575566
assert hex(frame.arbitration_id.id) == hex(0xcaa0022)
576567

577568

578-
def test_frame_get_j1939_properties():
579-
frame = canmatrix.canmatrix.Frame(j1939_source=0x11, j1939_pgn=0xFFFF, j1939_prio=1)
580-
frame.recalc_J1939_id() # pgn property is computed from id!
581-
assert frame.pgn == frame.j1939_pgn
582-
assert frame.source == frame.j1939_source
583-
assert frame.priority == frame.j1939_prio
584-
585-
586569
def test_frame_add_transmitter(empty_frame):
587570
empty_frame.add_transmitter("BCM")
588571
assert empty_frame.transmitters == ["BCM"]
@@ -822,7 +805,14 @@ def test_arbitration_id_is_instance():
822805
assert frame1.arbitration_id.id == 42
823806
assert frame2.arbitration_id.id == 0
824807

825-
808+
def test_arbitration_id_j1939_direct_setters():
809+
arb_id = canmatrix.ArbitrationId(0)
810+
arb_id.pgn = 0xF1AA
811+
arb_id.j1939_source = 0x22
812+
arb_id.j1939_priority = 3
813+
assert arb_id.pgn == 0xF1AA
814+
assert arb_id.j1939_source == 0x22
815+
assert arb_id.j1939_priority == 3
826816

827817
@pytest.fixture
828818
def empty_matrix():

0 commit comments

Comments
 (0)