From 18bc0ce5fc6c49d5cabed3d2fe43ee92b480bddc Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Thu, 13 Jun 2019 14:54:52 +0200 Subject: [PATCH] tests/driver_ltc4150: Use new fmt_table API --- tests/driver_ltc4150/Makefile | 2 +- tests/driver_ltc4150/main.c | 175 ++++++++++------------------------ 2 files changed, 52 insertions(+), 125 deletions(-) diff --git a/tests/driver_ltc4150/Makefile b/tests/driver_ltc4150/Makefile index ac50bbb77e..c6797ae4a0 100644 --- a/tests/driver_ltc4150/Makefile +++ b/tests/driver_ltc4150/Makefile @@ -4,7 +4,7 @@ BOARD_INSUFFICIENT_MEMORY += arduino-duemilanove arduino-leonardo arduino-nano \ arduino-uno BOARD ?= msba2 -USEMODULE += fmt +USEMODULE += fmt_table USEMODULE += ltc4150 include $(RIOTBASE)/Makefile.include diff --git a/tests/driver_ltc4150/main.c b/tests/driver_ltc4150/main.c index a539c78ae2..0591d43bae 100644 --- a/tests/driver_ltc4150/main.c +++ b/tests/driver_ltc4150/main.c @@ -19,9 +19,9 @@ */ #include -#include #include "fmt.h" +#include "fmt_table.h" #include "led.h" #include "ltc4150.h" #include "thread.h" @@ -62,22 +62,6 @@ static void *recorder_data[] = { #include "ltc4150_params.h" -/** - * @brief Like `puts()`, but do not append a newline - * - * Normally I would just use `fputs(str, stdout)` directly, but the msp430 - * toolchain lacks `fputs()`. This wrapper allows to add a less efficient - * fallback to printf() - */ -static inline void puts_no_nl(const char *s) -{ -#ifndef NO_FPUTS - fputs(s, stdout); -#else - printf("%s", s); -#endif -} - /** * @brief Callback function to reset/initialize the recorder data */ @@ -151,62 +135,6 @@ static void *busy_thread(void *arg) return NULL; } -/** - * @brief Print the given number of spaces - */ -static void print_spaces(size_t number) -{ - static const char *spaces = " "; - while (number > 16) { - puts_no_nl(spaces); - number -= 16; - } - - puts_no_nl(spaces + 16 - number); -} - -/** - * @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 - */ -static void print_col_u32(uint32_t number, size_t width) -{ - char sbuf[32]; - size_t slen; - - slen = fmt_u32_dec(sbuf, number); - sbuf[slen] = '\0'; - if (width > slen) { - print_spaces(width - slen); - } - puts_no_nl(sbuf); -} - -/** - * @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 - */ -static void print_col_i32(int32_t number, size_t width) -{ - char sbuf[32]; - size_t slen; - char *pos = sbuf; - - if (number < 0) { - *pos++ = '-'; - number = -number; - width--; - } - slen = fmt_u32_dec(sbuf, (uint32_t)number); - sbuf[slen] = '\0'; - if (width > slen) { - print_spaces(width - slen); - } - puts_no_nl(sbuf); -} - /** * @brief Print a table column with the given current as E-01 * @param current Value to print in the column (as E-01) @@ -214,13 +142,12 @@ static void print_col_i32(int32_t number, size_t width) */ static void print_current(int32_t current, size_t width) { - char sbuf[3]; + char sbuf[2]; - print_col_i32(current/10, width - 2); + print_col_s32_dec(current/10, width - 2); sbuf[0] = '.'; sbuf[1] = '0' + current % 10; - sbuf[2] = '\0'; - puts_no_nl(sbuf); + print(sbuf, 2); } int main(void) @@ -236,16 +163,16 @@ int main(void) ltc4150_pulses2c(<c4150, &ten_uc_per_pulse, NULL, 10000, 0); if (retval) { - puts_no_nl("Failed to initialize LTC4150 driver:"); + print_str("Failed to initialize LTC4150 driver:"); switch (retval) { case -EINVAL: - puts("Invalid parameter"); + print_str("Invalid parameter\n"); break; case -EIO: - puts("GPIO or interrupt configuration failed"); + print_str("GPIO or interrupt configuration failed\n"); break; default: - puts("Unknown (should no happen, file a bug)"); + print_str("Unknown (should no happen, file a bug)\n"); break; } return -1; @@ -256,39 +183,39 @@ int main(void) THREAD_PRIORITY_MAIN + 1, THREAD_CREATE_STACKTEST, busy_thread, NULL, "busy_thread"); - puts("This test will put three levels of load on the MCU:\n" - " 1. One minute of little to no load (LEDs(*) off)\n" - " 2. One minute of about 50% CPU load (LEDs(*) blinking)\n" - " 3. One minute of 100% CPU load (LEDs(*) constantly on)\n" - "\n" - " (*) LED0 and LED1, if present on your board\n" - "\n" - "During this time the charge drawn is measured and printed on every\n" - "pulse the LTC4150 generates. A horizontal line in the table\n" - "separates values of different load levels. On the MSB-A2 the\n" - "expected result per column is:\n" - "\n" - " Charging: Should remain zero\n" - " Discharging: Should increase for every pulse\n" - " Average: Should be something between 60mA to 80mA\n" - " Last Minute: Starts with 0 at boot up and is updated every 10s.\n" - " Should be higher for higher system load when looking\n" - " at the last update for each load level.\n" - " (Note: Not synchronized with load levels!)\n" - " Currently: Should be higher for higher system load. Might be\n" - " \"jumpy\" on 50% load, as this implemented by having\n" - " one second of 100% load and one second of ~0% load\n" - " in turns.\n" - "\n" - "Hint: You'll want to look mostly at the rightmost column.\n" - "Note: The test will repeat endlessly."); + print_str("This test will put three levels of load on the MCU:\n" + " 1. One minute of little to no load (LEDs(*) off)\n" + " 2. One minute of about 50% CPU load (LEDs(*) blinking)\n" + " 3. One minute of 100% CPU load (LEDs(*) constantly on)\n" + "\n" + " (*) LED0 and LED1, if present on your board\n" + "\n" + "During this time the charge drawn is measured and printed on every\n" + "pulse the LTC4150 generates. A horizontal line in the table\n" + "separates values of different load levels. On the MSB-A2 the\n" + "expected result per column is:\n" + "\n" + " Charging: Should remain zero\n" + " Discharging: Should increase for every pulse\n" + " Average: Should be something between 60mA to 80mA\n" + " Last Minute: Starts with 0 at boot up and is updated every 10s.\n" + " Should be higher for higher system load when looking\n" + " at the last update for each load level.\n" + " (Note: Not synchronized with load levels!)\n" + " Currently: Should be higher for higher system load. Might be\n" + " \"jumpy\" on 50% load, as this implemented by having\n" + " one second of 100% load and one second of ~0% load\n" + " in turns.\n" + "\n" + "Hint: You'll want to look mostly at the rightmost column.\n" + "Note: The test will repeat endlessly.\n"); LED0_OFF; - puts("+-------------------------------+-----------------------------------+\n" - "| Total Transferred Charge [mC] | Current from Power Supply [mA] |\n" - "| Charging | Discharging | Average | Last Minute | Currently |\n" - "+---------------+---------------+---------+-------------+-----------+"); + print_str("+-------------------------------+-----------------------------------+\n" + "| Total Transferred Charge [mC] | Current from Power Supply [mA] |\n" + "| Charging | Discharging | Average | Last Minute | Currently |\n" + "+---------------+---------------+---------+-------------+-----------+\n"); while (1) { /* Wait for the next pulse of the LTC4150 */ @@ -298,40 +225,40 @@ int main(void) int32_t current; if (change_of_load_level) { - puts("+---------------+---------------+---------+-------------+-----------+"); + print_str("+---------------+---------------+---------+-------------+-----------+\n"); change_of_load_level = 0; } /* Get & print total charge transferred */ if (ltc4150_charge(<c4150, &charged, &discharged)) { - puts("ltc4150_charge() failed!"); + print_str("ltc4150_charge() failed!\n"); return -1; } - puts_no_nl("| "); - print_col_u32(charged, 13); - puts_no_nl(" | "); - print_col_u32(discharged, 13); - puts_no_nl(" | "); + print_str("| "); + print_col_u32_dec(charged, 13); + print_str(" | "); + print_col_u32_dec(discharged, 13); + print_str(" | "); /* Get & print avg current */ if (ltc4150_avg_current(<c4150, &avg_current)) { - puts("ltc4150_avg_current() failed!"); + print_str("ltc4150_avg_current() failed!\n"); return -1; } print_current(avg_current, 7); - puts_no_nl(" | "); + print_str(" | "); /* Get & print last minute current */ if (ltc4150_last_minute_charge(<c4150, &last_minute_data, &charged, &discharged) ) { - puts("ltc4150_last_minute_charge() failed!"); + print_str("ltc4150_last_minute_charge() failed!\n"); return -1; } current = (int32_t)discharged - (int32_t)charged; current /= 60; - print_col_i32(current, 11); - puts_no_nl(" | "); + print_col_s32_dec(current, 11); + print_str(" | "); /* Calculate & print the current between the last two pulses */ current = (int32_t)((test_data.now_usec - test_data.last_usec) / MS_PER_SEC); @@ -340,7 +267,7 @@ int main(void) current = -current; } print_current(current, 9); - puts(" |"); + print_str(" |\n"); } return 0;