unit Unit1;

interface

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

Const gDataNo = 100 ;
type PBuf = Array [0.. gDataNo*3-1] of Single ;

type
  TForm1 = class(TForm)
    GroupBox3: TGroupBox;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    cbxCfgCode: TComboBox;
    cbxChannel0: TComboBox;
    GroupBox1: TGroupBox;
    imgDisplay: TImage;
    lDataNo: TLabel;
    YEnd: TEdit;
    YStart: TEdit;
    btnActive: TButton;
    btnExit: TButton;
    cbxSelectBoard: TComboBox;
    cbxCardType: TComboBox;
    Label1: TLabel;
    eSRate: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    cbxChannel1: TComboBox;
    cbxChannel2: TComboBox;
    Label4: TLabel;
    procedure cbxSelectBoardChange(Sender: TObject);

    procedure FormCreate(Sender: TObject);
    procedure btnActiveClick(Sender: TObject);
    procedure btnExitClick(Sender: TObject);
    procedure cbxCfgCodeClick(Sender: TObject);
    function  DrawWave( YStart , YEnd : Single ; Buf :PBuf ; DataNo : Integer ; im : TImage ):integer ;
    procedure ClearCanvas( Cv : TCanvas; cc:TColor );


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

var
  Form1: TForm1;

implementation
uses UniDAQ;
{$R *.DFM}
var
   wTotalBoards,wRtn,wBoardNo,wChannels: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;
   fRangeMax,fRangeMin:Single ;
   WaveData : PBuf ;
   fSamplingRate: Single;
   wChannelList:Array[0..2] of Word;
   wConfigList:Array[0..2] of Word;


procedure TForm1.cbxSelectBoardChange(Sender: TObject);
var
   wChannelIndex:Word;
begin
       sSelDevInfo := sDevInfo[cbxSelectBoard.ItemIndex];
       sSelCardInfo :=sCardInfo[cbxSelectBoard.ItemIndex];
       cbxChannel0.Items.Clear();
       cbxChannel1.Items.Clear();
       cbxChannel2.Items.Clear();
       //Get AI Channel Numbers
       if (sSelCardInfo.wAOChannels)>0 then
       begin
            for wChannelIndex:=0 to sSelCardInfo.wAIChannels -1 do
            begin
                cbxChannel0.Items.Add('Channel'+' '+IntToStr(wChannelIndex));
                cbxChannel1.Items.Add('Channel'+' '+IntToStr(wChannelIndex));
                cbxChannel2.Items.Add('Channel'+' '+IntToStr(wChannelIndex));
            end;

            cbxChannel0.ItemIndex := 0;
            cbxChannel1.ItemIndex := 1;
            cbxChannel2.ItemIndex := 2;
       end
       else
       begin
           cbxChannel0.Items.Add('None Input Port');
           cbxChannel0.Enabled := FALSE;

           cbxChannel1.Items.Add('None Input Port');
           cbxChannel1.Enabled := FALSE;

           cbxChannel2.Items.Add('None Input Port');
           cbxChannel2.Enabled := FALSE;
       end;

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);
         cbxSelectBoard.Items.Add(IntToStr(wBoardIndex)+':'+szModelName);
     end;
         
         cbxSelectBoard.ItemIndex :=0;
         cbxSelectBoardChange(Sender);
         cbxCfgCode.ItemIndex :=0;
         cbxCfgCodeClick(Sender);
         cbxCardType.ItemIndex :=0;
end;

