unit uR7000;

// Receiver address must be 8 .
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Registry, ExtCtrls,
  AbNumEdit,
  Buttons,
  AxCtrls, DBOleCtl,
  AbLED, AbSwitch, Mask,
  ComCtrls, Menus, uSETITypes,

  SDL_RasterLab, SDL_gradfl, _GClass, OleCtrls,
  AbGearDial, AdPacket, AdStatLt, OoMisc, AdPort, {KNOBCTRLLib_TLB,} VrControls,
  VrWheel;

const
  sbModeID = 0;
  sbMode = sbModeID + 1;
  sbAlertID = sbMode + 1;
  sbAlert = sbAlertID + 1;

  Preamble1 = $FE;
  Preamble2 = $FE;
  DestAddr = $8;
  SrcAddr = $1;
  EndByte = $FD;

  MinLoFreq = 0025000000;
  MaxLoFreq = 0999999900;
  MinHiFreq = 1025000000;
  MaxHiFreq = 1999999900;
  OneGHz = 1000000000;
  Hz = 1;

  Post = AnsiChar(EndByte);
  SETINetReceiverKey = '\SOFTWARE\SETI Net\Receiver\R7000';

type
  TfrmReceiver = class(TForm)
    pdComPort: TApdComPort;
    ledPort: TApdSLController;
    mnu: TMainMenu;
    mnuHelp: TMenuItem;
    Help1: TMenuItem;
    mnuAbout: TMenuItem;
    mnuHints: TMenuItem;
    mnuSimulation: TMenuItem;
    mnuSimOff: TMenuItem;
    mnuSimOn: TMenuItem;
    tmrRefresh: TTimer;
    tmrTimeOut: TTimer;
    pnlTop: TPanel;
    gbPort: TGroupBox;
    lblPort: TLabel;
    lblRate: TLabel;
    rgParity: TRadioGroup;
    rgDataBits: TRadioGroup;
    rgStopBits: TRadioGroup;
    gbPortStatus: TGroupBox;
    ledDCD: TApdStatusLight;
    ledDTR: TApdStatusLight;
    ledDSR: TApdStatusLight;
    ledRTS: TApdStatusLight;
    ledCTS: TApdStatusLight;
    lblCTS: TLabel;
    lblRTS: TLabel;
    lblDSR: TLabel;
    lblDTR: TLabel;
    lblDCD: TLabel;
    ledRx: TApdStatusLight;
    lblRX: TLabel;
    lblTX: TLabel;
    ledTx: TApdStatusLight;
    ledBK: TApdStatusLight;
    lblBX: TLabel;
    ledErr: TApdStatusLight;
    lblError: TLabel;
    comboRate: TComboBox;
    comboPort: TComboBox;
    lblRadioAddr: TLabel;
    comboRadioAddr: TComboBox;
    lblroR7000SvrPort: TLabel;
    pnlBottom: TPanel;
    gbR7000: TGroupBox;
    lbl1GHz: TLabel;
    rgMode: TRadioGroup;
    gbFreq: TGroupBox;
    ledOpFreq: TRasterLab;
    frequd100: TUpDown;
    freqUD1K: TUpDown;
    frequd10K: TUpDown;
    frequd1Meg: TUpDown;
    frequd10Meg: TUpDown;
    frequd100Meg: TUpDown;
    frequp100K: TUpDown;
    fregGhzud: TUpDown;
    gbOnOff: TGroupBox;
    swOpen: TAbRockerSwitch;
    lblSim: TLabel;
    cbSim: TCheckBox;
    btnSmall: TButton;
    btnLarge: TButton;
    sbReceiver: TStatusBar;
    pdGetMode: TApdDataPacket;
    pdGetFreq: TApdDataPacket;
    pdGetModeTx: TApdDataPacket;
    pdGetFreqTx: TApdDataPacket;
    ledOpen: TAbLED;
    knobFreq: TVrWheel;
    procedure FormCreate(Sender: TObject);
    procedure rgModeClick(Sender: TObject);

    procedure FormClose(Sender: TObject; var Action: TCloseAction);

    procedure pdGetModePacket(Sender: TObject; Data: Pointer; Size: Integer);
    procedure pdGetFreqPacket(Sender: TObject; Data: Pointer; Size: Integer);
    procedure pdGetFreqTXPacket(Sender: TObject; Data: Pointer; Size: Integer);
    procedure btnCloseClick(Sender: TObject);

    procedure btnSetFreqClick(Sender: TObject);
    procedure mnuExitClick(Sender: TObject);
    procedure Help1Click(Sender: TObject);
    procedure mnuAboutClick(Sender: TObject);
    procedure mnuHintsClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure swOpenClick(Sender: TObject);
    procedure btnSmallClick(Sender: TObject);
    procedure btnlargeClick(Sender: TObject);

    procedure mnuSimOnClick(Sender: TObject);
    procedure btnFClick(Sender: TObject);

    procedure cbSimClick(Sender: TObject);

    procedure btnTClick(Sender: TObject);
    procedure tmrRefreshTimer(Sender: TObject);

    procedure tmrTimeOutTimer(Sender: TObject);
    procedure fregGhzudChangingEx(Sender: TObject; var AllowChange: Boolean; NewValue: Integer; Direction: TUpDownDirection);
    procedure frequd100ChangingEx(Sender: TObject; var AllowChange: Boolean; NewValue: Integer; Direction: TUpDownDirection);
    procedure freqUD1KChangingEx(Sender: TObject; var AllowChange: Boolean; NewValue: Integer; Direction: TUpDownDirection);
    procedure frequd10KChangingEx(Sender: TObject; var AllowChange: Boolean; NewValue: Integer; Direction: TUpDownDirection);
    procedure frequp100KClick(Sender: TObject; Button: TUDBtnType);
    procedure frequp100KChangingEx(Sender: TObject; var AllowChange: Boolean; NewValue: Integer; Direction: TUpDownDirection);
    procedure frequd1MegChangingEx(Sender: TObject; var AllowChange: Boolean; NewValue: Integer; Direction: TUpDownDirection);
    procedure frequd10MegChangingEx(Sender: TObject; var AllowChange: Boolean; NewValue: Integer; Direction: TUpDownDirection);
    procedure frequd100MegChangingEx(Sender: TObject; var AllowChange: Boolean; NewValue: Integer; Direction: TUpDownDirection);
    procedure pdGetModeStringPacket(Sender: TObject; Data: AnsiString);
    procedure pdGetFreqStringPacket(Sender: TObject; Data: AnsiString);
    procedure pdGetModeTXTimeout(Sender: TObject);
    procedure comboPortChange(Sender: TObject);
    procedure knobFreqChange(Sender: TObject);

  protected
    { Protected declarations }
    FOpen: Boolean;
    FSimulation: Boolean;
    FFreq: Int64;
    FSimFreq: Int64;
    FMode: TReceiverMode;
  private
    { Private declarations }

    TimedOut: Boolean;

    FreqReady: Boolean;
    SetRadio: AnsiString;
    swGHz: Boolean;
    SETINetReg: TRegistry;
    knobFF: Boolean;

    Bigerizer: Boolean;
    function FrequencyReady: Boolean;

    procedure DisplayRecFreq(value: Integer);
    procedure ChangeRecFreq(Direction: TUpDownDirection; Amount: Integer);

    procedure RequestRecFreq;
    procedure SetRecFreq(value: Integer);

    function GetMode: TReceiverMode;
    procedure SetMode(value: TReceiverMode);
    procedure DisplayHint(Sender: TObject);

    procedure SetFrequency(value: Int64);
    function GetFrequency: Int64;
    procedure setSimulation(value: Boolean);
    function getOpen: Boolean;
    procedure SetOpen(value: Boolean);
  public { Public declarations }
    InUse: Boolean;

    property Ready: Boolean read getOpen write SetOpen;
    property Frequency: Int64 read GetFrequency write SetFrequency;
    property Simulation: Boolean read FSimulation write setSimulation;
    property Mode: TReceiverMode read GetMode write FMode;
  end;

