From f74a19b9fd32c3c6c3bb3a2626886cf422bf4ce9 Mon Sep 17 00:00:00 2001
From: patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>
Date: Mon, 17 Nov 2008 23:59:16 +0000
Subject: [PATCH] Add option to use C buffered I/O in examples/serloop

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1274 42af7a65-404d-4744-a932-0658087f49c3
---
 examples/README.txt     |  7 ++++++-
 examples/serloop/main.c | 22 +++++++++++++++++++++-
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/examples/README.txt b/examples/README.txt
index 84deee63d7..2d8ea8a2d1 100644
--- a/examples/README.txt
+++ b/examples/README.txt
@@ -120,7 +120,12 @@ examples/serloop
 ^^^^^^^^^^^^^^^^
 
   This is a mindlessly simple loopback test on the console.  Useful
-  for testing new serial drivers.
+  for testing new serial drivers.  Configuration options include:
+
+  * CONFIG_EXAMPLES_SERLOOP_BUFIO
+      Use C buffered I/O (getchar/putchar) vs. raw console I/O
+      (read/read).  The behavior of the NuttX getchar() call is
+      very hostile unless you also set CONFIG_STDIO_BUFFER_SIZE=0.
 
 examples/udp
 ^^^^^^^^^^^^
diff --git a/examples/serloop/main.c b/examples/serloop/main.c
index 990fe13ed2..d59e532090 100644
--- a/examples/serloop/main.c
+++ b/examples/serloop/main.c
@@ -38,6 +38,8 @@
  ****************************************************************************/
 
 #include <nuttx/config.h>
+
+#include <stdio.h>
 #include <unistd.h>
 
 /****************************************************************************
@@ -71,6 +73,23 @@ void user_initialize(void)
 
 int user_start(int argc, char *argv[])
 {
+#ifdef CONFIG_EXAMPLES_SERLOOP_BUFIO
+  int ch;
+
+  for (;;)
+    {
+      ch = getchar();
+      if (ch < 1)
+        {
+          ch = '!';
+        }
+      else if ((ch < 0x20 || ch > 0x7e) && ch != '\n')
+        {
+          ch = '.';
+        }
+      putchar(ch);
+    }
+#else
   ubyte ch;
   int ret;
 
@@ -81,12 +100,13 @@ int user_start(int argc, char *argv[])
         {
           ch = '!';
         }
-      else if (ch < 0x20 || ch > 0x7e)
+      else if ((ch < 0x20 || ch > 0x7e) && ch != '\n')
         {
           ch = '.';
         }
       ret = write(1, &ch, 1);
     }
+#endif
   return 0;
 }
 
-- 
GitLab