gccDppConsole Test C++ SDK
1.0.0.0
DPP C++ Console Demonstration
|
00001 00005 // gccDppConsole.cpp : Defines the entry point for the console application. 00006 #include <iostream> 00007 using namespace std; 00008 #include "ConsoleHelper.h" 00009 #include "stringex.h" 00010 00011 #ifdef _WIN32 00012 #include <windows.h> 00013 #include <conio.h> 00014 #define CLEAR_TERM "cls" 00015 #else 00016 #include <unistd.h> 00017 #define Sleep(x) usleep((x)*1000) 00018 #define _getch getchar 00019 #define CLEAR_TERM "clear" 00020 #endif 00021 00022 CConsoleHelper chdpp; // DPP communications functions 00023 bool bRunSpectrumTest = false; // run spectrum test 00024 bool bRunConfigurationTest = false; // run configuration test 00025 bool bHaveStatusResponse = false; // have status response 00026 bool bHaveConfigFromHW = false; // have configuration from hardware 00027 00028 // connect to default dpp 00029 // CConsoleHelper::LibUsb_Connect_Default_DPP // LibUsb connect to default DPP 00030 void ConnectToDefaultDPP() 00031 { 00032 cout << endl; 00033 cout << "Running DPP LibUsb tests from console..." << endl; 00034 cout << endl; 00035 cout << "\tConnecting to default LibUsb device..." << endl; 00036 if (chdpp.LibUsb_Connect_Default_DPP()) { 00037 cout << "\t\tLibUsb DPP device connected." << endl; 00038 cout << "\t\tLibUsb DPP devices present: " << chdpp.LibUsb_NumDevices << endl; 00039 } else { 00040 cout << "\t\tLibUsb DPP device not connected." << endl; 00041 cout << "\t\tNo LibUsb DPP device present." << endl; 00042 } 00043 } 00044 00045 // Get DPP Status 00046 // CConsoleHelper::LibUsb_isConnected // check if DPP is connected 00047 // CConsoleHelper::LibUsb_SendCommand(XMTPT_SEND_STATUS) // request status 00048 // CConsoleHelper::LibUsb_ReceiveData() // parse the status 00049 // CConsoleHelper::DppStatusString // display status string 00050 void GetDppStatus() 00051 { 00052 if (chdpp.LibUsb_isConnected) { // send and receive status 00053 cout << endl; 00054 cout << "\tRequesting Status..." << endl; 00055 if (chdpp.LibUsb_SendCommand(XMTPT_SEND_STATUS)) { // request status 00056 cout << "\t\tStatus sent." << endl; 00057 cout << "\t\tReceiving status..." << endl; 00058 if (chdpp.LibUsb_ReceiveData()) { 00059 cout << "\t\t\tStatus received..." << endl; 00060 cout << chdpp.DppStatusString << endl; 00061 bRunSpectrumTest = true; 00062 bHaveStatusResponse = true; 00063 bRunConfigurationTest = true; 00064 } else { 00065 cout << "\t\tError receiving status." << endl; 00066 } 00067 } else { 00068 cout << "\t\tError sending status." << endl; 00069 } 00070 } 00071 } 00072 00073 // Read Full DPP Configuration From Hardware // request status before sending/receiving configurations 00074 // CONFIG_OPTIONS // holds configuration command options 00075 // CConsoleHelper::CreateConfigOptions // creates configuration options from last status read 00076 // CConsoleHelper::ClearConfigReadFormatFlags(); // clear configuration format flags, for cfg readback 00077 // CConsoleHelper::CfgReadBack = true; // requesting general readback format 00078 // CConsoleHelper::LibUsb_SendCommand_Config // send command with options 00079 // CConsoleHelper::LibUsb_ReceiveData() // parse the configuration 00080 // CConsoleHelper::HwCfgReady // config is ready 00081 void ReadDppConfigurationFromHardware(bool bDisplayCfg) 00082 { 00083 CONFIG_OPTIONS CfgOptions; 00084 if (bHaveStatusResponse && bRunConfigurationTest) { 00085 //test configuration functions 00086 // Set options for XMTPT_FULL_READ_CONFIG_PACKET 00087 chdpp.CreateConfigOptions(&CfgOptions, "", chdpp.DP5Stat, false); 00088 cout << endl; 00089 cout << "\tRequesting Full Configuration..." << endl; 00090 chdpp.ClearConfigReadFormatFlags(); // clear all flags, set flags only for specific readback properties 00091 //chdpp.DisplayCfg = false; // DisplayCfg format overrides general readback format 00092 chdpp.CfgReadBack = true; // requesting general readback format 00093 if (chdpp.LibUsb_SendCommand_Config(XMTPT_FULL_READ_CONFIG_PACKET, CfgOptions)) { // request full configuration 00094 if (chdpp.LibUsb_ReceiveData()) { 00095 if (chdpp.HwCfgReady) { // config is ready 00096 bHaveConfigFromHW = true; 00097 if (bDisplayCfg) { 00098 cout << "\t\t\tConfiguration Length: " << (unsigned int)chdpp.HwCfgDP5.length() << endl; 00099 cout << "\t================================================================" << endl; 00100 cout << chdpp.HwCfgDP5 << endl; 00101 cout << "\t================================================================" << endl; 00102 cout << "\t\t\tScroll up to see configuration settings." << endl; 00103 cout << "\t================================================================" << endl; 00104 } else { 00105 cout << "\t\tFull configuration received." << endl; 00106 } 00107 } 00108 } 00109 } 00110 } 00111 } 00112 00113 // Display Preset Settings 00114 // CConsoleHelper::strPresetCmd // preset mode 00115 // CConsoleHelper::strPresetVal // preset setting 00116 void DisplayPresets() 00117 { 00118 if (bHaveConfigFromHW) { 00119 cout << "\t\t\tPreset Mode: " << chdpp.strPresetCmd << endl; 00120 cout << "\t\t\tPreset Settings: " << chdpp.strPresetVal << endl; 00121 } 00122 } 00123 00124 // Display Preset Settings 00125 // CONFIG_OPTIONS // holds configuration command options 00126 // CConsoleHelper::CreateConfigOptions // creates configuration options from last status read 00127 // CConsoleHelper::HwCfgDP5Out // preset setting 00128 // CConsoleHelper::LibUsb_SendCommand_Config // send command with options 00129 void SendPresetAcquisitionTime(string strPRET) 00130 { 00131 CONFIG_OPTIONS CfgOptions; 00132 cout << "\tSetting Preset Acquisition Time..." << strPRET << endl; 00133 chdpp.CreateConfigOptions(&CfgOptions, "", chdpp.DP5Stat, false); 00134 CfgOptions.HwCfgDP5Out = strPRET; 00135 // send PresetAcquisitionTime string, bypass any filters, read back the mode and settings 00136 if (chdpp.LibUsb_SendCommand_Config(XMTPT_SEND_CONFIG_PACKET_EX, CfgOptions)) { 00137 ReadDppConfigurationFromHardware(false); // read setting back 00138 DisplayPresets(); // display new presets 00139 } else { 00140 cout << "\t\tPreset Acquisition Time NOT SET" << strPRET << endl; 00141 } 00142 } 00143 00144 // Acquire Spectrum 00145 // CConsoleHelper::LibUsb_SendCommand(XMTPT_DISABLE_MCA_MCS) //disable for data/status clear 00146 // CConsoleHelper::LibUsb_SendCommand(XMTPT_SEND_CLEAR_SPECTRUM_STATUS) //clear spectrum/status 00147 // CConsoleHelper::LibUsb_SendCommand(XMTPT_ENABLE_MCA_MCS); // enabling MCA for spectrum acquisition 00148 // CConsoleHelper::LibUsb_SendCommand(XMTPT_SEND_SPECTRUM_STATUS)) // request spectrum+status 00149 // CConsoleHelper::LibUsb_ReceiveData() // process spectrum and data 00150 // CConsoleHelper::ConsoleGraph() (low resolution display) // graph data on console with status 00151 // CConsoleHelper::LibUsb_SendCommand(XMTPT_DISABLE_MCA_MCS) // disable mca after acquisition 00152 void AcquireSpectrum() 00153 { 00154 int MaxMCA = 11; 00155 bool bDisableMCA; 00156 00157 //bRunSpectrumTest = false; // disable test 00158 if (bRunSpectrumTest) { 00159 cout << "\tRunning spectrum test..." << endl; 00160 cout << "\t\tDisabling MCA for spectrum data/status clear." << endl; 00161 chdpp.LibUsb_SendCommand(XMTPT_DISABLE_MCA_MCS); 00162 Sleep(1000); 00163 cout << "\t\tClearing spectrum data/status." << endl; 00164 chdpp.LibUsb_SendCommand(XMTPT_SEND_CLEAR_SPECTRUM_STATUS); 00165 Sleep(1000); 00166 cout << "\t\tEnabling MCA for spectrum data acquisition with status ." << endl; 00167 chdpp.LibUsb_SendCommand(XMTPT_ENABLE_MCA_MCS); 00168 Sleep(1000); 00169 for(int idxSpectrum=0;idxSpectrum<MaxMCA;idxSpectrum++) { 00170 //cout << "\t\tAcquiring spectrum data set " << (idxSpectrum+1) << " of " << MaxMCA << endl; 00171 if (chdpp.LibUsb_SendCommand(XMTPT_SEND_SPECTRUM_STATUS)) { // request spectrum+status 00172 if (chdpp.LibUsb_ReceiveData()) { 00173 bDisableMCA = true; // we are aquiring data, disable mca when done 00174 system(CLEAR_TERM); 00175 chdpp.ConsoleGraph(chdpp.DP5Proto.SPECTRUM.DATA,chdpp.DP5Proto.SPECTRUM.CHANNELS,true,chdpp.DppStatusString); 00176 Sleep(2000); 00177 } 00178 } else { 00179 cout << "\t\tProblem acquiring spectrum." << endl; 00180 break; 00181 } 00182 } 00183 if (bDisableMCA) { 00185 //cout << "\t\tSpectrum acquisition with status done. Disabling MCA." << endl; 00186 chdpp.LibUsb_SendCommand(XMTPT_DISABLE_MCA_MCS); 00187 Sleep(1000); 00188 } 00189 } 00190 } 00191 00192 // Read Configuration File 00193 // CConsoleHelper::SndCmd.GetDP5CfgStr("PX5_Console_Test.txt"); 00194 void ReadConfigFile() 00195 { 00196 std::string strCfg; 00197 strCfg = chdpp.SndCmd.GetDP5CfgStr("PX5_Console_Test.txt"); 00198 cout << "\t\t\tConfiguration Length: " << (unsigned int)strCfg.length() << endl; 00199 cout << "\t================================================================" << endl; 00200 cout << strCfg << endl; 00201 cout << "\t================================================================" << endl; 00202 } 00203 00204 // Close Connection 00205 // CConsoleHelper::LibUsb_isConnected // LibUsb DPP connection indicator 00206 // CConsoleHelper::LibUsb_Close_Connection() // close connection 00207 void CloseConnection() 00208 { 00209 if (chdpp.LibUsb_isConnected) { // send and receive status 00210 cout << endl; 00211 cout << "\tClosing connection to default LibUsb device..." << endl; 00212 chdpp.LibUsb_Close_Connection(); 00213 cout << "\t\tDPP device connection closed." << endl; 00214 } 00215 } 00216 00217 int main(int argc, char* argv[]) 00218 { 00219 system(CLEAR_TERM); 00220 ConnectToDefaultDPP(); 00221 cout << "Press the Enter key to continue . . ."; 00222 _getch(); 00223 00224 system(CLEAR_TERM); 00225 GetDppStatus(); 00226 cout << "Press the Enter key to continue . . ."; 00227 _getch(); 00228 00229 system(CLEAR_TERM); 00230 ReadDppConfigurationFromHardware(true); 00231 cout << "Press the Enter key to continue . . ."; 00232 _getch(); 00233 00234 system(CLEAR_TERM); 00235 DisplayPresets(); 00236 cout << "Press the Enter key to continue . . ."; 00237 _getch(); 00238 00239 system(CLEAR_TERM); 00240 SendPresetAcquisitionTime("PRET=20;"); 00241 cout << "Press the Enter key to continue . . ."; 00242 _getch(); 00243 00244 system(CLEAR_TERM); 00245 AcquireSpectrum(); 00246 cout << "Press the Enter key to continue . . ."; 00247 _getch(); 00248 00249 system(CLEAR_TERM); 00250 SendPresetAcquisitionTime("PRET=OFF;"); 00251 cout << "Press the Enter key to continue . . ."; 00252 _getch(); 00253 00254 system(CLEAR_TERM); 00255 ReadConfigFile(); 00256 cout << "Press the Enter key to continue . . ."; 00257 _getch(); 00258 00259 system(CLEAR_TERM); 00260 CloseConnection(); 00261 cout << "Press the Enter key to continue . . ."; 00262 _getch(); 00263 00264 return 0; 00265 } 00266 00267 00268 00269 00270 00271 00272 00273 00274 00275 00276 00277 00278 00279 00280 00281 00282 00283 00284 00285 00286