var
  frmReceiver: TfrmReceiver;

implementation

{$R *.dfm}

uses uAbout;
{$REGION 'C R E A T E '}

(* C r e a t e *)
procedure TfrmReceiver.FormCreate(Sender: TObject);
var
  i: Integer;
  TempPort: Integer;
begin
  with TRegistry.create do
    try
      rootkey := HKEY_LOCAL_MACHINE;

      if keyexists('HARDWARE') then
      begin
        if openkeyReadOnly('HARDWARE') then
        begin
          if keyexists('DEVICEMAP') then
          begin
            if openkey('DEVICEMAP', False) then
            begin
              if keyexists('SERIALCOMM') then
              begin
                if openkey('SERIALCOMM', False) then
                begin
                  GetValueNames(comboPort.items);
                  for i := 0 to comboPort.items.count - 1 do
                  begin
                    comboPort.items[i] := ReadString(comboPort.items[i]);
                  end;
                end
              end;
            end;
          end;
        end;
      end;
    finally
      free;
    end;

  (* REGISTRY ENTRIES *)
  SETINetReg := TRegistry.create;
  SETINetReg.LazyWrite := False;
  try
    SETINetReg.rootkey := HKEY_CURRENT_USER;
    if SETINetReg.keyexists(SETINetReceiverKey) then
    begin { Key Exists }
      if SETINetReg.openkey(SETINetReceiverKey, False) then
      // OpenKey will create the key if the second parameter is TRUE and the key does not already exist
      // This one will NOT create the key
      begin // N O R M A L   R E G   E N T R I E S
        try // open key
          with SETINetReg do
          begin // Setup the GUI here - All of it
            TempPort := ReadInteger('Port');
            comboPort.ItemIndex := TempPort;

            comboRate.ItemIndex := ReadInteger('Rate');
            rgParity.ItemIndex := ReadInteger('Parity');
            rgDataBits.ItemIndex := ReadInteger('DataBits');
            rgStopBits.ItemIndex := ReadInteger('Stopbits');
            comboRadioAddr.ItemIndex := ReadInteger('RadioAddr');

            FFreq := ReadInteger('LastRecFreq');
            Bigerizer := ReadBool('Bigerizer');
            if Bigerizer then
              btnlargeClick(self)
            else
              btnSmallClick(self);

            swGHz := ReadBool('swGHZ');

          end { with SETINetReg }
        except // an exception is raised if there isn't an entry
          SETINetReg.DeleteKey(SETINetReceiverKey);
          // If the version value didn't exist delete the whole key
        end; // Open Key
      end; // Open key
    end; // SETINetReceiverKey exists

    if not SETINetReg.keyexists(SETINetReceiverKey) then
    // D E F A U L T   R E G   E N T R I E S
    begin // New User or incomplete registry entries so rebuild.
      with SETINetReg do
        openkey(SETINetReceiverKey, True);
      // OpenKey will create the key if the second parameter is true and the key does not already exist
      begin
        comboPort.ItemIndex := 0;
        comboRate.ItemIndex := 1;
        rgParity.ItemIndex := 0;
        rgDataBits.ItemIndex := 1;
        rgStopBits.ItemIndex := 0;
        comboRadioAddr.ItemHeight := 2;
        FFreq := 1420000000;
        swGHz := False;
        Bigerizer := True;

      end;
    end;

  finally

    SETINetReg.free;
  end;

  SetMode(rmPowerOff);

  pdGetFreqTx.StartString := #254#254#0#8#00; // Create a Set Freq TX
  pdGetMode.StartString := #254#254#0#8#01; // Create a Get Mode
  pdGetModeTx.StartString := #254#254#1#8#4#0; // Create a Get Mode TX
  sbReceiver.Panels[sbAlert].Text := ' Turn on Hints through HELP menu... ';
  knobFF := False;
  rgParity.ItemIndex := 0;
  sbReceiver.Panels[sbMode].Text := 'Off';
  ledOpFreq.Text := 'Off';

  FSimFreq := 1420000000;

