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

#include <vcl.h>
#pragma hdrstop

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

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

Word  wTotalBoard, wInitialCode ;
Word  wChNo , CfgCode , wBoardNo;
int wRtn;

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::btnExitClick(TObject *Sender)
{
 // Driver Close
   Ixud_DriverClose();
   Close();
}
//---------------------------------------------------------------------------
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;

    cbxBoardNo->ItemIndex=0;
    cbxBoardNoChange(Sender);


}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnActiveClick(TObject *Sender)
{
      wBoardNo = StrToInt(cbxBoardNo->ItemIndex);
      wChNo = StrToInt(cbxChannel->ItemIndex );
      CfgCode = StrToInt(cbxOutput->ItemIndex );


    if (cbVoltCur->ItemIndex == -1)
    { ShowMessage("Voltage/Current Error!!");
        return ;
    };

     if (cbxOutput->ItemIndex == -1)
    { ShowMessage("Output Rang Error!!");
        return ;
    };


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

     if (cbVoltCur->ItemIndex == 1 )  //'Current 16,17
     {
       CfgCode = StrToInt(cbxOutput->ItemIndex )+16;
     }

     wRtn = Ixud_ConfigAO(wBoardNo,wChNo, CfgCode);
     if (wRtn)
     {
        ShowMessage("AO Config Error!!Error Code:" + IntToStr(wRtn));
      }

   if (btnActive->Caption == "Active")
    {
        btnActive->Caption   = "Stop";
        btnExit->Enabled   = False;
        tkbAO->Enabled    = True;
        tkbAOChange(Sender);
    }
    else
   {
        btnActive->Caption   = "Active";
        btnExit->Enabled   = True;
        tkbAO->Enabled    = False;
    };

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

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

  int AOIndex;

        wBoardNo = StrToInt(cbxBoardNo->ItemIndex);
        sSelCardInfo = &sCardInfo[wBoardNo];

        for (AOIndex = 0 ; AOIndex <= sSelCardInfo->wAOChannels-1 ; AOIndex++)
        {
            cbxChannel->Items->Add(StrToInt(AOIndex));
         };

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


void __fastcall TForm1::tkbAOChange(TObject *Sender)
{
   float fValue;
   WORD RetValue;

    wBoardNo = StrToInt(cbxBoardNo->ItemIndex);
    wChNo = StrToInt(cbxChannel->ItemIndex );

   if (cbVoltCur->ItemIndex==0)//'AO Config for Voltage
   {
    switch (StrToInt(cbxOutput->ItemIndex))
     {
      case IXUD_AO_UNI_5V:  // 0  ~  5V
        fValue = (float)((tkbAO->Position /(float)4096) * 5);
        RetValue=Ixud_WriteAOVoltage(wBoardNo, wChNo , fValue); // Output V
        if (RetValue){
           ShowMessage("AO Voltage Error!!Error Code:" + IntToStr(RetValue));
           return ;
       }
        tbxAO->Text =FormatFloat("#0.####",fValue);
      break;

      case IXUD_AO_BI_5V:   // +/-   5V
        fValue = (float)(((tkbAO->Position  - 2048)/(float)2048)*5);
        RetValue = Ixud_WriteAOVoltage(wBoardNo, wChNo, fValue); // Output V
        if (RetValue){
           ShowMessage("AO Voltage Error!!Error Code:" + IntToStr(RetValue));
           return ;
        }
        tbxAO->Text =FormatFloat("#0.####",fValue);
      break;

     case IXUD_AO_UNI_10V:   // 0  ~ 10V
        fValue =(float)((tkbAO->Position /(float)4096) * 10) ;
        RetValue = Ixud_WriteAOVoltage(wBoardNo, wChNo, fValue); // Output V
        if (RetValue){
           ShowMessage("AO Voltage Error!!Error Code:" + IntToStr(RetValue));
           return ;
        }
       tbxAO->Text =FormatFloat("#0.####",fValue);
      break;

      case IXUD_AO_BI_10V:   // +/-  10V
        fValue =(float)(((tkbAO->Position  - 2048)/(float)2048)*10) ;
        RetValue = Ixud_WriteAOVoltage(wBoardNo, wChNo, fValue); // Output V
        if (RetValue){
           ShowMessage("AO Voltage Error!!Error Code:" + IntToStr(RetValue));
           return ;
         }
        tbxAO->Text =FormatFloat("#0.####",fValue);
      break;

      case IXUD_AO_UNI_20V:  // 0  ~ 20V
        fValue = (float)((tkbAO->Position /(float)4096) * 20) ;
        RetValue=Ixud_WriteAOVoltage(wBoardNo, wChNo, fValue); // Output V
        if (RetValue){
            ShowMessage("AO Voltage Error!!Error Code:" + IntToStr(RetValue));
            return ;
         }
        tbxAO->Text =FormatFloat("#0.####",fValue);
      break;

      case IXUD_AO_BI_20V:    // +/-  20V
        fValue =(float) (((tkbAO->Position  - 2048)/(float)2048)*20)  ;
        RetValue = Ixud_WriteAOVoltage(wBoardNo, wChNo, fValue); // Output V
        if (RetValue){
            ShowMessage("AO Voltage Error!!Error Code:" + IntToStr(RetValue));
            return ;
         }
        tbxAO->Text =FormatFloat("#0.####",fValue);
      break;
      }
     }


     if ((cbVoltCur->ItemIndex==1))// 'AO Config for Current
     {
       switch (StrToInt(cbxOutput->ItemIndex)+16)
     {
      case IXUD_AO_I_0_20_MA:   // 0 ~ 20mA
        fValue = (float)((tkbAO->Position /(float)4096) * 20);
        RetValue = Ixud_WriteAOCurrent(wBoardNo, wChNo, fValue); // Output Current
        if (RetValue){
            ShowMessage("AO Current Error!!Error Code:" + StrToInt(RetValue));
            return ;
        }
        tbxAO->Text =FormatFloat("#0.####",fValue);
        Label2->Caption="mA";
      break;

      case IXUD_AO_I_4_20_MA:    // 4 ~ 20mA
        fValue = (float) (((tkbAO->Position+1024 )/(float)5120)*20)  ;
        RetValue = Ixud_WriteAOCurrent(wBoardNo, wChNo, fValue); // Output current
        if (RetValue){
            ShowMessage("AO Current Error!!Error Code:" + IntToStr(RetValue));
            return ;
        }
        tbxAO->Text =FormatFloat("#0.####",fValue);
        Label2->Caption="mA";
      break;
     }
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::cbVoltCurChange(TObject *Sender)
{
     switch (StrToInt(cbVoltCur->ItemIndex))
     {
      case 0:
       cbxOutput->Items->Clear() ;
      cbxOutput->Items->Add("00: 0  ~ 5V");
      cbxOutput->Items->Add("01: +/-   5V");
      cbxOutput->Items->Add("02: 0  ~ 10V");
      cbxOutput->Items->Add("03: +/-   10V") ;
      cbxOutput->Items->Add("04: 0  ~ 20V");
      cbxOutput->Items->Add("05: +/-   20V");
      break;

      case 1:
      cbxOutput->Items->Clear() ;
      cbxOutput->Items->Add("00: 0  ~ 20 mA");
      cbxOutput->Items->Add("01: 4  ~ 20 mA");
      break;
     }
}
//---------------------------------------------------------------------------

