unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, nrcommbox, nrclasses, nrcomm, nrdataproc, nrgps,
  ComCtrls, nrlogfile;

type
  TForm1 = class(TForm)
    nrGps1: TnrGps;
    nrComm1: TnrComm;
    nrDeviceBox1: TnrDeviceBox;
    CheckBox1: TCheckBox;
    Button1: TButton;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    ListView1: TListView;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    nrLogFile1: TnrLogFile;
    procedure Button1Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure nrGps1Coordinate(Sender: TObject;
      Coordinate: TnrGpsCoordinate; Course: TnrGpsCourse;
      Quality: TnrGpsQuality);
    procedure nrGps1Satellite(Sender: TObject);
    procedure ListView1CustomDrawItem(Sender: TCustomListView;
      Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  nrComm1.ConfigDialog();
end;

procedure TForm1.CheckBox1Click(Sender: TObject);
begin
  try
    nrComm1.Active := CheckBox1.Checked;
  except
    CheckBox1.Checked := nrComm1.Active;
    raise;
  end;
end;

procedure TForm1.nrGps1Coordinate(Sender: TObject;
  Coordinate: TnrGpsCoordinate; Course: TnrGpsCourse;
  Quality: TnrGpsQuality);
begin
  Label1.Caption := 'Latitude: ' + FloatToStr(Coordinate.Latitude);
  Label2.Caption := 'Longitude: ' + FloatToStr(Coordinate.Longitude);
  Label3.Caption := 'Altitude: ' + FloatToStr(Coordinate.Altitude);

  Label4.Caption := 'Speed: ' + FloatToStr(Course.Speed);
  Label5.Caption := 'Track: ' + FloatToStr(Course.Track);

  Label6.Caption := 'Track satellites: ' + IntToStr(Quality.Satellites);
  Label7.Caption := 'Valid: ' + BoolToStr(Quality.Valid, True);

  Label8.Caption := 'DOP: ' + FloatToStr(Quality.DOP);
  Label9.Caption := 'HDOP: ' + FloatToStr(Quality.HDOP);
  Label10.Caption := 'VDOP:  ' + FloatToStr(Quality.VDOP);

end;

procedure TForm1.nrGps1Satellite(Sender: TObject);
var itm:TListItem;
    i:integer;
begin
  ListView1.Items.BeginUpdate;
  ListView1.Items.Clear;

  for i := 0 to nrGps1.SatelliteCount - 1 do begin
    itm := ListView1.Items.Add;
    itm.Caption := IntToStr(nrGps1.Satellites[i].No);
    itm.Data :=  Pointer(i);
    itm.SubItems.Add(FloatToStr(nrGps1.Satellites[i].Elevation));
    itm.SubItems.Add(FloatToStr(nrGps1.Satellites[i].Azimuth));
    itm.SubItems.Add(BoolToStr(nrGps1.Satellites[i].Used, True));
    itm.SubItems.Add(FloatToStr(nrGps1.Satellites[i].SNR));
  end;

  ListView1.Items.EndUpdate;

  Label11.Caption := 'Visible Satellites: ' + IntToStr(nrGps1.SatelliteCount);
end;

procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
var i : integer;
begin
  i := Integer(Item.Data);
  if i >= nrGps1.SatelliteCount then exit;
  if nrGps1.Satellites[i].Used
    then Sender.Canvas.Brush.Color := clLime // clLime
  else
    if nrGps1.Satellites[i].SNR >= 20
      then Sender.Canvas.Brush.Color := clOlive
      else Sender.Canvas.Brush.Color := clMaroon;

end;

end.
