add documentation
This commit is contained in:
parent
34def405e0
commit
fe5e5209c6
41
lib/print.c
41
lib/print.c
|
@ -25,14 +25,19 @@
|
|||
/* own libraries */
|
||||
#include "print.h" // printing utilities
|
||||
|
||||
/** output \r\n (Carriage Return + Line Feed) for each \r, \n, \r\n, or \n\r for better terminal compatibility */
|
||||
static const bool crlf = true;
|
||||
/** @defgroup print_crlf output \r\n (Carriage Return + Line Feed) for each \r, \n, \r\n, or \n\r for better terminal compatibility
|
||||
* @{
|
||||
**/
|
||||
#define CRLF true /**< if CR+LN new line should be enforced */
|
||||
/** @} */
|
||||
|
||||
uint8_t putc(char c)
|
||||
{
|
||||
uint8_t length = 0; // number of characters printed
|
||||
static char newline = 0; // to remember on which character we sent the newline
|
||||
if (!crlf) {
|
||||
if (0==c) {
|
||||
length = 0; // don't print string termination character
|
||||
} else if (!CRLF) {
|
||||
_putc(c); // print character
|
||||
length++; // remember we printed 1 character
|
||||
} else if ('\r' == c || '\n' == c) { // send CR+LF newline for most carriage return and line feed combination
|
||||
|
@ -59,6 +64,12 @@ uint32_t puts(const char* s)
|
|||
return length;
|
||||
}
|
||||
|
||||
/** print unsigned number
|
||||
* @param[in] u unsigned number to be printed
|
||||
* @param[in] padding number of 0's to pad
|
||||
* @param[in] sign if sign should be printed
|
||||
* @return number of characters printed
|
||||
**/
|
||||
static uint8_t print_unsigned(uint64_t u, uint8_t padding, bool sign) {
|
||||
char number[20] = {0}; // construct the number in reverse order (20 chars are required to store UINT64_MAX)
|
||||
uint8_t digits = 0; // to count the number of digits
|
||||
|
@ -82,6 +93,12 @@ static uint8_t print_unsigned(uint64_t u, uint8_t padding, bool sign) {
|
|||
return length; // return number of characters printed
|
||||
}
|
||||
|
||||
/** print signed number
|
||||
* @param[in] d unsigned number to be printed
|
||||
* @param[in] padding number of 0's to pad
|
||||
* @param[in] sign if sign should be printed
|
||||
* @return number of characters printed
|
||||
**/
|
||||
static uint8_t print_signed(int64_t d, uint8_t padding, bool sign) {
|
||||
uint8_t length = 0; // number of characters printed
|
||||
if (d<0) {
|
||||
|
@ -93,6 +110,11 @@ static uint8_t print_signed(int64_t d, uint8_t padding, bool sign) {
|
|||
return length; // return number of characters printed
|
||||
}
|
||||
|
||||
/** print nibble (half-byte)
|
||||
* @param[in] nibble nibble to be printed
|
||||
* @param[in] upcase use upcase digits (A-F)
|
||||
* @return number of characters printed
|
||||
**/
|
||||
static uint8_t print_nibble(uint8_t nibble, bool upcase) {
|
||||
uint8_t length = 0; // number of characters printed
|
||||
nibble &= 0x0f; // ensure we only have a nibble
|
||||
|
@ -106,6 +128,13 @@ static uint8_t print_nibble(uint8_t nibble, bool upcase) {
|
|||
return length; // return number of characters printed
|
||||
}
|
||||
|
||||
/** print hex value
|
||||
* @param[in] hex hex value to be printed
|
||||
* @param[in] padding number of 0's to pad
|
||||
* @param[in] prefix if 0x prefix should be printed
|
||||
* @param[in] upcase use upcase digits (A-F)
|
||||
* @return number of characters printed
|
||||
**/
|
||||
static uint8_t print_hex(uint32_t hex, uint8_t padding, bool prefix, bool upcase) {
|
||||
uint8_t length = 0; // number of characters printed
|
||||
if (prefix) { // print 0x prefix
|
||||
|
@ -132,6 +161,12 @@ static uint8_t print_hex(uint32_t hex, uint8_t padding, bool prefix, bool upcase
|
|||
return length; // return number of characters printed
|
||||
}
|
||||
|
||||
/** print bits
|
||||
* @param[in] u bits to be printed
|
||||
* @param[in] padding number of 0's to pad
|
||||
* @param[in] prefix if 0b prefix should be printed
|
||||
* @return number of characters printed
|
||||
**/
|
||||
static uint8_t print_bits(uint32_t u, uint8_t padding, bool prefix) {
|
||||
char bits[32] = {0}; // construct the bit string in reverse order
|
||||
uint8_t digits = 0; // to count the number of digits
|
||||
|
|
20
lib/print.h
20
lib/print.h
|
@ -19,7 +19,27 @@
|
|||
*/
|
||||
#pragma once
|
||||
|
||||
/** print a single character
|
||||
* @warning this must be implemented by the user (using the desired output interface)
|
||||
* @param[in] c character to be printed
|
||||
**/
|
||||
void _putc(char c);
|
||||
/** print character
|
||||
* @note a CR+LF new line can be enforced in the code using @ref print_crlf
|
||||
* @param[in] c character to be printed
|
||||
* @return number of characters printed
|
||||
*/
|
||||
uint8_t putc(char c);
|
||||
/** print string
|
||||
* @param[in] s string to be printed
|
||||
* @return number of characters printed (excluding null termination)
|
||||
*/
|
||||
uint32_t puts(const char* s);
|
||||
/** print format string
|
||||
* use % as format specifier prefix, followed by + to enforce sign of prefix, 0 and 0-9 for padding, and format specifier
|
||||
* format specifier supported are: c for far, s for string, u for uint32_t, d for int32_t, U for uint64_t, D for int64_t, x for lower case hex up to uint32_t, X for upper case hex up to uint32_t, b for bits up to uint32_t
|
||||
* @param[in] fmt format string to be printed
|
||||
* @param[in] ... arguments to to be formated
|
||||
* @return number of characters printed (excluding null termination)
|
||||
*/
|
||||
uint32_t printf(const char *fmt, ...);
|
||||
|
|
Loading…
Reference in New Issue