added fxs to class

Dependents:   ard2pmod_demo MAXREFDES130_Demo OneWire_DS18B20_Demo MAXREFDES132_OneWire_Demo

Fork of Terminal by Simon Ford

Files at this revision

API Documentation at this revision

Comitter:
j3
Date:
Sat Mar 25 19:10:15 2017 +0000
Parent:
7:aa29b7e10cd8
Commit message:
Updated get user input fxs to support systems that use CR+LF, CR, or just LF.;

Changed in this revision

Terminal.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r aa29b7e10cd8 -r 9ab22fc6e88b Terminal.cpp
--- a/Terminal.cpp	Mon Mar 06 23:17:02 2017 +0000
+++ b/Terminal.cpp	Sat Mar 25 19:10:15 2017 +0000
@@ -61,8 +61,63 @@
 int32_t Terminal::get_int32(const char *msg, const int32_t min_val, const int32_t max_val)
 {
     int32_t user_input;
+    //mbed uses C++ 2003 so no benifit in using C++ string for this fx
     char str[12];
-    uint8_t idx;
+    uint8_t idx = 0;
+    
+    do
+    {
+        //print prompt
+        this->printf(msg);
+        
+        //get user input
+        idx = 0;
+        do
+        {
+            //wait for first char
+            do
+            {
+                if(this->readable())
+                {
+                    str[idx++] = this->getc();
+                }
+            }
+            while(idx == 0);
+        }
+        while((str[idx - 1] != 0x0A) && (str[idx - 1] != 0x0D) && (idx < 12));
+        
+        //wait for possible additional char on windows machines
+        //at even the slowest of baudrates, hopefully.
+        //need a better way...
+        wait_ms(50);
+        
+        //gobble up trailing LF if there
+        while(this->readable())
+        {
+            this->getc();
+        }
+        
+        //replace CR or LF with \0
+        str[idx - 1] = '\0';
+        //convert str into int
+        user_input = atoi(str);
+        
+        if((user_input < min_val) || (user_input > max_val))
+        {
+            this->printf("\r\nYou entered = %d\r\n", user_input);
+            this->printf("\r\nOut of range\r\n");
+        }
+    }
+    while((user_input < min_val) || (user_input > max_val));
+    
+    return(user_input);
+}
+    
+char Terminal::get_char(const char *msg, const char min_val, const char max_val)
+{
+    //mbed uses C++ 2003 so no benifit in using C++ string for this fx
+    char c[3];
+    uint8_t idx = 0;
     
     do
     {
@@ -72,59 +127,32 @@
         idx = 0;
         do
         {
-            if(this->readable())
+            //wait for first char
+            do
             {
-                str[idx++] = this->getc();
+                if(this->readable())
+                {
+                    c[idx++] = this->getc();
+                }
             }
+            while(idx == 0);
         }
-        while((str[idx-1] != 0x0A) && (idx < 12));
+        while((c[idx-1] != 0x0A) && (c[idx-1] != 0x0D)  && (idx < 3));
         
-        //Remove trailing newline and CR, if there.
-        if((strlen(str) > 0 ) && (str[strlen(str) - 1] == 0x0A) && (str[strlen(str) - 2] == 0x0D))
+        //wait for possible additional char on windows machines
+        //at even the slowest of baudrates, hopefully.
+        //need a better way...
+        wait_ms(50);
+        
+        //gobble up trailing LF if there
+        while(this->readable())
         {
-            str[strlen(str) - 1] = '\0';
-            str[strlen(str) - 1] = '\0';
+            this->getc();
         }
         
-        user_input = strtol(str, NULL, 0);
+        //replace CR or LF with \0
+        c[idx - 1] = '\0';
         
-        if((user_input < min_val) || (user_input > max_val))
-        {
-            this->printf("\nYou entered = %d\n", user_input);
-            this->printf("\nOut of range\n");
-        }
-    }
-    while((user_input < min_val) || (user_input > max_val));
-    
-    return(user_input);
-}
-    
-char Terminal::get_char(const char *msg, const char min_val, const char max_val)
-{
-    char c[3];
-    uint8_t idx;
-    
-    do
-    {
-        this->printf(msg);
-        
-        //get user input
-        idx = 0;
-        do
-        {
-            if(this->readable())
-            {
-                c[idx++] = this->getc();
-            }
-        }
-        while((c[idx-1] != 0x0A)  && (idx < 3));
-        
-        //Remove trailing newline and CR, if there.
-        if((strlen(c) > 0 ) && (c[strlen(c) - 1] == 0x0A) && (c[strlen(c) - 2] == 0x0D))
-        {
-            c[strlen(c) - 1] = '\0';
-            c[strlen(c) - 1] = '\0';
-        }
         
         if((c[0] < min_val) || (c[0] > max_val))
         {
@@ -140,10 +168,10 @@
 
 float Terminal::get_float(const char *msg, const float min_val, const float max_val)
 {
-    char str[32];
-    uint8_t idx;
-    
     float user_input;
+    //mbed uses C++ 2003 so no benifit in using C++ string for this fx
+    char str[12];
+    uint8_t idx = 0;
     
     do
     {
@@ -153,20 +181,32 @@
         idx = 0;
         do
         {
-            if(readable())
+            //wait for first char
+            do
             {
-                str[idx++] = getc();
+                if(this->readable())
+                {
+                    str[idx++] = this->getc();
+                }
             }
+            while(idx == 0);
         }
-        while((str[idx-1] != 0x0A)  && (idx < 32));
+        while((str[idx-1] != 0x0A) && (str[idx-1] != 0x0D)  && (idx < 32));
         
-        //Remove trailing newline and CR, if there.
-        if((strlen(str) > 0 ) && (str[strlen(str) - 1] == 0x0A) && (str[strlen(str) - 2] == 0x0D))
+        //wait for possible additional char on windows machines
+        //at even the slowest of baudrates.
+        //Need a better way...
+        wait_ms(50);
+        
+        //gobble up trailing LF if there
+        while(this->readable())
         {
-            str[strlen(str) - 1] = '\0';
-            str[strlen(str) - 1] = '\0';
+            this->getc();
         }
         
+        //replace CR or LF with \0
+        str[idx - 1] = '\0';
+        //convert to float
         user_input = atof(str);
         
         if((user_input <= min_val) || (user_input >= max_val))