//
//----------------------------------------------
// Include
//----------------------------------------------
#include <Helper.h>
#include <SerialUart.h>
#include <TinyGPSPlus.h>
//
//----------------------------------------------
// Constant
//----------------------------------------------
#define SerialUartPC  Serial 
#define SerialUartGPS Serial2
//
const int BAUDRATE_UARTPC = 115200;
const int BAUDRATE_UARTGPS = 9600;
const int TIME_LONGITUDE_OFFSET = 1;
//
//----------------------------------------------
// Global Variable
//----------------------------------------------
CSerialUart UartPC(SerialUartPC);
CSerialUart UartGPS(SerialUartGPS);
TinyGPSPlus GpsReceiver;
//
//----------------------------------------------
// Helper
//----------------------------------------------
void PrintTwoDigits(int number)
{
  if (number < 10)
    UartPC.print("0");
  UartPC.print(number);
}
//
// 
//----------------------------------------------
// Main - Setup
//----------------------------------------------
void setup() 
{ //--------------------------------------------
  //  UartPC Initialisation
  //--------------------------------------------
  UartPC.begin(BAUDRATE_UARTPC);
  delay(INIT_SERIALUART_STARTUP);
  Message(HEADER_00);
  Message(HEADER_01);
  Message(HEADER_00);
  Message(HEADER_02);
  Message(HEADER_03);
  Message(HEADER_04);
  Message(HEADER_00);
  Message("UartPC started");
  //--------------------------------------------
  //  GPS Initialisation 
  //--------------------------------------------
  UartGPS.begin(BAUDRATE_UARTGPS);  
  Message("UartGPS started - waiting for GPS-Synchronisation...");
}

void loop() 
{
  while (UartGPS.available()) 
  {
    GpsReceiver.encode(UartGPS.read());
  }
  if (GpsReceiver.time.isUpdated()) 
  {
    UartPC.print("Date(UTC)[");
    PrintTwoDigits(GpsReceiver.date.day());
    UartPC.print(".");
    PrintTwoDigits(GpsReceiver.date.month());
    UartPC.print(".");
    UartPC.print(GpsReceiver.date.year());
    UartPC.print("]dd.mm.yyyy   Time(UTC)[");
    PrintTwoDigits(GpsReceiver.time.hour());
    UartPC.print(":");
    PrintTwoDigits(GpsReceiver.time.minute());
    UartPC.print(":");
    PrintTwoDigits(GpsReceiver.time.second());
    UartPC.print("]hs:ms:ss   Time(GER)["); 
    PrintTwoDigits(TIME_LONGITUDE_OFFSET + GpsReceiver.time.hour());
    UartPC.print(":");
    PrintTwoDigits(GpsReceiver.time.minute());
    UartPC.print(":");
    PrintTwoDigits(GpsReceiver.time.second());
    UartPC.println("]hs:ms:ss"); 
  }
  //
  if (GpsReceiver.location.isUpdated()) 
  {
    UartPC.print("Latitude[");
    UartPC.print(GpsReceiver.location.lat(), 6);
    UartPC.print("]deg   Longitude[");
    UartPC.print(GpsReceiver.location.lng(), 6);
    UartPC.print("]deg   Speed[");
    UartPC.print(GpsReceiver.speed.kmph());    
    UartPC.println("]km/h");
  }
}