unit Timer_Counter;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    cbxSelBoard: TComboBox;
    btnOutput: TButton;
    btnExit: TButton;
    ListBox1: TListBox;
    Value: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    Label2: TLabel;
    Channel: TComboBox;
    Mode: TComboBox;
    procedure FormCreate(Sender: TObject);
    procedure btnExitClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnOutputClick(Sender: TObject);
    procedure cbxSelBoardChange(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
uses UniDAQ;
{$R *.DFM}
var
   wTotalBoards,wRtn,wBoardNo,wChannel,wCfgCode:Word;
   sCardInfo:Array [0..31] of IXUD_CARD_INFO;
   sDevInfo:Array [0..31] of IXUD_DEVICE_INFO;
   sSelCardInfo:IXUD_CARD_INFO;
   sSelDevInfo:IXUD_DEVICE_INFO;
   szModelName: Array [0..19] of char;

procedure TForm1.FormCreate(Sender: TObject);
var
   wBoardIndex:WORD;
begin
     //Initital  resource and Get Boroad numbers from driver
     wRtn := Ixud_DriverInit(wTotalBoards);

     If wRtn <> NoError Then
     begin
        Application.MessageBox('*** DriverInit ! ***', 'Error' , IDOK);
        Exit;
     End;

     If wTotalBoards <1 Then
     begin
        Application.MessageBox('*** Do not Find Board! ***', 'Error' , IDOK);
        Exit;
     End;

     for wBoardIndex :=0  to  wTotalBoards - 1  do
     begin
         //Get Card Information
         wRtn := Ixud_GetCardInfo(wBoardIndex,sDevInfo[wBoardIndex],sCardInfo[wBoardIndex],szModelName);
         cbxSelBoard.Items.Add(IntToStr(wBoardIndex)+':'+szModelName);
     end;

     cbxSelBoard.ItemIndex :=0;
     Mode.ItemIndex :=2;
     cbxSelBoardChange(Sender);
end;

procedure TForm1.btnExitClick(Sender: TObject);
begin
     //wRtn:=Ixud_DriverClose();
     Close;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
     //Release the resource from driver
     wRtn := Ixud_DriverClose;
end;

procedure TForm1.btnOutputClick(Sender: TObject);
var
        wCounterCh,wCounterMode,i:WORD;
        wCounterValue : LongInt;
begin
     wBoardNo := cbxSelBoard.ItemIndex;
     if (sCardInfo[wBoardNo].wCounterChannels) =0 then
     begin
        Application.MessageBox ('Not support timer counter', 'Error' , IDOK);
        Exit;
     end;

     ListBox1.Items.Clear;

     wCounterValue := StrToInt(Value.Text);
     wCounterCh := Channel.ItemIndex;
     wCounterMode := Mode.ItemIndex;

     wRtn := Ixud_SetCounter(wBoardNo, wCounterCh, wCounterMode, wCounterValue);
     for i:=0 to 100 do
     begin
        wRtn:=Ixud_ReadCounter(wBoardNo, wCounterCh, wCounterValue);
        ListBox1.Items.Add(IntToStr(wCounterValue));
     end;
end;

procedure TForm1.cbxSelBoardChange(Sender: TObject);
var
        i:WORD;
begin

     Form1.Caption:= cbxSelBoard.Items.Strings[cbxSelBoard.ItemIndex]+'  Timer Counter';
     Channel.Clear;

     wBoardNo := cbxSelBoard.ItemIndex;

     //whether have the timer counter
     if (sCardInfo[wBoardNo].wCounterChannels) =0 then
     begin
        Application.MessageBox ('Not support timer counter', 'Error' , IDOK);
        btnOutput.Enabled := false;
     end
     else
     begin
         for i:=0 to sCardInfo[wBoardNo].wCounterChannels-1 do
           Channel.Items.Add(IntToStr(i));

        Channel.ItemIndex := 0;
        btnOutput.Enabled := true;
     end;
end;

end.

