⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 display.cpp

📁 基于USB接口的GPS应用程序
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#include <time.h>
//
#pragma hdrstop

#include "InitForm.h"
#include "display.h"
#include "Gpsnav.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

TForm1 *Form1;

//---------------------------------------------------------------------------
/*
 *  create usb gps interface object
 *  create update screen thread
 *  init global event object
 */
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
 int initresult;

 ch_allocate_manually = false;  // use almanac file to allocate chn
 //init prn settings
 for(int i=0; i< CH_NUM; i++)
  prn[i] = 0;

 llh[0]=llh[1]=llh[2] = 0.;


 CfgFileHandle = fopen("system.cfg","r");
 if( CfgFileHandle == NULL) // no config file
 {
   ;// should add some err handle coce here
 }
 else
 {
  read_cfg(CfgFileHandle);
 }

 for(int i =0; i< CH_NUM; i++){
  channel[i] = ListView1->Items->Add();
  channel[i]->Caption = IntToStr(i);
  for(int j=0; j<ITEM_NUM; j++)
   channel[i]->SubItems->Add(IntToStr(0));
 }

 for(int i=0; i< CH_NUM; i++){
  ch_result[i] = ResultListView->Items->Add();
  ch_result[i]->Caption = IntToStr(i);
  for(int j=0; j<RESULT_NUM; j++)
   ch_result[i]->SubItems->Add(" ");
 }


 Gpsfunc = new GPS_Func;

 handleopened = true;
 GpsInterface = new IoInterface;
 if(GpsInterface == NULL)
 {
        Application->MessageBoxA("Failed to create an interface object", "Error", MB_OKCANCEL);
        handleopened = false;
        exit(0);
 }
 else if(!GpsInterface->IsHandleOpened())
 {
        Application->MessageBoxA("Failed to open the USB-GPS interface", "Error", MB_OKCANCEL);
        handleopened = false;
        exit(0);
 }
 else
 {
  initresult= GpsInterface->InitProc();

  if(initresult)
   Application->MessageBoxA(IOINTERFACE_MSG[initresult],"Error", MB_OKCANCEL);
 }

 // create a sampling thread and update screen thread
 // also need to create two event for synchronization

 newdataEvent = CreateEvent(NULL,false,false,NULL);
 gpsEvent = CreateEvent(NULL, false, false, NULL);
 dispEvent = CreateEvent(NULL,false, false, NULL);

 InitializeCriticalSection(&GpsCriticalSection);


 if(handleopened)
 {
  Gpsnav = new GPS_Nav(GpsInterface);

  if(!ch_allocate_manually) // using almanac file to set prn
  {
   TInitializeForm *  InitPrnForm = new TInitializeForm(this);
   InitPrnForm->ShowModal();
   delete InitPrnForm;
  }
  //else  // set prn by system.cfg file
  Gpsnav->SetPrn(prn);

  initresult = Gpsnav->InitProc();
  if(initresult)
   Application->MessageBoxA(GPS_NAV_MSG[initresult],"Error", MB_OKCANCEL);
  Gpsnav->PrintDbgInfo(" Log information for GPS_Nav class ");
 }
 else
  Gpsnav = NULL;
}
//---------------------------------------------------------------------------
/*
 *  delete usb gps interface object
 *  delete update screen thread
 *  delete global event object
 */
__fastcall TForm1::~TForm1()
{
 CloseHandle(newdataEvent);
 CloseHandle(gpsEvent);
 CloseHandle(dispEvent);
 DeleteCriticalSection(&GpsCriticalSection);
}
//---------------------------------------------------------------------------

void TForm1::setprn(FILE* AlmFile, struct tm* gt)
{
 Gpsfunc->CalcPrnSetting(prn,gt,AlmFile,llh);
}