{$IF Defined ( EUREKALOG)}
  frmReceiver.Caption := frmReceiver.Caption + (' + EurekaLog ');
{$IFEND}
{$ENDREGION}
end; // End Create

procedure TfrmReceiver.setSimulation(value: Boolean);
begin
  FSimulation := value;
  cbSim.Checked := value;
  mnuSimOn.Checked := value;
  mnuSimOff.Checked := not mnuSimOn.Checked;

end;

procedure TfrmReceiver.DisplayHint(Sender: TObject);
begin
  if mnuHints.Checked then
    sbReceiver.Panels[sbAlert].Text := GetLongHint(Application.Hint);
end;

function TfrmReceiver.getOpen: Boolean;
begin
  if not Simulation then
    result := FOpen
  else
    result := True;
end;

function TfrmReceiver.FrequencyReady: Boolean;
var
  intT: Integer;
  strT: AnsiString;
begin
  FreqReady := False; // Not ready to begin with
  intT := StrToInt(comboRadioAddr.items[comboRadioAddr.ItemIndex]);
  SetRadio := AnsiChar(Preamble1) + AnsiChar(Preamble2) + AnsiChar(intT) + AnsiChar(SrcAddr);
  strT := SetRadio + #3 + Post;
  pdComPort.Output := SetRadio + #3 + Post; // Cause radio to respond
  TimedOut := False; //
  tmrTimeOut.Enabled := True; // Start time out timer running
  while Not FreqReady do
  begin
    if TimedOut then
    begin
      result := False;
      break;
    end
    else
    begin
      // sbReceiver.Panels[sbAlert].Text := 'Waiting For ' + IntToStr(tmrTimeOut.Interval div 1000) + ' seconds';
      Application.ProcessMessages;
    end;
  end;
  tmrTimeOut.Enabled := False;
  sbReceiver.Panels[sbAlert].Text := '';
  result := FreqReady;
