//=================================================================== // vcMinEx - Minimum Example Application // // Minimum C++ Example Application demonstrates spectrum data // acquisition using a minimum of code. // // Copyright (c)2005 Amptek, All Rights Reserved //=================================================================== // vcMinExDlg.cpp : implementation file // #include "stdafx.h" #include #include "vcMinEx.h" #include "vcMinExDlg.h" #include ".\vcMinExdlg.h" #include "DppApi.h" // DPPAPI header file #include ".\DppApi.h" #ifdef _DEBUG #define new DEBUG_NEW #endif //Amptek DPP USB devices #define DPPNONE 0x0 // none #define DPPDP4 0x1 // DP4 #define DPPPX4 0x2 // PX4 #define DPPDP4EMUL 0x3 // DP5 with DP4 Emulation #define DPPPX4EMUL 0x4 // DP5 with PX4 Emulation (Same as DPPDP5) #define DPPDP5 0x4 // DP5 with PX4 Emulation //=================================================================== // 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() //=================================================================== //=================================================================== // CvcMinExDlg dialog class //=================================================================== //------------------------------------------------------------------- // CvcMinExDlg // Constructor initializes the Minimum Example dialog class //------------------------------------------------------------------- CvcMinExDlg::CvcMinExDlg(CWnd* pParent /*=NULL*/) : CDialog(CvcMinExDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); for(int i = 0; i < 8192; i++) { PlotBuffer[i] = 0; //holds y position data for cursor updates } XPlotMAX = 0; YPlotMAX = 0; OptScaleType = false; OptPlotType = false; } //------------------------------------------------------------------- // DoDataExchange // Control data exchange definitions //------------------------------------------------------------------- void CvcMinExDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDC_PLOTPICTURECONTROL, m_PlotPictureControl); } //------------------------------------------------------------------- // Windows Message Mapping //------------------------------------------------------------------- BEGIN_MESSAGE_MAP(CvcMinExDlg, CDialog) //{{AFX_MSG_MAP(CvcMinExDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_GETDATABUTTON, OnBnClickedGetdatabutton) ON_BN_CLICKED(IDC_CONFIG_BUTTON, OnBnClickedConfigButton) ON_BN_CLICKED(IDC_PLOTSCALERADIOLINEAR, OnBnClickedPlotscaleradiolinear) ON_BN_CLICKED(IDC_PLOTSCALERADIOLOG, OnBnClickedPlotscaleradiolog) //}}AFX_MSG_MAP END_MESSAGE_MAP() // CvcMinExDlg message handlers //------------------------------------------------------------------- // OnInitDialog // Initializes dialog controls //------------------------------------------------------------------- BOOL CvcMinExDlg::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); // initialize plot scale type (plot scales false=LINEAR or true=LOG) // OptScaleType is initialized to false (LINEAR) for demonstration purposes if (OptScaleType) { CheckRadioButton(IDC_PLOTSCALERADIOLINEAR, IDC_PLOTSCALERADIOLOG, IDC_PLOTSCALERADIOLOG); } else { CheckRadioButton(IDC_PLOTSCALERADIOLINEAR, IDC_PLOTSCALERADIOLOG, IDC_PLOTSCALERADIOLINEAR); } 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); } } // 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 CvcMinExDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } //------------------------------------------------------------------- // OnPaint // Paints the display when Windows or an application makes a request // to repaint a portion of an application's window // also, // 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 CvcMinExDlg::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 { UpdatePlot(); CDialog::OnPaint(); } } //------------------------------------------------------------------- // OnQueryDragIcon // The system calls this function to obtain the cursor to display while the user drags // the minimized window. //------------------------------------------------------------------- HCURSOR CvcMinExDlg::OnQueryDragIcon() { return static_cast(m_hIcon); } //------------------------------------------------------------------- // OnBnClickedGetdatabutton // Demonstrates a spectrum data acquisition //------------------------------------------------------------------- void CvcMinExDlg::OnBnClickedGetdatabutton() { GetDlgItem(IDC_GETDATABUTTON)->EnableWindow(false); // disable this button until done int numdev; // number of usb devices void * objDppApi; // pointer to DPPAPI char szStatus[2000]; // status text long DataBuffer[8192]; // spectrum data CString cstrStatus; // status display string int NumChan; // number of acquired channels DPP_STATUS StatusLst; // status structure objDppApi = OpenDppApi(); // Create/Open DPPAPI numdev = OpenUSBDevice(objDppApi); // Open USB communications if (numdev > 0) { GetStatusStruct(objDppApi, true, &StatusLst); // get device status structure if (StatusLst.SerialNumber < 1) { GetDlgItem(IDC_STATUS_STATIC)->SetWindowText("Device status error."); } else if (! StatusLst.SwConfigRcvd) { GetDlgItem(IDC_STATUS_STATIC)->SetWindowText("Please configure device before taking data."); } else { if (! StatusLst.StatMcaEnabled) { PauseDppData(objDppApi, false); // enable data acquisition } GetStatusString(objDppApi, 1, szStatus, 2000); // get device status display string cstrStatus = szStatus; GetDlgItem(IDC_STATUS_STATIC)->SetWindowText(cstrStatus); GetConfigFromDpp(objDppApi); // configure px4 from stored configuration NumChan = GetDppData(objDppApi, DataBuffer); // aquire data PlotData(DataBuffer, NumChan); // plot data } } CloseUSBDevice (objDppApi); // close usb CloseDppApi (objDppApi); // close DPPAPI GetDlgItem(IDC_GETDATABUTTON)->EnableWindow(true); } //------------------------------------------------------------------- // PlotData // Calculates and saves the plot buffer data then calls UpdatePlot //------------------------------------------------------------------- void CvcMinExDlg::PlotData(long DataBuffer[8192], int NumChan) { int i; long YMAX; XPlotMAX = NumChan - 1; if (NumChan > 0) { //if number of channels > 0 plot data YMAX = 0; for(i = 0; i < NumChan; i++) { //get the largest value to be displayed if (OptScaleType) { //get the Y maximum value for the select scale if (log(0.1 + DataBuffer[i]) > YMAX) { YMAX = (long)log(0.1 + DataBuffer[i]); } } else { if (DataBuffer[i] > YMAX) { YMAX = DataBuffer[i]; } } } if (YMAX < 1) { return; } //if no y scale exit sub if (OptScaleType) { //adjust plot to include entire high peak YMAX = (YMAX * 10) + 10; } else { if (YMAX < 20) { YMAX = YMAX + 2; } else { YMAX = YMAX + 10; } } for (i = 0; i < NumChan;i++ ) { //get the largest value to be displayed if (OptScaleType) { PlotBuffer[i] = (long)log(0.1 + DataBuffer[i]) * 10; } else { PlotBuffer[i] = DataBuffer[i]; } } YPlotMAX = YMAX; UpdatePlot(); } } //------------------------------------------------------------------- // UpdatePlot // Draws the plot display with the current plot buffer //------------------------------------------------------------------- void CvcMinExDlg::UpdatePlot() { CDC *dc=m_PlotPictureControl.GetDC(); // device context for plot CRect rect; CPen pen,*oldpen; m_PlotPictureControl.GetClientRect(&rect); // get plot client area // Draw background rectangle pen.CreatePen(PS_SOLID,1,RGB(0xFF,0x00,0x00)); // drawing pen color oldpen=dc->SelectObject(&pen); // save old pen to restore CBrush WorkSpaceBrush( RGB(0xFF,0xFF,0xFF) ); // fill brush color CBrush *oldbrush = dc->SelectObject(&WorkSpaceBrush); // save old brush to restore dc->Rectangle(&rect); dc->Draw3dRect(&rect, (COLORREF)GetSysColor(COLOR_3DSHADOW), (COLORREF)GetSysColor(COLOR_3DHILIGHT)); dc->SetViewportOrg(0,rect.Height()); double XScaleFactor = (double) rect.Width(); if (XPlotMAX > 0) XScaleFactor /= (double)(XPlotMAX + 1); double YScaleFactor = (double) rect.Height(); if (YPlotMAX > 0) YScaleFactor /= (double)(YPlotMAX + 1); YScaleFactor *= -1.0; double y1, y2; for(int X=0; X<=XPlotMAX; X++) { y1 = PlotBuffer[X]; if (y1 < 0) { y1 = 0; } y2 = PlotBuffer[X + 1]; if (y2 < 0) { y2 = 0; } dc->LineTo((int)(X * XScaleFactor), (int)(y1 * YScaleFactor)); dc->LineTo((int)((X + 1) * XScaleFactor), (int)(y2 * YScaleFactor)); dc->MoveTo((int)(X * XScaleFactor), 0); dc->LineTo((int)(X * XScaleFactor), (int)(y1 * YScaleFactor)); dc->MoveTo((int)((X + 1) * XScaleFactor), 0); dc->LineTo((int)((X + 1) * XScaleFactor), (int)(y2 * YScaleFactor)); } dc->SelectObject(oldpen); pen.DeleteObject(); dc->SelectObject(oldbrush); WorkSpaceBrush.DeleteObject(); } //------------------------------------------------------------------- // PromptFileName // Creates a "file open" dialog with Amptek configuration file filters //------------------------------------------------------------------- bool CvcMinExDlg::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; } //------------------------------------------------------------------- // OnBnClickedConfigButton // Loads an Amptek DPP configuration file and configures a DPP device //------------------------------------------------------------------- void CvcMinExDlg::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 GetDlgItem(IDC_CONFIG_BUTTON)->EnableWindow(true); } //------------------------------------------------------------------- // OnBnClickedPlotscaleradiolinear // Radio button control selects LINEAR plot scale type //------------------------------------------------------------------- void CvcMinExDlg::OnBnClickedPlotscaleradiolinear() { OptScaleType = false; CheckRadioButton(IDC_PLOTSCALERADIOLINEAR, IDC_PLOTSCALERADIOLOG, IDC_PLOTSCALERADIOLINEAR); } //------------------------------------------------------------------- // OnBnClickedPlotscaleradiolog // Radio button control selects LOG plot scale type //------------------------------------------------------------------- void CvcMinExDlg::OnBnClickedPlotscaleradiolog() { OptScaleType = true; CheckRadioButton(IDC_PLOTSCALERADIOLINEAR, IDC_PLOTSCALERADIOLOG, IDC_PLOTSCALERADIOLOG); }