fsfw/globalfunctions/arrayprinter.cpp

62 lines
1.6 KiB
C++
Raw Normal View History

2020-06-05 16:44:00 +02:00
#include <framework/globalfunctions/arrayprinter.h>
2020-05-11 12:22:06 +02:00
#include <framework/serviceinterface/ServiceInterfaceStream.h>
2020-05-19 23:08:17 +02:00
#include <bitset>
2020-05-11 12:22:06 +02:00
2020-06-05 16:44:00 +02:00
void arrayprinter::print(const uint8_t *data, size_t size, OutputType type,
2020-05-19 20:26:12 +02:00
bool printInfo, size_t maxCharPerLine) {
2020-05-19 19:52:35 +02:00
if(printInfo) {
sif::info << "Printing data with size " << size << ": ";
}
sif::info << "[";
2020-05-15 18:47:46 +02:00
if(type == OutputType::HEX) {
2020-06-05 16:44:00 +02:00
arrayprinter::printHex(data, size, maxCharPerLine);
2020-05-15 18:47:46 +02:00
}
2020-05-19 23:08:17 +02:00
else if (type == OutputType::DEC) {
2020-06-05 16:44:00 +02:00
arrayprinter::printDec(data, size, maxCharPerLine);
2020-05-15 18:47:46 +02:00
}
2020-05-19 23:08:17 +02:00
else if(type == OutputType::BIN) {
2020-06-05 16:44:00 +02:00
arrayprinter::printBin(data, size);
2020-05-19 23:08:17 +02:00
}
2020-05-15 18:47:46 +02:00
}
2020-06-05 16:44:00 +02:00
void arrayprinter::printHex(const uint8_t *data, size_t size,
2020-05-19 20:26:12 +02:00
size_t maxCharPerLine) {
2020-05-15 18:47:46 +02:00
sif::info << std::hex;
2020-05-11 12:22:06 +02:00
for(size_t i = 0; i < size; i++) {
2020-05-15 18:47:46 +02:00
sif::info << "0x" << static_cast<int>(data[i]);
2020-05-11 12:22:06 +02:00
if(i < size - 1){
sif::info << " , ";
2020-05-19 19:52:35 +02:00
if(i > 0 and i % maxCharPerLine == 0) {
sif::info << std::endl;
}
2020-05-11 12:22:06 +02:00
}
2020-05-19 19:52:35 +02:00
2020-05-11 12:22:06 +02:00
}
2020-05-15 18:47:46 +02:00
sif::info << std::dec;
2020-05-15 18:54:14 +02:00
sif::info << "]" << std::endl;
2020-05-11 12:22:06 +02:00
}
2020-05-15 18:47:46 +02:00
2020-06-05 16:44:00 +02:00
void arrayprinter::printDec(const uint8_t *data, size_t size,
2020-05-19 20:26:12 +02:00
size_t maxCharPerLine) {
2020-05-15 18:54:14 +02:00
sif::info << std::dec;
2020-05-15 18:47:46 +02:00
for(size_t i = 0; i < size; i++) {
2020-05-19 20:26:12 +02:00
sif::info << static_cast<int>(data[i]);
2020-05-15 18:47:46 +02:00
if(i < size - 1){
sif::info << " , ";
2020-05-19 19:52:35 +02:00
if(i > 0 and i % maxCharPerLine == 0) {
sif::info << std::endl;
}
2020-05-15 18:47:46 +02:00
}
}
2020-05-15 18:54:14 +02:00
sif::info << "]" << std::endl;
2020-05-15 18:47:46 +02:00
}
2020-05-19 23:08:17 +02:00
2020-06-05 16:44:00 +02:00
void arrayprinter::printBin(const uint8_t *data, size_t size) {
2020-05-19 23:08:17 +02:00
sif::info << "\n" << std::flush;
for(size_t i = 0; i < size; i++) {
sif::info << "Byte " << i + 1 << ": 0b"<<
std::bitset<8>(data[i]) << ",\n" << std::flush;
}
sif::info << "]" << std::endl;
}