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:
0:41ca27337c2b
Child:
1:23d0a615756a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Command.h	Tue Jul 28 14:59:19 2015 +0000
@@ -0,0 +1,116 @@
+/* Aaron Birenboim 26jul15   http://boim.com
+
+Feel free to use as you wish, but please include above credits for the original work
+
+Command interpreter for serial port
+*/
+
+class CommandReader
+{
+public:
+  int nDig,val;
+  bool neg;
+  char code;
+  ASerial *_serial;  // we don't own this, we just use it
+  
+  CommandReader(ASerial s) : _serial(&s)
+  {
+    begin();
+  }
+
+  int get(char &cmdCode, int &cmdVal)
+  {
+    int i = _serial->readc();
+    if (i < 0) return(0);  // 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!");
+        begin();
+        return(0);
+
+      case '0':
+      case '1':
+      case '2':
+      case '3':
+      case '4':
+      case '5':
+      case '6':
+      case '7':
+      case '8':
+      case '9':
+        val = val*10 + (i-((int)('0')));
+        nDig++;
+        //Serial.print(nDig);Serial.print(")");Serial.println(val);
+        return(0);
+      case '-':
+        if ((nDig == 0) && ((int)code>0))
+          {
+            //Serial.println(F("negative command value follows:"));
+            neg = true; // value is negative
+          }
+        else
+          {
+            _serial->println("Not expecting a value.  '-' char ignored.");
+            begin();  // clear bad entry
+          }
+        return(0);
+      // commands without values
+      case '!':
+      case '^':
+      case '?':
+      case 'a':  // command to set Autonomous in manual mode
+      case 'A':
+        cmdCode = c;  // return prev command code (if any)
+        cmdVal = 0;
+        return(1);
+
+        // codes with values follow : 
+      case 'L':
+      case 'R':
+      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(0);  // wait for value
+        // seperator
+      case ' ':
+      case '\t':
+      case '\r':
+      case '\n':
+      case 0:
+      case ',':
+      case ';':
+        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
+          }
+      default: // treat any other character as a seperator
+        begin();  // clear any partial command
+        return(0);  // prev command not complete
+      }
+  }
+
+protected:
+  void begin()
+  {
+    nDig=val=0;
+    code=0;
+    neg = false;
+  }
+
+};