//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "FMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "ZylGPSReceiver"
#pragma link "ZylCustomGPSReceiver"
#pragma resource "*.dfm"
TfrmMain *frmMain;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnStartClick(TObject *Sender)
{
  ZylGPSReceiver->Port = (TCommPort)(cmbPort->ItemIndex + 1);
  ZylGPSReceiver->Open();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnStopClick(TObject *Sender)
{
  ZylGPSReceiver->Close();
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::ZylGPSReceiverConnect(TObject *Sender,
      TCommPort Port)
{
  memRaw->Lines->Add("Connected to:" + ZylGPSReceiver->CommPortToString(Port));
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::ZylGPSReceiverDisconnect(TObject *Sender,
      TCommPort Port)
{
  memRaw->Lines->Add("Disconnected from:" + ZylGPSReceiver->CommPortToString(Port));
}
//---------------------------------------------------------------------------


void __fastcall TfrmMain::ZylGPSReceiverReceive(TObject *Sender)
{
  efLatDeg->Text = IntToStr(ZylGPSReceiver->GetLatitudeDegree());
  efLatMin->Text = IntToStr(ZylGPSReceiver->GetLatitudeMinute());
  efLatSec->Text = FloatToStr(ZylGPSReceiver->GetLatitudeSecond());
  if (ZylGPSReceiver->GetLatitudeDirection() == cpNorth)
    efLatDir->Text = "North";
  else
    efLatDir->Text = "South";

  efLongDeg->Text = IntToStr(ZylGPSReceiver->GetLongitudeDegree());
  efLongMin->Text = IntToStr(ZylGPSReceiver->GetLongitudeMinute());
  efLongSec->Text = FloatToStr(ZylGPSReceiver->GetLongitudeSecond());
  if (ZylGPSReceiver->GetLongitudeDirection() == cpEast)
    efLongDir->Text = "East";
  else
    efLongDir->Text = "West";

  efAlt->Text = FloatToStr(ZylGPSReceiver->GetAltitude());
  efSpeed->Text = FloatToStr(ZylGPSReceiver->GetSpeed_KMH());
  efHeading->Text = FloatToStr(ZylGPSReceiver->GetHeading());
  efSatCount->Text = IntToStr(ZylGPSReceiver->GetSatelliteCount());

  try
  {
    memRaw->Lines->Add(ZylGPSReceiver->GetRawData());
  }
  catch(...)
  {
    memRaw->Lines->Clear();
  }
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::ZylGPSReceiver2DPositionChange(TObject *Sender,
      Extended Latitude, Extended Longitude)
{
  memRaw->Lines->Add("2D Position Changed. Distance to New York City = " + FloatToStr(ZylGPSReceiver->Distance_KM(Latitude, Longitude, 40.77, 73.98)) + " KM");
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::ZylGPSReceiver3DPositionChange(TObject *Sender,
      Extended Latitude, Extended Longitude, Extended Altitude)
{
  memRaw->Lines->Add("3D Position Changed.");
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::ZylGPSReceiverAltitudeChange(TObject *Sender,
      Extended Value)
{
  memRaw->Lines->Add("Altitude Changed:" + FloatToStr(Value) + " M");
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::ZylGPSReceiverHeadingChange(TObject *Sender,
      Extended Value)
{
  memRaw->Lines->Add("Heading Changed:" + FloatToStr(Value) + " Degrees");
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::ZylGPSReceiverLatitudeChange(TObject *Sender,
      int Degree, int Minute, Extended Second, TCardinalPoint Direction)
{
  memRaw->Lines->Add("Latitude Changed.");         
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::ZylGPSReceiverLongitudeChange(TObject *Sender,
      int Degree, int Minute, Extended Second, TCardinalPoint Direction)
{
  memRaw->Lines->Add("Longitude Changed.");        
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::ZylGPSReceiverSpeedChange(TObject *Sender,
      Extended Value)
{
  memRaw->Lines->Add("Speed Changed:" + FloatToStr(Value) + " KM/H");        
}
//---------------------------------------------------------------------------