end;

procedure TfrmReceiver.frequp100KChangingEx(Sender: TObject; var AllowChange: Boolean; NewValue: Integer; Direction: TUpDownDirection);
begin
  ChangeRecFreq(Direction, 100000);
end;

procedure TfrmReceiver.frequp100KClick(Sender: TObject; Button: TUDBtnType);
begin

end;

(* S E T   O P E N *)
procedure TfrmReceiver.SetOpen(value: Boolean);

var
  strT: string;
  BagOut: Boolean;
  procedure ClosePort;
  begin
    swOpen.Checked := False;
    gbFreq.Enabled := True;
    gbPort.Enabled := True;

    // knobFreq.Enabled := false;
    ledOpen.Checked := False;
    ledPort.Monitoring := False;
    FOpen := False;
    tmrRefresh.Enabled := False;
    pdComPort.Open := False; // Shut down the com port
    FOpen := False; //
  end;

begin // B e g i n   O P E N
  if ((FSimulation) OR (FOpen = value)) then
    exit;
  BagOut := False;
  if value = True then
  begin
    with pdComPort do
    begin
      Baud := StrToInt(comboRate.items[comboRate.ItemIndex]);
      // case rgParity.ItemIndex of
      // 0: pdComPort.Parity := 2;
      // 1: Parity := pOdd;
      // 3: Parity := pEven;
      // end;
      DataBits := StrToInt(rgDataBits.items[rgDataBits.ItemIndex]);
      StopBits := StrToInt(rgStopBits.items[rgStopBits.ItemIndex]);
      Try
        // ComNumber := StrToInt(StrRestOf(comboPort.items[comboPort.ItemIndex], 4));
        strT := comboPort.items[comboPort.ItemIndex];
        Delete(strT, 1, 3);

        ComNumber := StrToInt(strT);
      except
        Showmessage('Port: ' + IntToStr(ComNumber) + ' error');
        BagOut := True;
      end;

      Try
        Open := True; // Try to open the port
      Except
        Showmessage('I think this Port is in use!');
        BagOut := True;
      end;
      if BagOut then
        exit;

      sbReceiver.Panels[sbMode].Text := 'Port Open';
      swOpen.Checked := True;
      ledPort.Monitoring := True;
      gbPort.Enabled := False;
      ledOpFreq.Text := 'Searching ';
      sbReceiver.Panels[sbAlert].Text := 'Searching For about 1/2 second.';

      RequestRecFreq;
      if FrequencyReady = True then
      begin
        sbReceiver.Panels[sbMode].Text := 'Found Radio';
        sbReceiver.Panels[sbAlert].Text := 'Radio Ready';
        gbPort.Enabled := False;
        gbFreq.Enabled := True; // and frequency changes
        ledOpen.LED.ColorOn := clLime;
        ledOpen.Checked := True;

        pdGetFreqTx.Enabled := True; // start allowing TX input.
        pdGetModeTx.Enabled := True; //
        pdGetMode.Enabled := True;
        rgMode.Enabled := True; // and mode changes
        DisplayRecFreq(FFreq);
        tmrRefresh.Enabled := True;
        FOpen := True;
      end
      else
      begin
        ClosePort;
        ledOpFreq.Text := 'NoReceiver';
        sbReceiver.Panels[sbAlert].Text := 'Rcvr not found chk power & port settings';
      end;
    end;
  end
  else
  begin
    ClosePort;
  end;