procedure TForm1.btnActiveClick(Sender: TObject);
begin
    If btnActive.Caption = '&ACTIVE' Then
    begin
        wRtn := Ixud_ConfigAI(wBoardNo, 2, 2048, cbxCfgCode.ItemIndex, 0);
        If wRtn <> Ixud_NoErr Then
        begin
            ShowMessage('Config AI Error!!');
            Exit;
        End;
        wBoardNo:=cbxSelectBoard.ItemIndex;
        wChannelList[0]:=cbxChannel0.ItemIndex;
        wChannelList[1]:=cbxChannel1.ItemIndex;
        wChannelList[2]:=cbxChannel2.ItemIndex;

        wConfigList[0] :=cbxCfgCode.ItemIndex;
        wConfigList[1] :=cbxCfgCode.ItemIndex;
        wConfigList[2] :=cbxCfgCode.ItemIndex;

        wChannels := 3;
        btnActive.Caption   := '&STOP';
        btnExit.Enabled   := False;
        YStart.Enabled    := False;
        YEnd.Enabled      := False;


    end
    Else
    begin
        btnActive.Caption   := '&ACTIVE';
        btnExit.Enabled   := True;
        YStart.Enabled    := True;
        YEnd.Enabled      := True;
    End;
    fSamplingRate := StrToFloat(eSrate.Text);
    while(btnActive.Caption<>'&ACTIVE') do
    begin
    // Start AI Pacer Scan
     If Ixud_StartAIScan(wBoardNo,wChannels, @wChannelList, @wConfigList, fSamplingRate, gDataNo)<>Ixud_NoErr Then
     begin
        ShowMessage('AI Pacer Scan Error!' );
        btnExitClick(Sender);
        Exit;
     End;

    If(Ixud_GetAIBuffer(wBoardNo, (gDataNo *3), @WaveData[0]))<>Ixud_NoErr  then
    begin
        ShowMessage('AI Get Buffer Error!' );
        btnExitClick(Sender);
        Exit;
    End;
    Application.ProcessMessages();
    DrawWave(StrToFloat(YStart.Text),StrToFloat(YEnd.Text),WaveData, gDataNo , imgDisplay);
    end;

end;

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

procedure TForm1.cbxCfgCodeClick(Sender: TObject);
begin
          Case cbxCfgCode.ItemIndex of

            IXUD_BI_10V:
            begin
                fRangeMax := 10.0;
                fRangeMin := -10.0;
            end;
            IXUD_BI_5V:          //1  //Bipolar +/-  5V
            begin
                fRangeMax := 5.0;
                fRangeMin := -5.0;
            end;
            IXUD_BI_2V5:         //2 //Bipolar +/-  2.5V
            begin
                fRangeMax := 2.5;
                fRangeMin := -2.5;
            end;
            IXUD_BI_1V25:        //3 //Bipolar +/-  1.25V
            begin
                fRangeMax := 1.25;
                fRangeMin := -1.25;
            end;
            IXUD_BI_0V625:       //4 //Bipolar +/-  0.625V
            begin
                fRangeMax := 0.625;
                fRangeMin := -0.625;
            end;
            IXUD_BI_0V3125:       //5 //Bipolar +/-  0.3125V
            begin
                fRangeMax := 0.3125;
                fRangeMin := -0.3125;
            end;
            IXUD_BI_0V5:          //6 //Bipolar +/-  0.5V
            begin
                fRangeMax := 0.5;
                fRangeMin := -0.5;
            end;
            IXUD_BI_0V05:         //7 //Bipolar +/-  0.05V
            begin
                fRangeMax := 0.05;
                fRangeMin := -0.05;
            end;
            IXUD_BI_0V005:        //8 //Bipolar +/-  0.005V
            begin
                fRangeMax := 0.005;
                fRangeMin := -0.005;
            end;
            IXUD_BI_1V:            //9 //Bipolar +/-  1V
            begin
                fRangeMax := 1.0;
                fRangeMin := -1.0;
            end;
            IXUD_BI_0V1:           //10 //Bipolar +/-  0.1V
            begin
                fRangeMax := 0.1;
                fRangeMin := -0.1;
            end;
            IXUD_BI_0V01:          //11 //Bipolar +/-  0.01V
            begin
                fRangeMax := 0.01;
                fRangeMin := -0.01;
            end;
            IXUD_BI_0V001:          //12 //Bipolar +/-  0.001V
            begin
                fRangeMax := 0.001;
                fRangeMin := -0.001;
            end;
            IXUD_UNI_20V:          //13 //Unipolar 0 ~ 20V
            begin
                fRangeMax := 20.0;
                fRangeMin := 0.0;
            end;
            IXUD_UNI_10V:           //14 //Unipolar 0 ~ 10V
            begin
                fRangeMax := 10.0;
                fRangeMin := 0.0;
            end;
            IXUD_UNI_5V:            //15 //Unipolar 0 ~  5V
            begin
                fRangeMax := 5.0;
                fRangeMin := 0.0;
            end;

            IXUD_UNI_2V5:          //16 //Unipolar 0 ~  2.5V
            begin
                fRangeMax := 2.5;
                fRangeMin := 0.0;
            end;
            IXUD_UNI_1V25:         //17 //Unipolar 0 ~  1.25V
            begin
                fRangeMax := 1.25;
                fRangeMin := 0.0;
            end;

            IXUD_UNI_0V625:        //18 //Unipolar 0 ~  0.625V
            begin
                fRangeMax := 0.625;
                fRangeMin := 0.0;
            end;

            IXUD_UNI_1V:           //19 //Unipolar 0 ~  1V
            begin
                fRangeMax := 1.0;
                fRangeMin := 0.0;
            end;

            IXUD_UNI_0V1:          //20 //Unipolar 0 ~  0.1V
            begin
                fRangeMax := 0.1;
                fRangeMin := 0.0;
            end;
            IXUD_UNI_0V01:         //21 //Unipolar 0 ~  0.01V
            begin
                fRangeMax := 0.01;
                fRangeMin := 0.0;
            end;
            IXUD_UNI_0V001:        //22 //Unipolar 0 ~  0.001V
            begin
                fRangeMax := 0.001;
                fRangeMin := 0.0;
            end;
        end;
        YEnd.Text:= floatTostr(fRangeMax);
        //YS(0) = fRangeMax

        YStart.Text :=floatTostr(fRangeMin);
        //YS(1) = fRangeMin


