mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/sta-core.git
synced 2025-12-17 00:58:02 +00:00
Renamed debug folder to debugging to avoid gitignore nonsense
This commit is contained in:
209
src/debugging/printing/printable.cpp
Normal file
209
src/debugging/printing/printable.cpp
Normal file
@@ -0,0 +1,209 @@
|
||||
#include <sta/debug/printing/printable.hpp>
|
||||
|
||||
#ifdef STA_PLATFORM_ARDUINO
|
||||
# include <inttypes.h>
|
||||
# include <string.h>
|
||||
# include <stdio.h>
|
||||
# include <stdarg.h>
|
||||
#else
|
||||
# include <cinttypes>
|
||||
# include <cstring>
|
||||
# include <cstdio>
|
||||
# include <stdarg.h>
|
||||
#endif // STA_PLATFORM_ARDUINO
|
||||
|
||||
#include <sta/debug/assert.hpp>
|
||||
#include <sta/lang.hpp>
|
||||
|
||||
namespace sta
|
||||
{
|
||||
void BasePrintable::printf(const char * fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start (args, fmt);
|
||||
|
||||
char temp[1];
|
||||
int n = vsnprintf(temp, 1, fmt, args);
|
||||
|
||||
va_start (args, fmt);
|
||||
char str[n+1];
|
||||
vsnprintf(str, n+1, fmt, args);
|
||||
STA_ASSERT(n > 0);
|
||||
|
||||
println(str);
|
||||
}
|
||||
|
||||
void BasePrintable::print(char c)
|
||||
{
|
||||
print(&c, 1);
|
||||
}
|
||||
|
||||
void BasePrintable::print(bool b)
|
||||
{
|
||||
print(b ? "true" : "false");
|
||||
}
|
||||
|
||||
void BasePrintable::print(double d)
|
||||
{
|
||||
char buffer[64];
|
||||
snprintf(buffer, sizeof(buffer), "%f", d);
|
||||
print(buffer);
|
||||
}
|
||||
|
||||
void BasePrintable::print(uint8_t num, IntegerBase base)
|
||||
{
|
||||
printBase(num, base, "%" PRIu8, sizeof(num));
|
||||
}
|
||||
|
||||
void BasePrintable::print(uint16_t num, IntegerBase base)
|
||||
{
|
||||
printBase(num, base, "%" PRIu16, sizeof(num));
|
||||
}
|
||||
|
||||
void BasePrintable::print(uint32_t num, IntegerBase base)
|
||||
{
|
||||
printBase(num, base, "%" PRIu32, sizeof(num));
|
||||
}
|
||||
|
||||
void BasePrintable::print(const char * str)
|
||||
{
|
||||
print(str, strlen(str));
|
||||
}
|
||||
|
||||
void BasePrintable::println()
|
||||
{
|
||||
print("\r\n", 2);
|
||||
}
|
||||
|
||||
void BasePrintable::println(char c)
|
||||
{
|
||||
print(&c, 1);
|
||||
println();
|
||||
}
|
||||
|
||||
void BasePrintable::println(bool b)
|
||||
{
|
||||
print(b);
|
||||
println();
|
||||
}
|
||||
|
||||
void BasePrintable::println(double d)
|
||||
{
|
||||
print(d);
|
||||
println();
|
||||
}
|
||||
|
||||
void BasePrintable::println(uint8_t num, IntegerBase base)
|
||||
{
|
||||
print(num, base);
|
||||
println();
|
||||
}
|
||||
|
||||
void BasePrintable::println(uint16_t num, IntegerBase base)
|
||||
{
|
||||
print(num, base);
|
||||
println();
|
||||
}
|
||||
|
||||
void BasePrintable::println(uint32_t num, IntegerBase base)
|
||||
{
|
||||
print(num, base);
|
||||
println();
|
||||
}
|
||||
|
||||
void BasePrintable::println(const char * str)
|
||||
{
|
||||
println(str, strlen(str));
|
||||
}
|
||||
|
||||
void BasePrintable::println(const char * str, size_t length)
|
||||
{
|
||||
print(str, length);
|
||||
println();
|
||||
}
|
||||
|
||||
void BasePrintable::printBase(uintmax_t num, IntegerBase base, const char * fmt, size_t size)
|
||||
{
|
||||
switch (base)
|
||||
{
|
||||
case IntegerBase::BASE_DEC:
|
||||
printDec(num, fmt);
|
||||
break;
|
||||
|
||||
case IntegerBase::BASE_BIN:
|
||||
// Digits in base 2 = size in bytes * 8
|
||||
printBin(num, size * 8);
|
||||
break;
|
||||
|
||||
case IntegerBase::BASE_HEX:
|
||||
// Digits in base 16 = size in bytes * 2
|
||||
printHex(num, size * 2);
|
||||
break;
|
||||
|
||||
default:
|
||||
print("<invalid_base>");
|
||||
}
|
||||
}
|
||||
|
||||
void BasePrintable::printDec(uintmax_t num, const char * fmt)
|
||||
{
|
||||
char buffer[64];
|
||||
snprintf(buffer, sizeof(buffer), fmt, static_cast<uint32_t>(num));
|
||||
print(buffer);
|
||||
}
|
||||
|
||||
void BasePrintable::printBin(uintmax_t value, size_t digits)
|
||||
{
|
||||
// Need 8 digits for every byte
|
||||
char buffer[sizeof(value) * 8];
|
||||
|
||||
// Check bounds
|
||||
if (digits > sizeof(buffer))
|
||||
{
|
||||
print("<bin_value_too_big>");
|
||||
return;
|
||||
}
|
||||
// Nothing to do
|
||||
if (digits == 0)
|
||||
return;
|
||||
|
||||
for (size_t i = 0; i < digits; ++i)
|
||||
{
|
||||
// Convert bit to '0' or '1'
|
||||
// First digit in buffer is MSB in value, so shift from high to low
|
||||
buffer[i] = '0' + ((value >> (digits - 1 - i)) & 0x1);
|
||||
}
|
||||
|
||||
print(buffer, digits);
|
||||
}
|
||||
|
||||
void BasePrintable::printHex(uintmax_t value, size_t digits)
|
||||
{
|
||||
// Need 2 digits for every byte
|
||||
char buffer[sizeof(value) * 2];
|
||||
|
||||
// Check bounds
|
||||
if (digits > sizeof(buffer))
|
||||
{
|
||||
print("<hex_value_too_big>");
|
||||
return;
|
||||
}
|
||||
// Nothing to do
|
||||
if (digits == 0)
|
||||
return;
|
||||
|
||||
for (size_t i = 0; i < digits; ++i)
|
||||
{
|
||||
// Convert 4 bits to hex
|
||||
// First digit in buffer is 4 MSBs in value, so shift from high to low
|
||||
uint8_t hex = ((value >> ((digits - 1 - i) * 4)) & 0xF);
|
||||
if (hex > 9)
|
||||
buffer[i] = 'A' + (hex - 10);
|
||||
else
|
||||
buffer[i] = '0' + hex;
|
||||
}
|
||||
|
||||
print(buffer, digits);
|
||||
}
|
||||
|
||||
} // namespace sta
|
||||
Reference in New Issue
Block a user