end;
(* G E T T E R S  and  S E T T E R S *)

procedure TfrmReceiver.SetFrequency(value: Int64);

begin

  if ((FSimulation) OR (Not getOpen)) then
  begin
    // exit;
    FSimFreq := value;
    ledOpFreq.Text := IntToStr(FSimFreq);
  end
  else
  begin
    if ((value > MinLoFreq - 1) AND (value < MaxLoFreq)) then
    begin
      SetRecFreq(value div 100);
      swGHz := False;
      RequestRecFreq;
    end
    else if ((value > MinHiFreq - 1) AND (value < MaxHiFreq)) then
    begin
      SetRecFreq((value - OneGHz) div 100);
      swGHz := True;
      RequestRecFreq;
    end
  end;

end;

function TfrmReceiver.GetFrequency: Int64;
begin

  if not FSimulation then
  begin
    if FOpen then
    begin
      if swGHz then
        result := FFreq + OneGHz
      else
        result := FFreq
    end { good FRecFreq }

  end { was Ready }
  else
  begin // Simulation
    result := FSimFreq;
    ledOpFreq.Text := IntToStr(FSimFreq);
  end;

end;

(* R e q u e s t   R e c  F r e q *)
procedure TfrmReceiver.RequestRecFreq;
var
  PortOut: AnsiString;

  RecvNum: Integer;
begin
  RecvNum := StrToInt(comboRadioAddr.items[comboRadioAddr.ItemIndex]);
  // ------- Preamble         Preamble        Receiver            Computer      Command        END
  PortOut := AnsiChar($FE) + AnsiChar($FE) + AnsiChar(RecvNum) + AnsiChar($1) + AnsiChar($3) + AnsiChar($FD);
  pdComPort.Output := PortOut;
  FrequencyReady;
end;

(* G e t   F r e q    P a c k e t *)
procedure TfrmReceiver.pdGetFreqPacket(Sender: TObject; Data: Pointer; Size: Integer);

var
  buffer: array [0 .. 17] of byte;
  i: Integer;
  intFreq: Integer;
  inByte: byte;
  LeftNib: byte;
  rightNib: byte;
  twoDigits: Integer;
begin

  Move(Data^, buffer, Size);
  intFreq := 0;
  for i := Size - 1 downto 1 do
  begin
    intFreq := intFreq * 100;
    inByte := buffer[i];
    rightNib := inByte and $0F;
    LeftNib := inByte and $F0;
    LeftNib := LeftNib shr 4;
    twoDigits := rightNib + LeftNib * 10;
    intFreq := intFreq + twoDigits;
  end;
  FreqReady := True;

  FFreq := intFreq * 100;
end;

procedure TfrmReceiver.pdGetFreqStringPacket(Sender: TObject; Data: AnsiString);
begin

end;

(* G e t   M o d e   P a c k e t   T x   m o d e *)
procedure TfrmReceiver.pdGetModeTXTimeout(Sender: TObject);
begin

end;

(* G e t   F r e q    P a c k e t   T x   m o d e *)
procedure TfrmReceiver.pdGetFreqTXPacket(Sender: TObject; Data: Pointer; Size: Integer);
var
  buffer: array [0 .. 17] of byte;
  i: Integer;
  intFreq: Integer;
  inByte: byte;
  LeftNib: byte;
  rightNib: byte;
  twoDigits: Integer;
