// GPS skrysohledac

#include "gps.h"

#include <math.h>
#include <stdlib.h>
#include <stdlibm.h>

#define  LCD_RS         PIN_B1      // LCD control
#define  LCD_E          PIN_B0      // LCD enable
#define  LCD_DATA_LSB   PIN_B4      // LSB data bit LCD 
#include "MYLCD.C"

// NMEA
#define  TIME        6           // For NMEA parsing
#define  TIME_LEN    10
#define  LATDEG      19
#define  LATMIN      21
#define  LONDEG      32
#define  LONMIN      34
#define  DEG_LEN     2
#define  MIN_LEN     7
#define  AZIMUTH     46

float lat,lon;    // Latitude, Longitude, Azimuth
int16 az;

#define NMEA_LINESIZE     72

inline void read_NMEA()       // NMEA parsing
{
      auto char item[16];
      char *ptr,*line;
      auto int8  n;
      auto char  c;

      line=malloc(NMEA_LINESIZE);        // Space for one line
      if (line==NULL) {printf(lcd_putc,"Error"); sleep();};
      line[NMEA_LINESIZE-1]=0;

      while(fgetc(NMEA)!='$');            // Waiting for start character
      for(n=0;n<(NMEA_LINESIZE-1);n++)    // Read line up to checksum
      {
         c=fgetc(NMEA);
         if(c=='*')
         {
            line[n]=0;
            break;
         };
         line[n]=c;
      };

      lcd_gotoxy(12,4);
      printf(lcd_putc,"%c %06.0g ",line[17],strtod(&line[TIME],&ptr));

      lcd_gotoxy(1,3);
      strncpy(item,&line[LATDEG],DEG_LEN+MIN_LEN); item[DEG_LEN+MIN_LEN]=0;
      printf(lcd_putc,"%s*",item);
      strncpy(item,&line[LONDEG],DEG_LEN+MIN_LEN); item[DEG_LEN+MIN_LEN]=0;
      printf(lcd_putc,"%s*",item);

      lcd_gotoxy(1,2);
      lat=strtod(&line[LATMIN],&ptr);
      line[LATMIN]=0;
      lat+=60*strtod(&line[LATDEG],&ptr);
      lon=strtod(&line[LONMIN],&ptr);
      line[LONMIN]=0;
      lon+=60*strtod(&line[LONDEG],&ptr);
      strtod(&line[AZIMUTH],&ptr);
      az=strtoul(ptr+1,&ptr,10);
      printf(lcd_putc,"%.3g*%.3g*%03Lu",lat,lon,az);
      free(line);
}

void main()
{

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(VREF_LOW|-2);
   lcd_init();

   while(TRUE)
   {
      const float gc_lat=49*60+12.4452,gc_lon=14*60+18.9079; // Testovaci souradnice: Cisticka

      float bearing, heading, azim;
      
      read_NMEA();   // Read one NMEA line and parse

      lon=(gc_lon-lon)*1214;     // Kakona's projection :)
      lat=(gc_lat-lat)*1854;

lcd_gotoxy(1,4);
      printf(lcd_putc,"%.0g* ",sqrt((lon*lon) + (lat*lat))); // Distance
lcd_gotoxy(1,1);
      if (lon==0) lon=0.001;              // Divided by zero cure
      bearing=3-(6/PI*atan2(lat,lon));    // Bearing
      if (bearing<0) bearing+=12;         // overflow cure
      if (bearing>12) bearing-=12;
      printf(lcd_putc,"BE%2.0g*",bearing);
      azim=12.0/360*az;                   // Azimuth of the movement
      heading=bearing-lat;                // Heading
      if (heading<0) heading+=12;         // overflow cure
      if (heading>12) heading-=12;
      printf(lcd_putc,"HE%2.0g*AZ%2.0g* ",heading,azim);
   }
}