diff --git a/include/sta/debug_serial.hpp b/include/sta/debug_serial.hpp index 96c9e68..c997e23 100644 --- a/include/sta/debug_serial.hpp +++ b/include/sta/debug_serial.hpp @@ -30,22 +30,20 @@ #ifdef STA_DEBUG_SERIAL_ENABLE -#include +#include namespace sta { - extern UART * const DebugSerial; + extern PrintableUART DebugSerial; } // namespace sta -# define STA_DEBUG_PRINT(...) sta::DebugSerial->print(__VA_ARGS__) -# define STA_DEBUG_PRINTLN(...) sta::DebugSerial->println(__VA_ARGS__) -# define STA_DEBUG_WRITE(b, s) sta::DebugSerial->write(b, s) +# define STA_DEBUG_PRINT(...) sta::DebugSerial.print(__VA_ARGS__) +# define STA_DEBUG_PRINTLN(...) sta::DebugSerial.println(__VA_ARGS__) #else // !STA_DEBUG_SERIAL_ENABLE # define STA_DEBUG_PRINT(...) ((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_HPP diff --git a/include/sta/hal/uart.hpp b/include/sta/hal/uart.hpp index b4baa9b..b6ed632 100644 --- a/include/sta/hal/uart.hpp +++ b/include/sta/hal/uart.hpp @@ -28,8 +28,6 @@ namespace sta */ HalUART(UART_HandleTypeDef * handle); - using UART::print; - void write(const uint8_t * buffer, size_t size) override; private: diff --git a/include/sta/intf/uart.hpp b/include/sta/intf/uart.hpp index a287bd7..2590f50 100644 --- a/include/sta/intf/uart.hpp +++ b/include/sta/intf/uart.hpp @@ -1,8 +1,8 @@ /** * @brief UART interface definition. */ -#ifndef STA_UART_HPP -#define STA_UART_HPP +#ifndef STA_INTF_UART_HPP +#define STA_INTF_UART_HPP #include #include @@ -10,148 +10,12 @@ namespace sta { - /** - * @brief Integer representation. - */ - enum class IntegerBase - { - DEC, /**< Decimal */ - BIN, /**< Binary */ - HEX /**< Hexadecimal */ - }; - /** * @brief Interface for UART. */ class UART { 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. * @@ -160,39 +24,26 @@ namespace sta */ 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 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 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 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); + void write(uint32_t value); }; } // namespace sta -#endif // STA_UART_HPP +#endif // STA_INTF_UART_HPP diff --git a/include/sta/printable_uart.hpp b/include/sta/printable_uart.hpp new file mode 100644 index 0000000..c85745a --- /dev/null +++ b/include/sta/printable_uart.hpp @@ -0,0 +1,196 @@ +/** + * @brief UART interface definition. + */ +#ifndef STA_PRINTABLE_UART_HPP +#define STA_PRINTABLE_UART_HPP + +#include + +#include +#include + + +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 diff --git a/src/hal/uart.cpp b/src/hal/uart.cpp index 9baa861..e349825 100644 --- a/src/hal/uart.cpp +++ b/src/hal/uart.cpp @@ -34,7 +34,7 @@ namespace sta HalUART gHalDebugSerial(&STA_HAL_UART_DEBUG_SERIAL); // Used by - UART * const DebugSerial = &gHalDebugSerial; + PrintableUART DebugSerial(&gHalDebugSerial); } // namespace sta #endif // STA_HAL_UART_DEBUG_SERIAL diff --git a/src/intf/uart.cpp b/src/intf/uart.cpp index 77b6cfd..9d06dae 100644 --- a/src/intf/uart.cpp +++ b/src/intf/uart.cpp @@ -9,195 +9,21 @@ 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(&value), sizeof(value)); } - void UART::print(double d) + void UART::write(uint32_t value) { - char buffer[64]; - snprintf(buffer, sizeof(buffer), "%f", d); - 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(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(""); - } - } - - void UART::printDec(uintmax_t num, const char * fmt) - { - char buffer[64]; - snprintf(buffer, sizeof(buffer), fmt, static_cast(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(""); - 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(""); - 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); + // TODO Handle endian-ness + write(reinterpret_cast(&value), sizeof(value)); } } // namespace sta diff --git a/src/printable_uart.cpp b/src/printable_uart.cpp new file mode 100644 index 0000000..5ff9920 --- /dev/null +++ b/src/printable_uart.cpp @@ -0,0 +1,211 @@ +#include + +#include +#include + +#include +#include + + + +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(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(""); + } + } + + void PrintableUART::printDec(uintmax_t num, const char * fmt) + { + char buffer[64]; + snprintf(buffer, sizeof(buffer), fmt, static_cast(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(""); + 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(""); + 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