mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/sta-core.git
synced 2025-06-12 01:25:59 +00:00
Separate print methods from UART interface
This commit is contained in:
parent
e3be99514c
commit
bc35c44cb1
@ -30,22 +30,20 @@
|
|||||||
|
|
||||||
#ifdef STA_DEBUG_SERIAL_ENABLE
|
#ifdef STA_DEBUG_SERIAL_ENABLE
|
||||||
|
|
||||||
#include <sta/intf/uart.hpp>
|
#include <sta/printable_uart.hpp>
|
||||||
|
|
||||||
|
|
||||||
namespace sta
|
namespace sta
|
||||||
{
|
{
|
||||||
extern UART * const DebugSerial;
|
extern PrintableUART DebugSerial;
|
||||||
} // namespace sta
|
} // namespace sta
|
||||||
|
|
||||||
|
|
||||||
# define STA_DEBUG_PRINT(...) sta::DebugSerial->print(__VA_ARGS__)
|
# define STA_DEBUG_PRINT(...) sta::DebugSerial.print(__VA_ARGS__)
|
||||||
# define STA_DEBUG_PRINTLN(...) sta::DebugSerial->println(__VA_ARGS__)
|
# define STA_DEBUG_PRINTLN(...) sta::DebugSerial.println(__VA_ARGS__)
|
||||||
# define STA_DEBUG_WRITE(b, s) sta::DebugSerial->write(b, s)
|
|
||||||
#else // !STA_DEBUG_SERIAL_ENABLE
|
#else // !STA_DEBUG_SERIAL_ENABLE
|
||||||
# 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_WRITE(b, s) ((void)0)
|
|
||||||
#endif // !STA_DEBUG_SERIAL_ENABLE
|
#endif // !STA_DEBUG_SERIAL_ENABLE
|
||||||
|
|
||||||
#endif // STA_DEBUG_SERIAL_HPP
|
#endif // STA_DEBUG_SERIAL_HPP
|
||||||
|
@ -28,8 +28,6 @@ namespace sta
|
|||||||
*/
|
*/
|
||||||
HalUART(UART_HandleTypeDef * handle);
|
HalUART(UART_HandleTypeDef * handle);
|
||||||
|
|
||||||
using UART::print;
|
|
||||||
|
|
||||||
void write(const uint8_t * buffer, size_t size) override;
|
void write(const uint8_t * buffer, size_t size) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
/**
|
/**
|
||||||
* @brief UART interface definition.
|
* @brief UART interface definition.
|
||||||
*/
|
*/
|
||||||
#ifndef STA_UART_HPP
|
#ifndef STA_INTF_UART_HPP
|
||||||
#define STA_UART_HPP
|
#define STA_INTF_UART_HPP
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
@ -10,148 +10,12 @@
|
|||||||
|
|
||||||
namespace sta
|
namespace sta
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @brief Integer representation.
|
|
||||||
*/
|
|
||||||
enum class IntegerBase
|
|
||||||
{
|
|
||||||
DEC, /**< Decimal */
|
|
||||||
BIN, /**< Binary */
|
|
||||||
HEX /**< Hexadecimal */
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Interface for UART.
|
* @brief Interface for UART.
|
||||||
*/
|
*/
|
||||||
class UART
|
class UART
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/**
|
|
||||||
* @brief Print single character.
|
|
||||||
*
|
|
||||||
* @param c Character to print
|
|
||||||
*/
|
|
||||||
void print(char c);
|
|
||||||
/**
|
|
||||||
* @brief Print boolean value.
|
|
||||||
*
|
|
||||||
* @param b Boolean value
|
|
||||||
*/
|
|
||||||
void print(bool b);
|
|
||||||
/**
|
|
||||||
* @brief Print floating point value.
|
|
||||||
*
|
|
||||||
* @param d Floating point value
|
|
||||||
*/
|
|
||||||
void print(double d);
|
|
||||||
/**
|
|
||||||
* @brief Print integer in selected base.
|
|
||||||
*
|
|
||||||
* @param num 8-bit unsigned integer
|
|
||||||
* @param base Integer base
|
|
||||||
*/
|
|
||||||
void print(uint8_t num, IntegerBase base = IntegerBase::DEC);
|
|
||||||
/**
|
|
||||||
* @brief Print integer in selected base.
|
|
||||||
*
|
|
||||||
* @param num 16-bit unsigned integer
|
|
||||||
* @param base Integer base
|
|
||||||
*/
|
|
||||||
void print(uint16_t num, IntegerBase base = IntegerBase::DEC);
|
|
||||||
/**
|
|
||||||
* @brief Print integer in selected base.
|
|
||||||
*
|
|
||||||
* @param num 32-bit unsigned integer
|
|
||||||
* @param base Integer base
|
|
||||||
*/
|
|
||||||
void print(uint32_t num, IntegerBase base = IntegerBase::DEC);
|
|
||||||
/**
|
|
||||||
* @brief Print integer in selected base.
|
|
||||||
*
|
|
||||||
* @param num Integer
|
|
||||||
* @param base Integer base
|
|
||||||
*/
|
|
||||||
void print(size_t num, IntegerBase base = IntegerBase::DEC);
|
|
||||||
/**
|
|
||||||
* @brief Print c-string.
|
|
||||||
*
|
|
||||||
* @param str Null terminated string
|
|
||||||
*/
|
|
||||||
void print(const char * str);
|
|
||||||
/**
|
|
||||||
* @brief Print string.
|
|
||||||
*
|
|
||||||
* @param str String buffer
|
|
||||||
* @parma length String length
|
|
||||||
*/
|
|
||||||
void print(const char * str, size_t length);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Print new-line.
|
|
||||||
*/
|
|
||||||
void println();
|
|
||||||
/**
|
|
||||||
* @brief Print single character followed by a new-line.
|
|
||||||
*
|
|
||||||
* @param c Character to print
|
|
||||||
*/
|
|
||||||
void println(char c);
|
|
||||||
/**
|
|
||||||
* @brief Print boolean value followed by a new-line.
|
|
||||||
*
|
|
||||||
* @param b Boolean value
|
|
||||||
*/
|
|
||||||
void println(bool b);
|
|
||||||
/**
|
|
||||||
* @brief Print floating point value followed by a new-line.
|
|
||||||
*
|
|
||||||
* @param d Floating point value
|
|
||||||
*/
|
|
||||||
void println(double d);
|
|
||||||
/**
|
|
||||||
* @brief Print integer in selected base followed by a new-line.
|
|
||||||
*
|
|
||||||
* @param num 8-bit unsigned integer
|
|
||||||
* @param base Integer base
|
|
||||||
*/
|
|
||||||
void println(uint8_t num, IntegerBase base = IntegerBase::DEC);
|
|
||||||
/**
|
|
||||||
* @brief Print integer in selected base followed by a new-line.
|
|
||||||
*
|
|
||||||
* @param num 16-bit unsigned integer
|
|
||||||
* @param base Integer base
|
|
||||||
*/
|
|
||||||
void println(uint16_t num, IntegerBase base = IntegerBase::DEC);
|
|
||||||
/**
|
|
||||||
* @brief Print integer in selected base followed by a new-line.
|
|
||||||
*
|
|
||||||
* @param num 32-bit unsigned integer
|
|
||||||
* @param base Integer base
|
|
||||||
*/
|
|
||||||
void println(uint32_t num, IntegerBase base = IntegerBase::DEC);
|
|
||||||
/**
|
|
||||||
* @brief Print integer in selected base followed by a new-line.
|
|
||||||
*
|
|
||||||
* @param num Integer
|
|
||||||
* @param base Integer base
|
|
||||||
*/
|
|
||||||
void println(size_t num, IntegerBase base = IntegerBase::DEC);
|
|
||||||
/**
|
|
||||||
* @brief Print c-string followed by a new-line.
|
|
||||||
*
|
|
||||||
* @param str Null terminated string
|
|
||||||
*/
|
|
||||||
void println(const char * str);
|
|
||||||
/**
|
|
||||||
* @brief Print string followed by a new-line.
|
|
||||||
*
|
|
||||||
* @param str String buffer
|
|
||||||
* @parma length String length
|
|
||||||
*/
|
|
||||||
void println(const char * str, size_t length);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Write buffer to UART.
|
* @brief Write buffer to UART.
|
||||||
*
|
*
|
||||||
@ -160,39 +24,26 @@ namespace sta
|
|||||||
*/
|
*/
|
||||||
virtual void write(const uint8_t * buffer, size_t size) = 0;
|
virtual void write(const uint8_t * buffer, size_t size) = 0;
|
||||||
|
|
||||||
private:
|
|
||||||
/**
|
/**
|
||||||
* @brief Print unsigned integer in selected base.
|
* @brief Write unsigned integer to UART.
|
||||||
*
|
*
|
||||||
* @param value Unsigned integer value
|
* @param value Unsigned integer value
|
||||||
* @param base Integer base
|
|
||||||
* @param fmt printf format string for base 10
|
|
||||||
* @param size Size of value in bytes
|
|
||||||
*/
|
*/
|
||||||
void printBase(uintmax_t value, IntegerBase base, const char * fmt, size_t size);
|
void write(uint8_t value);
|
||||||
/**
|
/**
|
||||||
* @brief Print unsigned integer in base 10.
|
* @brief Write unsigned integer to UART.
|
||||||
*
|
*
|
||||||
* @param value Unsigned integer value
|
* @param value Unsigned integer value
|
||||||
* @param fmt printf format string
|
|
||||||
*/
|
*/
|
||||||
void printDec(uintmax_t value, const char * fmt);
|
void write(uint16_t value);
|
||||||
/**
|
/**
|
||||||
* @brief Print unsigned integer in base 2.
|
* @brief Write unsigned integer to UART.
|
||||||
*
|
*
|
||||||
* @param value Unsigned integer value
|
* @param value Unsigned integer value
|
||||||
* @param digits Number of digits to print
|
|
||||||
*/
|
*/
|
||||||
void printBin(uintmax_t value, size_t digits);
|
void write(uint32_t value);
|
||||||
/**
|
|
||||||
* @brief Print unsigned integer in base 16.
|
|
||||||
*
|
|
||||||
* @param value Unsigned integer value
|
|
||||||
* @param digits Number of digits to print
|
|
||||||
*/
|
|
||||||
void printHex(uintmax_t value, size_t digits);
|
|
||||||
};
|
};
|
||||||
} // namespace sta
|
} // namespace sta
|
||||||
|
|
||||||
|
|
||||||
#endif // STA_UART_HPP
|
#endif // STA_INTF_UART_HPP
|
||||||
|
196
include/sta/printable_uart.hpp
Normal file
196
include/sta/printable_uart.hpp
Normal file
@ -0,0 +1,196 @@
|
|||||||
|
/**
|
||||||
|
* @brief UART interface definition.
|
||||||
|
*/
|
||||||
|
#ifndef STA_PRINTABLE_UART_HPP
|
||||||
|
#define STA_PRINTABLE_UART_HPP
|
||||||
|
|
||||||
|
#include <sta/intf/uart.hpp>
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
|
||||||
|
namespace sta
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @brief Integer representation.
|
||||||
|
*/
|
||||||
|
enum class IntegerBase
|
||||||
|
{
|
||||||
|
DEC, /**< Decimal */
|
||||||
|
BIN, /**< Binary */
|
||||||
|
HEX /**< Hexadecimal */
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Interface for UART.
|
||||||
|
*/
|
||||||
|
class PrintableUART
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
PrintableUART(UART * intf);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Print single character.
|
||||||
|
*
|
||||||
|
* @param c Character to print
|
||||||
|
*/
|
||||||
|
void print(char c);
|
||||||
|
/**
|
||||||
|
* @brief Print boolean value.
|
||||||
|
*
|
||||||
|
* @param b Boolean value
|
||||||
|
*/
|
||||||
|
void print(bool b);
|
||||||
|
/**
|
||||||
|
* @brief Print floating point value.
|
||||||
|
*
|
||||||
|
* @param d Floating point value
|
||||||
|
*/
|
||||||
|
void print(double d);
|
||||||
|
/**
|
||||||
|
* @brief Print integer in selected base.
|
||||||
|
*
|
||||||
|
* @param num 8-bit unsigned integer
|
||||||
|
* @param base Integer base
|
||||||
|
*/
|
||||||
|
void print(uint8_t num, IntegerBase base = IntegerBase::DEC);
|
||||||
|
/**
|
||||||
|
* @brief Print integer in selected base.
|
||||||
|
*
|
||||||
|
* @param num 16-bit unsigned integer
|
||||||
|
* @param base Integer base
|
||||||
|
*/
|
||||||
|
void print(uint16_t num, IntegerBase base = IntegerBase::DEC);
|
||||||
|
/**
|
||||||
|
* @brief Print integer in selected base.
|
||||||
|
*
|
||||||
|
* @param num 32-bit unsigned integer
|
||||||
|
* @param base Integer base
|
||||||
|
*/
|
||||||
|
void print(uint32_t num, IntegerBase base = IntegerBase::DEC);
|
||||||
|
/**
|
||||||
|
* @brief Print integer in selected base.
|
||||||
|
*
|
||||||
|
* @param num Integer
|
||||||
|
* @param base Integer base
|
||||||
|
*/
|
||||||
|
void print(size_t num, IntegerBase base = IntegerBase::DEC);
|
||||||
|
/**
|
||||||
|
* @brief Print c-string.
|
||||||
|
*
|
||||||
|
* @param str Null terminated string
|
||||||
|
*/
|
||||||
|
void print(const char * str);
|
||||||
|
/**
|
||||||
|
* @brief Print string.
|
||||||
|
*
|
||||||
|
* @param str String buffer
|
||||||
|
* @parma length String length
|
||||||
|
*/
|
||||||
|
void print(const char * str, size_t length);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Print new-line.
|
||||||
|
*/
|
||||||
|
void println();
|
||||||
|
/**
|
||||||
|
* @brief Print single character followed by a new-line.
|
||||||
|
*
|
||||||
|
* @param c Character to print
|
||||||
|
*/
|
||||||
|
void println(char c);
|
||||||
|
/**
|
||||||
|
* @brief Print boolean value followed by a new-line.
|
||||||
|
*
|
||||||
|
* @param b Boolean value
|
||||||
|
*/
|
||||||
|
void println(bool b);
|
||||||
|
/**
|
||||||
|
* @brief Print floating point value followed by a new-line.
|
||||||
|
*
|
||||||
|
* @param d Floating point value
|
||||||
|
*/
|
||||||
|
void println(double d);
|
||||||
|
/**
|
||||||
|
* @brief Print integer in selected base followed by a new-line.
|
||||||
|
*
|
||||||
|
* @param num 8-bit unsigned integer
|
||||||
|
* @param base Integer base
|
||||||
|
*/
|
||||||
|
void println(uint8_t num, IntegerBase base = IntegerBase::DEC);
|
||||||
|
/**
|
||||||
|
* @brief Print integer in selected base followed by a new-line.
|
||||||
|
*
|
||||||
|
* @param num 16-bit unsigned integer
|
||||||
|
* @param base Integer base
|
||||||
|
*/
|
||||||
|
void println(uint16_t num, IntegerBase base = IntegerBase::DEC);
|
||||||
|
/**
|
||||||
|
* @brief Print integer in selected base followed by a new-line.
|
||||||
|
*
|
||||||
|
* @param num 32-bit unsigned integer
|
||||||
|
* @param base Integer base
|
||||||
|
*/
|
||||||
|
void println(uint32_t num, IntegerBase base = IntegerBase::DEC);
|
||||||
|
/**
|
||||||
|
* @brief Print integer in selected base followed by a new-line.
|
||||||
|
*
|
||||||
|
* @param num Integer
|
||||||
|
* @param base Integer base
|
||||||
|
*/
|
||||||
|
void println(size_t num, IntegerBase base = IntegerBase::DEC);
|
||||||
|
/**
|
||||||
|
* @brief Print c-string followed by a new-line.
|
||||||
|
*
|
||||||
|
* @param str Null terminated string
|
||||||
|
*/
|
||||||
|
void println(const char * str);
|
||||||
|
/**
|
||||||
|
* @brief Print string followed by a new-line.
|
||||||
|
*
|
||||||
|
* @param str String buffer
|
||||||
|
* @parma length String length
|
||||||
|
*/
|
||||||
|
void println(const char * str, size_t length);
|
||||||
|
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* @brief Print unsigned integer in selected base.
|
||||||
|
*
|
||||||
|
* @param value Unsigned integer value
|
||||||
|
* @param base Integer base
|
||||||
|
* @param fmt printf format string for base 10
|
||||||
|
* @param size Size of value in bytes
|
||||||
|
*/
|
||||||
|
void printBase(uintmax_t value, IntegerBase base, const char * fmt, size_t size);
|
||||||
|
/**
|
||||||
|
* @brief Print unsigned integer in base 10.
|
||||||
|
*
|
||||||
|
* @param value Unsigned integer value
|
||||||
|
* @param fmt printf format string
|
||||||
|
*/
|
||||||
|
void printDec(uintmax_t value, const char * fmt);
|
||||||
|
/**
|
||||||
|
* @brief Print unsigned integer in base 2.
|
||||||
|
*
|
||||||
|
* @param value Unsigned integer value
|
||||||
|
* @param digits Number of digits to print
|
||||||
|
*/
|
||||||
|
void printBin(uintmax_t value, size_t digits);
|
||||||
|
/**
|
||||||
|
* @brief Print unsigned integer in base 16.
|
||||||
|
*
|
||||||
|
* @param value Unsigned integer value
|
||||||
|
* @param digits Number of digits to print
|
||||||
|
*/
|
||||||
|
void printHex(uintmax_t value, size_t digits);
|
||||||
|
|
||||||
|
private:
|
||||||
|
UART * intf_;
|
||||||
|
};
|
||||||
|
} // namespace sta
|
||||||
|
|
||||||
|
|
||||||
|
#endif // STA_PRINTABLE_UART_HPP
|
@ -34,7 +34,7 @@ namespace sta
|
|||||||
HalUART gHalDebugSerial(&STA_HAL_UART_DEBUG_SERIAL);
|
HalUART gHalDebugSerial(&STA_HAL_UART_DEBUG_SERIAL);
|
||||||
|
|
||||||
// Used by <sta/debug.hpp>
|
// Used by <sta/debug.hpp>
|
||||||
UART * const DebugSerial = &gHalDebugSerial;
|
PrintableUART DebugSerial(&gHalDebugSerial);
|
||||||
} // namespace sta
|
} // namespace sta
|
||||||
|
|
||||||
#endif // STA_HAL_UART_DEBUG_SERIAL
|
#endif // STA_HAL_UART_DEBUG_SERIAL
|
||||||
|
@ -9,195 +9,21 @@
|
|||||||
|
|
||||||
namespace sta
|
namespace sta
|
||||||
{
|
{
|
||||||
void UART::print(char c)
|
void UART::write(uint8_t value)
|
||||||
{
|
{
|
||||||
print(&c, 1);
|
// TODO Handle endian-ness
|
||||||
|
write(&value, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void UART::print(bool b)
|
void UART::write(uint16_t value)
|
||||||
{
|
{
|
||||||
print(b ? "true" : "false");
|
// TODO Handle endian-ness
|
||||||
|
write(reinterpret_cast<uint8_t *>(&value), sizeof(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
void UART::print(double d)
|
void UART::write(uint32_t value)
|
||||||
{
|
{
|
||||||
char buffer[64];
|
// TODO Handle endian-ness
|
||||||
snprintf(buffer, sizeof(buffer), "%f", d);
|
write(reinterpret_cast<uint8_t *>(&value), sizeof(value));
|
||||||
print(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void UART::print(uint8_t num, IntegerBase base /* = IntegerBase::DEC */)
|
|
||||||
{
|
|
||||||
printBase(num, base, "%" PRIu8, sizeof(num));
|
|
||||||
}
|
|
||||||
|
|
||||||
void UART::print(uint16_t num, IntegerBase base /* = IntegerBase::DEC */)
|
|
||||||
{
|
|
||||||
printBase(num, base, "%" PRIu16, sizeof(num));
|
|
||||||
}
|
|
||||||
|
|
||||||
void UART::print(uint32_t num, IntegerBase base /* = IntegerBase::DEC */)
|
|
||||||
{
|
|
||||||
printBase(num, base, "%" PRIu32, sizeof(num));
|
|
||||||
}
|
|
||||||
|
|
||||||
void UART::print(size_t num, IntegerBase base /* = IntegerBase::DEC */)
|
|
||||||
{
|
|
||||||
printBase(num, base, "%z", sizeof(num));
|
|
||||||
}
|
|
||||||
|
|
||||||
void UART::print(const char * str)
|
|
||||||
{
|
|
||||||
print(str, strlen(str));
|
|
||||||
}
|
|
||||||
|
|
||||||
void UART::print(const char * str, size_t length)
|
|
||||||
{
|
|
||||||
write(reinterpret_cast<const uint8_t *>(str), length);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void UART::println()
|
|
||||||
{
|
|
||||||
print("\r\n", 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
void UART::println(char c)
|
|
||||||
{
|
|
||||||
print(&c, 1);
|
|
||||||
println();
|
|
||||||
}
|
|
||||||
|
|
||||||
void UART::println(bool b)
|
|
||||||
{
|
|
||||||
print(b);
|
|
||||||
println();
|
|
||||||
}
|
|
||||||
|
|
||||||
void UART::println(double d)
|
|
||||||
{
|
|
||||||
print(d);
|
|
||||||
println();
|
|
||||||
}
|
|
||||||
|
|
||||||
void UART::println(uint8_t num, IntegerBase base /* = IntegerBase::DEC */)
|
|
||||||
{
|
|
||||||
print(num, base);
|
|
||||||
println();
|
|
||||||
}
|
|
||||||
|
|
||||||
void UART::println(uint16_t num, IntegerBase base /* = IntegerBase::DEC */)
|
|
||||||
{
|
|
||||||
print(num, base);
|
|
||||||
println();
|
|
||||||
}
|
|
||||||
|
|
||||||
void UART::println(uint32_t num, IntegerBase base /* = IntegerBase::DEC */)
|
|
||||||
{
|
|
||||||
print(num, base);
|
|
||||||
println();
|
|
||||||
}
|
|
||||||
|
|
||||||
void UART::println(size_t num, IntegerBase base /* = IntegerBase::DEC */)
|
|
||||||
{
|
|
||||||
print(num, base);
|
|
||||||
println();
|
|
||||||
}
|
|
||||||
|
|
||||||
void UART::println(const char * str)
|
|
||||||
{
|
|
||||||
println(str, strlen(str));
|
|
||||||
}
|
|
||||||
|
|
||||||
void UART::println(const char * str, size_t length)
|
|
||||||
{
|
|
||||||
print(str, length);
|
|
||||||
println();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void UART::printBase(uintmax_t num, IntegerBase base, const char * fmt, size_t size)
|
|
||||||
{
|
|
||||||
switch (base)
|
|
||||||
{
|
|
||||||
case IntegerBase::DEC:
|
|
||||||
printDec(num, fmt);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case IntegerBase::BIN:
|
|
||||||
// Digits in base 2 = size in bytes * 8
|
|
||||||
printBin(num, size * 8);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case IntegerBase::HEX:
|
|
||||||
// Digits in base 16 = size in bytes * 2
|
|
||||||
printHex(num, size * 2);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
print("<invalid_base>");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void UART::printDec(uintmax_t num, const char * fmt)
|
|
||||||
{
|
|
||||||
char buffer[64];
|
|
||||||
snprintf(buffer, sizeof(buffer), fmt, static_cast<uint32_t>(num));
|
|
||||||
print(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void UART::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 UART::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
|
} // namespace sta
|
||||||
|
211
src/printable_uart.cpp
Normal file
211
src/printable_uart.cpp
Normal file
@ -0,0 +1,211 @@
|
|||||||
|
#include <sta/printable_uart.hpp>
|
||||||
|
|
||||||
|
#include <sta/assert.hpp>
|
||||||
|
#include <sta/printf.hpp>
|
||||||
|
|
||||||
|
#include <cinttypes>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
namespace sta
|
||||||
|
{
|
||||||
|
PrintableUART::PrintableUART(UART * intf)
|
||||||
|
: intf_{intf}
|
||||||
|
{
|
||||||
|
STA_ASSERT(intf != nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PrintableUART::print(char c)
|
||||||
|
{
|
||||||
|
print(&c, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintableUART::print(bool b)
|
||||||
|
{
|
||||||
|
print(b ? "true" : "false");
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintableUART::print(double d)
|
||||||
|
{
|
||||||
|
char buffer[64];
|
||||||
|
snprintf(buffer, sizeof(buffer), "%f", d);
|
||||||
|
print(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintableUART::print(uint8_t num, IntegerBase base /* = IntegerBase::DEC */)
|
||||||
|
{
|
||||||
|
printBase(num, base, "%" PRIu8, sizeof(num));
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintableUART::print(uint16_t num, IntegerBase base /* = IntegerBase::DEC */)
|
||||||
|
{
|
||||||
|
printBase(num, base, "%" PRIu16, sizeof(num));
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintableUART::print(uint32_t num, IntegerBase base /* = IntegerBase::DEC */)
|
||||||
|
{
|
||||||
|
printBase(num, base, "%" PRIu32, sizeof(num));
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintableUART::print(size_t num, IntegerBase base /* = IntegerBase::DEC */)
|
||||||
|
{
|
||||||
|
printBase(num, base, "%z", sizeof(num));
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintableUART::print(const char * str)
|
||||||
|
{
|
||||||
|
print(str, strlen(str));
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintableUART::print(const char * str, size_t length)
|
||||||
|
{
|
||||||
|
intf_->write(reinterpret_cast<const uint8_t *>(str), length);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PrintableUART::println()
|
||||||
|
{
|
||||||
|
print("\r\n", 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintableUART::println(char c)
|
||||||
|
{
|
||||||
|
print(&c, 1);
|
||||||
|
println();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintableUART::println(bool b)
|
||||||
|
{
|
||||||
|
print(b);
|
||||||
|
println();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintableUART::println(double d)
|
||||||
|
{
|
||||||
|
print(d);
|
||||||
|
println();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintableUART::println(uint8_t num, IntegerBase base /* = IntegerBase::DEC */)
|
||||||
|
{
|
||||||
|
print(num, base);
|
||||||
|
println();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintableUART::println(uint16_t num, IntegerBase base /* = IntegerBase::DEC */)
|
||||||
|
{
|
||||||
|
print(num, base);
|
||||||
|
println();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintableUART::println(uint32_t num, IntegerBase base /* = IntegerBase::DEC */)
|
||||||
|
{
|
||||||
|
print(num, base);
|
||||||
|
println();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintableUART::println(size_t num, IntegerBase base /* = IntegerBase::DEC */)
|
||||||
|
{
|
||||||
|
print(num, base);
|
||||||
|
println();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintableUART::println(const char * str)
|
||||||
|
{
|
||||||
|
println(str, strlen(str));
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintableUART::println(const char * str, size_t length)
|
||||||
|
{
|
||||||
|
print(str, length);
|
||||||
|
println();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void PrintableUART::printBase(uintmax_t num, IntegerBase base, const char * fmt, size_t size)
|
||||||
|
{
|
||||||
|
switch (base)
|
||||||
|
{
|
||||||
|
case IntegerBase::DEC:
|
||||||
|
printDec(num, fmt);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case IntegerBase::BIN:
|
||||||
|
// Digits in base 2 = size in bytes * 8
|
||||||
|
printBin(num, size * 8);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case IntegerBase::HEX:
|
||||||
|
// Digits in base 16 = size in bytes * 2
|
||||||
|
printHex(num, size * 2);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
print("<invalid_base>");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintableUART::printDec(uintmax_t num, const char * fmt)
|
||||||
|
{
|
||||||
|
char buffer[64];
|
||||||
|
snprintf(buffer, sizeof(buffer), fmt, static_cast<uint32_t>(num));
|
||||||
|
print(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintableUART::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 PrintableUART::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
|
Loading…
x
Reference in New Issue
Block a user