/* ROVER CONTROL SOFTWARE VERSION 1.3 */

#include <ctype.h>
#include <stdio.h>
#include <timing.h>
#include <stdlib.h>

#include "rovlib.h"
#include "rovint.h"
#include "rovsys.h"
#include "rovtest.h"
#include "rovfile.h"

#ifdef AZTEC_C
#include <userio.h>
#include <offload.h>

#else                 /* For Turbo C */
#include <conio.h>
#include "tt7pclib.h"
#include "tt7pcio.h"
#endif

/*FUNCTION PROTOTYPES*/

int deploy(void);
int setup(void);

/*********************************************/

main() {
  
  char menukey;

  tt7init();
  file_init();
  SetTickRate(1);
  printf("\nTattletale Model7 Initialized");

  while(1) {
    printf("\n\n\nRover System Main Menu V1.1");
    printf("\n\n  1 -- Test Functions");
    printf("\n  2 -- Setup Deployment");
    printf("\n  3 -- Deploy");
    printf("\n  4 -- Data Files");
    printf("\n  5 -- Sleep");
    printf("\n  8 -- Test Deployment");
    printf("\n  9 -- Reset");
    QueryChar("\nEnter Selection: ",'5',"1234589",&menukey);

    switch(menukey){
      case '1':
	testfunc();    /* In rovtest.c */
	break;

      case '2':
	setup();
	break;

      case '3':
	deploy();
	clear_auxports();
	transponder_on();
	SerInFlush();
	Sleep(0);
	while(!SerByteAvail()) Sleep(GetTickRate());
	break;

      case '4':
	datafiles();
	break;

      case '5':          /* Sleep */
	SerInFlush();
	Sleep(0);
	while(!SerByteAvail()) Sleep(GetTickRate());
	break;

      case '8':
	testdeploy();     /* In rovtest.c */
	break;

      case '9':
	do{
	  printf("\n\n  1 -- Reset system");
	  printf("\n  2 -- Disable Auto-Launch");
	  printf("\n  9 -- Exit");
	  QueryChar("\nEnter Selection: ",'9',"129",&menukey);
	  switch(menukey){

	    case '1':  /* Reset system */
	      reset();
	      break;

	    case '2':  /* Disable Auto-Launch */
	      MonCmd("RUN 0");
	      printf("\n\nAuto-Launch disabled.  Monitor has control on next reset.");
	      break;
	  }

	} while(menukey != '9');

     }    /* End of switch */
  }       /* End of while */
}         /* End of main */

/*********************************************
      SETUP FUNCTION
*********************************************/

int setup(void){
  char menukey;
  struct tm	t;

  do{
    printf("\n\n\nRover Setup Menu");
    printf("\n\n  1 -- Set Clock");
    printf("\n  2 -- Change Mission Defaults");
    printf("\n  3 -- Change System Defaults");
    printf("\n  4 -- Change Calibration Coefficients");
    printf("\n  9 -- Exit");
    QueryChar("\nEnter Selection: ",'9',"12349",&menukey);

    switch(menukey){
      case '1':  /*Set Clock*/
	printf("\nSet Current Date and Time (CMD-D from CrossCut)");
	if(! QueryDateTime("\n<CR> for default", TRUE, &t)) break;
	SetTimeTM(&t, NULL);
	break;

      case '2':  /* Mission Defaults */
	break;

      case '3':  /* System Defaults */
	break;

      case '4':  /* Calibration Coefficients */
	break;
    }

  } while(menukey != '9');

  return 0;
}

/*******************************************************
	      Deploy Function
********************************************************/

