#include <Servo.h>
#include <timeObj.h>
#include <blinker.h>
#include <lilParser.h>

lilParser ourParser;
Servo     elevator;
Servo     rotater;
blinker   laser(4,20,150);
timeObj   laserTimer(5000);

enum commands { noCommand, aim, fire };


void setup(void) {
   
   Serial.begin(9600);
   ourParser.addCmd(aim,"aim");
   ourParser.addCmd(fire,"fire");

   elevator.attach(2);
   rotater.attach(3);
}


void loop(void) {
  
  char  inChar;
  int   command;
  
  idle();
  if (laserTimer.ding()) {
    laser.setOnOff(false);
    laserTimer.reset();
  }

  if (Serial.available()) {                   // If serial has some data..
    inChar = Serial.read();                   // Read out a charactor.
    Serial.print(inChar);                     // *Optional* echo the charactor.
    command = ourParser.addChar(inChar);      // Try parsing what we have.
    switch (command) {                        // Check the results.
      case noCommand : break;                 // Nothing to report, move along.
      case aim       : doAim();   break;      // Call the handler function for aiming
      case fire      : dofire();   break;     // Call the handler function for firing
      default        :                        // User typed sinething we don't have..
        Serial.println("What?");              // Basically a bad command, tell 'em.
      break;                                  // Time to Jet.
    }
  }
}


void doAim() {

  int numParams;
  int elev;
  int rot;

  numParams = ourParser.numParams();      // Save the number of parameters.
  if (numParams == 2) {                   // If we have 2 parameters..
    rot   = atoi(ourParser.getNextParam()); // Grab rotation value.
    elev  = atoi(ourParser.getNextParam()); // Grab rotation value.
    rotater.write(rot);
    elevator.write(elev);
  }
}

void dofire() {

  laser.setOnOff(true);
  laserTimer.start();
}