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;
    cbxOutputPort: TComboBox;
    cbxInputPort: TComboBox;
    Label2: TLabel;
    Label4: TLabel;
    btnActive: TButton;
    Timer1: TTimer;
    GroupDO: TGroupBox;
    cbxDO0: TCheckBox;
    lblDO0: TLabel;
    cbxDO1: TCheckBox;
    cbxDO2: TCheckBox;
    cbxDO3: TCheckBox;
    cbxDO4: TCheckBox;
    cbxDO5: TCheckBox;
    cbxDO6: TCheckBox;
    cbxDO7: TCheckBox;
    cbxDO8: TCheckBox;
    cbxDO9: TCheckBox;
    cbxDO10: TCheckBox;
    cbxDO11: TCheckBox;
    cbxDO12: TCheckBox;
    cbxDO13: TCheckBox;
    cbxDO14: TCheckBox;
    cbxDO15: TCheckBox;
    GroupDI: TGroupBox;
    speDI0: TShape;
    speDI1: TShape;
    speDI3: TShape;
    speDI4: TShape;
    speDI5: TShape;
    speDI6: TShape;
    speDI7: TShape;
    speDI8: TShape;
    speDI9: TShape;
    speDI10: TShape;
    speDI11: TShape;
    speDI12: TShape;
    speDI13: TShape;
    speDI14: TShape;
    speDI15: TShape;
    speDI2: TShape;
    cbxDO16: TCheckBox;
    cbxDO17: TCheckBox;
    cbxDO18: TCheckBox;
    cbxDO19: TCheckBox;
    cbxDO20: TCheckBox;
    cbxDO21: TCheckBox;
    cbxDO22: TCheckBox;
    cbxDO23: TCheckBox;
    cbxDO24: TCheckBox;
    cbxDO25: TCheckBox;
    cbxDO26: TCheckBox;
    cbxDO27: TCheckBox;
    cbxDO28: TCheckBox;
    cbxDO29: TCheckBox;
    cbxDO30: TCheckBox;
    cbxDO31: TCheckBox;
    speDI16: TShape;
    speDI17: TShape;
    speDI18: TShape;
    speDI19: TShape;
    speDI20: TShape;
    speDI21: TShape;
    speDI22: TShape;
    speDI23: TShape;
    speDI24: TShape;
    speDI25: TShape;
    speDI26: TShape;
    speDI27: TShape;
    speDI28: TShape;
    speDI29: TShape;
    speDI30: TShape;
    speDI31: TShape;
    lblDO1: TLabel;
    lblDO2: TLabel;
    lblDO3: TLabel;
    lblDO4: TLabel;
    lblDO8: TLabel;
    lblDO9: TLabel;
    lblDO10: TLabel;
    lblDO11: TLabel;
    lblDO16: TLabel;
    lblDO17: TLabel;
    lblDO18: TLabel;
    lblDO19: TLabel;
    lblDO24: TLabel;
    lblDO25: TLabel;
    lblDO26: TLabel;
    lblDO27: TLabel;
    lblDO5: TLabel;
    lblDO6: TLabel;
    lblDO12: TLabel;
    lblDO13: TLabel;
    lblDO20: TLabel;
    lblDO21: TLabel;
    lblDO28: TLabel;
    lblDO29: TLabel;
    lblDO7: TLabel;
    lblDO14: TLabel;
    lblDO15: TLabel;
    lblDO22: TLabel;
    lblDO23: TLabel;
    lblDO30: TLabel;
    lblDO31: TLabel;
    lblDI0: TLabel;
    lblDI1: TLabel;
    lblDI2: TLabel;
    lblDI3: TLabel;
    lblDI4: TLabel;
    lblDI5: TLabel;
    lblDI6: TLabel;
    lblDI7: TLabel;
    lblDI8: TLabel;
    lblDI9: TLabel;
    lblDI10: TLabel;
    lblDI11: TLabel;
    lblDI12: TLabel;
    lblDI13: TLabel;
    lblDI14: TLabel;
    lblDI15: TLabel;
    lblDI16: TLabel;
    lblDI17: TLabel;
    lblDI18: TLabel;
    lblDI19: TLabel;
    lblDI20: TLabel;
    lblDI21: TLabel;
    lblDI22: TLabel;
    lblDI23: TLabel;
    lblDI24: TLabel;
    lblDI25: TLabel;
    lblDI26: TLabel;
    lblDI27: TLabel;
    lblDI28: TLabel;
    lblDI29: TLabel;
    lblDI30: TLabel;
    lblDI31: TLabel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure btnExitClick(Sender: TObject);
    procedure cbxSelBoardClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure btnActiveClick(Sender: TObject);
    procedure cbxDOClick(Sender: TObject);
    procedure cbxOutputPortChange(Sender: TObject);
    procedure cbxInputPortChange(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
uses UniDAQ;

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

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
 Close;
end;

procedure TForm1.cbxSelBoardClick(Sender: TObject);
var
   wPortIndex:Word;
   i:Word;
begin
       sSelDevInfo := sDevInfo[cbxSelBoard.ItemIndex];
       sSelCardInfo :=sCardInfo[cbxSelBoard.ItemIndex];
       wPortChNum := sSelCardInfo.wDIOPortWidth;
       btnActive.Enabled := True;
       cbxOutputPort.Items.Clear();
       cbxOutputPort.Text := '';
       cbxInputPort.Items.Clear();
       cbxInputPort.Text := '';
       cbxOutputPort.Enabled := False;
       cbxInputPort.Enabled := False;

       for i:=0 to 31 do
       begin
        (FindComponent('cbxDO' + IntToStr(i)) As TCheckBox).Visible := False;
        (FindComponent('lblDO' + IntToStr(i)) As TLabel).Visible := False;
        (FindComponent('speDI' + IntToStr(i)) As TShape).Visible := False;
        (FindComponent('lblDI' + IntToStr(i)) As TLabel).Visible := False;
       end;

       //Get DI,DO and DIO Port Numbers
       if (sSelCardInfo.wDIOPorts+sSelCardInfo.wDOPorts)>0 then
       begin
            for wPortIndex:=0 to (sSelCardInfo.wDIOPorts+sSelCardInfo.wDOPorts -1) do
            begin
                cbxOutputPort.Items.Add('Port'+' '+IntToStr(wPortIndex));
            end;

            cbxOutputPort.Enabled := True;
            cbxOutputPort.ItemIndex := 0;

            for i:=0 to 31 do
            begin
                if i < wPortChNum then
                begin
                 (FindComponent('cbxDO' + IntToStr(i)) As TCheckBox).Visible := True;
                 (FindComponent('lblDO' + IntToStr(i)) As TLabel).Visible := True;
                end;
            end;
       end;

       if (sSelCardInfo.wDIOPorts+sSelCardInfo.wDIPorts)>0 then
       begin
            for wPortIndex:=0 to (sSelCardInfo.wDIOPorts+sSelCardInfo.wDIPorts -1) do
            begin
                cbxInputPort.Items.Add('Port'+' '+IntToStr(wPortIndex));
            end;

            cbxInputPort.Enabled := True;
            cbxInputPort.ItemIndex := 0;

            for i:=0 to 31 do
            begin
                if i < wPortChNum then
                begin
                 (FindComponent('speDI' + IntToStr(i)) As TShape).Visible := True;
                 (FindComponent('lblDI' + IntToStr(i)) As TLabel).Visible := True;
                end;
            end;
       end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
Var
   i       : integer;
   dwDIVal : LongInt;

begin
    //Digital Input
    wRtn := Ixud_ReadDI(WORD(cbxSelBoard.ItemIndex),WORD(cbxInputPort.ItemIndex),dwDIVal);

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

    For i:= 0 To wPortChNum - 1 do
    begin
      //  if ( dwDIVal and (1 shl i)) <> 0 then
      //      (FindComponent('speDI' + IntToHex(i,2)) As TShape).Brush.Color := clRed
      //  else
       //     (FindComponent('speDI' + IntToHex(i,2)) As TShape).Brush.Color := clBlack;
         if ( dwDIVal and (1 shl i)) <> 0 then
            (FindComponent('speDI' + IntToStr(i)) As TShape).Brush.Color := clRed
        else
            (FindComponent('speDI' + IntToStr(i)) As TShape).Brush.Color := clBlack;



    End;
end;



procedure TForm1.btnActiveClick(Sender: TObject);

begin
  wBoardNo := cbxSelBoard.ItemIndex;
  wOutputPortNo := cbxOutputPort.ItemIndex;
  wInputPortNo := cbxInputPort.ItemIndex;


  If btnActive.Caption = '&ACTIVE' Then
  begin
       btnActive.Caption := '&STOP';
       btnExit.Enabled := false;

       if sSelCardInfo.wDIOPorts+sSelCardInfo.wDIPorts > 0 then
       begin
        Timer1.Enabled := True;
       end;

       if sSelCardInfo.wDIOPorts+sSelCardInfo.wDOPorts > 0 then
       begin
         GroupDO.Enabled := True;
       end;
  end
  Else
  begin
       btnActive.Caption := '&ACTIVE';
       Timer1.Enabled := False;
       btnExit.Enabled:= True;
       GroupDO.Enabled := False;
  end;
//Config the DIO Ports
If (sSelCardInfo.wDIOPorts > 1) Then
    wRtn := Ixud_SetDIOModes32(wBoardNo, 1 shl wOutputPortNo);

end;

procedure TForm1.cbxDOClick(Sender: TObject);

var
   i : integer;
begin

   dwDOValue :=0;

   for i := 0 to wPortChNum - 1 do
        if (FindComponent( 'cbxDO' + IntToStr(i) ) As TCheckBox).Checked then
             dwDOValue := dwDOValue + Round(Power(2, i));
   //Digital Output
   wRtn:=Ixud_WriteDO(WORD(cbxSelBoard.ItemIndex),WORD(cbxOutputPort.ItemIndex),dwDOValue);

end;

procedure TForm1.cbxOutputPortChange(Sender: TObject);
begin
  wOutputPortNo := cbxOutputPort.ItemIndex;
end;

procedure TForm1.cbxInputPortChange(Sender: TObject);
begin
  wInputPortNo := cbxInputPort.ItemIndex;
end;

end.
