Skip to content

Commit 661020b

Browse files
author
Avi SZYCHTER
committed
PrintAllFrames now prints a table with a summary of the database
1 parent 43e3258 commit 661020b

File tree

1 file changed

+47
-16
lines changed

1 file changed

+47
-16
lines changed

utils/can-parse/print-frame.cpp

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
#include "operations.h"
22
#include "CANDatabaseAnalysis.h"
33
#include "CANDatabase.h"
4+
#include <algorithm>
5+
#include <iomanip>
6+
7+
/*
8+
* Internal pretty-printing parameters
9+
*/
10+
static const int INT_COL_SIZE = 10;
411

512
void print_info_separator(int level = 0) {
613
static const char* level_0_separator = "##############";
@@ -21,32 +28,24 @@ void print_info_separator(int level = 0) {
2128
std::cout << *separator << std::endl;
2229
}
2330

24-
void print_frame_impl(const CANFrame& frame) {
25-
auto choicesStr = [](const std::map<unsigned int, std::string>& choices) {
26-
std::string result = "";
27-
for(const auto& choice: choices) {
28-
result += std::to_string(choice.first) + " -> \"" + choice.second + "\", ";
29-
}
30-
return result;
31-
};
32-
33-
std::cout << "FRAME[" << frame.name() << "]:\t"
31+
void print_frame_impl(const CANFrame& frame, unsigned name_col_size) {
32+
std::cout << std::left << std::setw(name_col_size) << frame.name() << ":\t"
3433
<< std::hex << std::showbase << frame.can_id() << "/"
35-
<< std::dec << std::noshowbase << frame.dlc() << "/" << std::endl
34+
<< std::dec << std::noshowbase << frame.dlc() << "/"
3635
<< frame.period() << "s" << std::endl;
3736

3837
if(frame.comment().size() > 0) {
39-
std::cout << "FRAME[" << frame.name() << "]:\t \"" << frame.comment() << "\"" << std::endl;
38+
std::cout << "COMMENT" << frame.name() << ":\t \"" << frame.comment() << "\"" << std::endl;
4039
}
4140
}
4241

4342
void print_signal_impl(const CANSignal& sig) {
4443
std::cout << "SIGNAL[" << sig.name() << "]: " << std::endl;
4544
std::cout << "\tstart bit:\t" << sig.start_bit() << std::endl;
46-
std::cout << "\tlength:\t" << sig.length() << std::endl;
47-
std::cout << "\tendianness:\t" << ((sig.endianness() == CANSignal::BigEndian)
45+
std::cout << "\tlength:\t\t" << sig.length() << std::endl;
46+
std::cout << "\tendianness:\t\t" << ((sig.endianness() == CANSignal::BigEndian)
4847
? "BigEndian" : "LittleEndian") << std::endl;
49-
std::cout << "\tsignedness:\t" << ((sig.signedness() == CANSignal::Signed)
48+
std::cout << "\tsignedness:\t\t" << ((sig.signedness() == CANSignal::Signed)
5049
? "Signed" : "Unsigned") << std::endl;
5150
std::cout << "\tscale:\t" << sig.scale() << std::endl;
5251
std::cout << "\toffset:\t" << sig.offset() << std::endl;
@@ -64,11 +63,42 @@ void print_signal_impl(const CANSignal& sig) {
6463
}
6564
}
6665

66+
void print_frame_line(const CANFrame& src, int name_col_size) {
67+
std::cout << std::left
68+
<< std::setw(name_col_size) << src.name()
69+
<< std::setw(INT_COL_SIZE) << std::hex << std::showbase << src.can_id()
70+
<< std::setw(INT_COL_SIZE) << std::dec << src.dlc()
71+
<< std::setw(INT_COL_SIZE) << std::to_string(src.period()) + " s"
72+
<< std::endl;
73+
}
6774

6875
void CppCAN::can_parse::print_all_frames(CANDatabase& db) {
6976
size_t i = 0;
77+
78+
// First, explore the database to find "pretty-printing" parameters
79+
int frame_name_maxsize = 12; // At least a reasonable column size
80+
for(const auto& frame : db) {
81+
if(frame.second->name().size() > frame_name_maxsize)
82+
frame_name_maxsize = frame.second->name().size() + 1;
83+
}
84+
85+
int full_line_size = frame_name_maxsize + INT_COL_SIZE * 3;
86+
std::cout << std::left;
87+
std::cout << std::setw(frame_name_maxsize) << "Frame name"
88+
<< std::setw(INT_COL_SIZE) << "CAN ID"
89+
<< std::setw(INT_COL_SIZE) << "DLC"
90+
<< std::setw(INT_COL_SIZE) << "Period"
91+
<< std::endl;
92+
std::cout << std::setfill('-') << std::setw(full_line_size) << "" << std::endl;
93+
std::cout.fill(' ');
94+
95+
for(const auto& frame : db) {
96+
print_frame_line(*frame.second, frame_name_maxsize);
97+
}
98+
99+
/*
70100
for(const auto& frame : db) {
71-
print_frame_impl(*frame.second);
101+
print_frame_impl(*frame.second, frame_name_maxsize);
72102
print_info_separator(1);
73103
74104
size_t j = 0;
@@ -82,4 +112,5 @@ void CppCAN::can_parse::print_all_frames(CANDatabase& db) {
82112
if(i++ < db.size() - 1)
83113
print_info_separator();
84114
}
115+
*/
85116
}

0 commit comments

Comments
 (0)