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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "cm100.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
static BYTE SelectedBoardNo;
static bool IsActiveFlag=false;
static unsigned char Handle_No[5];
static BYTE CyclicTxMsgCnt = 0;

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

void __fastcall TForm1::Button_ActiveClick(TObject *Sender)
{
  BYTE BoardNo,Baud;
  int Ret;
  WORD BaudMap[]={10,20,50,125,250,500,800,1000};
  AnsiString TitleStr;
  ConfigStruct Para;

  BoardNo=ComboBox_BoardNo->ItemIndex;
  Baud=ComboBox_BaudRate->ItemIndex;
  if (IsActiveFlag)
  { //If board is active before, close it first.
    CM100_CloseBoard(SelectedBoardNo);
    IsActiveFlag=false;
  }

  Ret=CM100_ActiveBoard(BoardNo); //Active board
  if (Ret)
  {
    TitleStr.printf("Active Board Error!(%d)",Ret);
    ShowMessage(TitleStr);
    Form1->Caption = "TransmitMsgCyclically --- No board is active";
    return;
  }

  Ret = CM100_Init(BoardNo,1);  //Init board
  if(Ret)
  {
     ShowMessage("Init Board Error!");
     CM100_CloseBoard(BoardNo);
     Form1->Caption = "TransmitMsgCyclically --- No board is active";
     return;
  }

	Para.AccMask[0]=0xff; //Set AccMask to 0xffffffff to pass all CAN messages from CAN bus
	Para.AccMask[1]=0xff;
	Para.AccMask[2]=0xff;
	Para.AccMask[3]=0xff;
	Para.AccCode[0]=0x00; //AccCode is useless when AccMask is 0xffffffff
	Para.AccCode[1]=0x00;
	Para.AccCode[2]=0x00;
	Para.AccCode[3]=0x00;
	Para.BaudRate=Baud+1; //Baud=0 for user-defined baud, 1:10K, 2:20K, 3:50K, 4:125K, 5:250K, 6:500K, 7:800K, 8:1M bps
	Para.BT0=0; //BT0 and BT1 are only useful when user-defined baud is used.
	Para.BT1=0;
	Ret = CM100_Config(BoardNo,1,&Para); //Configure board parameters
	if(Ret)
        {
	   ShowMessage("Configure Board Error!");
  	   CM100_CloseBoard(BoardNo);
	   Form1->Caption = "TransmitMsgCyclically --- No board is active";
	   return;
	}
	SelectedBoardNo=BoardNo;
	TitleStr.printf("TransmitMsgCyclically --- Board %d is active with baud %d K pbs",BoardNo,BaudMap[Baud]);
	Form1->Caption = TitleStr;
	IsActiveFlag=true;	
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button_ExitClick(TObject *Sender)
{
   if (IsActiveFlag) CM100_CloseBoard(SelectedBoardNo);
   Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
   if (IsActiveFlag) CM100_CloseBoard(SelectedBoardNo);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox_DataLenChange(TObject *Sender)
{
  int i,DataLen;
  AnsiString Str;
  DataLen = ComboBox_DataLen->ItemIndex;
  for(i = 0;i < 8;i++)
  {
    Str.printf("Edit_D%d",i);
    if((i < DataLen) && (CheckBox_RTR->Checked == false))
    {
     ((TEdit *)FindComponent(Str))->Color = clWindow;
     ((TEdit *)FindComponent(Str))->ReadOnly = false;
    }
    else
    {
     ((TEdit *)FindComponent(Str))->Color = clGrayText;
     ((TEdit *)FindComponent(Str))->ReadOnly = true;
    }
  }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button_AddClick(TObject *Sender)
{
  char* endtemp,TempArr[20];
  int i,Ret;
  DWORD TempValue,MsgID,TimePeriod;
  AnsiString TempStr,Str;
  BYTE Mode,RTR,DataLen,Data[8];

  if (CyclicTxMsgCnt>4)
  {
     ShowMessage("Only 5 cyclic tramsmission messages are allowed");
     return;
  }

   TempStr = Edit_MsgID->Text; //Get message id parameter
   TempValue=strtol(TempStr.c_str(),&endtemp,16);
   if (RadioButton_11Bit->Checked)
   { //Check the CAN message mode
     Mode=0;
     MsgID=TempValue&0x7ffUL;
   }
   else
   {
     Mode=1;
     MsgID=TempValue&0x1fffffffUL;
   }
   TempStr.printf("%lX",MsgID);
   Edit_MsgID->Text = TempStr; //Set message id parameter back to Edit box

   if (CheckBox_RTR->Checked) RTR=1; //Check if the RTR is used or not
   else RTR=0;

   DataLen= ComboBox_DataLen->ItemIndex;  //Get data length parameter

   for (i=0;i<8;i++)
   {
      Str.printf("Edit_D%d",i);
      TempStr = ((TEdit *)FindComponent(Str))->Text; //Get data parameters
      TempValue=strtol(TempStr.c_str(),&endtemp,16);
      Data[i]=(BYTE)(TempValue&0xff);
      TempStr.printf("%02X",Data[i]);
      ((TEdit *)FindComponent(Str))->Text = TempStr; //Set data parameters back to Edit box
   }

   TempStr = Edit_Time->Text;
   TimePeriod=strtol(TempStr.c_str(),&endtemp,10);
   if (TimePeriod<5) TimePeriod=5;
   TempStr.printf("%lu",TimePeriod);
   Edit_Time->Text = TempStr;  //Set time period parameter back to Edit box


   //Ret = 0;
   Ret=CM100_AddCyclicTxMsg(SelectedBoardNo,1,Mode,MsgID,RTR,DataLen,Data,TimePeriod,0,&Handle_No[CyclicTxMsgCnt]); //Add cyclic transmission message
   if (Ret)
   {
      AnsiString ErrStr;
      ErrStr.printf("Add Cyclic Messages Error, Error Code %d",Ret);
      ShowMessage(ErrStr);
      return;
   }

   TListItem    *ListItem;

   ListView1->Tag = 0; //Disable Change Event
   ListItem = ListView1->Items->Add();
   Str.printf("  %d",Handle_No[CyclicTxMsgCnt]);
   ListItem->Caption = Str; //Handle No

   Str.printf("%s",Mode?"29-Bit":"11-Bit");
   ListItem->SubItems->Add(Str);  //Mode

   Str.printf("%lX",MsgID);
   ListItem->SubItems->Add(Str); //MsgID

   Str.printf("%c",RTR?'Y':'N');
   ListItem->SubItems->Add(Str); //RTR

   Str.printf("%d",DataLen);
   ListItem->SubItems->Add(Str); //DataLen

   for(i=0;i<8;i++)
   {
     if((i < DataLen) && (RTR == 0))
       Str.printf("%lX",Data[i]);
     else
       Str = " ";
     ListItem->SubItems->Add(Str);
   }
   Str.printf("%lu",TimePeriod);
   ListItem->SubItems->Add(Str);
   ListView1->Tag = 1;
   CyclicTxMsgCnt++;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button_DeleteClick(TObject *Sender)
{
  char* endtemp,TempArr[20];
  BYTE SelectedHandle;
  int Ret,SelectedListNo;
  AnsiString ErrStr;

  if (CyclicTxMsgCnt==0)
  {
     ShowMessage("No cyclic tramsmission message can be deleted");
     return;
  }

  if(ListView1->Selected == NULL)
  {
     ShowMessage("Please select an message to be deleted");
     return;
  }

  SelectedHandle = ListView1->Selected->Caption.ToInt();
  Ret=CM100_DeleteCyclicTxMsg(SelectedBoardNo,1,SelectedHandle); //Delete selected cyclic transmission message
  if (Ret)
  {
    ErrStr.printf("Delete Cyclic Message Error, Error Code=%d",Ret);
    ShowMessage(ErrStr);
    return;
  }
  ListView1->Items->Delete(ListView1->ItemIndex);
  CyclicTxMsgCnt--;

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

void __fastcall TForm1::ListView1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
   AnsiString Str;
   TListItem *Item;
   BYTE SelectedHandle;
   int Ret;
   Item = ListView1->GetItemAt(X,Y);
   if(X < 4 || X > 15) return;

   //User click check box
   SelectedHandle = Item->Caption.ToInt();
   if(Item->Checked == true)
   {
      Ret=CM100_EnableCyclicTxMsg(SelectedBoardNo,1,SelectedHandle); //Enable selected cyclic transmission message.
      Str.printf("Enable Cyclic Message Error, Error Code=%d",Ret);
   }
   else
   {
      Ret=CM100_DisableCyclicTxMsg(SelectedBoardNo,1,SelectedHandle); //Disable selected cyclic transmission message.
      Str.printf("Disable Cyclic Message Error, Error Code=%d",Ret);
   }
   if (Ret)  ShowMessage(Str);
}
//---------------------------------------------------------------------------

