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

Command.h

Committer:
Mr_What
Date:
2015-08-02
Revision:
2:54d27fdcbe5c
Parent:
1:23d0a615756a
Child:
3:502f90649834

File content as of revision 2:54d27fdcbe5c:

/* 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;

  void begin(const char c=0)
  {
    nDig=val=0;
    code=c;
    neg = false;
  }
  bool get(char &cmdCode, int &cmdVal)
  {
    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 '~' :
        CmdSerial.puts("Command Stream RESET!\r\n");
        begin();
        return(false);

      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(false);
      case '-':
        if ((nDig == 0) && ((int)code>0))
          {
            //Serial.println(F("negative command value follows:"));
            neg = true; // value is negative
          }
        else
          {
            CmdSerial.puts("Not expecting a value.  '-' char ignored.\r\n");
            begin();  // clear bad entry
          }
        return(false);
      // 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(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
      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':

        // 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(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(false);  // prev command not complete
      }
  }

};