8000 Viewer fixes (#1118) · projectgus/python-can@2d79b51 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2d79b51

Browse files
authored
Viewer fixes (hardbyte#1118)
* The parsed arguments should be parsed to "_create_bus" and not the channel This was introduced in: 49a42b6 * Put the verbosity flag into the "optional" argument group * Clear the old data bytes when the length of the new message is shorter in the viewer script This bug could easily be replicated by sending: cansend vcan0 123#0102 Followed by: cansend vcan0 123#01
1 parent 44bd901 commit 2d79b51

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

can/viewer.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,16 @@ def draw_can_bus_mess 10000 age(self, msg, sorting=False):
188188
elif msg.dlc != self.ids[key]["msg"].dlc:
189189
length_changed = True
190190

191+
# Clear the old data bytes when the length of the new message is shorter
192+
if msg.dlc < self.ids[key]["msg"].dlc:
193+
self.draw_line(
194+
self.ids[key]["row"],
195+
# Start drawing at the last byte that is not in the new message
196+
52 + msg.dlc * 3,
197+
# Draw spaces where the old bytes were drawn
198+
" " * ((self.ids[key]["msg"].dlc - msg.dlc) * 3 - 1),
199+
)
200+
191201
if new_id_added or length_changed:
192202
# Increment the index if it was just added, but keep it if the length just changed
193203
row = len(self.ids) + 1 if new_id_added else self.ids[key]["row"]
@@ -441,7 +451,7 @@ def parse_args(args):
441451
choices=sorted(can.VALID_INTERFACES),
442452
)
443453

444-
parser.add_argument(
454+
optional.add_argument(
445455
"-v",
446456
action="count",
447457
dest="verbosity",
@@ -515,7 +525,7 @@ def main() -> None:
515525
parsed_args, can_filters, data_structs = parse_args(sys.argv[1:])
516526

517527
additional_config = {"can_filters": can_filters} if can_filters else {}
518-
bus = _create_bus(parsed_args.channel, **additional_config)
528+
bus = _create_bus(parsed_args, **additional_config)
519529
# print(f"Connected to {bus.__class__.__name__}: {bus.channel_info}")
520530

521531
curses.wrapper(CanViewer, bus, data_structs)

test/test_viewer.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import struct
3131
import time
3232
import unittest
33+
from collections import defaultdict
3334
from typing import Dict, Tuple, Union
3435
from unittest.mock import patch
3536

@@ -53,6 +54,7 @@
5354
class StdscrDummy:
5455
def __init__(self):
5556
self.key_counter = 0
57+
self.draw_buffer = defaultdict(dict)
5658

5759
@staticmethod
5860
def clear():
@@ -65,13 +67,18 @@ def erase():
6567
@staticmethod
6668
def getmaxyx():
6769
# Set y-value, so scrolling gets tested
68-
return 1, 1
70+
# Set x-value, so the text will fit in the window
71+
return 1, 100
6972

70-
@staticmethod
71-
def addstr(row, col, txt, *args):
73+
def addstr(self, row, col, txt, *args):
7274
assert row >= 0
7375
assert col >= 0
7476
assert txt is not None
77+
78+
# Save the text written into the buffer
79+
for i, t in enumerate(txt):
80+
self.draw_buffer[row][col + i] = t
81+
7582
# Raise an exception 50 % of the time, so we can make sure the code handles it
7683
if random.random() < 0.5:
7784
raise curses.error
@@ -114,7 +121,7 @@ def setUpClass(cls):
114121
random.seed(0)
115122

116123
def setUp(self):
117-
stdscr = StdscrDummy()
124+
self.stdscr_dummy = StdscrDummy()
118125
config = {"interface": "virtual", "receive_own_messages": True}
119126
bus = can.Bus(**config)
120127
data_structs = None
@@ -145,7 +152,7 @@ def setUp(self):
145152
patch_resizeterm.start()
146153
self.addCleanup(patch_resizeterm.stop)
147154

148-
self.can_viewer = CanViewer(stdscr, bus, data_structs, testing=True)
155+
self.can_viewer = CanViewer(self.stdscr_dummy, bus, data_structs, testing=True)
149156

150157
def tearDown(self):
151158
# Run the viewer after the test, this is done, so we can receive the CAN-Bus messages and make sure that they
@@ -213,6 +220,11 @@ def test_receive(self):
213220
if _id["msg"].arbitration_id == 0x101:
214221
# Check if the counter is reset when the length has changed
215222
self.assertEqual(_id["count"], 1)
223+
224+
# Make sure the line has been cleared after the shorted message was send
225+
for col, v in self.stdscr_dummy.draw_buffer[_id["row"]].items():
226+
if col >= 52 + _id["msg"].dlc * 3:
227+
self.assertEqual(v, " ")
216228
elif _id["msg"].arbitration_id == 0x123456:
217229
# Check if the counter is incremented
218230
if _id["dt"] == 0:

0 commit comments

Comments
 (0)
0