|
1 | 1 | import cantools |
| 2 | +from cantools.subparsers.dump import formatting |
2 | 3 |
|
3 | 4 | # Load the DBC file |
4 | 5 | db = cantools.database.load_file("uart_com.dbc") |
@@ -42,50 +43,14 @@ def safe(value): |
42 | 43 | ] |
43 | 44 |
|
44 | 45 | def format_bit_matrix(msg): |
45 | | - bit_width = 20 |
46 | | - byte_label_width = 3 |
47 | | - bitfield = [None] * (msg.length * 8) |
48 | | - |
49 | | - for signal in msg.signals: |
50 | | - if signal.byte_order == "little_endian": |
51 | | - bit_indices = list(range(signal.start, signal.start + signal.length)) |
52 | | - else: |
53 | | - bit_indices = list(range(signal.start, signal.start - signal.length, -1)) |
54 | | - |
55 | | - for idx in bit_indices: |
56 | | - if 0 <= idx < len(bitfield): |
57 | | - bitfield[idx] = signal.name |
58 | | - |
59 | | - total_bits = msg.length * 8 |
60 | | - num_rows = total_bits // 8 |
61 | | - lines = [] |
62 | | - |
63 | | - # Header with byte index |
64 | | - border = " +" + "+".join(["-" * bit_width] * 8) + "+" |
65 | | - header = " |" + "|".join([f"{7 - i:^{bit_width}}" for i in range(8)]) + "|" |
66 | | - lines.extend([border, header, border]) |
67 | | - |
68 | | - for row in range(num_rows): |
69 | | - base = row * 8 |
70 | | - seen = set() |
71 | | - row_cells = [] |
72 | | - for i in range(8): |
73 | | - bit_index = base + (7 - i) |
74 | | - label = bitfield[bit_index] |
75 | | - if label and label not in seen: |
76 | | - cell = label[:bit_width].center(bit_width) |
77 | | - seen.add(label) |
78 | | - else: |
79 | | - cell = " " * bit_width |
80 | | - row_cells.append(cell) |
81 | | - lines.append(f"{row:>3}|" + "|".join(row_cells) + "|") |
82 | | - lines.append(border) |
83 | | - |
84 | 46 | rst = "\n**Bitfield Layout**\n\n::\n\n" |
85 | | - rst += "\n".join(lines) + "\n" |
| 47 | + rst += '\n'.join([ |
| 48 | + (' ' + line).rstrip() |
| 49 | + for line in formatting.layout_string(msg).splitlines() |
| 50 | + ]) |
| 51 | + rst += "\n\n\n" |
86 | 52 | return rst |
87 | 53 |
|
88 | | - |
89 | 54 | # Format one message block as RST |
90 | 55 | def format_message_rst(msg): |
91 | 56 | rst = f"{msg.name}\n{'^' * len(msg.name)}\n\n" |
|
0 commit comments