diff --git a/makefiles/pseudomodules.inc.mk b/makefiles/pseudomodules.inc.mk index 1bbc7c0e43..cd8b31ddc6 100644 --- a/makefiles/pseudomodules.inc.mk +++ b/makefiles/pseudomodules.inc.mk @@ -12,6 +12,7 @@ PSEUDOMODULES += cpu_check_address PSEUDOMODULES += ecc_% PSEUDOMODULES += emb6_router PSEUDOMODULES += event_% +PSEUDOMODULES += fmt_% PSEUDOMODULES += gnrc_ipv6_default PSEUDOMODULES += gnrc_ipv6_router PSEUDOMODULES += gnrc_ipv6_router_default diff --git a/sys/Makefile.dep b/sys/Makefile.dep index f9afc587e3..7194dd5dd7 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -2,6 +2,10 @@ ifneq (,$(filter eepreg,$(USEMODULE))) FEATURES_REQUIRED += periph_eeprom endif +ifneq (,$(filter fmt_table,$(USEMODULE))) + USEMODULE += fmt +endif + ifneq (,$(filter i2c_scan,$(USEMODULE))) FEATURES_REQUIRED += periph_i2c endif diff --git a/sys/fmt/Makefile b/sys/fmt/Makefile index 48422e909a..062d4ea5c5 100644 --- a/sys/fmt/Makefile +++ b/sys/fmt/Makefile @@ -1 +1,7 @@ +# exclude submodule sources from *.c wildcard source selection +SRC := $(filter-out table.c,$(wildcard *.c)) + +# enable submodules +SUBMODULES := 1 + include $(RIOTBASE)/Makefile.base diff --git a/sys/fmt/table.c b/sys/fmt/table.c new file mode 100644 index 0000000000..19d1c0fa17 --- /dev/null +++ b/sys/fmt/table.c @@ -0,0 +1,74 @@ +/* + * Copyright 2019 Otto-von-Guericke-Universität Magdeburg + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup sys_fmt_table + * @{ + * + * @file + * @brief Implementation of the table extensions of the string formatting + * library + * + * @author Marian Buschsieweke + * + * @} + */ + +#include +#include +#include +#include +#include + +#include "fmt.h" + +static const char fmt_table_spaces[16] = " "; + +/** + * @brief Prints @p fill_size bytes of the given pattern, repeating the + * pattern if needed + * @param pat Pattern to print + * @param pat_size Size of the pattern in bytes + * @param fill_size Number of bytes to print (if bigger than @p pat_size, the + * pattern will be repeated) + * + * E.g. `print_pattern("ab", 2, 5);` will print `ababa` to the console. + * This can be used to fill table columns with spaces, draw lines, etc. + */ +static void print_pattern(const char *pat, size_t pat_size, size_t fill_size) +{ + while (fill_size > pat_size) { + print(pat, pat_size); + } + + print(pat, fill_size); +} + +void print_col_u32_dec(uint32_t number, size_t width) +{ + char sbuf[10]; /* "4294967295" */ + size_t slen; + + slen = fmt_u32_dec(sbuf, number); + if (width > slen) { + print_pattern(fmt_table_spaces, sizeof(fmt_table_spaces), width - slen); + } + print(sbuf, slen); +} + +void print_col_s32_dec(int32_t number, size_t width) +{ + char sbuf[11]; /* "-2147483648" */ + size_t slen; + + slen = fmt_s32_dec(sbuf, number); + if (width > slen) { + print_pattern(fmt_table_spaces, sizeof(fmt_table_spaces), width - slen); + } + print(sbuf, slen); +} diff --git a/sys/include/fmt_table.h b/sys/include/fmt_table.h new file mode 100644 index 0000000000..ecdb167263 --- /dev/null +++ b/sys/include/fmt_table.h @@ -0,0 +1,56 @@ +/* + * Copyright 2019 Otto-von-Guericke-Universität Magdeburg + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @defgroup sys_fmt_table Table extension of the string formatting API (fmt_table) + * @ingroup sys_fmt + * @brief Provides utilities to print tables. + * + * \note The print functions in this library do not buffer any output. + * Mixing calls to standard @c printf from stdio.h with the @c print_xxx + * functions in fmt, especially on the same output line, may cause garbled + * output. + * + * @{ + * + * @file + * @brief Table extension of the string formatting API + * + * @author Marian Buschsieweke + */ + +#ifndef FMT_TABLE_H +#define FMT_TABLE_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Print a table column with the given number as decimal + * @param number Number to print in the column + * @param width Width of the column + */ +void print_col_u32_dec(uint32_t number, size_t width); + +/** + * @brief Print a table column with the given number as decimal + * @param number Number to print in the column + * @param width Width of the column + */ +void print_col_s32_dec(int32_t number, size_t width); + +#ifdef __cplusplus +} +#endif + +/** @} */ +#endif /* FMT_TABLE_H */