unit Timer_Counter;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    cbxSelBoard: TComboBox;
    btnExit: TButton;
    ListBox1: TListBox;
    DelayMs: TEdit;
    Label4: TLabel;
    Label2: TLabel;
    Mode: TComboBox;
    Active: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btnExitClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure cbxSelBoardChange(Sender: TObject);
    procedure ActiveClick(Sender: TObject);
    procedure ModeChange(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;
   wChannelIndex: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 :=0;

    for wBoardIndex :=0 to wTotalBoards - 1 do
    begin
     for wChannelIndex := 0 to 15 do
     begin
        wRtn := Ixud_SetFCChannelMode(wBoardIndex, wChannelIndex, 2, 20); //Read Frequency Mode
     end;
    end;

     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.cbxSelBoardChange(Sender: TObject);
var
        i:WORD;
begin

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

     wBoardNo := cbxSelBoard.ItemIndex;

     //whether have the timer counter
     if (sCardInfo[wBoardNo].wCounterChannels) =0 then
     begin
        Application.MessageBox ('Not support timer counter', 'Error' , IDOK);
        Active.Enabled := false;
     end
     else
     begin
        Active.Enabled := true;
     end;
end;

procedure TForm1.ActiveClick(Sender: TObject);
var
        wChannelIndex,wStatus:WORD;
        fFrequency:Single;
        //Dim wChannelIndex As Long
begin
        ListBox1.Clear;
        wBoardNo := cbxSelBoard.ItemIndex;

        for wChannelIndex:=0 to 15 do
        begin

        wRtn := Ixud_ReadFrequency(wBoardNo, wChannelIndex, fFrequency, StrToInt(DelayMs.Text), wStatus); //Read Frequency
        ListBox1.Items.Add ('Channel '+Format('%0.2d',[wChannelIndex])+';  '+FloatToStr(fFrequency)+' Hz'+';  Status = '+IntToStr(wStatus));

        end;
end;

procedure TForm1.ModeChange(Sender: TObject);
var
        wBoardIndex,wChannelIndex,wMode:WORD;
begin

if Mode.ItemIndex = 0 then
    wMode := $2
else
    wMode := $12;


    for wBoardIndex :=0 to wTotalBoards - 1 do
    begin
     for wChannelIndex := 0 to 15 do
     begin
        wRtn := Ixud_SetFCChannelMode(wBoardIndex, wChannelIndex, wMode, 20); //Read Frequency Mode
     end;
    end;

end;

end.

