📄 _main_form.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "_main_form.h"
#include <IniFiles.hpp>
#include <Registry.hpp>
#include <stdio.h>
#include "_string_token.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "ColorListBox"
#pragma resource "*.dfm"
TMAIN_FORM *MAIN_FORM;
DWORD baudrate_list[] = {2400, 4800, 9600, 19200, 38400, 46875, 57600, 93750,
115200, 187500, 230400, 375000, 460800, 750000, 921600};
static void HCOMM_ON_RECEIVE(char *data, UINT16 count);
static void CTRLFSM_SEND(unsigned __int8 *data, unsigned __int16 len);
static void ZEEVO_PRINT(Thci_evt_zeevo_event_print *Value);
static void ZEEVO_OnUSBClose(void);
static TEngineerControl EngineerCtrl[] = {
{CC_BUILD, NULL,"build", "Display Build Information",true},
{CC_AIRENC, NULL,"", "Select SCO Air Encoding", false},
{CC_CODEC, NULL,"", "Select CODEC Type", false},
{CC_SETBDADDR, NULL,"dumpcurrbdaddr", "Set and Read BDADDR", true},
{CC_DEVCMD, NULL,"", "Development Command", true},
{CC_CRYSTAL, NULL,"rctff", "Crystal Tunning", true}
};
extern int SEARCH_STR(char *target, char *str);
//---------------------------------------------------------------------------
__fastcall TMAIN_FORM::TMAIN_FORM(TComponent* Owner)
: TForm(Owner)
{
//Initialize setting
hcomm_sets.count = 0;
hcomm_sets.selected = 0;
hcomm_sets.item = NULL;
hcomm.OnReceiveHandle = HCOMM_ON_RECEIVE;
ctrlfsm.DataOut = CTRLFSM_SEND;
ctrlfsm.ON_ZEEVO_PRINT = ZEEVO_PRINT;
led_count = 0;
EngineerCtrl[0].Panel = ZV_ENGINEER_PANEL_BUILD;
EngineerCtrl[3].Panel = ZV_ENGINEER_PANEL_BDADDR;
EngineerCtrl[4].Panel = ZV_ENGINEER_PANEL_DEV;
EngineerCtrl[5].Panel = ZV_ENGINEER_PANEL_CRYSTAL;
for (int i = 0; i < MAX_USB_DEVICES; i++)
husb[i] = NULL;
}
//---------------------------------------------------------------------------
void __fastcall TMAIN_FORM::USB_Detect(void)
{
// Enable USB Function
for (int i = 0; i < MAX_USB_DEVICES; i++)
if(husb[i])
{
delete husb[i];
husb[i] = NULL;
}
husb[0] = new TUsbDevice;
if(ERROR_SUCCESS == husb[0]->getDeviceStatus())
{
//MENU_CFG_USEUSB->Enabled = true;
DebugPrintf("USB Device Plugged.");
delete husb[0];
}
else
{
//MENU_CFG_USEUSB->Enabled = false;
DebugPrintf("No USB Device Detected.");
}
husb[0] = NULL;
}
void __fastcall TMAIN_FORM::USB_Close(void)
{
UsbReadThreadEnd();
if(husb[0])
{
delete husb[0];
husb[0] = NULL;
}
ZV_ENGINEER_LIST->Enabled = false;
MENU_CFG_USEUSB->Checked = false;
//USB_READ_TASK->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TMAIN_FORM::COMM_LIST_UPDATE(void)
{
TMenuItem *new_item = NULL;
bool selected_found = false;
MENU_CFG_COMM_OPEN->Clear();
hcomm.ReInitialize();
hcomm.GetDevicePortList(&hcomm_sets.count, hcomm_sets.portlist);
if(hcomm_sets.count)
{
AnsiString stmp;
for(int idx=0; idx<hcomm_sets.count; idx++)
{
hcomm.GetPortInfo(hcomm_sets.portlist[idx], hcomm_sets.portdscp[idx]);
new_item = new TMenuItem(MENU_CFG_COMM_OPEN);
if(strlen(hcomm_sets.portdscp[idx]))
stmp.sprintf("COM%d (%s)", hcomm_sets.portlist[idx], hcomm_sets.portdscp[idx]);
else
stmp.sprintf("COM%d", hcomm_sets.portlist[idx]);
new_item->Caption = stmp;
new_item->OnClick = MENU_CFG_COMM_OPEN_PORT_Click;
new_item->RadioItem = true;
//new_item->;
MENU_CFG_COMM_OPEN->Add(new_item);
//new_item->ShortCut
if(hcomm_sets.selected == hcomm_sets.portlist[idx])
{
new_item->Checked = true;
selected_found = true;
hcomm_sets.item = new_item;
}
}
}
if(!selected_found)
{
if(hcomm_sets.selected)
{
AnsiString msg;
msg.sprintf("COM%d have been removed and the connection would be closed.", hcomm_sets.selected);
hcomm_sets.selected = 0;
Application->MessageBoxA(msg.c_str(), "NOTIFY!!", MB_OK);
DebugPrintf("Serial Communication Closed");
}
hcomm_sets.selected = 0;
hcomm_sets.item = NULL;
hcomm.Close();
MAIN_FORM->Caption = "Zeevo MP Tool";
MENU_CFG_COMM_OPEN->Checked = false;
}
}
//---------------------------------------------------------------------------
void __fastcall TMAIN_FORM::FormClose(TObject *Sender,
TCloseAction &Action)
{
hcomm.Close();
MAIN_FORM->Caption = "Zeevo MP Tool";
WriteReg();
}
//---------------------------------------------------------------------------
void __fastcall TMAIN_FORM::FormShow(TObject *Sender)
{
USB_Detect();
COMM_LIST_UPDATE();
ReadReg();
for(int i=0; i<MENU_CFG_COMM_BAUDRATE->Count; i++)
{
if(hcomm_sets.used_baudrate == baudrate_list[i])
MENU_CFG_COMM_BAUDRATE->Items[i]->Checked = true;
}
if(hcomm_sets.used_interface == CI_HCI)
MENU_CFG_INTEFACE_HCI->Checked = true;
else if(hcomm_sets.used_interface == CI_ZERIAL)
MENU_CFG_INTEFACE_ZERIAL->Checked = true;
ctrlfsm.Interface = hcomm_sets.used_interface;
ZV_ENGINEER_LIST->MaxItemCount = sizeof(EngineerCtrl) / sizeof(TEngineerControl);
TColor color;
for(int i=0; i<sizeof(EngineerCtrl) / sizeof(TEngineerControl); i++)
{
if(!EngineerCtrl[i].CanUse)
color = clSilver;
else
color = clNavy;
ZV_ENGINEER_LIST->ItemAdd(EngineerCtrl[i].Description, clRed);
ZV_ENGINEER_LIST->SetColor(i, color);
}
ZV_ENGINEER_LISTClick(Sender);
}
//--------------------------------------------------------------------------
void __fastcall TMAIN_FORM::MENU_CFG_COMM_OPEN_PORT_Click(TObject *Sender)
{
TMenuItem *port_item = (TMenuItem*)Sender;
bool err= false;
AnsiString errmsg;
if(MENU_CFG_USEUSB->Checked)
{
USB_Close();
}
if(port_item->Checked)
{
//close the port
port_item->Checked = false;
hcomm_sets.selected = 0;
hcomm_sets.item = NULL;
hcomm.Close();
MAIN_FORM->Caption = "Zeevo MP Tool";
DebugPrintf("Serial Communication Closed");
MENU_CFG_COMM_OPEN->Checked = false;
ZV_ENGINEER_LIST->Enabled = false;
}
else
{
if(hcomm_sets.selected)
{
hcomm.Close();
MAIN_FORM->Caption = "Zeevo MP Tool";
hcomm_sets.selected = 0;
if(hcomm_sets.item)
{
hcomm_sets.item->Checked = false;
hcomm_sets.item = NULL;
}
MENU_CFG_COMM_OPEN->Checked = false;
ZV_ENGINEER_LIST->Enabled = false;
}
// try to open the port
T_PORT_SET config;
config.port = hcomm_sets.portlist[port_item->MenuIndex];
config.baudrate = b_115200;
config.databit = d_8;
config.parity = p_none;
config.stopbit = s_1;
config.dtr = true;
config.dsr = false;
config.rts = true;
config.cts = true;
config.xonxoff = false;
if(hcomm.Open(&config) == ERR_NONE)
{
if(hcomm.SetBaudrate(hcomm_sets.used_baudrate)== ERR_NONE)
{
port_item->Checked = true;
hcomm_sets.selected = hcomm_sets.portlist[port_item->MenuIndex];
hcomm_sets.item = port_item;
AnsiString newcap;
newcap.sprintf("Zeevo MP Tool - COM%d(%d bps)", hcomm_sets.selected, hcomm_sets.used_baudrate);
MAIN_FORM->Caption = newcap;
MENU_CFG_COMM_OPEN->Checked = true;
DebugPrintf("Serial Communication Opened...Send Reset Command First.");
if(MENU_CFG_INTEFACE_HCI->Checked)
ctrlfsm.CmdReset();
else
ctrlfsm.CmdConsoleReset();
Sleep(2000);
//ZV_ENGINEER_LIST->Enabled = true;
ZV_ENGINEER_DELAY->Enabled = true;
}
else
{
if(hcomm_sets.item)
{
hcomm_sets.item->Checked = false;
hcomm_sets.item = NULL;
}
hcomm_sets.selected = 0;
hcomm.Close();
MAIN_FORM->Caption = "Zeevo MP Tool";
MENU_CFG_COMM_OPEN->Checked = false;
ZV_ENGINEER_LIST->Enabled = false;
err= true;
errmsg.sprintf("Cannot open COM%d in %d bps.",hcomm_sets.portlist[port_item->MenuIndex],
hcomm_sets.used_baudrate);;
}
}
else
{
err= true;
errmsg.sprintf("Cannot open COM%d.",hcomm_sets.portlist[port_item->MenuIndex]);
}
}
if(err)
Application->MessageBoxA(errmsg.c_str(), "ERROR!!", MB_OK);
}
//--------------------------------------------------------------------------
void TMAIN_FORM::OnDeviceChange(TMessage msg)
{
//TODO: Add your source code here
USB_Detect();
COMM_LIST_UPDATE();
}
//--------------------------------------------------------------------------
void __fastcall TMAIN_FORM::ReadReg(void)
{
TRegistry *Registry = new TRegistry();
Registry->CurrentKey;
if(Registry->OpenKey("SOFTWARE", false))
{
TRegDataInfo info;
Registry->OpenKey("ZEEVO APP", true);
Registry->OpenKey("MP TOOL", true);
Registry->OpenKey("CONFIG", true);
if(!Registry->GetDataInfo("COMM BAUDRATE", info))
Registry->WriteInteger("COMM BAUDRATE", 115200);
else if(info.RegData != rdInteger)
Registry->WriteInteger("COMM BAUDRATE", 115200);
hcomm_sets.used_baudrate = Registry->ReadInteger("COMM BAUDRATE");
if(!Registry->GetDataInfo("COMM INTERFACE", info))
Registry->WriteInteger("COMM INTERFACE", CI_HCI);
else if(info.RegData != rdInteger)
Registry->WriteInteger("COMM INTERFACE", CI_HCI);
hcomm_sets.used_interface = (_CtrlInterface)Registry->ReadInteger("COMM INTERFACE");
char RegName[128];
for(int i=0; i<20; i++)
{
sprintf(RegName,"CONSOLE CMD%02d",i);
if(!Registry->GetDataInfo(RegName, info))
Registry->WriteString(RegName, "");
else if(info.RegData != rdString)
Registry->WriteString(RegName, "");
USER_CONSOLE_CMD->Items->Add(Registry->ReadString(RegName));
}
}
delete Registry;
}
//--------------------------------------------------------------------------
void __fastcall TMAIN_FORM::WriteReg(void)
{
TRegistry *Registry = new TRegistry();
Registry->CurrentKey;
if(Registry->OpenKey("SOFTWARE", false))
{
TRegDataInfo info;
Registry->OpenKey("ZEEVO APP", true);
Registry->OpenKey("MP TOOL", true);
Registry->OpenKey("CONFIG", true);
Registry->WriteInteger("COMM BAUDRATE", hcomm_sets.used_baudrate);
Registry->WriteInteger("COMM INTERFACE", hcomm_sets.used_interface);
char RegName[128];
for(int i=0; i<20; i++)
{
sprintf(RegName,"CONSOLE CMD%02d",i);
if(i < USER_CONSOLE_CMD->Items->Count)
Registry->WriteString(RegName, USER_CONSOLE_CMD->Items->Strings[i].c_str());
else
Registry->WriteString(RegName, "");
}
}
delete Registry;
}
//---------------------------------------------------------------------------
void __fastcall TMAIN_FORM::MENU_CFG_COMM_BAUDRATE_Click(
TObject *Sender)
{
TMenuItem *baudrate_item = (TMenuItem*)Sender;
if(hcomm_sets.selected)
{
if(hcomm.SetBaudrate(baudrate_list[baudrate_item->MenuIndex]) == ERR_INTERNAL)
{
AnsiString errmsg;
errmsg.sprintf("Cannot open COM%d in %d bps.",hcomm_sets.selected,
baudrate_list[baudrate_item->MenuIndex]);
Application->MessageBoxA(errmsg.c_str(), "ERROR!!", MB_OK);
}
else
{
AnsiString newcap;
baudrate_item->Checked = true;
hcomm_sets.used_baudrate = baudrate_list[baudrate_item->MenuIndex];
newcap.sprintf("Zeevo MP Tool - COM%d(%d bps)", hcomm_sets.selected, hcomm_sets.used_baudrate);
MAIN_FORM->Caption = newcap;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -