tests/driver_ltc4150: Use new fmt_table API

This commit is contained in:
Marian Buschsieweke 2019-06-13 14:54:52 +02:00
parent 26d73116f6
commit 18bc0ce5fc
No known key found for this signature in database
GPG Key ID: 61F64C6599B1539F
2 changed files with 52 additions and 125 deletions

View File

@ -4,7 +4,7 @@ BOARD_INSUFFICIENT_MEMORY += arduino-duemilanove arduino-leonardo arduino-nano \
arduino-uno arduino-uno
BOARD ?= msba2 BOARD ?= msba2
USEMODULE += fmt USEMODULE += fmt_table
USEMODULE += ltc4150 USEMODULE += ltc4150
include $(RIOTBASE)/Makefile.include include $(RIOTBASE)/Makefile.include

View File

@ -19,9 +19,9 @@
*/ */
#include <errno.h> #include <errno.h>
#include <stdio.h>
#include "fmt.h" #include "fmt.h"
#include "fmt_table.h"
#include "led.h" #include "led.h"
#include "ltc4150.h" #include "ltc4150.h"
#include "thread.h" #include "thread.h"
@ -62,22 +62,6 @@ static void *recorder_data[] = {
#include "ltc4150_params.h" #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 * @brief Callback function to reset/initialize the recorder data
*/ */
@ -151,62 +135,6 @@ static void *busy_thread(void *arg)
return NULL; 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 * @brief Print a table column with the given current as E-01
* @param current Value to print in the column (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) 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[0] = '.';
sbuf[1] = '0' + current % 10; sbuf[1] = '0' + current % 10;
sbuf[2] = '\0'; print(sbuf, 2);
puts_no_nl(sbuf);
} }
int main(void) int main(void)
@ -236,16 +163,16 @@ int main(void)
ltc4150_pulses2c(&ltc4150, &ten_uc_per_pulse, NULL, 10000, 0); ltc4150_pulses2c(&ltc4150, &ten_uc_per_pulse, NULL, 10000, 0);
if (retval) { if (retval) {
puts_no_nl("Failed to initialize LTC4150 driver:"); print_str("Failed to initialize LTC4150 driver:");
switch (retval) { switch (retval) {
case -EINVAL: case -EINVAL:
puts("Invalid parameter"); print_str("Invalid parameter\n");
break; break;
case -EIO: case -EIO:
puts("GPIO or interrupt configuration failed"); print_str("GPIO or interrupt configuration failed\n");
break; break;
default: default:
puts("Unknown (should no happen, file a bug)"); print_str("Unknown (should no happen, file a bug)\n");
break; break;
} }
return -1; return -1;
@ -256,39 +183,39 @@ int main(void)
THREAD_PRIORITY_MAIN + 1, THREAD_CREATE_STACKTEST, THREAD_PRIORITY_MAIN + 1, THREAD_CREATE_STACKTEST,
busy_thread, NULL, "busy_thread"); busy_thread, NULL, "busy_thread");
puts("This test will put three levels of load on the MCU:\n" 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" " 1. One minute of little to no load (LEDs(*) off)\n"
" 2. One minute of about 50% CPU load (LEDs(*) blinking)\n" " 2. One minute of about 50% CPU load (LEDs(*) blinking)\n"
" 3. One minute of 100% CPU load (LEDs(*) constantly on)\n" " 3. One minute of 100% CPU load (LEDs(*) constantly on)\n"
"\n" "\n"
" (*) LED0 and LED1, if present on your board\n" " (*) LED0 and LED1, if present on your board\n"
"\n" "\n"
"During this time the charge drawn is measured and printed on every\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" "pulse the LTC4150 generates. A horizontal line in the table\n"
"separates values of different load levels. On the MSB-A2 the\n" "separates values of different load levels. On the MSB-A2 the\n"
"expected result per column is:\n" "expected result per column is:\n"
"\n" "\n"
" Charging: Should remain zero\n" " Charging: Should remain zero\n"
" Discharging: Should increase for every pulse\n" " Discharging: Should increase for every pulse\n"
" Average: Should be something between 60mA to 80mA\n" " Average: Should be something between 60mA to 80mA\n"
" Last Minute: Starts with 0 at boot up and is updated every 10s.\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" " Should be higher for higher system load when looking\n"
" at the last update for each load level.\n" " at the last update for each load level.\n"
" (Note: Not synchronized with load levels!)\n" " (Note: Not synchronized with load levels!)\n"
" Currently: Should be higher for higher system load. Might be\n" " Currently: Should be higher for higher system load. Might be\n"
" \"jumpy\" on 50% load, as this implemented by having\n" " \"jumpy\" on 50% load, as this implemented by having\n"
" one second of 100% load and one second of ~0% load\n" " one second of 100% load and one second of ~0% load\n"
" in turns.\n" " in turns.\n"
"\n" "\n"
"Hint: You'll want to look mostly at the rightmost column.\n" "Hint: You'll want to look mostly at the rightmost column.\n"
"Note: The test will repeat endlessly."); "Note: The test will repeat endlessly.\n");
LED0_OFF; LED0_OFF;
puts("+-------------------------------+-----------------------------------+\n" print_str("+-------------------------------+-----------------------------------+\n"
"| Total Transferred Charge [mC] | Current from Power Supply [mA] |\n" "| Total Transferred Charge [mC] | Current from Power Supply [mA] |\n"
"| Charging | Discharging | Average | Last Minute | Currently |\n" "| Charging | Discharging | Average | Last Minute | Currently |\n"
"+---------------+---------------+---------+-------------+-----------+"); "+---------------+---------------+---------+-------------+-----------+\n");
while (1) { while (1) {
/* Wait for the next pulse of the LTC4150 */ /* Wait for the next pulse of the LTC4150 */
@ -298,40 +225,40 @@ int main(void)
int32_t current; int32_t current;
if (change_of_load_level) { if (change_of_load_level) {
puts("+---------------+---------------+---------+-------------+-----------+"); print_str("+---------------+---------------+---------+-------------+-----------+\n");
change_of_load_level = 0; change_of_load_level = 0;
} }
/* Get & print total charge transferred */ /* Get & print total charge transferred */
if (ltc4150_charge(&ltc4150, &charged, &discharged)) { if (ltc4150_charge(&ltc4150, &charged, &discharged)) {
puts("ltc4150_charge() failed!"); print_str("ltc4150_charge() failed!\n");
return -1; return -1;
} }
puts_no_nl("| "); print_str("| ");
print_col_u32(charged, 13); print_col_u32_dec(charged, 13);
puts_no_nl(" | "); print_str(" | ");
print_col_u32(discharged, 13); print_col_u32_dec(discharged, 13);
puts_no_nl(" | "); print_str(" | ");
/* Get & print avg current */ /* Get & print avg current */
if (ltc4150_avg_current(&ltc4150, &avg_current)) { if (ltc4150_avg_current(&ltc4150, &avg_current)) {
puts("ltc4150_avg_current() failed!"); print_str("ltc4150_avg_current() failed!\n");
return -1; return -1;
} }
print_current(avg_current, 7); print_current(avg_current, 7);
puts_no_nl(" | "); print_str(" | ");
/* Get & print last minute current */ /* Get & print last minute current */
if (ltc4150_last_minute_charge(&ltc4150, &last_minute_data, if (ltc4150_last_minute_charge(&ltc4150, &last_minute_data,
&charged, &discharged) &charged, &discharged)
) { ) {
puts("ltc4150_last_minute_charge() failed!"); print_str("ltc4150_last_minute_charge() failed!\n");
return -1; return -1;
} }
current = (int32_t)discharged - (int32_t)charged; current = (int32_t)discharged - (int32_t)charged;
current /= 60; current /= 60;
print_col_i32(current, 11); print_col_s32_dec(current, 11);
puts_no_nl(" | "); print_str(" | ");
/* Calculate & print the current between the last two pulses */ /* Calculate & print the current between the last two pulses */
current = (int32_t)((test_data.now_usec - test_data.last_usec) / MS_PER_SEC); current = (int32_t)((test_data.now_usec - test_data.last_usec) / MS_PER_SEC);
@ -340,7 +267,7 @@ int main(void)
current = -current; current = -current;
} }
print_current(current, 9); print_current(current, 9);
puts(" |"); print_str(" |\n");
} }
return 0; return 0;