unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Button_Active: TButton;
    Button_Exit: TButton;
    Label13: TLabel;
    Edit_Time: TEdit;
    Label12: TLabel;
    Edit_D7: TEdit;
    Label11: TLabel;
    Edit_D6: TEdit;
    Label10: TLabel;
    Edit_D5: TEdit;
    Label9: TLabel;
    Edit_D4: TEdit;
    Label8: TLabel;
    Edit_D3: TEdit;
    Label7: TLabel;
    Edit_D2: TEdit;
    Label6: TLabel;
    Edit_D1: TEdit;
    Label5: TLabel;
    Edit_D0: TEdit;
    Label4: TLabel;
    CheckBox_RTR: TCheckBox;
    Edit_MsgID: TEdit;
    Label3: TLabel;
    RadioButton_11Bit: TRadioButton;
    RadioButton_29Bit: TRadioButton;
    ListView1: TListView;
    Button_Add: TButton;
    Button_Delete: TButton;
    ComboBox_BoardNo: TComboBox;
    ComboBox_BaudRate: TComboBox;
    ComboBox_DataLen: TComboBox;
    procedure FormCreate(Sender: TObject);
    procedure Button_ActiveClick(Sender: TObject);
    procedure Button_ExitClick(Sender: TObject);
    procedure ComboBox_DataLenChange(Sender: TObject);
    procedure Button_AddClick(Sender: TObject);
    procedure Button_DeleteClick(Sender: TObject);
    procedure ListView1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses CM100;

var
 SelectedBoardNo:BYTE;
 IsActiveFlag:bool=false;
 Handle_No: array[0..4] of BYTE;
 CyclicTxMsgCnt:BYTE=0;
{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
   ComboBox_BoardNo.ItemIndex := 0;
   ComboBox_BaudRate.ItemIndex := 0;
   ComboBox_DataLen.ItemIndex := 8;
end;

procedure TForm1.Button_ActiveClick(Sender: TObject);
var
   BoardNo,Baud: BYTE;
   Ret: Integer;
   Para: ConfigStruct;
   TitleStr: AnsiString;
   BaudMap: array[0..7] of WORD;
begin
   BaudMap[0] := 10;
   BaudMap[1] := 20;
   BaudMap[2] := 50;
   BaudMap[3] := 125;
   BaudMap[4] := 250;
   BaudMap[5] := 500;
   BaudMap[6] := 800;
   BaudMap[7] := 1000;

   BoardNo:=ComboBox_BoardNo.ItemIndex;
   Baud:=ComboBox_BaudRate.ItemIndex;
   if (IsActiveFlag) then //If board is active before, close it first.
     begin
	CM100_CloseBoard(SelectedBoardNo);
	IsActiveFlag:=false;
     end;

   Ret := CM100_ActiveBoard(BoardNo); //Active board
   if (Ret <> 0) then
     begin
        ShowMessage('Active Board Error!');
        Form1.Caption := 'TransmitMsgCyclically --- No board is active';
        exit;
     end;

   Ret := CM100_Init(BoardNo,1);  //Init board
   if (Ret <> 0) then
     begin
        ShowMessage('Init Board Error!');
        CM100_CloseBoard(BoardNo);
        Form1.Caption := 'TransmitMsgCyclically --- No board is active';
        exit;
     end;

   Para.AccMask[0]:=$ff; //Set AccMask to 0xffffffff to pass all CAN messages from CAN bus
   Para.AccMask[1]:=$ff;
   Para.AccMask[2]:=$ff;
   Para.AccMask[3]:=$ff;
   Para.AccCode[0]:=$00; //AccCode is useless when AccMask is 0xffffffff
   Para.AccCode[1]:=$00;
   Para.AccCode[2]:=$00;
   Para.AccCode[3]:=$00;
   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 <> 0) then
     begin
        ShowMessage('Configure Board Error!');
        CM100_CloseBoard(BoardNo);
        Form1.Caption := 'TransmitMsgCyclically --- No board is active';
        exit;
     end;

   SelectedBoardNo := BoardNo;
   TitleStr := 'TransmitMsgCyclically --- Board ' + IntToStr(BoardNo) + ' is active with baud ' + IntToStr(BaudMap[Baud]) + ' K pbs';
   Form1.Caption := TitleStr;
   IsActiveFlag := true;
end;

procedure TForm1.Button_ExitClick(Sender: TObject);
begin
   if (IsActiveFlag) then CM100_CloseBoard(SelectedBoardNo);
   Close();
end;

procedure TForm1.ComboBox_DataLenChange(Sender: TObject);
var
  i,DataLen:Integer;
  Str:AnsiString;
begin
  DataLen := ComboBox_DataLen.ItemIndex;
  for i:=0 to 7 do
    begin
       Str := 'Edit_D' + IntToStr(i);
       if((i < DataLen) and (CheckBox_RTR.Checked = false)) then
         begin
           TEdit(FindComponent(Str)).Color := clWindow;
           TEdit(FindComponent(Str)).ReadOnly := false;
         end
       else
         begin
           TEdit(FindComponent(Str)).Color := clGrayText;
           TEdit(FindComponent(Str)).ReadOnly := true;
         end;
    end;
end;

procedure TForm1.Button_AddClick(Sender: TObject);
var
  i,Ret: Integer;
  TempValue,MsgID,TimePeriod: DWORD;
  TempStr,Str,ErrStr: AnsiString;
  Mode,RTR,DataLen: BYTE;
  Data: array[0..7] of BYTE;
  ListItem:TListItem;
