Basic tank-style drive robot control firmware for Freescale FRDM-K64F. Controls motors on a Dual-Full-H-Bridge with EN, like DBH-1x series, from Bluetooth serial commands

Dependencies:   mbed

Revision:
2:54d27fdcbe5c
Parent:
1:23d0a615756a
Child:
3:502f90649834
--- a/Command.h	Fri Jul 31 17:37:52 2015 +0000
+++ b/Command.h	Sun Aug 02 18:34:12 2015 +0000
@@ -9,27 +9,27 @@
 {
 public:
   int nDig,val;
-  bool neg;
+  BOOL neg;
   char code;
-  ASerial *_serial;  // we don't own this, we just use it
-  
-  CommandReader(ASerial s) : _serial(&s)
+
+  void begin(const char c=0)
   {
-    begin();
+    nDig=val=0;
+    code=c;
+    neg = false;
   }
-
-  int get(char &cmdCode, int &cmdVal)
+  bool get(char &cmdCode, int &cmdVal)
   {
-    int i = _serial->readc();
-    if (i < 0) return(0);  // no command yet
+    int i = cSerial.cread();
+    if (i < 0) return(false);  // no command yet
     char c = i;
 //Serial.print('[');Serial.print(i);Serial.print(',');Serial.print(c);Serial.println(']');
     switch(c)
       {
       case '~' :
-        _serial->println("Command Stream RESET!");
+        CmdSerial.puts("Command Stream RESET!\r\n");
         begin();
-        return(0);
+        return(false);
 
       case '0':
       case '1':
@@ -44,7 +44,7 @@
         val = val*10 + (i-((int)('0')));
         nDig++;
         //Serial.print(nDig);Serial.print(")");Serial.println(val);
-        return(0);
+        return(false);
       case '-':
         if ((nDig == 0) && ((int)code>0))
           {
@@ -53,26 +53,43 @@
           }
         else
           {
-            _serial->println("Not expecting a value.  '-' char ignored.");
+            CmdSerial.puts("Not expecting a value.  '-' char ignored.\r\n");
             begin();  // clear bad entry
           }
-        return(0);
+        return(false);
       // commands without values
       case '!':
- //     case '^':
       case '?':
- //     case 'a':  // command to set Autonomous in manual mode
- //     case 'A':
+      case '^':
+      case 'a':  // command to set Autonomous in manual mode
+      case 'A':
         cmdCode = c;  // return prev command code (if any)
         cmdVal = 0;
-        return(1);
+        return(true);
 
-        // codes with values follow : 
+      // codes with values follow : 
+      //         might want to leave some of these in here to keep car app from 
+      //         throwing error messages, which could saturate the serial connection
+      case 'p':
+      case 't':
+      case 'm':
+      case 'C':
+      case 'c':
+      case 'S':
+      case 'T':
+      case 'G':
+      case 'g':
+      case 'r':
+      case 'd':
+        begin();  // clear old command, if any
+        code = c; // remember command for wich the following value applies
+        return(false);  // wait for value
+
+      // some android version/settings, there is no seperator.
+      // be robust to this
       case 'L':
       case 'R':
-        begin();  // clear old command, if any
-        code = c; // remember command for wich the following value applies
-        return(0);  // wait for value
+
         // seperator
       case ' ':
       case '\t':
@@ -81,25 +98,17 @@
       case 0:
       case ',':
       case ';':
-        if ( code > (char)0 )
-          { // command was in progress, close it out
+        //if ( code > (char)0 )
+        //  { // command was in progress, close it out
             cmdCode = code;
             cmdVal = neg ? -val : val;
-            begin();  // clear for next command
-            return(1);  // had a complete command
-          }
+            begin(c);  // clear for next command
+            return(true);  // had a complete command
+        //  }
       default: // treat any other character as a seperator
         begin();  // clear any partial command
-        return(0);  // prev command not complete
+        return(false);  // prev command not complete
       }
   }
 
-protected:
-  void begin()
-  {
-    nDig=val=0;
-    code=0;
-    neg = false;
-  }
-
 };