unit Settings;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TSettingsForm = class(TForm)
    bCancel: TButton;
    bOk: TButton;
    PageControl: TPageControl;
    tsSettings: TTabSheet;
    tsCalibration: TTabSheet;
    gbCalibName: TGroupBox;
    Label1: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label12: TLabel;
    eK: TEdit;
    eS: TEdit;
    cbCalibGain: TComboBox;
    cbCalibChannel: TComboBox;
    Button3: TButton;
    gbSettingsADC: TGroupBox;
    gbGeneral: TGroupBox;
    Label4: TLabel;
    cbFreequ: TComboBox;
    gbPGA: TGroupBox;
    cbTestADC1: TCheckBox;
    cbTestADC2: TCheckBox;
    cbGain0: TComboBox;
    cbGain1: TComboBox;
    gbBuffer: TGroupBox;
    rbBufferEnable: TRadioButton;
    rbBufferDisable: TRadioButton;
    gbInRange: TGroupBox;
    rbUnipolar: TRadioButton;
    rbBipolar: TRadioButton;
    gbFmod: TGroupBox;
    rbFmod128: TRadioButton;
    rbFmod256: TRadioButton;
    chbChannel0: TCheckBox;
    chbChannel1: TCheckBox;
    eRound0: TEdit;
    eRound1: TEdit;
    bgRange: TGroupBox;
    rbRangeH: TRadioButton;
    rbRangeL: TRadioButton;
    procedure FormCreate(Sender: TObject);
    procedure bOkClick(Sender: TObject);
    procedure cbCalibChannelChange(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
    // Загрузить конфигурацию из реестра
    procedure LoadConfiguration;

    // Сохранить конфигурацию в реестр
    function SaveConfiguration: boolean;

    function TryConvertInt(Sender : TCustomEdit;var  Value : integer; Range : boolean = false; Min: integer = 0; Max: integer = 100): boolean;
  public
    class procedure Execute;
    { Public declarations }
  end;

implementation

uses
  Edit,Config, Calib;

{$R *.dfm}

class procedure TSettingsForm.Execute;
begin
  with TSettingsForm.Create(Application) do
  try
    ShowModal;
  finally
    Free;
  end;
end;

function TSettingsForm.TryConvertInt(Sender : TCustomEdit;var  Value : integer; Range : boolean = false; Min: integer = 0; Max: integer = 100): boolean;
begin
  Result := False;
  try
    Value := StrToInt(Sender.Text);
    if Range and ((Value < Min) or (Value > Max)) then
        raise EConvertError.CreateFmt('Value is out of range [%d..%d]', [Min, Max]);
    Result := True;
  except
    on E : EConvertError do
    begin
      PageControl.ActivePageIndex := TTabSheet(Sender.Parent.Parent.Parent).TabIndex;
      Sender.SetFocus;
      Application.ShowException(E);
    end;
  end;
end;

procedure TSettingsForm.LoadConfiguration;
begin
  cbFreequ.ItemIndex  := Configuration.General.Freequ;
  chbChannel0.Checked := Configuration.General.Channel[0];
  chbChannel1.Checked := Configuration.General.Channel[1];
  cbGain0.ItemIndex   := Configuration.General.Gain[0];
  cbGain1.ItemIndex   := Configuration.General.Gain[1];
  cbTestADC1.Checked  := Configuration.General.TestADC0;
  cbTestADC2.Checked  := Configuration.General.TestADC1;

  rbBufferEnable.Checked  := Configuration.General.BufferEnable;
  rbBufferDisable.Checked := not Configuration.General.BufferEnable;

  rbRangeH.Checked   := Configuration.General.RangeH;
  rbRangeL.Checked   := not Configuration.General.RangeH;

  rbUnipolar.Checked := Configuration.General.Unipolar;
  rbBipolar.Checked  := not Configuration.General.Unipolar;

  rbFmod128.Checked  := Configuration.General.Fmod128;
  rbFmod256.Checked  := not Configuration.General.Fmod128;

  eRound0.Text := IntToStr(Configuration.General.Round[0]);
  eRound1.Text := IntToStr(Configuration.General.Round[1]);
end;

function TSettingsForm.SaveConfiguration: boolean;
var
  Round : array [0..1] of integer;
begin
  Result := TryConvertInt(eRound0, Round[0], true, 0, 15) and
    TryConvertInt(eRound1, Round[1], true, 0, 15);

  if Result then
  begin
    Configuration.General.Round[0] := Round[0];
    Configuration.General.Round[1] := Round[1];

    Configuration.General.Freequ       := cbFreequ.ItemIndex;
    Configuration.General.Channel[0]   := chbChannel0.Checked;
    Configuration.General.Channel[1]   := chbChannel1.Checked;
    Configuration.General.Gain[0]      := cbGain0.ItemIndex;
    Configuration.General.Gain[1]      := cbGain1.ItemIndex;
    Configuration.General.TestADC0     := cbTestADC1.Checked;
    Configuration.General.TestADC1     := cbTestADC2.Checked;
    Configuration.General.BufferEnable := rbBufferEnable.Checked;
    Configuration.General.RangeH       := rbRangeH.Checked;
    Configuration.General.Unipolar     := rbUnipolar.Checked;
    Configuration.General.Fmod128      := rbFmod128.Checked;
  end;
end;

procedure TSettingsForm.FormCreate(Sender: TObject);
begin
  LoadConfiguration;
  gbCalibName.Caption := Calibration.CalibName;
  Calibration.Load(gbCalibName.Caption);

  cbCalibChannelChange(nil);

end;

procedure TSettingsForm.bOkClick(Sender: TObject);
begin
  if SaveConfiguration then
      Calibration.Save(gbCalibName.Caption)
  else
      ModalResult := mrNone;
end;

procedure TSettingsForm.cbCalibChannelChange(Sender: TObject);
begin
  eK.Text := FloatToStr(Calibration.Calib.K[cbCalibChannel.ItemIndex, cbCalibGain.ItemIndex]);
  eS.Text := FloatToStr(Calibration.Calib.S[cbCalibChannel.ItemIndex, cbCalibGain.ItemIndex]);
end;

procedure TSettingsForm.Button3Click(Sender: TObject);
begin
  with TEditForm.Create(Self) do
  try
    GroupBox.Caption := Format('Канал: %s; Усиление: %s', [cbCalibChannel.Text, cbCalibGain.Text]);
    eK.Text := FloatToStr(Calibration.Calib.K[cbCalibChannel.ItemIndex, cbCalibGain.ItemIndex]);
    eS.Text := FloatToStr(Calibration.Calib.S[cbCalibChannel.ItemIndex, cbCalibGain.ItemIndex]);
    ShowModal;
    if ModalResult = mrOk then
    begin
      Calibration.Calib.K[cbCalibChannel.ItemIndex, cbCalibGain.ItemIndex] := StrToFloat(eK.Text);
      Calibration.Calib.S[cbCalibChannel.ItemIndex, cbCalibGain.ItemIndex] := StrToFloat(eS.Text);
      cbCalibChannelChange(nil);
    end;
  finally
    free;
  end;
end;

end.




