//=================================================================== // vcDppApi - C++ Plot Example Application // // C++ Plot Example Application demonstrates spectrum data // acquisition using a timer. // // Copyright (c)2005 Amptek, All Rights Reserved //=================================================================== // DppApiCppDlg.cpp : implementation file // #include "stdafx.h" #include #include "DppApiCpp.h" #include "DppApiCppDlg.h" #include ".\dppapicppdlg.h" #include "DppApi.h" #include ".\DppApi.h" #ifdef _DEBUG #define new DEBUG_NEW #endif //=================================================================== // CAboutDlg dialog used for App About Box //=================================================================== class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support // Implementation protected: DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) END_MESSAGE_MAP() //=================================================================== // CDppApiCppDlg dialog class //=================================================================== //------------------------------------------------------------------- // CDppApiCppDlg // Constructor initializes the C++ Example dialog class //------------------------------------------------------------------- CDppApiCppDlg::CDppApiCppDlg(CWnd* pParent /*=NULL*/) : CDialog(CDppApiCppDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_PlotPictureControl.SetCursorPosition(0); //m_PlotPictureControl.ClearTrace(); CursorPosition.x = 0; CursorPosition.y = 0; m_nTimerId = 0; m_nElapse = 0; objDppApi = 0; } //------------------------------------------------------------------- // DoDataExchange // Control data exchange definitions //------------------------------------------------------------------- void CDppApiCppDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDC_PLOTPICTURECONTROL, m_PlotPictureControl); } //------------------------------------------------------------------- // Windows Message Mapping //------------------------------------------------------------------- BEGIN_MESSAGE_MAP(CDppApiCppDlg, CDialog) //{{AFX_MSG_MAP(CDppApiCppDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_STN_CLICKED(IDC_PLOTPICTURECONTROL, OnStnClickedPlotpicturecontrol) ON_WM_LBUTTONDOWN() ON_BN_CLICKED(IDC_CONFIG_BUTTON, OnBnClickedConfigButton) ON_BN_CLICKED(IDC_GETDATABUTTON, OnBnClickedGetdatabutton) ON_BN_CLICKED(IDC_START, OnBnClickedStart) ON_BN_CLICKED(IDC_STOP, OnBnClickedStop) ON_WM_TIMER() ON_BN_CLICKED(IDOK, OnBnClickedOk) ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel) ON_WM_DESTROY() ON_BN_CLICKED(IDC_PLOTSCALERADIOLINEAR, OnBnClickedPlotscaleradiolinear) ON_BN_CLICKED(IDC_PLOTSCALERADIOLOG, OnBnClickedPlotscaleradiolog) ON_BN_CLICKED(IDC_PLOTTYPERADIOSOLID, OnBnClickedPlottyperadiosolid) ON_BN_CLICKED(IDC_PLOTTYPERADIOLINE, OnBnClickedPlottyperadioline) //}}AFX_MSG_MAP END_MESSAGE_MAP() // CDppApiCppDlg message handlers //------------------------------------------------------------------- // OnInitDialog // Initializes dialog controls //------------------------------------------------------------------- BOOL CDppApiCppDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } if (m_PlotPictureControl.OptScaleType) { CheckRadioButton(IDC_PLOTSCALERADIOLINEAR, IDC_PLOTSCALERADIOLOG, IDC_PLOTSCALERADIOLOG); } else { CheckRadioButton(IDC_PLOTSCALERADIOLINEAR, IDC_PLOTSCALERADIOLOG, IDC_PLOTSCALERADIOLINEAR); } if (m_PlotPictureControl.OptPlotType) { CheckRadioButton(IDC_PLOTTYPERADIOSOLID, IDC_PLOTTYPERADIOLINE, IDC_PLOTTYPERADIOLINE); } else { CheckRadioButton(IDC_PLOTTYPERADIOSOLID, IDC_PLOTTYPERADIOLINE, IDC_PLOTTYPERADIOSOLID); } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control } //------------------------------------------------------------------- // OnSysCommand // System command message handler, calls AboutBox from system menu //------------------------------------------------------------------- void CDppApiCppDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } //------------------------------------------------------------------- // OnPaint // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. //------------------------------------------------------------------- void CDppApiCppDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } //------------------------------------------------------------------- // OnQueryDragIcon // The system calls this function to obtain the cursor to display while the user drags // the minimized window. //------------------------------------------------------------------- HCURSOR CDppApiCppDlg::OnQueryDragIcon() { return static_cast(m_hIcon); } //------------------------------------------------------------------- // OnStnClickedPlotpicturecontrol // Moves cursor //------------------------------------------------------------------- void CDppApiCppDlg::OnStnClickedPlotpicturecontrol() { m_PlotPictureControl.SetCursorPosition(CursorPosition.x); CString strCursorPos; strCursorPos.Format("%d",CursorPosition.x); AfxMessageBox(strCursorPos); m_PlotPictureControl.ClearTrace(); } //------------------------------------------------------------------- // OnLButtonDown // Moves cursor //------------------------------------------------------------------- void CDppApiCppDlg::OnLButtonDown(UINT nFlags, CPoint point) { CursorPosition = point; m_PlotPictureControl.SetCursorPosition(CursorPosition.x); CString strCursorPos; strCursorPos.Format("%d",CursorPosition.x); m_PlotPictureControl.ClearTrace(); CDialog::OnLButtonDown(nFlags, point); } //------------------------------------------------------------------- // OnBnClickedConfigButton // Loads an Amptek DPP configuration file and configures a DPP device //------------------------------------------------------------------- void CDppApiCppDlg::OnBnClickedConfigButton() { int numdev; // number of usb devices void * objDppApi; // pointer to dpp api CString cstrStatus; // status display string long FilenameLen; // byte DppDevice; byte indDPP5; DPP_STATUS StatusLst; BOOLEAN Dpp80MHzMode; GetDlgItem(IDC_CONFIG_BUTTON)->EnableWindow(false); // disable button until done objDppApi = OpenDppApi(); // Create/Open DPPAPI numdev = OpenUSBDevice(objDppApi); // Open USB communications if (numdev > 0) { GetStatusStruct(objDppApi, true, &StatusLst); // get device status cstrStatus.Format("(s/n %7i)",StatusLst.SerialNumber); if (StatusLst.SerialNumber < 1) { // check if device status can be read GetDlgItem(IDC_STATUS_STATIC)->SetWindowText("Device status error."); } else { // device is communicating, get the config file if(PromptFileName()) { // get the configuration filename, if any config dpp DppDevice = StatusLst.StatDevInd; // determine device type (0=dp4,1=px4) indDPP5 = (byte)(StatusLst.Firmware >= 5.0); //DPP5 20071023 indicates DPP5 type device DppDevice = ((indDPP5 * 2) + (StatusLst.StatDevInd * 1)) + 1; // //Run GetStatusStruct or GetStatusString before running Get80MHzMode // Dpp80MHzMode = (BOOLEAN) Get80MHzMode(objDppApi); SetFPGAClockDefault (objDppApi, Dpp80MHzMode, DppDevice); FilenameLen = (long)m_csCurrentFile.GetLength(); // get configuration filename buffer length GetConfigFromFile(objDppApi, m_csCurrentFile, FilenameLen, DppDevice); // load cfg into dppapi SendConfigToDpp(objDppApi); // send cfg to dpp GetDlgItem(IDC_STATUS_STATIC)->SetWindowText("Configuration loaded from file and sent to device. " + cstrStatus); } else { GetDlgItem(IDC_STATUS_STATIC)->SetWindowText("No configuration file selected."); } } } else { GetDlgItem(IDC_STATUS_STATIC)->SetWindowText("No devices detected."); } CloseUSBDevice (objDppApi); // close usb CloseDppApi (objDppApi); // close DPPAPI Sleep(750); GetDlgItem(IDC_CONFIG_BUTTON)->EnableWindow(true); } //------------------------------------------------------------------- // PromptFileName // Creates a "file open" dialog with Amptek configuration file filters //------------------------------------------------------------------- bool CDppApiCppDlg::PromptFileName() { CString csDfltName; CString csFilter; // file extension filter CString csDfltExt; // default extension CString csCfgDisplayStr; // holds copy of last file read for display bool bOpen = true; // open dialog csFilter = "DP4 PX4 Setup Files (*.cfg)|*.cfg|Text Files (*.txt)|*.txt|All Files (*.*)|*.*||"; csDfltName = "DPPCFG"; csDfltExt = "cfg"; bool bRes(FALSE); CFileDialog dlg(bOpen, csDfltExt, csDfltName, OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_NONETWORKBUTTON | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY, csFilter); INT_PTR iResult = dlg.DoModal(); if(iResult == IDOK) { bRes = TRUE; m_csCurrentFile = dlg.GetPathName(); } return iResult == IDOK; } //------------------------------------------------------------------- // OnBnClickedGetdatabutton // Acquires the current spectrum data (one time only) //------------------------------------------------------------------- void CDppApiCppDlg::OnBnClickedGetdatabutton() { EnableButtons(FALSE); // disable buttons until done int numdev; // number of usb devices void * objDppApi; // pointer to dpp api char szStatus[2000]; // status text long DataBuffer[8192]; // spectrum data CString cstrStatus; // status display string int NumChan; // number of acquired channels DPP_STATUS StatusLst; objDppApi = OpenDppApi(); // Create/Open DPPAPI numdev = OpenUSBDevice(objDppApi); // Open USB communications if (numdev > 0) { GetStatusStruct(objDppApi, true, &StatusLst); // get device status (part 1) if (StatusLst.SerialNumber < 1) { GetDlgItem(IDC_STATUS_STATIC)->SetWindowText("Device status error."); OnBnClickedStop(); } else if (! StatusLst.SwConfigRcvd) { GetDlgItem(IDC_STATUS_STATIC)->SetWindowText("Please configure device before taking data."); OnBnClickedStop(); } else { if (! StatusLst.StatMcaEnabled) { PauseDppData(objDppApi, false); // enable data acquisition } GetStatusString(objDppApi, 1, szStatus, 2000); // get device status cstrStatus = szStatus; GetDlgItem(IDC_STATUS_STATIC)->SetWindowText(cstrStatus); GetConfigFromDpp(objDppApi); // configure px4 from stored configuration NumChan = GetDppData(objDppApi, DataBuffer); // acquire data m_PlotPictureControl.DataAcq(DataBuffer, NumChan); // plot data } } CloseUSBDevice (objDppApi); // close usb CloseDppApi (objDppApi); // close DPPAPI Sleep(200); EnableButtons(TRUE); // disable buttons until done GetDlgItem(IDC_STOP)->EnableWindow(false); } //------------------------------------------------------------------- // OnBnClickedStart // Starts and runs a data acquisition using a timer for data requests //------------------------------------------------------------------- void CDppApiCppDlg::OnBnClickedStart() { int numdev; // number of usb devices DPP_STATUS StatusLst; EnableButtons(FALSE); GetDlgItem(IDC_STOP)->EnableWindow(TRUE); objDppApi = OpenDppApi(); // Create/Open DPPAPI numdev = OpenUSBDevice(objDppApi); // Open USB communications if (numdev > 0) { GetStatusStruct(objDppApi, true, &StatusLst); // get device status (part 1) if (StatusLst.SerialNumber < 1) { GetDlgItem(IDC_STATUS_STATIC)->SetWindowText("Device status error."); OnBnClickedStop(); } else if (! StatusLst.SwConfigRcvd) { GetDlgItem(IDC_STATUS_STATIC)->SetWindowText("Please configure device before taking data."); OnBnClickedStop(); } else { GetDlgItem(IDC_GETDATABUTTON)->EnableWindow(FALSE); GetConfigFromDpp(objDppApi); // configure px4 from stored configuration if (! StatusLst.StatMcaEnabled) { PauseDppData(objDppApi, false); // enable data acquisition if necessary } m_nEventId = 1; m_nElapse = 1000; m_nTimerId = SetTimer(m_nEventId, m_nElapse, NULL); // start the data acquisition timer } } else { GetDlgItem(IDC_STATUS_STATIC)->SetWindowText("No device detected."); OnBnClickedStop(); } } //------------------------------------------------------------------- // EnableButtons // Enables/disables the dialog buttons //------------------------------------------------------------------- void CDppApiCppDlg::EnableButtons(BOOL bEnable) { GetDlgItem(IDC_CONFIG_BUTTON)->EnableWindow(bEnable); GetDlgItem(IDC_GETDATABUTTON)->EnableWindow(bEnable); GetDlgItem(IDC_START)->EnableWindow(bEnable); GetDlgItem(IDC_STOP)->EnableWindow(bEnable); } //------------------------------------------------------------------- // OnBnClickedStop // Stops the current data acquisition //------------------------------------------------------------------- void CDppApiCppDlg::OnBnClickedStop() { CString cstrStatusWin; GetDlgItem(IDC_STATUS_STATIC)->GetWindowText(cstrStatusWin); cstrStatusWin += "Data Stop called."; GetDlgItem(IDC_STATUS_STATIC)->SetWindowText(cstrStatusWin); if (m_nTimerId != 0) { //timer with WM_TIMER/eventid KillTimer(m_nEventId); m_nTimerId = 0; if (objDppApi != 0) { CloseUSBDevice (objDppApi); // close usb CloseDppApi (objDppApi); // close DPPAPI Sleep(750); objDppApi = 0; } } EnableButtons(TRUE); GetDlgItem(IDC_STOP)->EnableWindow(FALSE); } //------------------------------------------------------------------- // OnTimer // Data acquisition timer, calls for spectrum data when timer fires //------------------------------------------------------------------- void CDppApiCppDlg::OnTimer(UINT nIDEvent) { int numdev; // number of usb devices char szStatus[2000]; // status text long DataBuffer[8192]; // spectrum data CString cstrStatus; // status display string int NumChan; // number of acquired channels DPP_STATUS StatusLst; // holds dpp status in a structure if (nIDEvent == 1) { numdev = MonitorUSBDppDevices(objDppApi); // check for the number of devices if (numdev > 0) { // proceed if a device is attached GetStatusStruct(objDppApi, true, &StatusLst); // get device status structure if (StatusLst.SerialNumber < 1) { // if no serial number device is not ready GetDlgItem(IDC_STATUS_STATIC)->SetWindowText("Device status error."); } else if (! StatusLst.SwConfigRcvd) { // if dpp not configured display message GetDlgItem(IDC_STATUS_STATIC)->SetWindowText("Please configure device before taking data."); } else { if (! StatusLst.StatMcaEnabled) { // check if mca is enabled PauseDppData(objDppApi, false); // enable data acquisition } GetStatusString(objDppApi, 1, szStatus, 2000); // get device status display string cstrStatus = szStatus; GetDlgItem(IDC_STATUS_STATIC)->SetWindowText(cstrStatus); NumChan = GetDppData(objDppApi, DataBuffer); // aquire data m_PlotPictureControl.DataAcq(DataBuffer, NumChan); // plot data } } else { GetDlgItem(IDC_STATUS_STATIC)->SetWindowText("no USB devices detected"); } } CDialog::OnTimer(nIDEvent); } //------------------------------------------------------------------- // OnBnClickedOk // WM_OK message handler (dialog shutdown) //------------------------------------------------------------------- void CDppApiCppDlg::OnBnClickedOk() { OnQuitDuringAcq(); if (m_nTimerId != 0) { //timer with WM_TIMER/eventid KillTimer(m_nEventId); m_nTimerId = 0; } OnOK(); } //------------------------------------------------------------------- // OnBnClickedCancel // WM_CANCEL message handler (dialog shutdown) //------------------------------------------------------------------- void CDppApiCppDlg::OnBnClickedCancel() { OnQuitDuringAcq(); if (m_nTimerId != 0) { //timer with WM_TIMER/eventid KillTimer(m_nEventId); m_nTimerId = 0; } OnCancel(); } //------------------------------------------------------------------- // OnDestroy // WM_DESTROY message handler (dialog shutdown) //------------------------------------------------------------------- void CDppApiCppDlg::OnDestroy() { CDialog::OnDestroy(); OnQuitDuringAcq(); if (m_nTimerId != 0) { //timer with WM_TIMER/eventid KillTimer(m_nEventId); m_nTimerId = 0; } } //------------------------------------------------------------------- // OnQuitDuringAcq // DPPAPI Shutdown handler (dialog shutdown) //------------------------------------------------------------------- void CDppApiCppDlg::OnQuitDuringAcq() { if (GetDlgItem(IDC_STOP)->IsWindowEnabled()) { GetDlgItem(IDC_STATUS_STATIC)->SetWindowText("Please wait...\nShutting down...\n"); OnBnClickedStop(); } } //------------------------------------------------------------------- // OnBnClickedPlotscaleradiolinear // Selects linear plot scale //------------------------------------------------------------------- void CDppApiCppDlg::OnBnClickedPlotscaleradiolinear() { m_PlotPictureControl.OptScaleType = false; CheckRadioButton(IDC_PLOTSCALERADIOLINEAR, IDC_PLOTSCALERADIOLOG, IDC_PLOTSCALERADIOLINEAR); } //------------------------------------------------------------------- // OnBnClickedPlotscaleradiolog // Selects log plot scale //------------------------------------------------------------------- void CDppApiCppDlg::OnBnClickedPlotscaleradiolog() { m_PlotPictureControl.OptScaleType = true; CheckRadioButton(IDC_PLOTSCALERADIOLINEAR, IDC_PLOTSCALERADIOLOG, IDC_PLOTSCALERADIOLOG); } //------------------------------------------------------------------- // OnBnClickedPlottyperadiosolid // Selects solid (bar) plot type //------------------------------------------------------------------- void CDppApiCppDlg::OnBnClickedPlottyperadiosolid() { m_PlotPictureControl.OptPlotType = false; CheckRadioButton(IDC_PLOTTYPERADIOSOLID, IDC_PLOTTYPERADIOLINE, IDC_PLOTTYPERADIOSOLID); } //------------------------------------------------------------------- // OnBnClickedPlottyperadioline // Selects line plot type //------------------------------------------------------------------- void CDppApiCppDlg::OnBnClickedPlottyperadioline() { m_PlotPictureControl.OptPlotType = true; CheckRadioButton(IDC_PLOTTYPERADIOSOLID, IDC_PLOTTYPERADIOLINE, IDC_PLOTTYPERADIOLINE); }