forked from hardbyte/python-can
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharbitrationid.py
More file actions
60 lines (49 loc) · 1.69 KB
/
Copy patharbitrationid.py
File metadata and controls
60 lines (49 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from can.protocols.j1939.pgn import PGN
class ArbitrationID(object):
def __init__(self, priority=7, pgn=None, source_address=0):
"""
:param int priority:
Between 0 and 7, where 0 is highest priority.
:param :class:`can.protocols.j1939.PGN`/int pgn:
The parameter group number.
:param int source_address:
Between 0 and 255.
"""
self.priority = priority
self.pgn = pgn
self.source_address = source_address
@property
def can_id(self):
return (self.source_address + (self.pgn.value << 8) + (self.priority << 26))
@can_id.setter
def can_id(self, value):
"""
Int between 0 and (2**29) - 1
"""
self.priority = (value & 0x1C000000) >> 26
self.pgn.value = (value & 0x03FFFF00) >> 8
self.source_address = value & 0x000000FF
@property
def destination_address(self):
if self.pgn.is_destination_specific:
return self.pgn.pdu_specific
else:
return None
@property
def pgn(self):
return self._pgn
@pgn.setter
def pgn(self, other):
if other is None:
self._pgn = PGN()
elif not isinstance(other, PGN):
self._pgn = PGN.from_value(other)
else:
self._pgn = other
def __str__(self):
if self.destination_address is not None:
retval = "PRI=%d PGN=%6s DST=0x%.2x SRC=0x%.2x" % (
self.priority, self.pgn, self.destination_address, self.source_address)
else:
retval = "PRI=%d PGN=%6s SRC=0x%.2x" % (self.priority, self.pgn, self.source_address)
return retval