unit MainUnit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls, Loadcell;

type
  TMainForm = class(TForm)
    OpenButton: TButton;
    ExitButton: TButton;
    ReadButton: TButton;
    TestButton: TButton;
    TestResultLabel: TLabel;
    ReadResultLabel: TLabel;
    Image1: TImage;
    Label1: TLabel;
    SetupButton: TButton;
    procedure OpenButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ReadButtonClick(Sender: TObject);
    procedure TestButtonClick(Sender: TObject);
    procedure SetupButtonClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ExitButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    TestLoops: Integer;
    hDriver: THandle;
  end;

var
  MainForm: TMainForm;

implementation

{$R *.DFM}

procedure TMainForm.OpenButtonClick(Sender: TObject);
begin
  if hDriver = INVALID_HANDLE_VALUE then
  begin
    OpenButton.Caption:= 'Opening...';
    OpenButton.Update;
    hDriver:= LCICOpen('LCIC1');
    if hDriver = INVALID_HANDLE_VALUE then
    begin
      OpenButton.Caption:= 'Open &Driver';
      MessageDlg(Format('Open Driver failed. Error code %.8x', [GetLastError]),
                 mtError, [mbOK], 0)
    end else
    begin
      LCICInit(hDriver);
      OpenButton.Caption:= 'Close &Driver'
    end;
  end else
  begin
    LCICDone(hDriver);
    LCICClose(hDriver);
    hDriver:= INVALID_HANDLE_VALUE;
    OpenButton.Caption:= 'Open &Driver'
  end
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  hDriver:= INVALID_HANDLE_VALUE;
  TestLoops:= 1000
end;

procedure TMainForm.ReadButtonClick(Sender: TObject);
var
  Val: Single;
  Msg: String;
  tc: DWORD;
begin
  tc:= GetTickCount;
  if LCICRead(hDriver, Val) then
    Msg:= FloatToStrF(Val, ffFixed, 6, 0)
  else
    Msg:= 'read error';
  ReadResultLabel.Caption:= Msg + ' tc ' + IntToStr(GetTickCount - tc);
end;

procedure TMainForm.TestButtonClick(Sender: TObject);
var
  Inp: String;
  Val: Single;
  i: Integer;
  gtc: DWORD;
begin
  Inp:= IntToStr(TestLoops);
  if InputQuery('Speed Test', 'Please enter number of read loops', Inp) then
  begin
    try
      TestLoops:= StrToInt(Inp);
      TestResultLabel.Caption:= 'Testing. Please wait';
      TestResultLabel.Update;
      gtc:= GetTickCount;
      for i:= 0 to TestLoops - 1 do
      if not LCICRead(hDriver, Val) then
        RaiseLastWin32Error;
      Val:= (GetTickCount - gtc)/TestLoops;
      TestResultLabel.Caption:= Format('%d reads at %.3f ms per read', [TestLoops, Val]);
    except
      on E:EConvertError do
        Application.ShowException(E)
    end
  end
end;

procedure TMainForm.SetupButtonClick(Sender: TObject);
begin
  LCICSetup(Handle, 'LCIC1')
end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if hDriver <> INVALID_HANDLE_VALUE then
    LCICClose(hDriver)
end;

procedure TMainForm.ExitButtonClick(Sender: TObject);
begin
  Close
end;

end.