begin

  Move(Data^, buffer, Size);
  intFreq := 0;
  for i := Size - 1 downto 1 do
  begin
    intFreq := intFreq * 100;
    inByte := buffer[i];
    rightNib := inByte and $0F;
    LeftNib := inByte and $F0;
    LeftNib := LeftNib shr 4;
    twoDigits := rightNib + LeftNib * 10;
    intFreq := intFreq + twoDigits;
  end;
  FreqReady := True;

  FFreq := (intFreq * 100);
end;

procedure TfrmReceiver.SetRecFreq(value: Integer);
(* S e t  R e c  F r e q *)
var
  strValue: AnsiString;
  byte1, byte2, byte3, byte4: byte;
  PortOut: AnsiString;

begin
  if Ready then
  begin
    // knobFreq.Position := value DIV 100;
    strValue := IntToStr(value);
    if value < 1000000 then
      strValue := '0' + strValue; // Must have seven digits

    byte1 := (StrToInt(strValue[6]) shl 4) OR StrToInt(strValue[7]);
    byte2 := (StrToInt(strValue[4]) shl 4) OR StrToInt(strValue[5]);
    byte3 := (StrToInt(strValue[2]) shl 4) OR StrToInt(strValue[3]);
    byte4 := StrToInt(strValue[1]);
    // ------- Preamble         Preamble        Receiver      Computer      Command       SubCommand
    PortOut := AnsiChar($FE) + AnsiChar($FE) + AnsiChar(8) + AnsiChar(1) + AnsiChar(5) + AnsiChar($00) +
    // LSBs              Middle two        Left two          MSB               END
      AnsiChar(byte1) + AnsiChar(byte2) + AnsiChar(byte3) + AnsiChar(byte4) + AnsiChar($FD);

    pdComPort.Output := PortOut;
    // DelayTicks(20, True);
    // DelayTicks(20, True);
    // DelayTicks(20, True);
    // FRecFreq := 0;
  end;
end;

procedure TfrmReceiver.SetMode(value: TReceiverMode);
(* S e t   M o d e   C o m m a n d *)
var
  strT: AnsiString;
begin
  if not FSimulation then
  begin
    case value of
      rmAM:
        begin // AM
          strT := '#254#254#8#1#6#2#255';
          pdComPort.Output := SetRadio + AnsiChar($6) + AnsiChar($2) + Post;
          FMode := rmAM;
          rgMode.ItemIndex := 0;
        end;
      rmFMW:
        begin
          pdComPort.Output := SetRadio + AnsiChar($6) + AnsiChar($5) + Post;
          FMode := rmFMW;
          rgMode.ItemIndex := 1;
        end;
      rmFMN:
        begin
          pdComPort.Output := SetRadio + AnsiChar($6) + AnsiChar($5) + AnsiChar($2) + Post;
          FMode := rmFMN;
          rgMode.ItemIndex := 2;
        end;
      rmCW, rmLSB, rmUSB, rmSSB: // on the R7000 these are all the same.
        begin
          pdComPort.Output := SetRadio + AnsiChar($6) + AnsiChar($5) + AnsiChar($0) + Post;
          FMode := rmSSB;
          rgMode.ItemIndex := 3;
        end;
    end;
  end
  else
  begin
    //
  end;
end;

function TfrmReceiver.GetMode: TReceiverMode;
(* G e t  M o d e *)
begin
  if not FSimulation then
  begin
    pdComPort.Output := SetRadio + AnsiChar($4) + Post;
    result := FMode;
  end;
end;

procedure TfrmReceiver.pdGetModePacket(Sender: TObject; Data: Pointer; Size: Integer);
(* G e t   M o d e   P a c k e t *)
var
  buffer: array [0 .. 7] of byte;
  intMode: Integer;
begin
  Move(Data^, buffer, Size);
  intMode := buffer[0];
  if Size = 2 then
  begin
    intMode := intMode * 10;
    intMode := intMode + buffer[1];
  end;
  // Ready := True;
  case intMode of
    2:
      begin
        SetMode(rmAM);
        FMode := rmAM;
        rgMode.ItemIndex := 0
      end;
    5:
      begin
        SetMode(rmFMW);
        FMode := rmFMW;
        rgMode.ItemIndex := 1
      end;
    52:
      begin
        SetMode(rmFMN);
        FMode := rmFMN;
        rgMode.ItemIndex := 2
      end;
    50:
      begin
        SetMode(rmSSB);
        FMode := rmSSB;
        rgMode.ItemIndex := 3
      end;
  else
    // ShowMessage('bad Mode packet.');
  end;
