From 2ba57ac00c1f6b926101eac7945a0723b8d273bb Mon Sep 17 00:00:00 2001
From: Marian Buschsieweke <marian.buschsieweke@ovgu.de>
Date: Fri, 11 Jan 2019 21:55:04 +0100
Subject: [PATCH] tests/driver_ltc4150: Workarround for msp430

The msp430 toolchain is missing an `fputs()` implementation. This commit makes
them use the `printf("%s", str);` instead of `fputs(str, stdout);`, which is
semantically equivalent (but has more overhead).
---
 tests/driver_ltc4150/Makefile |  4 ++++
 tests/driver_ltc4150/main.c   | 38 +++++++++++++++++++++++++----------
 2 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/tests/driver_ltc4150/Makefile b/tests/driver_ltc4150/Makefile
index b891f0b130..69e41df12e 100644
--- a/tests/driver_ltc4150/Makefile
+++ b/tests/driver_ltc4150/Makefile
@@ -6,3 +6,7 @@ USEMODULE += fmt
 USEMODULE += ltc4150
 
 include $(RIOTBASE)/Makefile.include
+
+ifneq (,$(filter $(BOARD),msb-430 msb-430h telosb wsn430-v1_3b wsn430-v1_4 z1))
+  CFLAGS += -DNO_FPUTS
+endif
diff --git a/tests/driver_ltc4150/main.c b/tests/driver_ltc4150/main.c
index 12ced98827..a539c78ae2 100644
--- a/tests/driver_ltc4150/main.c
+++ b/tests/driver_ltc4150/main.c
@@ -62,6 +62,22 @@ 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
  */
@@ -142,11 +158,11 @@ static void print_spaces(size_t number)
 {
     static const char *spaces = "                ";
     while (number > 16) {
-        fputs(spaces, stdout);
+        puts_no_nl(spaces);
         number -= 16;
     }
 
-    fputs(spaces + 16 - number, stdout);
+    puts_no_nl(spaces + 16 - number);
 }
 
 /**
@@ -164,7 +180,7 @@ static void print_col_u32(uint32_t number, size_t width)
     if (width > slen) {
         print_spaces(width - slen);
     }
-    fputs(sbuf, stdout);
+    puts_no_nl(sbuf);
 }
 
 /**
@@ -188,7 +204,7 @@ static void print_col_i32(int32_t number, size_t width)
     if (width > slen) {
         print_spaces(width - slen);
     }
-    fputs(sbuf, stdout);
+    puts_no_nl(sbuf);
 }
 
 /**
@@ -204,7 +220,7 @@ static void print_current(int32_t current, size_t width)
     sbuf[0] = '.';
     sbuf[1] = '0' + current % 10;
     sbuf[2] = '\0';
-    fputs(sbuf, stdout);
+    puts_no_nl(sbuf);
 }
 
 int main(void)
@@ -220,7 +236,7 @@ int main(void)
     ltc4150_pulses2c(&ltc4150, &ten_uc_per_pulse, NULL, 10000, 0);
 
     if (retval) {
-        fputs("Failed to initialize LTC4150 driver:", stdout);
+        puts_no_nl("Failed to initialize LTC4150 driver:");
         switch (retval) {
             case -EINVAL:
                 puts("Invalid parameter");
@@ -291,11 +307,11 @@ int main(void)
             puts("ltc4150_charge() failed!");
             return -1;
         }
-        fputs("| ", stdout);
+        puts_no_nl("| ");
         print_col_u32(charged, 13);
-        fputs(" | ", stdout);
+        puts_no_nl(" | ");
         print_col_u32(discharged, 13);
-        fputs(" | ", stdout);
+        puts_no_nl(" | ");
 
         /* Get & print avg current */
         if (ltc4150_avg_current(&ltc4150, &avg_current)) {
@@ -303,7 +319,7 @@ int main(void)
             return -1;
         }
         print_current(avg_current, 7);
-        fputs(" | ", stdout);
+        puts_no_nl(" | ");
 
         /* Get & print last minute current */
         if (ltc4150_last_minute_charge(&ltc4150, &last_minute_data,
@@ -315,7 +331,7 @@ int main(void)
         current = (int32_t)discharged - (int32_t)charged;
         current /= 60;
         print_col_i32(current, 11);
-        fputs(" | ", stdout);
+        puts_no_nl(" | ");
 
         /* Calculate & print the current between the last two pulses */
         current = (int32_t)((test_data.now_usec - test_data.last_usec) / MS_PER_SEC);
-- 
GitLab