/* ROVFILE.C, ROVER FILE SYSTEM */

#include "rovfile.h"

/* Global Variables */

long g_memsize;
short g_filetype, g_filecount, g_disk_file;
short g_curr_dir, g_curr_speed, g_heading;
struct datafile{
  short filetype;
  time_t caltime;
  short analog[8];
  short heading;
  short curr_speed;
  short curr_dir;
} ;
struct datafile *g_memstart, *g_dataptr;


/*******************************************************
	      Datafiles Function
********************************************************/

int datafiles(void){
  char menukey;
  XmdmErr xerr;
  int i, chan;

  do{
    printf("\n\n\nRover Datafiles Menu");
    printf("\n\n  1 -- Display File Status");
    printf("\n  2 -- Offload Rover Data Files");
    printf("\n  3 -- Offload Benthic Chamber Files");
    printf("\n  4 -- Offload Microprofiler Files");
    printf("\n  7 -- Display Data");
    printf("\n  8 -- Fill Memory with Test Data");
    printf("\n  9 -- Exit");
    QueryChar("\nEnter Selection: ",'9',"1234789", &menukey);

    switch(menukey){

      case '1':  /*Datafile Status*/

	printf("\n\nMemory Size = %ld bytes", g_memsize);
	printf("\nFree Memory = %ld bytes", g_memsize - (g_filecount * sizeof(struct datafile)));
	printf("\nData Records in Memory = %hd", g_filecount);
	printf("\nDisk Files = %hd", g_disk_file);
	break;

      case '2':  /* Rover Data */
	if(QueryYesNo("\n\nOffload the data", FALSE)){
	  printf("\n\nStarting XMODEM transfer ... ");
	  offload();
	}
	break;

      case '3':  /* Benthic Chamber Data */
	break;

      case '4':  /* Microprofiler Data */
	break;

      case '7':  /* Display Data */
	display_data();
	break;

      case '8':  /* Test Data */
	file_init();
	g_filecount = 100;

	g_dataptr = g_memstart;
	g_dataptr->filetype = g_filecount;
	g_dataptr++;

	for(i = 1; i <= g_filecount; i++){
	  g_dataptr->filetype = TRANSIT;
	  g_dataptr->caltime = time(0);
	  for(chan = 0; chan <= 7; chan++) g_dataptr->analog[chan] = 16380;
	  g_dataptr->heading = 360;
	  g_dataptr->curr_speed = 10;
	  g_dataptr->curr_dir = 360;

	  g_dataptr++;
	}

      printf("\n%d test records loaded in memory", g_filecount);
      break;
    }  /* Switch ends */

  } while(menukey != '9');   /* Do loop ends */

  return 0;
}


/****************************************************
	    File Functions
****************************************************/

int file_init(void){

  PSMemFreeAll();
  g_memstart = PSMemAllocAll(&g_memsize);   /* Allocate all free memory to file system */
  if(!g_memstart){
    printf("\nOut of memory");
    return -1;
  }
  g_dataptr = g_memstart;
  g_dataptr++;          /* Save the first data structure for the filecount */

  return 0;
}


/***********************************************/

int store_data(short filetype){
  short chan;

  g_dataptr->filetype = filetype;
  g_dataptr->caltime = time(0);
  for(chan = 0; chan <=7; chan++) g_dataptr->analog[chan] = get_ad_counts(chan);
  g_dataptr->heading = g_heading;
  if(filetype == CURRMET_DATA){
    g_dataptr->curr_speed = g_curr_speed;
    g_dataptr->curr_dir = g_curr_dir;
  }
  else{
    g_dataptr->curr_speed = 0;
    g_dataptr->curr_dir = 0;
  }
  g_dataptr++;
  g_filecount++;
  return 0;
}

/***********************************************/

int offload(void){
  XmdmErr xerr;

  g_dataptr = g_memstart;     /* Hide the filecount in the first record */
  g_dataptr->filetype = g_filecount;

  xerr = XmodemSendMem(g_memstart, g_filecount * sizeof(struct datafile), 30);
  printf("  Complete [%d]\n", xerr);
  return 0;
}

/***************************************************/

/*****************************************************/

int display_data(void){
  int i, chan;

  g_dataptr = g_memstart;
  g_dataptr++;

  for(i = 1; i <= g_filecount; i++){
    printf("\n%hd ", g_dataptr->filetype);
    printf("%ld ", g_dataptr->caltime);
    for(chan = 0; chan <= 7; chan++) printf("%hd ", g_dataptr->analog[chan]);
    printf("%hd ", g_dataptr->heading);
    printf("%hd ", g_dataptr->curr_speed);
    printf("%hd ", g_dataptr->curr_dir);

    g_dataptr++;
  }

  printf("\nEnd of data, press any key to continue");
  while( !kbhit() );
  SerInFlush();
  return 0;
}

/***********************************************************/

int mem_to_disk(void){

  printf("\nWriting memory to disk");
  DFWrite( g_memstart, g_disk_file);
  DriveOff();
  DisableParIO();
  g_dataptr = g_memstart;
  g_disk_file++;

  return(0);
}

