serialdata/uart_filter.py

Sun, 25 Oct 2020 17:12:22 +0100

author
David Douard <david.douard@sdf3.org>
date
Sun, 25 Oct 2020 17:12:22 +0100
changeset 31
794401fbd5d5
parent 23
daf26b083899
permissions
-rw-r--r--

Add a few patterns in hgignore

23
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
1 import sys
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
2 import re
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
3 import click
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
4
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
5 F = 1e6
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
6
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
7 reg = re.compile(r'^(?P<fr1>\d+)-(?P<fr2>\d+) .*: (?P<rxtx>rx|tx)-data: "(?P<value>..)"$') # noqa
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
8
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
9
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
10 class ProtocolError(Exception):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
11 pass
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
12
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
13 class ProtocolReset(Exception):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
14 pass
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
15
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
16 pkts = []
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
17
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
18
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
19 class Packet:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
20 def __init__(self, frame, rxtx, value):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
21 self.frame = frame
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
22 self.rxtx = rxtx
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
23 self.value = value
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
24
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
25 @property
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
26 def timestamp(self):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
27 return self.frame / F * 1000
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
28
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
29 @property
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
30 def relts(self):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
31 return self.timestamp - pkts[0].timestamp
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
32
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
33 def __str__(self):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
34 return '[%06.2f] %s: %02X' % (self.timestamp, self.rxtx, self.value)
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
35
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
36
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
37 def read_line(seq):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
38 for line in seq:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
39 m = reg.match(line)
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
40 if m:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
41 frame = int(m.group('fr1'))
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
42 v = int(m.group('value'), base=16)
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
43 rxtx = m.group('rxtx')
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
44 pkt = Packet(frame, rxtx, v)
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
45 pkts.append(pkt)
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
46 yield pkt
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
47
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
48
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
49 def read_input(stream=None):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
50 if stream is None:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
51 stream = sys.stdin
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
52 for line in stream:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
53 yield line
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
54
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
55
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
56 def print_input():
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
57 t0 = None
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
58 for pkt in read_line(read_input()):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
59 c = chr(pkt.value) if 32 <= pkt.value <= 127 else ''
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
60 if t0 is None:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
61 t0 = pkt.timestamp
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
62 cnxdir = 'CPU-->DP' if pkt.rxtx == 'rx' else 'CPU<--DP'
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
63 print('[%s] %06.1f: %02X # %s' % (
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
64 cnxdir, (pkt.timestamp-t0), pkt.value, c))
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
65
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
66
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
67 def flipdir(rxtx):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
68 return 'rx' if rxtx == 'tx' else 'tx'
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
69
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
70
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
71 def check_rxtx(cur, pkt):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
72 if cur.rxtx == pkt.rxtx:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
73 raise ProtocolError('Expected %s transmission %02X %02X' %
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
74 (flipdir(cur.rxtx), cur.value, pkt.value))
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
75
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
76
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
77 def check_sot_ack(ack, sot):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
78 check_rxtx(ack, sot)
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
79 if ack.value != (0xFF - sot.value):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
80 if ack.value == 0x66:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
81 raise ProtocolReset()
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
82 raise ProtocolError('Expected ACK value %02X, got %02X' %
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
83 (0xFF - sot.value, ack.value))
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
84
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
85
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
86 def wait_for_sot(seq, sot_pkt=None):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
87 if sot_pkt is None:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
88 for sot_pkt in seq:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
89 if sot_pkt.value not in SOT_VALUES:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
90 print('Unexpected value %s: %02X' %
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
91 (sot_pkt.rxtx, sot_pkt.value))
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
92 else:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
93 break
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
94 for ack_pkt in seq:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
95 try:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
96 check_sot_ack(sot_pkt, ack_pkt)
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
97 # print('New packet %s' % sot_pkt)
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
98 return sot_pkt, ack_pkt
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
99 except ProtocolReset:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
100 # print('reset')
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
101 sot_pkt = ack_pkt
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
102 raise StopIteration()
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
103
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
104
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
105 SOT_VALUES = [0x33, 0x66]
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
106
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
107
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
108 def recv_packet(seq):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
109 sot_pkt = None
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
110 while True:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
111 sot_pkt, ack_pkt = wait_for_sot(seq, sot_pkt)
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
112 payload = []
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
113 recv_char = None
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
114 may_stop = False
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
115
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
116 cur_pkt = ack_pkt
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
117 for pkt in seq:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
118 if pkt.value == 0x66:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
119 sot_pkt = pkt
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
120 break
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
121
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
122 check_rxtx(pkt, cur_pkt)
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
123 if recv_char is None:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
124 if pkt.rxtx != sot_pkt.rxtx:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
125 raise ProtocolError()
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
126 recv_char = pkt.value
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
127 cur_pkt = pkt
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
128 if may_stop:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
129 if recv_char == 0x55:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
130 cur_pkt = None
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
131 sot_pkt = None
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
132 break
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
133 else:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
134 if pkt.rxtx == sot_pkt.rxtx:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
135 raise ProtocolError()
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
136 if pkt.value == 0x00:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
137 payload.append(recv_char)
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
138 if check_payload(sot_pkt, payload):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
139 may_stop = True
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
140 yield (sot_pkt, payload)
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
141 else:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
142 may_stop = False
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
143 elif pkt.value != 0xFF:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
144 # if FF, ignore the char (ie. its a NACK, so resend)
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
145 raise ProtocolError('Not really but hey %02X [%02X] %s' %
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
146 (pkt.value, recv_char, payload))
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
147 recv_char = None
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
148 cur_pkt = pkt
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
149 else:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
150 break
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
151
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
152
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
153 def check_payload(sot, payload):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
154 if sot.rxtx == 'tx':
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
155 if sot.value == 0x33:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
156 return len(payload) == 2
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
157 else:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
158 return len(payload) == 1
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
159 else:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
160 if len(payload) > 1:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
161 return len(payload) == (payload[1]+2)
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
162 return False
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
163
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
164
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
165 KEYCODES = {
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
166 0x00: 'View',
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
167 0x01: 'Mon',
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
168 0x02: 'Sto/Rcl',
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
169 0x03: 'Scan',
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
170 0x04: 'Alarm',
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
171 0x05: 'Mx+B',
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
172 0x06: 'Measure',
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
173 0x07: 'Interval',
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
174 0x08: 'Card Reset',
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
175 0x09: 'Close',
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
176 0x0A: 'Open',
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
177 0x0B: 'Read',
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
178 0x0C: 'Shift',
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
179 0x0D: 'Write',
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
180 0x0E: 'Left',
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
181 0x0F: 'Right',
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
182 0x10: 'Advanced',
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
183 0x11: 'Step',
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
184 0x80: 'Knob right',
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
185 0x81: 'Knob left',
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
186 }
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
187
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
188
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
189 FLAGS = [
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
190 ['<Bell>', 'Mx+B', 'Ch. frame', 'Channel', 'LO', 'Alarm', 'HI', 'Al. frame'],
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
191 ['2', '4', '3', '1', '4W', 'OC', '', 'AVG'],
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
192 ['MAX', 'MIN', 'LAST', 'MEM', '', 'ONCE', 'EXT', 'ERROR'],
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
193 ['REMOTE', 'ADRS', '*', 'VIEW', 'MON', 'SCAN', 'CONFIG', '']
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
194 ]
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
195
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
196
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
197 def flag(payload):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
198 flags = []
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
199 for flg, byte in zip(FLAGS, payload[2:]):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
200 for i in range(8):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
201 if byte & 2**i:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
202 flags.append(flg[i])
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
203 return ','.join(flags)
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
204
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
205
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
206 def flag_by_num(num):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
207 for b in FLAGS:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
208 for f in b:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
209 if num == 0:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
210 return f
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
211 num -= 1
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
212
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
213 CMDS = {
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
214 0x00: ('DISPLAY ', lambda pl: ''.join(chr(x) for x in pl[2:])),
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
215 0x0C: ('CHANNEL ', lambda pl: ''.join(chr(x) for x in pl[2:])),
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
216 0x0A: ('FLAGS ', flag),
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
217 0x01: ('UNSHIFT ', None),
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
218 0x86: ('SHUTDOWN', None),
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
219 0x0D: ('CHAR LO ', lambda pl: str(pl[2])),
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
220 0x08: ('FLAG LO ', lambda pl: flag_by_num(pl[2])),
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
221 0x09: ('FLAG HI ', lambda pl: flag_by_num(pl[2])),
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
222 0x02: ('RST? ', None),
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
223 }
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
224
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
225
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
226 def parse_packet(sot, payload):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
227 if sot.value == 0x33:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
228 return 'INIT KEY=%s' % KEYCODES.get(payload[1], 'None')
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
229 if sot.rxtx == 'tx':
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
230 kc = payload[0]
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
231 if kc in (0x80, 0x81):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
232 return 'KNOB\t\t%s' % click.style(KEYCODES.get(kc), fg='magenta')
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
233 if kc & 0x40:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
234 kc = kc & 0xBF
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
235 event = 'RELEASED'
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
236 else:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
237 event = 'PRESSED '
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
238 return 'KEY %s\t%s' % (click.style(event, fg='green'),
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
239 click.style(KEYCODES.get(kc), fg='cyan'))
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
240 lbl, dspfunc = CMDS[payload[0]]
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
241 if dspfunc:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
242 lbl += '\t%s' % click.style(dspfunc(payload), fg='green')
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
243 return lbl
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
244
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
245
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
246 @click.command()
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
247 @click.option('-p', '--packets/--no-packets', default=False)
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
248 def main(packets):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
249 if packets:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
250 try:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
251 for sot, payload in recv_packet(read_line(read_input())):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
252 cnxdir = 'CPU=>DP' if sot.rxtx == 'rx' else 'CPU<-DP'
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
253 print('[%s]: %02X=[%s]' %
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
254 (cnxdir, sot.value,
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
255 ','.join('%02x' % x for x in payload)))
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
256 except:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
257 print('\n'.join(str(x) for x in pkts[-10:]))
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
258 raise
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
259 else:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
260 try:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
261 t0 = None
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
262 for sot, payload in recv_packet(read_line(read_input())):
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
263 cnxdir = '>>>' if sot.rxtx == 'rx' else '<<<'
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
264 if t0 is None:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
265 t0 = sot.timestamp
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
266 click.secho('%08.0f ' % sot.relts,
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
267 fg='yellow', nl=False)
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
268 click.secho('[+%04.2f] ' % ((sot.timestamp - t0)/1000.),
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
269 fg='blue', nl=False)
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
270 click.secho(cnxdir + ' ', fg='red', nl=False)
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
271 click.secho(parse_packet(sot, payload), fg='white')
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
272 t0 = sot.timestamp
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
273 except:
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
274 print('\n'.join(str(x) for x in pkts[-10:]))
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
275 raise
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
276
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
277
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
278 if __name__ == '__main__':
daf26b083899 Add the serialdata tool
David Douard <david.douard@logilab.fr>
parents:
diff changeset
279 main()

mercurial