/* ROVFILE.C, ROVER FILE SYSTEM */

#include "rovfile.h"

/* Global Variables */

long g_memsize;
short g_filetype, g_filecount;
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;

/****************************************************
	    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 */
  g_filecount = 0;

  printf("\nStruct size: %d bytes", sizeof(struct datafile));

  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;
}

/***********************************************/

void file_status(void){

  printf("\n\nData Records = %hd", g_filecount);
  printf("\nMemory Size = %ld bytes", g_memsize);
  printf("\nFree Memory = %ld bytes", g_memsize - (g_filecount * sizeof(struct datafile)));
}

/***********************************************/

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 test_data(void){
  int i, chan;

  file_init();
  g_filecount = 10;

  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);
  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;
}