end;

{*********************************************************
 Function : DrawWave
 Description : this function will compute the YStart , YEnd
               , DataNo , Image's Width , Image's Height
               to draw the appropriate wave graphicals.
 Parameter : YStart --> the Start Value (Base)
             YEnd   --> the End Value   (Max)
             Buf    --> Array [ 0 .. DataNo ] of Single
                        Store the AD value
             DataNo --> How many Data will be process
             im     --> What IMAGE control will be draw on
 Return : 0 --> No error occur
          ? --> Error
 *********************************************************}
function TForm1.DrawWave( YStart , YEnd : Single ; Buf : PBuf ; DataNo : Integer ; im : TImage ):integer;
Var
   DeltaX , DeltaY : Single ;
   px0 , py0, px1, py1, px2, py2, i : integer ;

Begin
     ClearCanvas( im.Canvas , clYellow ) ;    //Clear Canvas before checking error
     lDataNo.Caption := IntToStr( DataNo );

     if YStart >= YEnd then  //if any error occur, user will see a white window
     begin
        result := 1;         //Error : YEnd must great then YStart
        exit;
     end;

     DeltaX := im.Width / (DataNo ) ;                  //make some margin
     DeltaY := im.Height / ( YEnd - YStart ) ;

     px0 := 0;                      // close to 0 * DeltaX
     px1 := 0;
     px2 := 0;

     py0 := Round(( YEnd - Buf[0] ) * DeltaY );
     py1 := Round(( YEnd - Buf[1] ) * DeltaY );
     py2 := Round(( YEnd - Buf[2] ) * DeltaY );
     //Draw the First Channel
     With im.Canvas.Pen do
     begin
          Style := psSolid ;
          color := clRed   ;
          mode  := pmCopy  ;
     end ;

     im.Canvas.MoveTo(px0 , py0);
     for i := 1 to DataNo -1 do
     begin
          px0 := Round(i * DeltaX) ;
          py0 := Round(( YEnd - Buf[i*wChannels+0] ) * DeltaY );

          im.Canvas.LineTo( px0,py0 );
     end;
     //Draw the Second Channel
     With im.Canvas.Pen do
     begin
          Style := psSolid ;
          color := clGreen   ;
          mode  := pmCopy  ;
     end ;

     im.Canvas.MoveTo(px1 , py1);
     for i := 1 to DataNo -1 do
     begin
          px1 := Round(i * DeltaX) ;
          py1 := Round(( YEnd - Buf[i*wChannels+1] ) * DeltaY );
          im.Canvas.LineTo( px1,py1 );
     end;

     //Draw the Third Channel
     With im.Canvas.Pen do
     begin
          Style := psSolid ;
          color := clBlue   ;
          mode  := pmCopy  ;
     end ;

     im.Canvas.MoveTo(px2 , py2);
     for i := 1 to DataNo -1 do
     begin

        px2 := Round(i * DeltaX) ;
        py2 := Round(( YEnd - Buf[i*wChannels+2] ) * DeltaY );
        im.Canvas.LineTo( px2,py2 );
     end;

     result := 0;
     Application.ProcessMessages;
End;

//This function will Clear the specify Canvas.
procedure TForm1.ClearCanvas( Cv : TCanvas ; cc : TColor );
Begin
     with Cv do
     begin
          Brush.Style := bsSolid;
          Brush.Color := cc;
          FillRect(ClipRect);
     end;
end;



end.
