//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "UniDAQ.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CSPIN"
#pragma link "cspin"
#pragma resource "*.dfm"

Word  wTotalBoard, wInitialCode ;
Word  wChNo , CfgCode , wBoardNo;
int wRtn;
Single fRangeMax, fRangeMin;
WORD wConfig;

const int  gDataNo = 100;
float WaveData[100];

IXUD_DEVICE_INFO sDevInfo[MAX_BOARD_NUMBER];
IXUD_CARD_INFO sCardInfo[MAX_EVENT_NUMBER];

PIXUD_DEVICE_INFO sSelDevInfo;
PIXUD_CARD_INFO sSelCardInfo;

TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------


void __fastcall TForm1::FormCreate(TObject *Sender)
{

   //********************************************************************
   //* NOTICE: call Ixud_DriverInit() to initialize the driver.         *
   //* Initial the device driver, and return the board number in the PC *
   //********************************************************************

    char szModelName[20];
    WORD wBoardIndex;

     // Driver Initialize
    wInitialCode = Ixud_DriverInit(&wTotalBoard);
    btnActive->Caption = "Active";
    if ( wInitialCode != Ixud_NoErr )
    {
        ShowMessage( "Driver Initial Error!!Error Code:" + IntToStr(wInitialCode)) ;
        btnActive->Enabled = False;
        Close();
    }
    else
        btnActive->Enabled = True;

         // Get Card Information
       for (wBoardIndex = 0 ; wBoardIndex <= wTotalBoard-1 ; wBoardIndex++){

         wRtn = Ixud_GetCardInfo(wBoardIndex,&sDevInfo[wBoardIndex],&sCardInfo[wBoardIndex],&szModelName[wBoardIndex]);

        if (wRtn)
         {
            ShowMessage("Get Card Information Err!!Error Code:" + IntToStr(wRtn));
         }

        cbxBoardNo->Items->Add(IntToStr(wBoardIndex) + " " + &szModelName[wBoardIndex]);
     }

    nudActive->Text     = "0";
    tbxTotal->Text     = IntToStr(wTotalBoard);
    nudActive->MaxValue = wTotalBoard - 1;
    nudActive->MinValue = 0;
    if (wTotalBoard < 2)
        nudActive->Enabled = False;

    cbCardType->ItemIndex = 0; //'Card Type default is 0(0:L  1:H)
    cbxBoardNo->ItemIndex = 0; //'Board select default is 0
    cbSingDiff->ItemIndex = 0; //'Jp1 default value is 0(0:Singel-Ended 1:Differential)
    cbSingDiffChange( Sender );
    lDataNo->Caption=StrToInt(gDataNo); // 'AD Channel default is Channel 0
    InRange->ItemIndex = 0;  //'AI Range is 0
    YStart->Text = "-10";
    YEnd->Text = "10" ;

    ClearCanvas( Image1->Canvas , clYellow ) ;

}
//---------------------------------------------------------------------------

