mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/sta-core.git
synced 2025-06-10 16:55:58 +00:00
Updated printable to reduce mutex operations when printing newlines
This commit is contained in:
parent
bcdec5de60
commit
1e41189480
@ -19,7 +19,7 @@ namespace sta
|
|||||||
*
|
*
|
||||||
* @ingroup sta_core_debug
|
* @ingroup sta_core_debug
|
||||||
*/
|
*/
|
||||||
# define STA_DEBUG_PRINT(...) sta::Debug->print(__VA_ARGS__)
|
# define STA_DEBUG_PRINT(...) sta::Debug->print(__VA_ARGS__)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Debug print message followed by new-line to UART.
|
* @brief Debug print message followed by new-line to UART.
|
||||||
@ -28,7 +28,7 @@ namespace sta
|
|||||||
*
|
*
|
||||||
* @ingroup sta_core_debug
|
* @ingroup sta_core_debug
|
||||||
*/
|
*/
|
||||||
# define STA_DEBUG_PRINTLN(...) sta::Debug->println(__VA_ARGS__)
|
# define STA_DEBUG_PRINTLN(...) sta::Debug->println(__VA_ARGS__)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Formatted debug printing with new-line.
|
* @brief Formatted debug printing with new-line.
|
||||||
@ -36,13 +36,16 @@ namespace sta
|
|||||||
* @param fmt See @ref sta::PrintableUART::printf
|
* @param fmt See @ref sta::PrintableUART::printf
|
||||||
* @param ... See @ref sta::PrintableUART::printf
|
* @param ... See @ref sta::PrintableUART::printf
|
||||||
*/
|
*/
|
||||||
# define STA_DEBUG_PRINTF(fmt, ...) sta::Debug->printf(fmt, __VA_ARGS__)
|
# define STA_DEBUG_PRINTF(fmt, ...) sta::Debug->printf(fmt, __VA_ARGS__)
|
||||||
|
|
||||||
|
# define STA_DEBUG_READ(buffer, length) sta::Debug->read(buffer, length)
|
||||||
|
|
||||||
#else // !STA_DEBUGGING_ENABLED
|
#else // !STA_DEBUGGING_ENABLED
|
||||||
|
|
||||||
# define STA_DEBUG_PRINT(...) ((void)0)
|
# define STA_DEBUG_PRINT(...) ((void)0)
|
||||||
# define STA_DEBUG_PRINTLN(...) ((void)0)
|
# define STA_DEBUG_PRINTLN(...) ((void)0)
|
||||||
# define STA_DEBUG_PRINTF(fmt, ...) ((void)0)
|
# define STA_DEBUG_PRINTF(fmt, ...) ((void)0)
|
||||||
|
# define STA_DEBUG_READ(buffer, length) ((void)0)
|
||||||
|
|
||||||
#endif // STA_DEBUGGING_ENABLED
|
#endif // STA_DEBUGGING_ENABLED
|
||||||
|
|
||||||
|
@ -40,62 +40,70 @@ namespace sta
|
|||||||
/**
|
/**
|
||||||
* @brief Print single character.
|
* @brief Print single character.
|
||||||
*
|
*
|
||||||
* @param c Character to print
|
* @param c Character to print
|
||||||
|
* @param newline If true, send a \r\n to start a new line.
|
||||||
*/
|
*/
|
||||||
void print(char c);
|
void print(char c, bool newline = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Print boolean value.
|
* @brief Print boolean value.
|
||||||
*
|
*
|
||||||
* @param b Boolean value
|
* @param b Boolean value
|
||||||
|
* @param newline If true, send a \r\n to start a new line.
|
||||||
*/
|
*/
|
||||||
void print(bool b);
|
void print(bool b, bool newline = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Print floating point value.
|
* @brief Print floating point value.
|
||||||
*
|
*
|
||||||
* @param d Floating point value
|
* @param d Floating point value
|
||||||
|
* @param newline If true, send a \r\n to start a new line.
|
||||||
*/
|
*/
|
||||||
void print(double d);
|
void print(double d, bool newline = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Print integer in selected base.
|
* @brief Print integer in selected base.
|
||||||
*
|
*
|
||||||
* @param num 8-bit unsigned integer
|
* @param num 8-bit unsigned integer
|
||||||
* @param base Integer base
|
* @param base Integer base
|
||||||
|
* @param newline If true, send a \r\n to start a new line.
|
||||||
*/
|
*/
|
||||||
void print(uint8_t num, IntegerBase base = IntegerBase::DEC);
|
void print(uint8_t num, IntegerBase base = IntegerBase::DEC, bool newline = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Print integer in selected base.
|
* @brief Print integer in selected base.
|
||||||
*
|
*
|
||||||
* @param num 16-bit unsigned integer
|
* @param num 16-bit unsigned integer
|
||||||
* @param base Integer base
|
* @param base Integer base
|
||||||
|
* @param newline If true, send a \r\n to start a new line.
|
||||||
*/
|
*/
|
||||||
void print(uint16_t num, IntegerBase base = IntegerBase::DEC);
|
void print(uint16_t num, IntegerBase base = IntegerBase::DEC, bool newline = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Print integer in selected base.
|
* @brief Print integer in selected base.
|
||||||
*
|
*
|
||||||
* @param num 32-bit unsigned integer
|
* @param num 32-bit unsigned integer
|
||||||
* @param base Integer base
|
* @param base Integer base
|
||||||
|
* @param newline If true, send a \r\n to start a new line.
|
||||||
*/
|
*/
|
||||||
void print(uint32_t num, IntegerBase base = IntegerBase::DEC);
|
void print(uint32_t num, IntegerBase base = IntegerBase::DEC, bool newline = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Print c-string.
|
* @brief Print c-string.
|
||||||
*
|
*
|
||||||
* @param str Null terminated string
|
* @param str Null terminated string
|
||||||
|
* @param newline If true, send a \r\n to start a new line.
|
||||||
*/
|
*/
|
||||||
void print(const char * str);
|
void print(const char * str, bool newline = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Print string.
|
* @brief Print string.
|
||||||
*
|
*
|
||||||
* @param str String buffer
|
* @param str String buffer
|
||||||
* @param length String length
|
* @param length String length
|
||||||
|
* @param newline If true, send a \r\n to start a new line.
|
||||||
*/
|
*/
|
||||||
virtual void print(const char * str, size_t length) = 0;
|
virtual void print(const char * str, size_t length, bool newline = false) = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Print new-line.
|
* @brief Print new-line.
|
||||||
@ -161,6 +169,14 @@ namespace sta
|
|||||||
* @param length String length
|
* @param length String length
|
||||||
*/
|
*/
|
||||||
void println(const char * str, size_t length);
|
void println(const char * str, size_t length);
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Read string.
|
||||||
|
*
|
||||||
|
* @param str String buffer
|
||||||
|
* @param length String length
|
||||||
|
*/
|
||||||
|
virtual void read(char* str, size_t length) = 0;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
@ -170,32 +186,36 @@ namespace sta
|
|||||||
* @param base Integer base
|
* @param base Integer base
|
||||||
* @param fmt printf format string for base 10
|
* @param fmt printf format string for base 10
|
||||||
* @param size Size of value in bytes
|
* @param size Size of value in bytes
|
||||||
|
* @param newline If true, send a \r\n to start a new line.
|
||||||
*/
|
*/
|
||||||
void printBase(uintmax_t value, IntegerBase base, const char * fmt, size_t size);
|
void printBase(uintmax_t value, IntegerBase base, const char * fmt, size_t size, bool newline = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Print unsigned integer in base 10.
|
* @brief Print unsigned integer in base 10.
|
||||||
*
|
*
|
||||||
* @param value Unsigned integer value
|
* @param value Unsigned integer value
|
||||||
* @param fmt printf format string
|
* @param fmt printf format string
|
||||||
|
* @param newline If true, send a \r\n to start a new line.
|
||||||
*/
|
*/
|
||||||
void printDec(uintmax_t value, const char * fmt);
|
void printDec(uintmax_t value, const char * fmt, bool newline = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Print unsigned integer in base 2.
|
* @brief Print unsigned integer in base 2.
|
||||||
*
|
*
|
||||||
* @param value Unsigned integer value
|
* @param value Unsigned integer value
|
||||||
* @param digits Number of digits to print
|
* @param digits Number of digits to print
|
||||||
|
* @param newline If true, send a \r\n to start a new line.
|
||||||
*/
|
*/
|
||||||
void printBin(uintmax_t value, size_t digits);
|
void printBin(uintmax_t value, size_t digits, bool newline = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Print unsigned integer in base 16.
|
* @brief Print unsigned integer in base 16.
|
||||||
*
|
*
|
||||||
* @param value Unsigned integer value
|
* @param value Unsigned integer value
|
||||||
* @param digits Number of digits to print
|
* @param digits Number of digits to print
|
||||||
|
* @param newline If true, send a \r\n to start a new line.
|
||||||
*/
|
*/
|
||||||
void printHex(uintmax_t value, size_t digits);
|
void printHex(uintmax_t value, size_t digits, bool newline = false);
|
||||||
};
|
};
|
||||||
} // namespace sta
|
} // namespace sta
|
||||||
|
|
||||||
|
@ -23,8 +23,17 @@ namespace sta
|
|||||||
*
|
*
|
||||||
* @param str String buffer
|
* @param str String buffer
|
||||||
* @param length String length
|
* @param length String length
|
||||||
|
* @param newline If true, send a \r\n to start a new line.
|
||||||
*/
|
*/
|
||||||
void print(const char * str, size_t length) override;
|
void print(const char * str, size_t length, bool newline = false) override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Print string.
|
||||||
|
*
|
||||||
|
* @param str String buffer
|
||||||
|
* @param length String length
|
||||||
|
*/
|
||||||
|
void read(char * str, size_t length) override;
|
||||||
};
|
};
|
||||||
} // namespace sta
|
} // namespace sta
|
||||||
|
|
||||||
|
@ -32,8 +32,17 @@ namespace sta
|
|||||||
*
|
*
|
||||||
* @param str String buffer
|
* @param str String buffer
|
||||||
* @param length String length
|
* @param length String length
|
||||||
|
* @param newline If true, send a \r\n to start a new line.
|
||||||
*/
|
*/
|
||||||
void print(const char * str, size_t length) override;
|
void print(const char * str, size_t length, bool newline = false) override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Read string.
|
||||||
|
*
|
||||||
|
* @param str String buffer
|
||||||
|
* @param length String length
|
||||||
|
*/
|
||||||
|
void read(char * str, size_t length) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
UART * intf_;
|
UART * intf_;
|
||||||
|
@ -27,82 +27,76 @@ namespace sta
|
|||||||
println(str);
|
println(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Printable::print(char c)
|
void Printable::print(char c, bool newline /* = false */)
|
||||||
{
|
{
|
||||||
print(&c, 1);
|
print(&c, 1, newline);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Printable::print(bool b)
|
void Printable::print(bool b, bool newline /* = false */)
|
||||||
{
|
{
|
||||||
print(b ? "true" : "false");
|
print(b ? "true" : "false", newline);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Printable::print(double d)
|
void Printable::print(double d, bool newline /* = false */)
|
||||||
{
|
{
|
||||||
char buffer[64];
|
char buffer[64];
|
||||||
snprintf(buffer, sizeof(buffer), "%f", d);
|
snprintf(buffer, sizeof(buffer), "%f", d);
|
||||||
print(buffer);
|
print(buffer, newline);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Printable::print(uint8_t num, IntegerBase base)
|
void Printable::print(uint8_t num, IntegerBase base, bool newline /* = false */)
|
||||||
{
|
{
|
||||||
printBase(num, base, "%" PRIu8, sizeof(num));
|
printBase(num, base, "%" PRIu8, sizeof(num), newline);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Printable::print(uint16_t num, IntegerBase base)
|
void Printable::print(uint16_t num, IntegerBase base, bool newline /* = false */)
|
||||||
{
|
{
|
||||||
printBase(num, base, "%" PRIu16, sizeof(num));
|
printBase(num, base, "%" PRIu16, sizeof(num), newline);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Printable::print(uint32_t num, IntegerBase base)
|
void Printable::print(uint32_t num, IntegerBase base, bool newline /* = false */)
|
||||||
{
|
{
|
||||||
printBase(num, base, "%" PRIu32, sizeof(num));
|
printBase(num, base, "%" PRIu32, sizeof(num), newline);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Printable::print(const char * str)
|
void Printable::print(const char * str, bool newline /* = false */)
|
||||||
{
|
{
|
||||||
print(str, strlen(str));
|
print(str, strlen(str), newline);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Printable::println()
|
void Printable::println()
|
||||||
{
|
{
|
||||||
print("\r\n", 2);
|
print("\r\n", 2, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Printable::println(char c)
|
void Printable::println(char c)
|
||||||
{
|
{
|
||||||
print(&c, 1);
|
print(&c, 1, true);
|
||||||
println();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Printable::println(bool b)
|
void Printable::println(bool b)
|
||||||
{
|
{
|
||||||
print(b);
|
print(b, true);
|
||||||
println();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Printable::println(double d)
|
void Printable::println(double d)
|
||||||
{
|
{
|
||||||
print(d);
|
print(d, true);
|
||||||
println();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Printable::println(uint8_t num, IntegerBase base)
|
void Printable::println(uint8_t num, IntegerBase base)
|
||||||
{
|
{
|
||||||
print(num, base);
|
print(num, base, true);
|
||||||
println();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Printable::println(uint16_t num, IntegerBase base)
|
void Printable::println(uint16_t num, IntegerBase base)
|
||||||
{
|
{
|
||||||
print(num, base);
|
print(num, base, true);
|
||||||
println();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Printable::println(uint32_t num, IntegerBase base)
|
void Printable::println(uint32_t num, IntegerBase base)
|
||||||
{
|
{
|
||||||
print(num, base);
|
print(num, base, true);
|
||||||
println();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Printable::println(const char * str)
|
void Printable::println(const char * str)
|
||||||
@ -112,26 +106,25 @@ namespace sta
|
|||||||
|
|
||||||
void Printable::println(const char * str, size_t length)
|
void Printable::println(const char * str, size_t length)
|
||||||
{
|
{
|
||||||
print(str, length);
|
print(str, length, true);
|
||||||
println();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Printable::printBase(uintmax_t num, IntegerBase base, const char * fmt, size_t size)
|
void Printable::printBase(uintmax_t num, IntegerBase base, const char * fmt, size_t size, bool newline /* = false */)
|
||||||
{
|
{
|
||||||
switch (base)
|
switch (base)
|
||||||
{
|
{
|
||||||
case IntegerBase::DEC:
|
case IntegerBase::DEC:
|
||||||
printDec(num, fmt);
|
printDec(num, fmt, newline);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IntegerBase::BIN:
|
case IntegerBase::BIN:
|
||||||
// Digits in base 2 = size in bytes * 8
|
// Digits in base 2 = size in bytes * 8
|
||||||
printBin(num, size * 8);
|
printBin(num, size * 8, newline);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IntegerBase::HEX:
|
case IntegerBase::HEX:
|
||||||
// Digits in base 16 = size in bytes * 2
|
// Digits in base 16 = size in bytes * 2
|
||||||
printHex(num, size * 2);
|
printHex(num, size * 2, newline);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -139,14 +132,14 @@ namespace sta
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Printable::printDec(uintmax_t num, const char * fmt)
|
void Printable::printDec(uintmax_t num, const char * fmt, bool newline /* = false */)
|
||||||
{
|
{
|
||||||
char buffer[64];
|
char buffer[64];
|
||||||
snprintf(buffer, sizeof(buffer), fmt, static_cast<uint32_t>(num));
|
snprintf(buffer, sizeof(buffer), fmt, static_cast<uint32_t>(num));
|
||||||
print(buffer);
|
print(buffer, newline);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Printable::printBin(uintmax_t value, size_t digits)
|
void Printable::printBin(uintmax_t value, size_t digits, bool newline /* = false */)
|
||||||
{
|
{
|
||||||
// Need 8 digits for every byte
|
// Need 8 digits for every byte
|
||||||
char buffer[sizeof(value) * 8];
|
char buffer[sizeof(value) * 8];
|
||||||
@ -168,10 +161,10 @@ namespace sta
|
|||||||
buffer[i] = '0' + ((value >> (digits - 1 - i)) & 0x1);
|
buffer[i] = '0' + ((value >> (digits - 1 - i)) & 0x1);
|
||||||
}
|
}
|
||||||
|
|
||||||
print(buffer, digits);
|
print(buffer, digits, newline);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Printable::printHex(uintmax_t value, size_t digits)
|
void Printable::printHex(uintmax_t value, size_t digits, bool newline /* = false */)
|
||||||
{
|
{
|
||||||
// Need 2 digits for every byte
|
// Need 2 digits for every byte
|
||||||
char buffer[sizeof(value) * 2];
|
char buffer[sizeof(value) * 2];
|
||||||
@ -179,7 +172,7 @@ namespace sta
|
|||||||
// Check bounds
|
// Check bounds
|
||||||
if (digits > sizeof(buffer))
|
if (digits > sizeof(buffer))
|
||||||
{
|
{
|
||||||
print("<hex_value_too_big>");
|
print("<hex_value_too_big>", newline);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Nothing to do
|
// Nothing to do
|
||||||
@ -197,7 +190,7 @@ namespace sta
|
|||||||
buffer[i] = '0' + hex;
|
buffer[i] = '0' + hex;
|
||||||
}
|
}
|
||||||
|
|
||||||
print(buffer, digits);
|
print(buffer, digits, newline);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace sta
|
} // namespace sta
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#include <sta/debug/printing/printable_printf.hpp>
|
#include <sta/debug/printing/printable_printf.hpp>
|
||||||
#include <sta/debug/assert.hpp>
|
#include <sta/debug/assert.hpp>
|
||||||
|
#include <sta/lang.hpp>
|
||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
@ -10,11 +11,20 @@ namespace sta
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrintablePrintf::print(const char * str, size_t length)
|
void PrintablePrintf::print(const char * str, size_t length, bool newline /* = false */)
|
||||||
{
|
{
|
||||||
STA_ASSERT(str != nullptr);
|
STA_ASSERT(str != nullptr);
|
||||||
STA_ASSERT(length > 0);
|
STA_ASSERT(length > 0);
|
||||||
|
|
||||||
printf("%.*s", length, str);
|
printf("%.*s", length, str);
|
||||||
|
printf("\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintablePrintf::read(char * str, size_t length)
|
||||||
|
{
|
||||||
|
STA_ASSERT(str != nullptr);
|
||||||
|
STA_ASSERT(length > 0);
|
||||||
|
|
||||||
|
STA_NOT_IMPLEMENTED();
|
||||||
}
|
}
|
||||||
} // namespace sta
|
} // namespace sta
|
||||||
|
@ -17,11 +17,21 @@ namespace sta
|
|||||||
STA_ASSERT(intf->settings().mode == UARTMode::RX || intf->settings().mode == UARTMode::RX_TX);
|
STA_ASSERT(intf->settings().mode == UARTMode::RX || intf->settings().mode == UARTMode::RX_TX);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrintableUART::print(const char * str, size_t length)
|
void PrintableUART::print(const char * str, size_t length, bool newline /* = false */)
|
||||||
{
|
{
|
||||||
|
const char * linebreak = "\r\n";
|
||||||
|
|
||||||
intf_->acquire();
|
intf_->acquire();
|
||||||
intf_->transfer(reinterpret_cast<const uint8_t *>(str), length);
|
intf_->transfer(reinterpret_cast<const uint8_t *>(str), length);
|
||||||
|
intf_->transfer(reinterpret_cast<const uint8_t *>(linebreak), 2);
|
||||||
intf_->release();
|
intf_->release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PrintableUART::read(char * str, size_t length)
|
||||||
|
{
|
||||||
|
intf_->acquire();
|
||||||
|
intf_->receive(reinterpret_cast<uint8_t *>(str), length);
|
||||||
|
intf_->release();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace sta
|
} // namespace sta
|
||||||
|
Loading…
x
Reference in New Issue
Block a user