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

#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;

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]);
     }

    cbxBoardNo->ItemIndex=0;
    Mode->ItemIndex=2;
    cbxBoardNoChange(this);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnActiveClick(TObject *Sender)
{
int wCounterCh , wCounterMode;
unsigned long wCounterValue;
int i,wRn;

    ListBox1->Items->Clear();

    wCounterValue = StrToInt(Value->Text);
    wCounterCh = StrToInt(Channel->ItemIndex);
    wCounterMode = StrToInt(Mode->ItemIndex);

    //set counter
    Ixud_SetCounter( wBoardNo, wCounterCh, wCounterMode, wCounterValue);

    //read counter
    for (i = 0 ;i<100;i++)
    {
        wRn=Ixud_ReadCounter (wBoardNo, wCounterCh, &wCounterValue);
        ListBox1->Items->Add(IntToStr(wCounterValue));
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::cbxBoardNoChange(TObject *Sender)
{
int i;

     Form1->Caption= cbxBoardNo->Items->Strings[cbxBoardNo->ItemIndex]+"Timer Counter";
     Channel->Items->Clear();
     Channel->Text="";

     wBoardNo = StrToInt(cbxBoardNo->ItemIndex);

     //whether have the timer counter
    if (((&sCardInfo[wBoardNo])->wCounterChannels) == 0)
    {   ShowMessage ("Not support timer counter");
        btnActive->Enabled=false;
    }
    else
    {
        for (i=0;i<(((&sCardInfo[wBoardNo])->wCounterChannels));i++)
           Channel->Items->Add(IntToStr(i));

        Channel->ItemIndex = 0;
        btnActive->Enabled = true;
    }
}
//---------------------------------------------------------------------------


