unit DIO;

interface

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

type
  TForm1 = class(TForm)
    btnExit: TButton;
    Label1: TLabel;
    cbxSelBoard: TComboBox;
    btnActive: TButton;
    ListBox1: TListBox;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure btnExitClick(Sender: TObject);
    procedure cbxSelBoardClick(Sender: TObject);

    procedure btnActiveClick(Sender: TObject);

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

  function MyApcFunc0(Parm:DWORD):DWORD; StdCall;
  function MyApcFunc1(Parm:DWORD):DWORD; StdCall;
  function MyApcFunc2(Parm:DWORD):DWORD; StdCall;

var
  Form1: TForm1;

implementation
uses UniDAQ;

{$R *.DFM}
var
   wTotalBoards,wRtn,wBoardNo: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;

   dwIntSource0Count : LongInt;
   dwIntSource1Count : LongInt;
   dwIntSource2Count : LongInt;
   hEvent0 : THandle;
   hEvent1 : THandle;
   hEvent2 : THandle;

//*** This thread is notified by the device driver when a interrupt occurs ***/
//Callback function 0
function MyApcFunc0(Parm:DWORD):DWORD; StdCall;
begin
        result := 0;
        inc( dwIntSource0Count);

end;

//*** This thread is notified by the device driver when a interrupt occurs ***/
//Callback function 1
function MyApcFunc1(Parm:DWORD):DWORD; StdCall;
begin
        result := 0;
        dwIntSource1Count:=dwIntSource1Count + 10;

end;

//*** This thread is notified by the device driver when a interrupt occurs ***/
//Callback function 2
function MyApcFunc2(Parm:DWORD):DWORD; StdCall;
begin
        result := 0;
        dwIntSource2Count:=dwIntSource2Count + 100;

end;

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

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;

end;

procedure TForm1.btnExitClick(Sender: TObject);
begin
 //Application.MessageBox ('Test','UniDAQ',IDOK);
 //wRtn := Ixud_DriverClose;
 Close;
end;

procedure TForm1.cbxSelBoardClick(Sender: TObject);
begin
       sSelDevInfo := sDevInfo[cbxSelBoard.ItemIndex];
       sSelCardInfo :=sCardInfo[cbxSelBoard.ItemIndex];
end;

procedure TForm1.btnActiveClick(Sender: TObject);
var
  i:integer;
begin
  wBoardNo := cbxSelBoard.ItemIndex;

  If btnActive.Caption = '&ACTIVE' Then
  begin
       btnActive.Caption := '&STOP';
       btnExit.Enabled := false;
       dwIntSource0Count := 0; //Callback function count for Int Source 0
       dwIntSource1Count := 0; //Callback function count for Int Source 1
       dwIntSource2Count := 0; //Callback function count for Int Source 2

       hEvent0 := 0;
       hEvent1 := 0;
       hEvent2 := 0;
  end
  Else
  begin
       btnActive.Caption := '&ACTIVE';
       btnExit.Enabled:= True;
  end;
  //Config the DIO Ports
  If (sSelCardInfo.wDIOPorts > 0) Then
  begin
    wRtn := Ixud_SetDIOModes32(wBoardNo, 1 );//Port 0 to Output Mode, Port 1,2 to Input Mode
    If wRtn <> NoError Then
    begin
        Application.MessageBox('*** SetDIOMode Error  ! ***', 'Error' , IDOK);
        Exit;
    End;
  end;

  //Digital Output
  wRtn:=Ixud_WriteDO(wBoardNo,0,0);//Initial the Port 0 to Value 0

  //Register Callback function, Source = 1 Event Type = Active High +Hardware Interrupt
  wRtn := Ixud_SetEventCallback(wBoardNo, IXUD_HARDWARE_INT OR IXUD_ACTIVE_HIGH, 1, hEvent0, @MyApcFunc0, 0);
  If wRtn <> NoError Then
  begin
        Application.MessageBox('SetEventCallback Error***', 'Error' , IDOK);
        Exit;
  End;

  //Register Callback function, Source = 2 Event Type = Active High&Low +Hardware Interrupt
  wRtn := Ixud_SetEventCallback(wBoardNo, IXUD_HARDWARE_INT OR IXUD_ACTIVE_LOW OR IXUD_ACTIVE_HIGH, 2,hEvent1, @MyApcFunc1, 0);
  If wRtn <> NoError Then
  begin
        Application.MessageBox('SetEventCallback Error***', 'Error' , IDOK);
        Exit;
  End;

  //Register Callback function, Source = 3 Event Type = Active Low +Hardware Interrupt
  wRtn := Ixud_SetEventCallback(wBoardNo, IXUD_HARDWARE_INT OR IXUD_ACTIVE_LOW, 3, hEvent2, @MyApcFunc2, 0);

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

  wRtn := Ixud_InstallIrq(wBoardNo, $F);
  If wRtn <> NoError Then
  begin
        Application.MessageBox('Install IRQ Error***', 'Error' , IDOK);
        Exit;
  End;

  ListBox1.Items.Add( '' );
  ListBox1.Items.Add( 'Send the HIGH and LOW to the Port-0 to generate the TRIGGER' );
  ListBox1.Items.Add( 'The users can replace the internal-trigger by external-trigger.' );
  for i:=0 to 9 do
  begin
        ListBox1.Items.Add( '' );
        Sleep(100);
        ListBox1.Items.Add( '   Output 0x07 to Port-0' );
        wRtn := Ixud_WriteDO(wBoardNo,0,$07); // Output

        Sleep(100);
        ListBox1.Items.Add( '   Output 0x00 to Port-0' );
        wRtn := Ixud_WriteDO(wBoardNo,0,$00); // Output

        Application.ProcessMessages();
        Sleep(100);
  end;

  // Revmove the Interrupt
  wRtn := Ixud_RemoveIrq(wBoardNo);
  wRtn := Ixud_RemoveEventCallback(wBoardNo, 0); //Remove Callback function


  ListBox1.Items.Add ('Event Count' + IntToStr(dwIntSource0Count) + ' ' + IntToStr(dwIntSource1Count) + ' ' + IntToStr(dwIntSource2Count));

  ListBox1.Items.Add( '' );
  btnActive.Caption := '&ACTIVE';
  btnExit.Enabled:= True;
end;


end.
