printf support for array printer

This commit is contained in:
Robin Müller 2021-01-15 16:32:25 +01:00
parent f69d6d49c7
commit 05508418a7
2 changed files with 95 additions and 51 deletions

View File

@ -3,64 +3,101 @@
#include <bitset> #include <bitset>
void arrayprinter::print(const uint8_t *data, size_t size, OutputType type, void arrayprinter::print(const uint8_t *data, size_t size, OutputType type,
bool printInfo, size_t maxCharPerLine) { bool printInfo, size_t maxCharPerLine) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
if(printInfo) { if(printInfo) {
sif::info << "Printing data with size " << size << ": "; sif::info << "Printing data with size " << size << ": ";
} }
sif::info << "["; sif::info << "[";
#else #else
// TODO: Use %zu or %lu depending on whether C99 support is given. #if FSFW_NO_C99_IO == 1
sif::printInfo("Printing data with size %lu: [", static_cast<unsigned long>(size)); sif::printInfo("Printing data with size %lu: [", static_cast<unsigned long>(size));
#endif #else
if(type == OutputType::HEX) { sif::printInfo("Printing data with size %zu: [", size);
arrayprinter::printHex(data, size, maxCharPerLine); #endif /* FSFW_NO_C99_IO == 1 */
} #endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
else if (type == OutputType::DEC) { if(type == OutputType::HEX) {
arrayprinter::printDec(data, size, maxCharPerLine); arrayprinter::printHex(data, size, maxCharPerLine);
} }
else if(type == OutputType::BIN) { else if (type == OutputType::DEC) {
arrayprinter::printBin(data, size); arrayprinter::printDec(data, size, maxCharPerLine);
} }
else if(type == OutputType::BIN) {
arrayprinter::printBin(data, size);
}
} }
void arrayprinter::printHex(const uint8_t *data, size_t size, void arrayprinter::printHex(const uint8_t *data, size_t size,
size_t maxCharPerLine) { size_t maxCharPerLine) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << std::hex; sif::info << std::hex;
for(size_t i = 0; i < size; i++) { for(size_t i = 0; i < size; i++) {
sif::info << "0x" << static_cast<int>(data[i]); sif::info << "0x" << static_cast<int>(data[i]);
if(i < size - 1){ if(i < size - 1) {
sif::info << " , "; sif::info << " , ";
if(i > 0 and i % maxCharPerLine == 0) { if(i > 0 and i % maxCharPerLine == 0) {
sif::info << std::endl; sif::info << std::endl;
} }
} }
} }
sif::info << std::dec; sif::info << std::dec;
sif::info << "]" << std::endl; sif::info << "]" << std::endl;
#else #else
// how much memory to reserve for printout? // General format: 0x01, 0x02, 0x03 so it is number of chars times 6
// plus line break plus small safety margin.
char printBuffer[(size + 1) * 7 + 1];
size_t currentPos = 0;
for(size_t i = 0; i < size; i++) {
// To avoid buffer overflows.
if(sizeof(printBuffer) - currentPos <= 7) {
break;
}
currentPos += snprintf(printBuffer + currentPos, 6, "0x%02x", data[i]);
if(i < size - 1) {
currentPos += sprintf(printBuffer + currentPos, ", ");
if(i > 0 and i % maxCharPerLine == 0) {
currentPos += sprintf(printBuffer + currentPos, "\n");
}
}
}
#endif #endif
} }
void arrayprinter::printDec(const uint8_t *data, size_t size, void arrayprinter::printDec(const uint8_t *data, size_t size,
size_t maxCharPerLine) { size_t maxCharPerLine) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << std::dec; sif::info << std::dec;
for(size_t i = 0; i < size; i++) { for(size_t i = 0; i < size; i++) {
sif::info << static_cast<int>(data[i]); sif::info << static_cast<int>(data[i]);
if(i < size - 1){ if(i < size - 1){
sif::info << " , "; sif::info << " , ";
if(i > 0 and i % maxCharPerLine == 0) { if(i > 0 and i % maxCharPerLine == 0) {
sif::info << std::endl; sif::info << std::endl;
} }
} }
} }
sif::info << "]" << std::endl; sif::info << "]" << std::endl;
#else #else
// how much memory to reserve for printout? // General format: 32, 243, -12 so it is number of chars times 5
// plus line break plus small safety margin.
char printBuffer[(size + 1) * 5 + 1];
size_t currentPos = 0;
for(size_t i = 0; i < size; i++) {
// To avoid buffer overflows.
if(sizeof(printBuffer) - currentPos <= 5) {
break;
}
currentPos += snprintf(printBuffer + currentPos, 3, "%d", data[i]);
if(i < size - 1) {
currentPos += sprintf(printBuffer + currentPos, ", ");
if(i > 0 and i % maxCharPerLine == 0) {
currentPos += sprintf(printBuffer + currentPos, "\n");
}
}
}
#endif #endif
} }
@ -68,11 +105,14 @@ void arrayprinter::printBin(const uint8_t *data, size_t size) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << "\n" << std::flush; sif::info << "\n" << std::flush;
for(size_t i = 0; i < size; i++) { for(size_t i = 0; i < size; i++) {
sif::info << "Byte " << i + 1 << ": 0b"<< sif::info << "Byte " << i + 1 << ": 0b" << std::bitset<8>(data[i]) << "," << std::endl;
std::bitset<8>(data[i]) << ",\n" << std::flush;
} }
sif::info << "]" << std::endl; sif::info << "]" << std::endl;
#else #else
// how much memory to reserve for printout? sif::printInfo("\n");
for(size_t i = 0; i < size; i++) {
sif::printInfo("Byte %d: 0b" BYTE_TO_BINARY_PATTERN ",\n", BYTE_TO_BINARY(data[i]));
}
sif::printInfo("]\n");
#endif #endif
} }

View File

@ -1,20 +1,24 @@
#ifndef FRAMEWORK_GLOBALFUNCTIONS_ARRAYPRINTER_H_ #ifndef FRAMEWORK_GLOBALFUNCTIONS_ARRAYPRINTER_H_
#define FRAMEWORK_GLOBALFUNCTIONS_ARRAYPRINTER_H_ #define FRAMEWORK_GLOBALFUNCTIONS_ARRAYPRINTER_H_
#include <cstdint> #include <cstdint>
#include <cstddef> #include <cstddef>
enum class OutputType { enum class OutputType {
DEC, DEC,
HEX, HEX,
BIN BIN
}; };
//! TODO: Write unit tests for this module.
namespace arrayprinter { namespace arrayprinter {
void print(const uint8_t* data, size_t size, OutputType type = OutputType::HEX, void print(const uint8_t* data, size_t size, OutputType type = OutputType::HEX,
bool printInfo = true, size_t maxCharPerLine = 12); bool printInfo = true, size_t maxCharPerLine = 12);
void printHex(const uint8_t* data, size_t size, size_t maxCharPerLine = 12); void printHex(const uint8_t* data, size_t size, size_t maxCharPerLine = 12);
void printDec(const uint8_t* data, size_t size, size_t maxCharPerLine = 12); void printDec(const uint8_t* data, size_t size, size_t maxCharPerLine = 12);
void printBin(const uint8_t* data, size_t size); void printBin(const uint8_t* data, size_t size);
} }
#endif /* FRAMEWORK_GLOBALFUNCTIONS_ARRAYPRINTER_H_ */ #endif /* FRAMEWORK_GLOBALFUNCTIONS_ARRAYPRINTER_H_ */