//---------------------------------------------------------------------------
void TForm1::update_channel(unsigned char idx, unsigned short *newdata)
{
 for(int i =0; i< ITEM_NUM; i++)
  channel[idx]->SubItems->Strings[i] = IntToHex((int)newdata[i],4);
}
//---------------------------------------------------------------------------
void TForm1::update_allchannel( PT_allch_rawdata_disp rawdata_pt)
{
 for( int i=0; i< CH_NUM; i++){
  channel[i]->SubItems->Strings[0] = IntToStr((int)rawdata_pt->allch_rawdata[i].prn_num);
  channel[i]->SubItems->Strings[1] = IntToStr((int)rawdata_pt->allch_rawdata[i].code_slew);
  channel[i]->SubItems->Strings[2] = IntToStr((int)rawdata_pt->allch_rawdata[i].code_phase);
  channel[i]->SubItems->Strings[3] = IntToHex((int)rawdata_pt->allch_rawdata[i].carrier_cycle_high,4);
  channel[i]->SubItems->Strings[4] = IntToHex((int)rawdata_pt->allch_rawdata[i].carrier_cycle_low,4);
  channel[i]->SubItems->Strings[5] = IntToHex((int)rawdata_pt->allch_rawdata[i].carrier_dco_phase,4);
  channel[i]->SubItems->Strings[6] = IntToHex((int)rawdata_pt->allch_rawdata[i].code_dco_phase,4);
  channel[i]->SubItems->Strings[7] = IntToHex((int)rawdata_pt->allch_rawdata[i].epoch,4);
  channel[i]->SubItems->Strings[8] = IntToStr((int)rawdata_pt->allch_rawdata[i].i_track);
  channel[i]->SubItems->Strings[9] = IntToStr((int)rawdata_pt->allch_rawdata[i].q_track);
  channel[i]->SubItems->Strings[10] = IntToStr((int)rawdata_pt->allch_rawdata[i].i_prompt);
  channel[i]->SubItems->Strings[11] = IntToStr((int)rawdata_pt->allch_rawdata[i].q_prompt);
  channel[i]->SubItems->Strings[12] = IntToStr((int)rawdata_pt->allch_rawdata[i].freq_off);
 }
}
void TForm1::update_trkloop(PT_channel_disp chdisp)
{
 double signo,log_signo;
  for(int i=0; i<CH_NUM; i++){
  signo = (chdisp+i)->SigNoise/(15.*1395.);
  if( signo >0)
   log_signo = 10*log10(signo*signo*25.*1.7777);
  else
   log_signo = 0.;

  ch_result[i]->Caption = IntToStr((chdisp+i)->prn_num);

  ch_result[i]->SubItems->Strings[0] = AnsiString(STATE_MSG[(chdisp+i)->state_info]);
  ch_result[i]->SubItems->Strings[1] = FloatToStrF((chdisp+i)->freq_off, ffGeneral,5,9)+ "Hz";
  ch_result[i]->SubItems->Strings[2] = FloatToStrF(log_signo, ffGeneral,3,5)+"dB";
  ch_result[i]->SubItems->Strings[3] = "SubFrm " + IntToStr( (chdisp+i)->frm_idx);
  ch_result[i]->SubItems->Strings[4] = IntToHex((chdisp+i)->tow, 5);
  ch_result[i]->SubItems->Strings[5] = IntToHex((chdisp+i)->tlm, 4);
  ch_result[i]->SubItems->Strings[6] = FloatToStrF((chdisp+i)->pseudorange, ffGeneral, 10,10);
  ch_result[i]->SubItems->Strings[7] = FloatToStrF((chdisp+i)->ele, ffGeneral, 5,5);
  ch_result[i]->SubItems->Strings[8] = FloatToStrF((chdisp+i)->azi, ffGeneral, 5,5);
 }
}

