/** * @file * @brief Printable UART interface definition. */ #ifndef STA_CORE_PRINTABLE_HPP #define STA_CORE_PRINTABLE_HPP #include #include namespace sta { /** * @brief Integer representation. * * @ingroup sta_core */ enum class IntegerBase { DEC, /**< Decimal */ BIN, /**< Binary */ HEX /**< Hexadecimal */ }; /** * @brief Printable UART interface. * * @ingroup sta_core */ class Printable { public: /** * @brief fmt The string defining the desired format. * @brief ... The parameters for the formatted string. */ void printf(const char * fmt, ...); /** * @brief Print single character. * * @param c Character to print * @param newline If true, send a \r\n to start a new line. */ void print(char c, bool newline = false); /** * @brief Print boolean value. * * @param b Boolean value * @param newline If true, send a \r\n to start a new line. */ void print(bool b, bool newline = false); /** * @brief Print 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, bool newline = false); /** * @brief Print integer in selected base. * * @param num 8-bit unsigned integer * @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, bool newline = false); /** * @brief Print integer in selected base. * * @param num 16-bit unsigned integer * @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, bool newline = false); /** * @brief Print integer in selected base. * * @param num 32-bit unsigned integer * @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, bool newline = false); /** * @brief Print c-string. * * @param str Null terminated string * @param newline If true, send a \r\n to start a new line. */ void print(const char * str, bool newline = false); /** * @brief Print string. * * @param str String buffer * @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, bool newline = false) = 0; /** * @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 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 * @param length String length */ void println(const char * str, size_t length); public: /** * @brief Read bytes. * * @param buffer Byte buffer * @param length Buffer length */ virtual void read(uint8_t * buffer, size_t length) = 0; /** * @brief Read string. * * @param str String buffer * @param length String length */ void read(char * str, size_t length); /** * @brief Read floats. * * @param buffer Float buffer * @param length Buffer length */ void read(float * buffer, size_t length); /** * @brief Read doubles. * * @param buffer Double buffer * @param length Buffer length */ void read(double * buffer, size_t length); public: virtual void request(uint8_t * txBuffer, size_t txLength, uint8_t * rxBuffer, size_t rxLength) = 0; void request(uint8_t * txBuffer, size_t txLength, float * rxBuffer, size_t rxLength); void request(uint8_t * txBuffer, size_t txLength, double * rxBuffer, size_t rxLenght); 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 * @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, bool newline = false); /** * @brief Print unsigned integer in base 10. * * @param value Unsigned integer value * @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, bool newline = false); /** * @brief Print unsigned integer in base 2. * * @param value Unsigned integer value * @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, bool newline = false); /** * @brief Print unsigned integer in base 16. * * @param value Unsigned integer value * @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, bool newline = false); }; } // namespace sta #endif // STA_CORE_PRINTABLE_HPP