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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include <math.h>
#include "UniDAQ.h"

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

Word wTotalBoard, wInitialCode ;
int wInputPortNo,wOutputPortNo;
Word wBoardNo;
DWord dwSet;
int wRtn;
int wPortChNum;

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

    if ( wInitialCode != Ixud_NoErr )
    {
      ShowMessage( "Driver Initial Err!!Error Code:" + IntToStr(wInitialCode)) ;
    }

        // 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]);
       }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnEndClick(TObject *Sender)
{
   // Driver Close
   Ixud_DriverClose();
   Close();
}
//---------------------------------------------------------------------------

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

    wBoardNo = StrToInt(cbxBoardNo->ItemIndex);
    wOutputPortNo = StrToInt(cbxOutputPort->ItemIndex);
    wInputPortNo = StrToInt(cbxInputPort->ItemIndex);

    if (btnActive->Caption == "Active")
    {
        btnActive->Caption   = "Stop";
        btnEnd->Enabled   = False;
      //  CheckBox1Click( CheckBox1 );

        if ((sSelCardInfo->wDIOPorts + sSelCardInfo->wDIPorts)> 0)
                Timer1->Enabled = True;

        if((sSelCardInfo->wDIOPorts + sSelCardInfo->wDOPorts)>0)
                GroupBox1->Enabled = True;

    }else{
        btnActive->Caption   = "Active";
        btnEnd->Enabled   = True;
        Timer1->Enabled    = False;
        GroupBox1->Enabled = False;
    };

    if(sSelCardInfo->wDIOPorts > 1)
        wRtn=Ixud_SetDIOModes32(wBoardNo,2^wOutputPortNo);

}

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

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
   int   i;
   WORD  wRetVal;
   DWORD dwDIVal;


   // Read Digital Input port Val
   wRetVal = Ixud_ReadDI(wBoardNo, wInputPortNo, &dwDIVal);

   if ( wRetVal != Ixud_NoErr )
    {
       ShowMessage(" Read DI Err!!Error Code:" + IntToStr(wRetVal));
       return ;
   };



   for (i=0;i<sSelCardInfo->wDIOPortWidth;i++) {
        if (( dwDIVal & (1 << i)) != 0)
            ((TShape*)FindComponent("PSp" + IntToStr(i)))->Brush->Color = clRed;
        else
            ((TShape*)FindComponent("PSp" + IntToStr(i)))->Brush->Color = clBlack;
   }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::CheckBox1Click(TObject *Sender)
{
    Word i;

    dwSet = 0;

    for (i=0;i<wPortChNum;i++)
    {
         if (((TCheckBox *)FindComponent( "CheckBox" + IntToStr(i+1) ))->Checked)
         {
             dwSet = (DWORD)(dwSet + (DWord)pow(2, i));
         }
    }

    //if ( ((TCheckBox*)Sender)->Checked )
    //   wSet = wSet | (Word)(1 << ((TCheckBox*)Sender)->Tag);
    //else
    //   wSet = wSet & (Word)( ~(1 << ((TCheckBox*)Sender)->Tag));

    // Write Digital Output port Val
    wRtn = Ixud_WriteDO(wBoardNo,wOutputPortNo,dwSet);

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

void __fastcall TForm1::cbxBoardNoChange(TObject *Sender)
{
      int PortIndex;
      int wDO,wDI;
      int i;

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

        wPortChNum = sSelCardInfo->wDIOPortWidth ;
        wDO = sSelCardInfo->wDIOPorts + sSelCardInfo->wDOPorts;
        wDI = sSelCardInfo->wDIOPorts + sSelCardInfo->wDIPorts;

        btnActive->Enabled = true;
        cbxOutputPort->Items->Clear();
        cbxOutputPort->Text = " ";
        cbxInputPort->Items->Clear();
        cbxInputPort->Text = " ";
        cbxOutputPort->Enabled = false;
        cbxInputPort->Enabled = false;

         for(i=0;i<32;i++)
         {
          ((TShape*)FindComponent("CheckBox" + IntToStr(i+1)))->Visible = False;
          ((TShape*)FindComponent("lblOut" + IntToStr(i+1)))->Visible = False;
          ((TShape*)FindComponent("Psp" + IntToStr(i)))->Visible = False;
          ((TShape*)FindComponent("lblIn" + IntToStr(i)))->Visible = False;
          ((TShape*)FindComponent("Shape" + IntToStr(i)))->Visible = False;
         }

        if (wDO>0)
        {
        //Total DO port
        for (PortIndex = 0 ; PortIndex <= wDO-1 ; PortIndex++)
         {
           cbxOutputPort->Items->Add("Port" + IntToStr(PortIndex));
         }

        cbxOutputPort->Enabled = true;
        cbxOutputPort->ItemIndex = 0;

        for(i=0;i<32;i++)
         {
          if(i<wPortChNum)
          {
           ((TShape*)FindComponent("CheckBox" + IntToStr(i+1)))->Visible = True;
           ((TShape*)FindComponent("lblOut" + IntToStr(i+1)))->Visible = True;
          }
         }
        }

        if(wDI>0)
        {
        //Total DI port
        for (PortIndex = 0 ; PortIndex <= wDI-1 ; PortIndex++)
         {
            cbxInputPort->Items->Add("Port" + IntToStr(PortIndex));
         }

        cbxInputPort->Enabled = true;
        cbxInputPort->ItemIndex = 0;

         for(i=0;i<32;i++)
         {
          if(i<wPortChNum)
          {
           ((TShape*)FindComponent("Psp" + IntToStr(i)))->Visible = True;
           ((TShape*)FindComponent("lblIn" + IntToStr(i)))->Visible = True;
           ((TShape*)FindComponent("Shape" + IntToStr(i)))->Visible = True;
          }
         }
        }

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

void __fastcall TForm1::cbxOutputPortChange(TObject *Sender)
{
    wOutputPortNo = StrToInt(cbxOutputPort->ItemIndex);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::cbxInputPortChange(TObject *Sender)
{
    wInputPortNo = StrToInt(cbxInputPort->ItemIndex);
}
//---------------------------------------------------------------------------

