7
7
import cereal .messaging as messaging
8
8
from tools .lib .logreader import logreader_from_route_or_segment
9
9
10
+ RED = '\033 [91m'
11
+ CLEAR = '\033 [0m'
10
12
11
- def update (msgs , bus , low_to_high , high_to_low , quiet = False ):
13
+ def update (msgs , bus , dat , low_to_high , high_to_low , quiet = False ):
12
14
for x in msgs :
13
15
if x .which () != 'can' :
14
16
continue
15
17
16
18
for y in x .can :
17
19
if y .src == bus :
18
- i = int . from_bytes ( y .dat , byteorder = 'big' )
20
+ dat [ y . address ] = y .dat
19
21
22
+ i = int .from_bytes (y .dat , byteorder = 'big' )
20
23
l_h = low_to_high [y .address ]
21
24
h_l = high_to_low [y .address ]
22
25
@@ -34,35 +37,51 @@ def update(msgs, bus, low_to_high, high_to_low, quiet=False):
34
37
35
38
36
39
def can_printer (bus = 0 , init_msgs = None , new_msgs = None ):
37
- logcan = messaging .sub_sock ('can' )
40
+ logcan = messaging .sub_sock ('can' , timeout = 10 )
38
41
42
+ dat = defaultdict (int )
39
43
low_to_high = defaultdict (int )
40
44
high_to_low = defaultdict (int )
41
45
42
46
if init_msgs is not None :
43
- update (init_msgs , bus , low_to_high , high_to_low , quiet = True )
47
+ update (init_msgs , bus , dat , low_to_high , high_to_low , quiet = True )
48
+
49
+ low_to_high_init = low_to_high .copy ()
50
+ high_to_low_init = high_to_low .copy ()
44
51
45
52
if new_msgs is not None :
46
- update (new_msgs , bus , low_to_high , high_to_low )
53
+ update (new_msgs , bus , dat , low_to_high , high_to_low )
47
54
else :
48
55
# Live mode
49
- while 1 :
50
- can_recv = messaging .drain_sock (logcan , wait_for_one = True )
51
- update (can_recv , bus , low_to_high , high_to_low )
56
+ try :
57
+ while 1 :
58
+ can_recv = messaging .drain_sock (logcan )
59
+ update (can_recv , bus , dat , low_to_high , high_to_low )
60
+ time .sleep (0.02 )
61
+ except KeyboardInterrupt :
62
+ pass
63
+
64
+ print ("\n \n " )
65
+ for addr in sorted (dat .keys ()):
66
+ init = low_to_high_init [addr ] & high_to_low_init [addr ]
67
+ now = low_to_high [addr ] & high_to_low [addr ]
68
+ d = now & ~ init
69
+ if d == 0 :
70
+ continue
71
+ b = d .to_bytes (len (dat [addr ]), byteorder = 'big' )
72
+ byts = '' .join ([(c if c == '0' else f'{ RED } { c } { CLEAR } ' ) for c in str (binascii .hexlify (b ))[2 :- 1 ]])
73
+ print (f"{ hex (addr ).ljust (6 )} ({ str (addr ).ljust (4 )} )" , byts )
52
74
53
75
54
76
if __name__ == "__main__" :
55
77
desc = """Collects messages and prints when a new bit transition is observed.
56
78
This is very useful to find signals based on user triggered actions, such as blinkers and seatbelt.
57
79
Leave the script running until no new transitions are seen, then perform the action."""
58
-
59
80
parser = argparse .ArgumentParser (description = desc ,
60
81
formatter_class = argparse .ArgumentDefaultsHelpFormatter )
61
-
62
82
parser .add_argument ("--bus" , type = int , help = "CAN bus to print out" , default = 0 )
63
-
64
- parser .add_argument ("init" , type = str , help = "Route or segment to initialize with" )
65
- parser .add_argument ("comp" , type = str , help = "Route or segment to compare against init" )
83
+ parser .add_argument ("init" , type = str , nargs = '?' , help = "Route or segment to initialize with" )
84
+ parser .add_argument ("comp" , type = str , nargs = '?' , help = "Route or segment to compare against init" )
66
85
67
86
args = parser .parse_args ()
68
87
0 commit comments