/**** GPS skrysohledac ****/
#define ID "$Id: gps.c 984 2008-01-06 23:41:31Z kakl $"

#include "gpsrl.h"

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

#define  SCINTILAK   1           //kanal adc pro scintilacni detektor NB3201
#define  RANGE       PIN_A4      // vystup detektoru urcujici rozsah mereni.


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

#define  TL1   PIN_C0
#define  TL2   PIN_C1
#define  TL3   PIN_C2

//$GPRMC,105815.503,V,4915.5877,N,01442.3896,E,0.0,0.00,060108,,,N*44
//$GPGGA,000415.000,4915.6225,N,01442.3608,E,1,03,50.0,409.6,M,45.2,M,0.0,0000*48
void main()
{

   setup_adc_ports(AN0);
   setup_adc(ADC_CLOCK_INTERNAL);
   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();

   delay_ms(100);
   lcd_gotoxy(5,1);
   printf(lcd_putc,"GPSRL01B \n");
   printf(lcd_putc,"(c) Kaklik 2008");
   delay_ms(300);
   printf(lcd_putc,"\f");

   set_adc_channel(SCINTILAK);

   while(TRUE)
   {


   char tab1[20];
   char tab2[20];
   char tab3[20];
   char tab4[20];
   char tab5[20];

   int8  n;
   int16 intensity;
   char  c;

      tab1[19]=0;
      tab2[19]=0;
      tab3[19]=0;
      tab4[19]=0;
      tab5[19]=0;

      while(fgetc(NMEA)!='$');            // Waiting for start character

      intensity=read_adc();
      tab1[0]='$';
      
      for(n=1;n<(19);n++)
      {
         tab1[n]=fgetc(NMEA);
      };

      for(n=0;n<(19);n++)
      {
         tab2[n]=fgetc(NMEA);
      };

      for(n=0;n<(19);n++)
      {
         tab3[n]=fgetc(NMEA);
      };

      for(n=0;n<(19);n++)
      {
         c=fgetc(NMEA);
         if(c=='*')
         {
            tab4[n]=0;
            tab5[0]=c;
            tab5[1]=fgetc(NMEA);
            tab5[2]=fgetc(NMEA);
            tab5[3]=0;
            goto tisk;
         };
         tab4[n]=c;
      };

     for(n=0;n<(19);n++)    // Read line up to checksum
      {
         c=fgetc(NMEA);
         if(c=='*')
         {
            tab5[n]=c;
            tab5[n+1]=fgetc(NMEA);
            tab5[n+2]=fgetc(NMEA);
            tab5[n+3]=0;
            break;
         };
         tab5[n]=c;
      };

tisk:

   printf(lcd_putc,"\f");

if(!input(TL1))
{
    lcd_gotoxy(1,1);
      printf(lcd_putc,"%s\n",tab1);
      printf(lcd_putc,"%s",tab2);
}
else
{
    lcd_gotoxy(1,1);
    printf(lcd_putc,"%s\n",tab3);
    printf(lcd_putc,"%s",tab4);
}

puts(strcat(tab1,strcat(tab2,tab3)),PC);
puts(strcat(tab4,tab5),PC);
printf(" /%u/%Lu\n\r",input(RANGE), intensity);

   }
}