end;

procedure TfrmReceiver.pdGetModeStringPacket(Sender: TObject; Data: AnsiString);
begin

end;

(* End G e t t e r s   and   S e t t e r s *)

procedure TfrmReceiver.DisplayRecFreq(value: Integer);
var
  TempValue: Integer;
  valueString: AnsiString;
  readyString: AnsiString;
begin
  TempValue := value;
  if TempValue = 0 then
  begin
    ledOpFreq.Text := 'Zero Boss';
    exit;
  end;
  // if swGHz then
  // TempValue := TempValue - 1000000000;
  TempValue := TempValue DIV 100;

  valueString := '1234567';
  readyString := '123.456.7';
  // if ((TempValue < 0) OR (TempValue > 9999999)) then
  // valueString := '0000000'
  // else
  valueString := IntToStr(TempValue);
  if TempValue < 1000000 then
    valueString := '0' + valueString;

  readyString[9] := valueString[7];
  readyString[8] := '.';
  readyString[7] := valueString[6];
  readyString[6] := valueString[5];
  readyString[5] := valueString[4];
  readyString[4] := '.';
  readyString[3] := valueString[3];
  readyString[2] := valueString[2];
  readyString[1] := valueString[1];
  if swGHz then
    readyString := '1' + readyString
  else
    readyString := '0' + readyString;

  ledOpFreq.Text := readyString;
end;

procedure TfrmReceiver.rgModeClick(Sender: TObject);
begin

  case rgMode.ItemIndex of
    0:
      SetMode(rmAM);
    1:
      SetMode(rmFMW);
    2:
      SetMode(rmFMN);
    3:
      SetMode(rmSSB);
  end;
end;

procedure TfrmReceiver.btnCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmReceiver.btnSetFreqClick(Sender: TObject);
begin
  SetRecFreq(4203963)
end;

procedure TfrmReceiver.ChangeRecFreq(Direction: TUpDownDirection; Amount: Integer);
var
  NewRecFreq: Integer;
begin
  if (Direction = updUp) then
    NewRecFreq := FFreq + Amount
  else
    NewRecFreq := FFreq - Amount;
  SetRecFreq(NewRecFreq);
  RequestRecFreq;

end;

procedure TfrmReceiver.comboPortChange(Sender: TObject);
var
  s: string;
begin
  s := comboPort.items[comboPort.ItemIndex];
  Delete(s, 1, 3);
  pdComPort.ComNumber := StrToInt(s);
end;

procedure TfrmReceiver.fregGhzudChangingEx(Sender: TObject; var AllowChange: Boolean; NewValue: Integer; Direction: TUpDownDirection);
begin
  if Direction = updUp then
    swGHz := True;
  if Direction = updDown then
    swGHz := False;
end;

procedure TfrmReceiver.frequd100ChangingEx(Sender: TObject; var AllowChange: Boolean; NewValue: Integer; Direction: TUpDownDirection);
begin
  ChangeRecFreq(Direction, 100);
end;

procedure TfrmReceiver.frequd100MegChangingEx(Sender: TObject; var AllowChange: Boolean; NewValue: Integer; Direction: TUpDownDirection);
begin
  ChangeRecFreq(Direction, 100000000);
end;

procedure TfrmReceiver.frequd10KChangingEx(Sender: TObject; var AllowChange: Boolean; NewValue: Integer; Direction: TUpDownDirection);
begin
  ChangeRecFreq(Direction, 10000);
end;

procedure TfrmReceiver.frequd10MegChangingEx(Sender: TObject; var AllowChange: Boolean; NewValue: Integer; Direction: TUpDownDirection);
begin
  ChangeRecFreq(Direction, 10000000);
end;

procedure TfrmReceiver.freqUD1KChangingEx(Sender: TObject; var AllowChange: Boolean; NewValue: Integer; Direction: TUpDownDirection);
begin
  ChangeRecFreq(Direction, 1000);
