Merge pull request 'Global Printer functions' (#78) from KSat/fsfw:mueller_globalPrinter into master
Helpers to print arrays. Implements #79
This commit is contained in:
commit
95aac66dc1
61
globalfunctions/printer.cpp
Normal file
61
globalfunctions/printer.cpp
Normal file
@ -0,0 +1,61 @@
|
||||
#include <framework/globalfunctions/printer.h>
|
||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||
#include <bitset>
|
||||
|
||||
void printer::print(const uint8_t *data, size_t size, OutputType type,
|
||||
bool printInfo, size_t maxCharPerLine) {
|
||||
if(printInfo) {
|
||||
sif::info << "Printing data with size " << size << ": ";
|
||||
}
|
||||
sif::info << "[";
|
||||
if(type == OutputType::HEX) {
|
||||
printer::printHex(data, size, maxCharPerLine);
|
||||
}
|
||||
else if (type == OutputType::DEC) {
|
||||
printer::printDec(data, size, maxCharPerLine);
|
||||
}
|
||||
else if(type == OutputType::BIN) {
|
||||
printer::printBin(data, size);
|
||||
}
|
||||
}
|
||||
|
||||
void printer::printHex(const uint8_t *data, size_t size,
|
||||
size_t maxCharPerLine) {
|
||||
sif::info << std::hex;
|
||||
for(size_t i = 0; i < size; i++) {
|
||||
sif::info << "0x" << static_cast<int>(data[i]);
|
||||
if(i < size - 1){
|
||||
sif::info << " , ";
|
||||
if(i > 0 and i % maxCharPerLine == 0) {
|
||||
sif::info << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
sif::info << std::dec;
|
||||
sif::info << "]" << std::endl;
|
||||
}
|
||||
|
||||
void printer::printDec(const uint8_t *data, size_t size,
|
||||
size_t maxCharPerLine) {
|
||||
sif::info << std::dec;
|
||||
for(size_t i = 0; i < size; i++) {
|
||||
sif::info << static_cast<int>(data[i]);
|
||||
if(i < size - 1){
|
||||
sif::info << " , ";
|
||||
if(i > 0 and i % maxCharPerLine == 0) {
|
||||
sif::info << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
sif::info << "]" << std::endl;
|
||||
}
|
||||
|
||||
void printer::printBin(const uint8_t *data, size_t size) {
|
||||
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;
|
||||
}
|
21
globalfunctions/printer.h
Normal file
21
globalfunctions/printer.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef FRAMEWORK_GLOBALFUNCTIONS_PRINTER_H_
|
||||
#define FRAMEWORK_GLOBALFUNCTIONS_PRINTER_H_
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
|
||||
namespace printer {
|
||||
|
||||
enum class OutputType {
|
||||
DEC,
|
||||
HEX,
|
||||
BIN
|
||||
};
|
||||
|
||||
void print(const uint8_t* data, size_t size, OutputType type = OutputType::HEX,
|
||||
bool printInfo = true, 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 printBin(const uint8_t* data, size_t size);
|
||||
}
|
||||
|
||||
#endif /* FRAMEWORK_GLOBALFUNCTIONS_PRINTER_H_ */
|
Loading…
Reference in New Issue
Block a user