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:
3:502f90649834
Parent:
2:54d27fdcbe5c
Child:
4:7620d21baef3
--- a/Command.h	Sun Aug 02 18:34:12 2015 +0000
+++ b/Command.h	Thu Aug 13 17:50:28 2015 +0000
@@ -8,21 +8,30 @@
 class CommandReader
 {
 public:
-  int nDig,val;
-  BOOL neg;
-  char code;
+  int _nDig,_val;
+  BOOL _neg;
+  char _code;
+
+  static BOOL hasValue(const char c)
+  {
+      if ( (c == 'L') || (c == 'R') )
+          return(true);
+      return(false);    
+  }
 
   void begin(const char c=0)
   {
-    nDig=val=0;
-    code=c;
-    neg = false;
+    _nDig=_val=0;
+    _code= hasValue(c) ? c : 0;
+    _neg = false;
   }
-  bool get(char &cmdCode, int &cmdVal)
+        
+  BOOL get(char &cmdCode, int &cmdVal)
   {
     int i = cSerial.cread();
     if (i < 0) return(false);  // no command yet
     char c = i;
+static int nc=0;DiagSerial.printf("%d got %d(%c)\r\n",nc++,i,i);
 //Serial.print('[');Serial.print(i);Serial.print(',');Serial.print(c);Serial.println(']');
     switch(c)
       {
@@ -41,19 +50,19 @@
       case '7':
       case '8':
       case '9':
-        val = val*10 + (i-((int)('0')));
-        nDig++;
+        _val = _val*10 + (i-((int)('0')));
+        _nDig++;
         //Serial.print(nDig);Serial.print(")");Serial.println(val);
         return(false);
       case '-':
-        if ((nDig == 0) && ((int)code>0))
+        if ((_nDig == 0) && ((int)_code>0))
           {
             //Serial.println(F("negative command value follows:"));
-            neg = true; // value is negative
+            _neg = true; // value is negative
           }
         else
           {
-            CmdSerial.puts("Not expecting a value.  '-' char ignored.\r\n");
+            DiagSerial.puts("Not expecting a value.  '-' char ignored.\r\n");
             begin();  // clear bad entry
           }
         return(false);
@@ -66,7 +75,7 @@
         cmdCode = c;  // return prev command code (if any)
         cmdVal = 0;
         return(true);
-
+/*
       // 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
@@ -84,6 +93,7 @@
         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
@@ -98,15 +108,15 @@
       case 0:
       case ',':
       case ';':
-        //if ( code > (char)0 )
-        //  { // command was in progress, close it out
-            cmdCode = code;
-            cmdVal = neg ? -val : val;
+        if ( _code > (char)0 )
+          { // command was in progress, close it out
+            cmdCode = _code;
+            cmdVal = _neg ? -_val : _val;
             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
+        begin(c);  // clear any partial command
         return(false);  // prev command not complete
       }
   }