begin
  if (CyclicTxMsgCnt>4) then
    begin
       ShowMessage('Only 5 cyclic tramsmission messages are allowed');
       exit;
    end;

  TempStr := Edit_MsgID.Text; //Get message id parameter
  TempValue := StrToInt('$'+ TempStr);
  if (RadioButton_11Bit.Checked) then  //Check the CAN message mode
     begin
        Mode := 0;
        MsgID := TempValue and $7ff;
     end
   else
     begin
        Mode := 1;
        MsgID := TempValue and $1fffffff;
     end;

   TempStr := IntToHex(MsgID,0);
   Edit_MsgID.Text := TempStr; //Set message id parameter back to Edit box

   if (CheckBox_RTR.Checked) then RTR := 1 //Check if the RTR is used or not
   else RTR := 0;

   DataLen := ComboBox_DataLen.ItemIndex;  //Get data length parameter

   for i:=0 to 7 do
     begin
      Str := 'Edit_D' + IntToStr(i);
      TempStr := (TEdit(FindComponent(Str))).Text; //Get data parameters
      TempValue := StrToInt('$'+ TempStr);
      Data[i] := BYTE((TempValue and $ff));
      TempStr := IntToHex(Data[i],2);
      (TEdit(FindComponent(Str))).Text := TempStr; //Set data parameters back to Edit box
     end;

   TempStr := Edit_Time.Text;
   TimePeriod := StrToInt(TempStr);
   if (TimePeriod<5) then TimePeriod :=5;
   TempStr := IntToStr(TimePeriod);
   Edit_Time.Text := TempStr;  //Set time period parameter back to Edit box

   Ret:=CM100_AddCyclicTxMsg(SelectedBoardNo,1,Mode,MsgID,RTR,DataLen,Data[0],TimePeriod,0,Handle_No[CyclicTxMsgCnt]); //Add cyclic transmission message
   if (Ret <> 0) then
     begin
        ErrStr := 'Add Cyclic Messages Error, Error Code ' + IntToStr(Ret);
        ShowMessage(ErrStr);
        exit;
     end;


   ListView1.Tag := 0; //Disable Change Event
   ListItem := ListView1.Items.Add();
   Str := '  ' + IntToStr(Handle_No[CyclicTxMsgCnt]);
   ListItem.Caption := Str; //Handle No

   if(Mode = 1) then Str := '29-Bit'
   else Str := '11-Bit';
   ListItem.SubItems.Add(Str);  //Mode

   Str := IntToHex(MsgID,1);
   ListItem.SubItems.Add(Str); //MsgID

   if(RTR = 1) then Str := 'Y'
   else Str := 'N';
   ListItem.SubItems.Add(Str); //RTR

   Str := IntToStr(DataLen);
   ListItem.SubItems.Add(Str); //DataLen

   for i:=0 to 7 do
     begin
        if((i < DataLen) and (RTR = 0)) then Str := IntToHex(Data[i],1)
        else Str := ' ';
        ListItem.SubItems.Add(Str);
     end;

   Str := IntToStr(TimePeriod);
   ListItem.SubItems.Add(Str);
   ListView1.Tag := 1;
   CyclicTxMsgCnt := CyclicTxMsgCnt+1;
end;

procedure TForm1.Button_DeleteClick(Sender: TObject);
var
  SelectedHandle:BYTE;
  Ret:Integer;
  ErrStr:AnsiString;
begin
  if(CyclicTxMsgCnt=0) then
    begin
     ShowMessage('No cyclic tramsmission message can be deleted');
     exit;
    end;

  if(ListView1.Focused() = false) then
    begin
     ShowMessage('Please select an message to be deleted');
     exit;
    end;

  SelectedHandle := StrToInt(ListView1.Selected.Caption);
  Ret:=CM100_DeleteCyclicTxMsg(SelectedBoardNo,1,SelectedHandle); //Delete selected cyclic transmission message
  if (Ret <> 0) then
    begin
       ErrStr := 'Delete Cyclic Message Error, Error Code=' + IntToStr(Ret);
       ShowMessage(ErrStr);
       exit;
    end;
  ListView1.Items.Delete(ListView1.Selected.Index);
  CyclicTxMsgCnt := CyclicTxMsgCnt-1;
end;

procedure TForm1.ListView1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
   Str:AnsiString;
   Item:TListItem;
   SelectedHandle:BYTE;
   Ret:Integer;
begin

   Item := ListView1.GetItemAt(X,Y);
   if((X < 4) or (X > 15)) then exit;

   //User click check box
   SelectedHandle := StrToInt(Item.Caption);
   if(Item.Checked = true) then
     begin
      Ret:=CM100_EnableCyclicTxMsg(SelectedBoardNo,1,SelectedHandle); //Enable selected cyclic transmission message.
      Str := 'Enable Cyclic Message Error, Error Code=' + IntToStr(Ret);
     end
   else
     begin
      Ret:=CM100_DisableCyclicTxMsg(SelectedBoardNo,1,SelectedHandle); //Disable selected cyclic transmission message.
      Str := 'Disable Cyclic Message Error, Error Code=' + IntToStr(Ret);
     end;
   if (Ret <> 0) then ShowMessage(Str);
end;

end.
