#include "gps_config.h"
#include "bsp_usart.h"
#include "nmea.h"

uint8_t gps_rbuff[GPS_RBUFF_SIZE];
uint8_t GPS_TransferEnd = 0, GPS_HalfTransferEnd = 0;
extern  uint8_t GPS_TransferCount ;
void GPS_ProcessDMAIRQ(void)
{
  
  if(GPS_TransferCount==GPS_RBUFF_SIZE/2 )        
  {
    GPS_HalfTransferEnd = 1;                
  }
  else if(GPS_TransferCount==GPS_RBUFF_SIZE)     
  {
    GPS_TransferEnd = 1;                    
   }
}

void GPS_Config(void)
{
 // GPS_USART_INIT();

  
}

void trace(const char *str, int str_size)
{
  #ifdef __GPS_DEBUG
    uint16_t i;
    printf("\r\nTrace: ");
    for(i=0;i<str_size;i++)
      printf("%c",*(str+i));
  
    printf("\n");
  #endif
}

void error(const char *str, int str_size)
{
    #ifdef __GPS_DEBUG

    uint16_t i;
    printf("\r\nError: ");
    for(i=0;i<str_size;i++)
      printf("%c",*(str+i));
    printf("\n");
    #endif
}

static uint8_t IsLeapYear(uint8_t iYear) 
{ 
    uint16_t    Year; 
    Year    =    2000+iYear; 
    if((Year&3)==0) 
    { 
        return ((Year%400==0) || (Year%100!=0)); 
    } 
     return 0; 
} 

void    GMTconvert(nmeaTIME *SourceTime, nmeaTIME *ConvertTime, uint8_t GMT,uint8_t AREA) 
{ 
    uint32_t    YY,MM,DD,hh,mm,ss;
     
    if(GMT==0)    return;
    if(GMT>12)    return;

    YY    =    SourceTime->year;
    MM    =    SourceTime->mon;
    DD    =    SourceTime->day;
    hh    =    SourceTime->hour;
    mm    =    SourceTime->min;
    ss    =    SourceTime->sec;

    if(AREA)
    { 
        if(hh+GMT<24)    hh    +=    GMT;
        else
        { 
            hh    =    hh+GMT-24;
            if(MM==1 || MM==3 || MM==5 || MM==7 || MM==8 || MM==10)
            { 
                if(DD<31)    DD++; 
                else 
                { 
                    DD    =    1; 
                    MM    ++; 
                } 
            } 
            else if(MM==4 || MM==6 || MM==9 || MM==11)
            { 
                if(DD<30)    DD++; 
                else 
                { 
                    DD    =    1; 
                    MM    ++; 
                } 
            } 
            else if(MM==2)
            { 
                if((DD==29) || (DD==28 && IsLeapYear(YY)==0))
                { 
                    DD    =    1; 
                    MM    ++; 
                } 
                else    DD++; 
            } 
            else if(MM==12)
            { 
                if(DD<31)    DD++; 
                else
                {               
                    DD    =    1; 
                    MM    =    1; 
                    YY    ++; 
                } 
            } 
        } 
    } 
    else 
    {     
        if(hh>=GMT)    hh    -=    GMT;
        else
        { 
            hh    =    hh+24-GMT;
            if(MM==2 || MM==4 || MM==6 || MM==8 || MM==9 || MM==11)
            { 
                if(DD>1)    DD--; 
                else 
                { 
                    DD    =    31; 
                    MM    --; 
                } 
            } 
            else if(MM==5 || MM==7 || MM==10 || MM==12)
            { 
                if(DD>1)    DD--; 
                else 
                { 
                    DD    =    30; 
                    MM    --; 
                } 
            } 
            else if(MM==3)
            { 
                if((DD==1) && IsLeapYear(YY)==0)
                { 
                    DD    =    28; 
                    MM    --; 
                } 
                else    DD--; 
            } 
            else if(MM==1)    //����1�·�
            { 
                if(DD>1)    DD--; 
                else        //�����һ��
                {               
                    DD    =    31; 
                    MM    =    12; 
                    YY    --; 
                } 
            } 
        } 
    }         

    ConvertTime->year   =    YY;             
    ConvertTime->mon    =    MM;               
    ConvertTime->day    =    DD;              
    ConvertTime->hour   =    hh;                
    ConvertTime->min    =    mm;               
    ConvertTime->sec    =    ss;               
}  

/*********************************************************end of file**************************************************/