void TForm1::update_navresult(PT_navinfo_disp navinfo_pt)
{
 if( navinfo_pt->NEDpos[2] > -1000. && navinfo_pt->NEDpos[2]<10000. ) // fast check reasonablity
 {
  NED_P_LatEdit->Font->Color = clGreen;
  NED_P_LongEdit->Font->Color = clGreen;
  NED_P_HighEdit->Font->Color = clGreen;

  ECEF_P_XEdit->Font->Color = clGreen;
  ECEF_P_YEdit->Font->Color = clGreen;
  ECEF_P_ZEdit->Font->Color = clGreen;

  ECEF_V_XEdit->Font->Color = clGreen;
  ECEF_V_YEdit->Font->Color = clGreen;
  ECEF_V_ZEdit->Font->Color = clGreen;
 }
 else
 {
  NED_P_LatEdit->Font->Color = clRed;
  NED_P_LongEdit->Font->Color = clRed;
  NED_P_HighEdit->Font->Color = clRed;

  ECEF_P_XEdit->Font->Color = clRed;
  ECEF_P_YEdit->Font->Color = clRed;
  ECEF_P_ZEdit->Font->Color = clRed;

  ECEF_V_XEdit->Font->Color = clRed;
  ECEF_V_YEdit->Font->Color = clRed;
  ECEF_V_ZEdit->Font->Color = clRed;

 }
 NED_P_LatEdit->Text = FloatToStrF( navinfo_pt->NEDpos[0], ffGeneral, 8,9);
 NED_P_LongEdit->Text = FloatToStrF( navinfo_pt->NEDpos[1], ffGeneral, 8,9);
 NED_P_HighEdit->Text = FloatToStrF( navinfo_pt->NEDpos[2], ffGeneral, 8,9);

 ECEF_P_XEdit->Text = FloatToStrF( navinfo_pt->ECEFpos[0], ffGeneral, 9,9);
 ECEF_P_YEdit->Text = FloatToStrF( navinfo_pt->ECEFpos[1], ffGeneral, 9,9);
 ECEF_P_ZEdit->Text = FloatToStrF( navinfo_pt->ECEFpos[2], ffGeneral, 9,9);

 ECEF_V_XEdit->Text = FloatToStrF( navinfo_pt->ECEFvel[0], ffGeneral, 9,9);
 ECEF_V_YEdit->Text = FloatToStrF( navinfo_pt->ECEFvel[1], ffGeneral, 9,9);
 ECEF_V_ZEdit->Text = FloatToStrF( navinfo_pt->ECEFvel[2], ffGeneral, 9,9);

 CLK_OFF_Edit->Text = FloatToStrF( navinfo_pt->clk_bias, ffGeneral, 6,6);
 GDOP_Edit->Text = FloatToStrF( navinfo_pt->GDOP, ffGeneral, 4,4);
 VDOP_Edit->Text = FloatToStrF( navinfo_pt->PDOP, ffGeneral, 4,4);

}
//---------------------------------------------------------------------------
void TForm1::update_disp(void)
{
  time_t t;
  struct tm *gmt,*area;
  AnsiString time_str;

  ListView1->UpdateItems(0,CH_NUM-1);
  ResultListView->UpdateItems(0,CH_NUM-1);

  t = time(NULL);
  gmt = gmtime(&t);
  time_str = "GMT Time: " + AnsiString(asctime(gmt));
  area = localtime(&t);
  StatusBar->Panels->Items[2]->Text = time_str+",  Local Time: " + AnsiString(asctime(area));
}

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