end;

procedure TfrmReceiver.frequd1MegChangingEx(Sender: TObject; var AllowChange: Boolean; NewValue: Integer; Direction: TUpDownDirection);
begin
  ChangeRecFreq(Direction, 1000000);
end;

procedure TfrmReceiver.mnuExitClick(Sender: TObject);
begin
  close;
end;

procedure TfrmReceiver.Help1Click(Sender: TObject);
begin
  Showmessage('Help available from support@SETI.Net');
end;

procedure TfrmReceiver.mnuAboutClick(Sender: TObject);
begin
  frmAbout := TfrmAbout.create(self);

  frmAbout.ShowVersion;

end;

procedure TfrmReceiver.mnuHintsClick(Sender: TObject);
begin
  mnuHints.Checked := not mnuHints.Checked;
  if not mnuHints.Checked then
    sbReceiver.Panels[sbAlert].Text := ' Turn on Hints through top menu... ';
  gbR7000.ShowHint := mnuHints.Checked;
  rgMode.ShowHint := mnuHints.Checked;
end;

procedure TfrmReceiver.mnuSimOnClick(Sender: TObject);
begin
  setSimulation(mnuSimOn.Checked);
end;

procedure TfrmReceiver.FormActivate(Sender: TObject);
begin
  { Assign the application’s OnHint event handler at runtime  because the Application is not available in the Object Inspector at design time }
  Application.OnHint := DisplayHint;
end;

procedure TfrmReceiver.knobFreqChange(Sender: TObject);
var
  KnobPos: Integer;
begin
  KnobPos := knobFreq.Position;
  sbReceiver.Panels[1].Text := IntToStr(KnobPos);
  // setRecFreq(FFreq + KnobPos);

end;

procedure TfrmReceiver.swOpenClick(Sender: TObject);
begin
  SetOpen(swOpen.Checked);
end;

procedure TfrmReceiver.tmrRefreshTimer(Sender: TObject);
begin
  DisplayRecFreq(FFreq);
end;

procedure TfrmReceiver.tmrTimeOutTimer(Sender: TObject);
begin
  TimedOut := True;
end;

procedure TfrmReceiver.btnlargeClick(Sender: TObject);
begin
  pnlTop.Visible := True;
  pnlTop.Align := altop;
  pnlBottom.Align := alBottom;

  Height := pnlTop.Height + pnlBottom.Height + 50;
  Bigerizer := True;
end;

procedure TfrmReceiver.btnSmallClick(Sender: TObject);
begin
  pnlTop.Visible := False;
  pnlBottom.Align := altop;
  Height := pnlTop.Height + 90;
  Bigerizer := False;
end;

procedure TfrmReceiver.btnTClick(Sender: TObject);
begin

  RequestRecFreq;
end;

procedure TfrmReceiver.FormClose(Sender: TObject; var Action: TCloseAction);
(* C l o s e   F o r m *)
begin
  if getOpen then
    SetOpen(False);

  SETINetReg := TRegistry.create;
  try
    with SETINetReg do
    begin
      rootkey := HKEY_CURRENT_USER;
      DeleteKey(SETINetReceiverKey); // Kill the whole key every time.
      openkey(SETINetReceiverKey, True); // Then write it back

      WriteInteger('Port', comboPort.ItemIndex);
      WriteInteger('Rate', comboRate.ItemIndex);
      WriteInteger('Parity', rgParity.ItemIndex);
      WriteInteger('DataBits', rgDataBits.ItemIndex);
      WriteInteger('StopBits', rgStopBits.ItemIndex);
      WriteInteger('RadioAddr', comboRadioAddr.ItemIndex);
      WriteInteger('LastRecFreq', FFreq);
      WriteBool('swGHZ', swGHz);
      WriteBool('Bigerizer', Bigerizer);

      CloseKey;
    end; // SETINetReg
  finally
    SETINetReg.free;
  end; // try..finally

end; // CloseForm

procedure TfrmReceiver.btnFClick(Sender: TObject);
begin
  RequestRecFreq;

end;

procedure TfrmReceiver.cbSimClick(Sender: TObject);
begin
  setSimulation(cbSim.Checked);
end;

end.