void __fastcall TForm1::cbSingDiffChange(TObject *Sender)
{

 int i , ttl;

     cbChNo->Clear();

     // Single-End 32 Channels, Differential 16 Channels
     ttl = (2 - cbSingDiff->ItemIndex) * 16 ;

     for (i=0; i <= ttl -1; i++)
         cbChNo->Items->Add( IntToStr(i) );

     cbChNo->ItemIndex = 0;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::btnExitClick(TObject *Sender)
{
 // Driver Close
   Ixud_DriverClose();
   Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::InRangeChange(TObject *Sender)
{

     switch (StrToInt(InRange->ItemIndex))
     {
            case IXUD_BI_10V:      // '0 Bipolar +/-  10V
                 fRangeMax = 10.0 ;
                 fRangeMin = -10.0;
                 break;

            case IXUD_BI_5V:      // 1 Bipolar +/-  5V
                 fRangeMax = 5.0;
                 fRangeMin = -5.0;
                 break;

            case IXUD_BI_2V5:    // '2 Bipolar +/-  2.5V
                 fRangeMax = 2.5;
                 fRangeMin = -2.5;
                 break;

            case IXUD_BI_1V25:    // '3 Bipolar +/-  1.25V
                 fRangeMax = 1.25;
                 fRangeMin = -1.25;
                 break;

            case IXUD_BI_0V625:   // '4 Bipolar +/-  0.625V
                 fRangeMax = 0.625;
                 fRangeMin = -0.625;
                  break;

            case IXUD_BI_0V3125:  // '5 Bipolar +/-  0.3125V
                 fRangeMax = 0.3125 ;
                 fRangeMin = -0.3125 ;
                  break;

            case IXUD_BI_0V5:    //'6 Bipolar +/-  0.5V
                 fRangeMax = 0.5 ;
                 fRangeMin = -0.5 ;
                 break;

            case IXUD_BI_0V05:   // '7 Bipolar +/-  0.05V
                 fRangeMax = 0.05  ;
                 fRangeMin = -0.05 ;
                 break;

            case IXUD_BI_0V005:  // '8 Bipolar +/-  0.005V
                 fRangeMax = 0.005;
                 fRangeMin = -0.005;
                 break;

            case IXUD_BI_1V:    // '9 Bipolar +/-  1V
                 fRangeMax = 1.0  ;
                 fRangeMin = -1.0 ;
                 break;

            case IXUD_BI_0V1:     // '10 Bipolar +/-  0.1V
                 fRangeMax = 0.1 ;
                 fRangeMin = -0.1;
                 break;

            case IXUD_BI_0V01:   // '11 Bipolar +/-  0.01V
                 fRangeMax = 0.01;
                 fRangeMin = -0.01 ;
                 break;

            case IXUD_BI_0V001:  // '12 Bipolar +/-  0.001V
                 fRangeMax = 0.001 ;
                 fRangeMin = -0.001 ;
                 break;

            case IXUD_UNI_20V:   // '13 Unipolar 0 ~ 20V
                 fRangeMax = 20.0 ;
                 fRangeMin = 0.0;
                 break;

            case IXUD_UNI_10V:   // '14 Unipolar 0 ~ 10V
                 fRangeMax = 10.0 ;
                 fRangeMin = 0.0 ;
                 break;

            case IXUD_UNI_5V:    // '15 Unipolar 0 ~  5V
                 fRangeMax = 5.0 ;
                 fRangeMin = 0.0 ;
                 break;

            case IXUD_UNI_2V5:   // '16 Unipolar 0 ~  2.5V
                 fRangeMax = 2.5  ;
                 fRangeMin = 0.0 ;
                 break;

            case IXUD_UNI_1V25:  // '17 Unipolar 0 ~  1.25V
                 fRangeMax = 1.25  ;
                 fRangeMin = 0.0 ;
                  break;

            case IXUD_UNI_0V625:   // '18 Unipolar 0 ~  0.625V
                 fRangeMax = 0.625 ;
                 fRangeMin = 0.0 ;
                 break;

            case IXUD_UNI_1V:      // '19 Unipolar 0 ~  1V
                 fRangeMax = 1.0 ;
                 fRangeMin = 0.0;
                  break;

            case IXUD_UNI_0V1:   //'20 Unipolar 0 ~  0.1V
                 fRangeMax = 0.1;
                 fRangeMin = 0.0;
                 break;

            case IXUD_UNI_0V01:  //'21 Unipolar 0 ~  0.01V
                 fRangeMax = 0.01;
                 fRangeMin = 0.0;
                 break;

            case IXUD_UNI_0V001: //'22 Unipolar 0 ~  0.001V
                 fRangeMax = 0.001;
                 fRangeMin = 0.0;
                 break;
     }
         YEnd->Text = FormatFloat("0.000#",fRangeMax);

         YStart->Text = FormatFloat("0.000#",fRangeMin);

}
//---------------------------------------------------------------------------

void __fastcall TForm1::btnActiveClick(TObject *Sender)
{
   wBoardNo = cbxBoardNo->ItemIndex;

   if ((StrToInt(nudActive->Text) > StrToInt(tbxTotal->Text) -1)
      || ( StrToInt(nudActive->Text) < 0 ))
     {
        ShowMessage("Invalid board number, Pls retry!!");
        return ;
     };

     wRtn = Ixud_ConfigAI(wBoardNo, 2, 2048, cbCardType->ItemIndex, 0);
     if (wRtn)
     {
        ShowMessage("AI Config Error!!Error Code:" + IntToStr(wRtn));
      }
    if (btnActive->Caption == "Active")
    {
        btnActive->Caption   = "Stop";
        btnExit->Enabled   = False;
        Timer1->Enabled    = True;
    }
    else
   {
        btnActive->Caption   = "Active";
        btnExit->Enabled   = True;
        Timer1->Enabled    = False;
    };


}
//---------------------------------------------------------------------------

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{

   unsigned long iVal;
   float fVal;

    wBoardNo = cbxBoardNo->ItemIndex;
    wChNo  = cbChNo->ItemIndex;
    wConfig = InRange->ItemIndex ;

    Ixud_ReadAIH(wBoardNo, wChNo, wConfig, &iVal);
    tbxADH->Text = IntToHex(__int64(iVal),3);

    Ixud_ReadAI(wBoardNo, wChNo, wConfig, &fVal);
    tbxADF->Text = FormatFloat("###,###.000",fVal);

   // 'Run A/D Polling
   if ( Ixud_PollingAI( wBoardNo, wChNo, wConfig, gDataNo, WaveData) != Ixud_NoErr )
   {
        ShowMessage("AD Polling Error!");
        btnActiveClick(Sender);
        return;
   };


  DrawWave(StrToFloat(YStart->Text),StrToFloat(YEnd->Text),WaveData, gDataNo , Image1);

}
//---------------------------------------------------------------------------

//This function will Clear the specify Canvas.
void __fastcall TForm1::ClearCanvas(TCanvas* Cv, TColor cc)
{
     ((TCanvas*)Cv)->Brush->Style = bsSolid;
     ((TCanvas*)Cv)->Brush->Color = cc;
     ((TCanvas*)Cv)->FillRect(((TCanvas*)Cv)->ClipRect);

}
//---------------------------------------------------------------------------

//* -------------------------------------------------------------*//
//* 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                                         *//
//*--------------------------------------------------------------*//

int  __fastcall TForm1::DrawWave(float YStart ,float YEnd, float* Buf, int DN, TImage* im)
{

   float DeltaX , DeltaY ;
   int   px , py , i     ;

   ClearCanvas( im->Canvas , clYellow ) ;   //'Clear Canvas before checking error
   lDataNo->Caption = IntToStr( DN );
   Application->ProcessMessages();

     if ( YStart >= YEnd )   //'if any error occur, user will see a white window
     {
        return 1;         //'Error : YEnd must great then YStart
     };

     DeltaX = im->Width / (float) DN ;                  //'make some margin
     DeltaY = im->Height / (float)( YEnd - YStart );
     im->Canvas->Pen->Style = psSolid ;
     im->Canvas->Pen->Color = clRed   ;
     im->Canvas->Pen->Mode  = pmCopy  ;

     px = 0;              // 'close to 0 * DeltaX
     py = (long)(( YEnd - Buf[0] ) * DeltaY) ;
     im->Canvas->MoveTo(px , py);
     for (i=1;i<DN;i++)
     {
          px = (long)(i * DeltaX) ;
          py = (long)(( YEnd - Buf[i] ) * DeltaY );
          im->Canvas->LineTo( px , py );
     };

     return 0;
}
//---------------------------------------------------------------------------