int deploy(void){
  unsigned long delay_time = 240;
  unsigned long transit_dist = 10, max_sites = 5, rack_down_time = 240;
  short prime_heading, heading_diff, errcode;
  int site_count;

  g_disk_file = 0;
  g_filecount = 0;

  do {
    if(! QueryNum("\nEnter travel distance in meters: ", "%ld",
	 "%ld", &transit_dist)) return -1;
  }  while (transit_dist < 0 || transit_dist > 20);
  transit_dist *= 100/ODOCAL;   /* Convert to odometer ticks */

  do {
    if(! QueryNum("\nEnter rack down time (min): ", "%ld", "%ld", &rack_down_time))
      return -1;
  }  while (rack_down_time < 0 || rack_down_time > 18000);

  do {
    if(! QueryNum("\nEnter number of sites: ", "%ld",
	 "%ld", &max_sites)) return -1;
  }  while (max_sites < 0 || max_sites > 20);


  QueryNum("\n\nEnter Deployment Delay Time (min): ","%ld","%ld", &delay_time);
  printf("\nRover will activate in %ld minutes.", delay_time);
  printf("\nDisconnect comm. cable and deploy...");

  delay_time *= 60;             /* Convert to seconds */
  sleep_with_break(delay_time);

   /* Raise Current Meter */

  printf("\n\nCurrent Meter Up Cycle");
  sensor_pwr_on();
  delaysecs(1);
  errcode = currmet_up();
  if(errcode){
    printf("\nCurrent meter error: %hd", errcode);
    sensor_pwr_off();
  }
  printf("\nCurrent meter up");


   /********* Cycle Begin ***********/

       /* Which way to go? */

  sleep_with_break(60);
  prime_heading = ( heading() + cmvane_dir() ) % 360;
  printf("\nPrime heading = %hd", prime_heading);

      /* Let's go that way a few times */

  for(site_count=1; site_count<= max_sites; site_count++){

      /* Wait till the current is right direction */

    while(1){
      heading_diff = prime_heading - ( (heading() + cmvane_dir()) % 360 );
      if(heading_diff > 180) heading_diff -= 360;
      else if(heading_diff < (-180)) heading_diff += 360;
      printf("\nHeading_diff = %hd", heading_diff);

      if(abs(heading_diff < 60)) break;  /* Current is right, let's go! */
      sleep_with_break(60);          /* Otherwise, wait and check again */
    }

      /* Turn */

    printf("\n\nTurn Cycle");
    errcode = turn_rov(prime_heading);
    if( errcode ){
      printf("First turn error: %d", errcode);
      return errcode;
    }
    else printf("\nTurn complete");

    /* Transit */

    delaysecs(1);
    printf("\n\nTransit Cycle");
    errcode = transit_rov(transit_dist);
    if( errcode ){
      printf("\nTransit error:", errcode);
      return errcode;
    }
    else printf("\nFirst transit complete");

/*********************Rack Cycle********************/

    delaysecs(1);
    flood1_pwr_on();
    flood2_pwr_on();
    delaysecs(1);

    printf("\n\nRack Down Cycle");
    errcode = rack_down_cycle();
    if(errcode){
      switch(errcode){
	case (RACK_LIMITDWN_ERR):
	  printf("\nRack Limit Down Error");
	  flood1_pwr_off();
	  flood2_pwr_off();
	  rack_up_cycle();
	  break;

	default:
	  printf("\nRack down error: %d", errcode);
	  return errcode;
      }
    }
    else printf("\nRack Down");

    delaysecs(1);
    flood1_pwr_off();
    flood2_pwr_off();
    sensor_pwr_off();
    sleep_with_break(rack_down_time);

    printf("\n\nRack Up Cycle");
    errcode = rack_up_cycle();
    if(errcode){
      printf("\nRack up error: %d", errcode);
    }
    else printf("\nRack Up");

    delaysecs(1);
    mem_to_disk();

  }    /* End of for loop */

/**********Lower Current Meter**************/

  printf("\n\nCurrent meter down cycle");
  errcode = currmet_down();
  if(errcode){
    printf("\nCurrent meter error: %d", errcode);
    sensor_pwr_off();
    return errcode;
  }
  else printf("\nCurrent meter down");

  printf("\n\nEnd of mission");
  sensor_pwr_off();

  SerInFlush();
  Sleep(0);
  while(!SerByteAvail()) Sleep(1);

  return 0;
}


/*******************************************************
	       Misc. Functions
*******************************************************/

