//This defines the pins and properties of the GPS Serial Stream
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=GPSSTREAM,ERRORS)
//----GLOBAL VARIABLES----
char GPSbuffer[83]=""; //this is where the incoming string will be stored
int8 BuffIndex = 0; //this is the index of the char array

//Test NMEA String to use for debugging
//char gps_test[83] = "$GPRMC,184547,A,3648.1513,N,12147.2863,W,000.0,239.9,050707,014.8,E*67"; 

//GPRMC string used for calculations
char GPRMC[83] = "";

//-----FLAGS-------
int1 noUpdate = 0;   //this is raised when the GPRMC string is being utilized
int1 needUpdate = 0; //raise this flag if GPSbuffer needs to be written to GPRMC after noUpdate flag is lowered
int1 GPSRecieving = FALSE; //this is true if we're accepting characters

/**
  * This struct is nested in the GPS struct
  * It contains all the GPS parameters
**/
struct gps_data
{
   char  time[7];    //the UTC time in HHMMSS
   char  status;     //Status of Reciever, A=valid V=warning
   float lat;        //Lattitude in Degrees ie:36.80252166666 degrees 
   char  latHem;     //Lattitude Hemisphere N or S 
   float lon;        //Longitude in Degrees, ie: 121.788105 degrees
   char  lonHem;     //Longitude Hemisphere E or W
   float sog;       //speed over ground (SOG) in Knots
   float course;     //Course over ground 000.0 to 359.9 degrees true 
   char  date[7];    //UTC date in ddmmyy
   float magVar;     //Magnetic variation in degrees 000.0 to 180.000
   char  magVarDir;  //Magnetic Variation Direction E or W

};

/**
  *   This struct contains all the data for the current and last state
  *   access properties of this by GPS.now.course
**/
struct gps
{
   struct gps_data now;    //current state of the system
   struct gps_data last;   //The last state of the system
} GPS;

/**
 *
 * input: NMEA $GPRMC STRING POINTER- NULL TERMINATED
 * This function takes in a GRPMC STRING and parses it
 * Written by Todd Berk, toddberk@gmail.com 2007
**/
void parse_gps()
{
   //Define Local Variables
   char separators[3] = ",";  //separators in GPRMC string
   char parts[12][13];        //The parts of the GPRMC string, ie
   char *part;                //Pointer Used while splitting string
   int i=0;                   //counter used in loops
   char degrees[4]="";           //used for converting ddmm.mmmm to degrees
   char minutes[10]="";           //used for converting ddmm.mmmm to degrees
//char teststr[13]="";//USED SOLELY FOR DEBUGGING STRINGS
//float testf = 0;//USED FOR DEBUGGING FLOATS
   //copy GPS.now to GPS.last before we change GPS.now
   GPS.last = GPS.now;
   //First Split string into array of string parameters
      //raise noUpdate flag here
      noUpdate = 1;
      //this first call ensures the string contains separators 
      part = strtok(GPRMC,separators);
      while(part!=0)
      {
         strcpy(parts[i], part);//*REPLACE THIS WILL CALL TO GPSAPPEND(part,i) function
         part = strtok(0,separators);
         i++;
      }
      //lower noUPdate flag
      noUpdate = 0;
      //perform update if flag is raised
      if(needUpdate && !GPSRecieving)
      {
         //copy buffer to string
         strcpy(GPRMC,GPSbuffer);
         //lower needupdate flag
         needUpdate = 0;
      }

//*BELOW HERE SHOULD BE SPLIT INTO A NEW FUNCTION AND CALLED 
   //FROM while(part!=0) TO REDUCE MEMORY FROM 12x13 ARRAY
   
   //Append new parameters to GPS.now
      //time
      strcpy(GPS.now.time,parts[1]);
      //status
      GPS.now.status = parts[2][0];
      //lattitude
         //We need to convert from ddmm.mmmm to degrees
         //point to lat
         part=&parts[3][0];
         //copy first two character to degrees
         strncpy(degrees,part,2);
         //increment part twice
         part++;part++;
         //copy rest of string to minutes
         strcpy(minutes,part);
         //convert and set lattitude in degrees
         GPS.now.lat = atof(degrees)+(atof(minutes)/60);
      //lattitude hemisphere
      GPS.now.latHem = parts[4][0];
      //Longitude 
         //We need to convert from ddmm.mmmm to degrees
         //point to lng
         part=&parts[5][0];
         //copy first three characters to degrees
         strncpy(degrees,part,3);
         //increment part thrice
         part++;part++;part++;
         //copy rest of string to minutes
         strcpy(minutes,part);
         //convert and set longitude in degrees
         GPS.now.lon = atof(degrees)+atof(minutes)/60;
      //longitude hemisphere
      GPS.now.lonHem = parts[6][0];
      //velocity
      GPS.now.sog = atof(parts[7]);
      //course
      GPS.now.course = atof(parts[8]);
      //date
      strcpy(GPS.now.date,parts[9]);
      //magnetic variation
      GPS.now.magVar = atof(parts[10]);
      //magnetic variation direction
      GPS.now.magVarDir = parts[11][0];

}