void __fastcall TForm1::FormShow(TObject *Sender)
{
 if(handleopened){

     update_gpsweek();

     GpsInterface->control_gp2021(STOP_TIMER); //start firmware timer0
     Sleep(100);
     GpsInterface->control_gp2021(START_TIMER); //start firmware timer0
     Sleep(100);
     Gpsnav->init_gp2021();
     Sleep(20);

     Gpsnav->CreateNavThrd();
     Gpsfunc->CreateGpsThrd();

     c_thread = new ScreenThread(true, this);
     c_thread->Resume();
 }   
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormDestroy(TObject *Sender)
{
  if(c_thread)
 {
  c_thread->Terminate();
  Sleep(100);
  delete c_thread;
 }

 if( Gpsfunc )
 {
  Gpsfunc->TerminateThrd();
  // wait for the gpsthrd to be terminated
  while(!Gpsfunc->NavThrdIsTermed())
  {
   SetEvent(gpsEvent); //thus no need to wait for 6 secs
   Sleep(100);
  }

  delete Gpsfunc;
 }
 if( Gpsnav )
 {
  Gpsnav->ResumeThrd(); // to terminate this thrd, it must be running
  Sleep(100);
  Gpsnav->TerminateThrd();
  // wait for the navgps thrd to te terminated
  while(!Gpsnav->NavThrdIsTermed())
   Sleep(100);

  delete Gpsnav;
 }

 if( GpsInterface)
 {
  if(handleopened){
   GpsInterface->ThrdTerminate();
   // wait for the smapling thrd to te terminated
   while(!GpsInterface->NavThrdIsTermed())
    Sleep(100);
   GpsInterface->control_gp2021(STOP_TIMER); // stop firmware timer
  }
  delete GpsInterface;
 }

}
//---------------------------------------------------------------------------
void TForm1::update_gpsweek(void)
{
 Gpsfunc->get_gpsweek();
 StatusBar->Panels->Items[1]->Text = "GPS Week: " + IntToStr(GpsWeek);
}

//---------------------------------------------------------------------------
GPS_Func* TForm1::get_gpsfunc_pt(void)
{
 return Gpsfunc;

}
//---------------------------------------------------------------------------
GPS_Nav* TForm1::get_gpsnav_pt(void)
{
 return Gpsnav;
}
IoInterface* TForm1::get_iointerface_pt(void)
{
 return GpsInterface;
}

void __fastcall TForm1::QuitButtonClick(TObject *Sender)
{
 Close();        
}
//---------------------------------------------------------------------------
// read config info from cinfiguration file
void TForm1::read_cfg(FILE* fp)
{
 char oneline[256], *next_charpt, tmp_str[256] ;

 while( (fgets(oneline, 256, fp)) != NULL )
 {
  if( oneline[0] != '#' && oneline[0]!='\n' ) // not comment
  {
   if( next_charpt = strstr(oneline, chn_alt_str) ) // found "ALTCHANNELMAN"
   {
    sscanf(next_charpt, "ALTCHANNELMAN=%s", &tmp_str);
    if( strstr( StrLower(tmp_str),"yes") )
     ch_allocate_manually = true;
    goto READ_END;
   }

   if( next_charpt = strstr(oneline, chprn_str) ) // found "CHPRN"
   {
    sscanf(next_charpt, "CHPRN=%d,%d,%d,%d,%d,%d",
                        prn,prn+1,prn+2,prn+3,prn+4,prn+5);
    goto READ_END;
   }
   if( next_charpt = strstr(oneline, almfile_str) ) // found "ALMFILE"
   {
    sscanf(next_charpt, "ALMFILE=%s",&tmp_str);
    almFileNm = tmp_str;
    goto READ_END;
   }
   if( next_charpt = strstr(oneline, long_str) ) // found "LONGITUDE"
   {
    sscanf(next_charpt, "LONGITUDE=%lg",&llh[0]);
    goto READ_END;
   }
   if( next_charpt = strstr(oneline, lat_str) ) // found "LATITUDE"
   {
    sscanf(next_charpt, "LATITUDE=%lg",&llh[1]);
    goto READ_END;
   }
   if( next_charpt = strstr(oneline, alt_str) ) // found "ALTITUDE"
   {
    sscanf(next_charpt, "ALTITUDE=%lg",&llh[2]);
    goto READ_END;
   }

READ_END:
   oneline[0] = 0;
  }
 } // end of while
}

double TForm1::getlongi(void)
{return llh[0];}

void TForm1::setlongi(double l)
{ llh[0] = l;}

double TForm1::getlati(void)
{return llh[1];}

void TForm1::setlati(double l)
{ llh[1] = l; }

double TForm1::getalti(void)
{return llh[2];}

void TForm1::setalti(double a)
{ llh[2] = a; }

AnsiString TForm1::getalmfilenm(void)
{ return almFileNm;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -