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

#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 BYTE MaxListCnt;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(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.
		Timer1->Enabled = false; //Stop timer event
		CM100_CloseBoard(SelectedBoardNo);
		IsActiveFlag=false;
	}

	Ret=CM100_ActiveBoard(BoardNo); //Active board
	if (Ret){
		ShowMessage("Active Board Error!");
		Form1->Caption = "ReceiveMsg --- No board is active";
		return;
	}

	Ret = CM100_Init(BoardNo,1);  //Init board
	if(Ret){
		ShowMessage("Init Board Error!");
		CM100_CloseBoard(BoardNo);
		Form1->Caption = "ReceiveMsg --- 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 = "ReceiveMsg --- No board is active";
		return;
	}
	SelectedBoardNo=BoardNo;
	TitleStr.printf("ReceiveMsg --- Board %d is active with baud %d K pbs",BoardNo,BaudMap[Baud]);
	Form1->Caption = TitleStr;
	IsActiveFlag=true;
        Timer1->Enabled = true;

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

void __fastcall TForm1::Button_ClearStatusClick(TObject *Sender)
{
 CM100_ClearDataOverrun(SelectedBoardNo,1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button_ExitClick(TObject *Sender)
{
  Timer1->Enabled = false;
  if (IsActiveFlag) CM100_CloseBoard(SelectedBoardNo);
  Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
  BYTE CANStatus;
  AnsiString Str;
  CM100_Status(SelectedBoardNo,1,&CANStatus); //Get CAN status
  Str.printf("0x%02X",CANStatus);
  Edit_Status->Text = Str;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button_ClearListClick(TObject *Sender)
{
  Memo1->Clear();
  MaxListCnt = 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button_ReceiveClick(TObject *Sender)
{
   int i,j,MsgCnt,Ret;
   double Timestamp;
   AnsiString ListStr,DataStr;
   PacketStruct CANMsg;

   MsgCnt=CM100_RxMsgCount(SelectedBoardNo,1); //Check the numbers of received CAN messages
   for (i=0;i<MsgCnt;i++)
   {
   	Ret=CM100_ReceiveMsg(SelectedBoardNo,1,&CANMsg); //Receive CAN message
   	if (Ret)
        {
		AnsiString ErrStr;
		ErrStr.printf("Get Message Error, Error code=%d",Ret);
		ShowMessage(ErrStr);
                break;
        }
	Timestamp=(double)CANMsg.MsgTimeStamps;
	if (CANMsg.rtr)
        { //if rtr bit is set, data is useless
	  ListStr.printf("%03d Time=%8.1fms, Mode:%d, ID:%08lX, RTR:%d, DLen:%d",
			                MaxListCnt,Timestamp/10,CANMsg.mode,CANMsg.id,CANMsg.rtr,CANMsg.len);
          Memo1->Lines->Add(ListStr); //Show CAN messages
	}
	else
        {
	  ListStr.printf("%03d Time=%8.1fms, Mode:%d, ID:%08lX, RTR:%d, DLen:%d, Data(hex):",
			                MaxListCnt,Timestamp/10,CANMsg.mode,CANMsg.id,CANMsg.rtr,CANMsg.len);
          if (CANMsg.len==0)
          {
             ListStr += " X";
          }
	  else
          {
             ListStr += "[";
             for (j=0;j<CANMsg.len;j++)
             {
	     	if (j<CANMsg.len-1)
                {
                  DataStr.printf("%02X,",CANMsg.data[j]);
                  ListStr += DataStr;
                }
                else
                {
                  DataStr.printf("%02X",CANMsg.data[j]);
                  ListStr += DataStr;
                }
             }
	     ListStr += "]";
          }
	  Memo1->Lines->Add(ListStr); //Show CAN messages
        }
	++MaxListCnt;
	if (!MaxListCnt)
        { //If ListBox buffer is full, clean it
	  Memo1->Clear();
        }
   }

}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
  Timer1->Enabled = false;
  if (IsActiveFlag) CM100_CloseBoard(SelectedBoardNo);
}
//---------------------------------------------------------------------------
