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

#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;
//---------------------------------------------------------------------------
__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 = "TransmitMsg --- No board is active";
    return;
  }

  Ret = CM100_Init(BoardNo,1);  //Init board
  if(Ret)
  {
     ShowMessage("Init Board Error!");
     CM100_CloseBoard(BoardNo);
     Form1->Caption = "TransmitMsg --- 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 = "TransmitMsg --- No board is active";
	   return;
	}
	SelectedBoardNo=BoardNo;
	TitleStr.printf("TransmitMsg --- 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_SendClick(TObject *Sender)
{
   char* endtemp,TempArr[20];
   int i,Ret;
   DWORD TempValue;
   AnsiString TempStr,Str;
   PacketStruct CANMsg;


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

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

   CANMsg.len= 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);
      CANMsg.data[i]=(BYTE)(TempValue&0xff);
      TempStr.printf("%02X",CANMsg.data[i]);
      ((TEdit *)FindComponent(Str))->Text = TempStr; //Set data parameters back to Edit box
   }

   Ret=CM100_SendMsg(SelectedBoardNo,1,&CANMsg); //Send CAN message to CAN bus
   if (Ret)
   {
      AnsiString ErrStr;
      ErrStr.printf("Send Message Error, Error Code %d",Ret);
      ShowMessage(ErrStr);
   }
}
//---------------------------------------------------------------------------
