unit MainUnit;

interface

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

type
  TMainForm = class(TForm)
    Panel1: TPanel;
    PollTime: TTimer;
    VoltsList: TListBox;
    RangeRadios: TRadioGroup;
    GroupBox1: TGroupBox;
    DeviceCombo: TComboBox;
    DifferentialCheck: TCheckBox;
    Panel2: TPanel;
    GoButton: TButton;
    StopButton: TButton;
    procedure FormCreate(Sender: TObject);
    procedure GoButtonClick(Sender: TObject);
    procedure StopButtonClick(Sender: TObject);
    procedure PollTimeTimer(Sender: TObject);
    procedure DifferentialCheckClick(Sender: TObject);
  protected
    bDifferential: Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

{$R *.DFM}

uses
  AIOAIO;

procedure TMainForm.DifferentialCheckClick(Sender: TObject);
var
  iBoard: Integer;

begin
  bDifferential := DifferentialCheck.Checked;
  if not PollTime.Enabled then Exit;

  iBoard := DeviceCombo.ItemIndex;
  ADC_SetDifferential(iBoard, bDifferential);
end;

procedure TMainForm.FormCreate(Sender: TObject);
var
  I: Integer;
  DeviceStr: AnsiString;
  DeviceStrSize: TUInt32;
  Status: TWinErrCode;

begin
  DeviceCombo.Items.Clear;
  for I := 0 to BRD_GetHighestBoardIndex do begin
    DeviceStrSize := MAX_PATH;
    DeviceStr := AnsiString(StringOfChar(#0, DeviceStrSize));
    Status := BRD_GetName(I, DeviceStrSize, TPAnsiChar(DeviceStr), @DeviceStrSize);
    if Status <> ERROR_SUCCESS then
      DeviceStr := '<Error ' + AnsiString(IntToStr(Status)) + ' from BRD_GetName>'
    else
      SetLength(DeviceStr, DeviceStrSize)
    ;
    DeviceCombo.Items.Add('[' + IntToStr(I) + '] ' + String(DeviceStr));
  end;
  if DeviceCombo.Items.Count <> 0 then DeviceCombo.ItemIndex := 0;
end;

procedure TMainForm.GoButtonClick(Sender: TObject);
var
  iBoard: Integer;

begin
  StopButton.Enabled := True;

  iBoard := DeviceCombo.ItemIndex;
  AIOAIO.BRD_Reset(iBoard);
  ADC_SetDifferential(iBoard, bDifferential);

  PollTime.Enabled := True;
  GoButton.Enabled := False;
end;

procedure TMainForm.StopButtonClick(Sender: TObject);
begin
  GoButton.Enabled := True;
  PollTime.Enabled := False;
  StopButton.Enabled := False;
end;

procedure TMainForm.PollTimeTimer(Sender: TObject);
var
  iCh, dCh, iBoard, iRange: Integer;
  Status: TWinErrCode;
  V: array of TDouble;

begin
  iBoard := DeviceCombo.ItemIndex;
  iRange := RangeRadios.ItemIndex;

  if bDifferential then begin
    SetLength(V, 8);
    dCh := 2;
  end
  else begin
    SetLength(V, 16);
    dCh := 1;
  end;
  Status := ADC_GetImmediateScanV(iBoard, @V[0], iRange);
  if Status <> ERROR_SUCCESS then begin
    VoltsList.Items.Text := 'Error ' + IntToStr(Status) + '.';
    StopButton.Click;
    Exit;
  end;

  if VoltsList.Items.Count <> Length(V) then begin
    VoltsList.Items.Clear;
    for iCh := 0 to High(V) do VoltsList.Items.Add('');
  end;
  for iCh := 0 to High(V) do
      VoltsList.Items[iCh] := format('Ch %2d: %10.7f V', [iCh*dCh, V[iCh]] )
  ;
end;

end.
