// 9222WavDlg.cpp : implementation file // #include "stdafx.h" #include "9222Wav.h" #include "9222WavDlg.h" #include "math.h" #ifndef M_PI #define M_PI 3.14159265359 #endif #ifdef _DEBUG #define new DEBUG_NEW #endif CRect plotRect; #define PCI9222_DIO_STATUS_TIMER 1 U16 DoBuf[10240]; BOOLEAN bOutStopped; ULONG out_count; // CAboutDlg dialog used for App About 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() // C9222WavDlg dialog C9222WavDlg::C9222WavDlg(I16 hCard, CWnd* pParent /*=NULL*/) : CDialog(C9222WavDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_hCard = hCard; OnBrush = OffBrush = NULL; n_PlotWidth = -1; n_PlotHeight = -1; min_val = 0x00; max_val = 0xFFFF; m_fSampleRate = 4000.0; // 100KHz m_dwDISamples = 8000; m_pDIAddr1 = NULL; m_pDIAddr2 = NULL; m_nTimer = -1; m_dwOverrunCnt = 0; for (int i=0; i<10240; i++) DoBuf[i] = (U16)(sin((double)i/1024*M_PI)*0x00007FFF)+0x00008000; } void C9222WavDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(C9222WavDlg, CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP ON_WM_DESTROY() ON_BN_CLICKED(IDC_BT_START, &C9222WavDlg::OnStart) ON_BN_CLICKED(IDC_BT_STOP, &C9222WavDlg::OnStop) ON_WM_TIMER() ON_WM_VSCROLL() END_MESSAGE_MAP() // C9222WavDlg message handlers BOOL C9222WavDlg::OnInitDialog() { CString strTemp; 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) { BOOL bNameValid; CString strAboutMenu; bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX); ASSERT(bNameValid); 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 x_divider = 5; y_divider = 2; OnBrush = CreateSolidBrush( RGB(255,0,0) ); OffBrush = CreateSolidBrush( RGB(125,0,0) ); RedPen.CreatePen( PS_SOLID, 1, RGB(255,0,0)); GreenPen.CreatePen( PS_SOLID, 1, RGB(0,255,0)); if( n_PlotWidth == -1 && n_PlotHeight == -1 ) { ((CWnd*)GetDlgItem(IDC_STATIC_DRAW))->GetWindowRect(&plotRect); ScreenToClient(&plotRect); n_PlotWidth = min( plotRect.Width(), 512); n_PlotHeight = plotRect.Height() - 10; } // set the default SampleRate SetDlgItemInt( IDC_EDIT_SAMPLE_RATE, (UINT)(m_fSampleRate/1000) ); // Set the default DI Sample SetDlgItemInt( IDC_EDIT_SAMPLE, (UINT)(m_dwDISamples) ); return TRUE; // return TRUE unless you set the focus to a control } void C9222WavDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // 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 C9222WavDlg::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 { redraw_display_area(); CDialog::OnPaint(); } } // The system calls this function to obtain the cursor to display while the user drags // the minimized window. HCURSOR C9222WavDlg::OnQueryDragIcon() { return static_cast(m_hIcon); } void C9222WavDlg::OnDestroy() { U32 count; CDialog::OnDestroy(); // TODO: Add your message handler code here if( OnBrush != NULL ) ::DeleteObject( OnBrush ); if( OffBrush != NULL ) ::DeleteObject( OffBrush ); RedPen.DeleteObject(); GreenPen.DeleteObject(); if( m_nTimer != -1 ) { KillTimer( m_nTimer ); m_nTimer = -1; } if( m_pDIAddr1 != NULL ) { free( m_pDIAddr1 ); m_pDIAddr1 = NULL; } if( m_pDIAddr2 != NULL ) { free( m_pDIAddr2 ); m_pDIAddr2 = NULL; } DI_AsyncClear( m_hCard, &count ); DO_AsyncClear( m_hCard, &count ); } void C9222WavDlg::OnStart() { // TODO: Add your control notification handler code here U32 dwMemSize, dwDISamples; void* pMem; I16 err; CString strMsg; dwDISamples = GetDlgItemInt( IDC_EDIT_SAMPLE ); if( dwDISamples != m_dwDISamples ) { if( m_pDIAddr1 != NULL ) { free( m_pDIAddr1 ); m_pDIAddr1 = NULL; } if( m_pDIAddr2 != NULL ) { free( m_pDIAddr2 ); m_pDIAddr2 = NULL; } m_dwDISamples = dwDISamples; } dwMemSize = m_dwDISamples * sizeof(U16); pMem = malloc( dwMemSize ); if( pMem == NULL ) { AfxMessageBox( TEXT("Memory allocated failed") ); return; } else { m_pDIAddr1 = (U16*) pMem; memset( m_pDIAddr1, '\0', dwMemSize ); } pMem = malloc( dwMemSize ); if( pMem == NULL ) { AfxMessageBox( TEXT("Memory allocated failed") ); return; } else { m_pDIAddr2 = (U16*) pMem; memset( m_pDIAddr2, '\0', dwMemSize ); } if ((err=DO_9222_Config(m_hCard, P922x_DO_CONVSRC_INT, (P922x_DO_TRGMOD_DELAY|P922x_DO_TRGSRC_GPI0), 0, 40000000, 0, 1)) != NoError) { strMsg.Format( TEXT("DO_9222_Config Error = %d") ,err ); AfxMessageBox( strMsg ); return; } if ((err = DO_ContBufferSetup(m_hCard, DoBuf, 5120/*10240*/, &m_wDOBufferId)) != NoError) { strMsg.Format( TEXT("DO_ContBufferSetup Error = %d") ,err ); AfxMessageBox( strMsg ); return; } if ((err=DI_9222_Config(m_hCard, P922x_DI_CONVSRC_INT, (P922x_DI_TRGMOD_POST|P922x_DI_TRGSRC_SOFT), 0, 1)) != NoError) { strMsg.Format( TEXT("DI_9222_Config Error = %d") ,err ); AfxMessageBox( strMsg ); return; } if ((err=DI_AsyncDblBufferMode (m_hCard, 1)) != NoError) { strMsg.Format( TEXT("DI_AsyncDblBufferMode Error = %d\n"), err ); AfxMessageBox( strMsg ); return; } /*Setup Buffer for DI DMA Transfer*/ if ((err=DI_ContBufferSetup(m_hCard, m_pDIAddr1, m_dwDISamples, &m_wDIBufId1)) != NoError) { strMsg.Format( TEXT("DI_ContBufferSetup Error = %d") ,err ); AfxMessageBox( strMsg ); return; } /*Setup Buffer for DI DMA Transfer*/ if ((err=DI_ContBufferSetup(m_hCard, m_pDIAddr2, m_dwDISamples, &m_wDIBufId2)) != NoError) { strMsg.Format( TEXT("DI_ContBufferSetup Error = %d") ,err ); AfxMessageBox( strMsg ); return; } // clear display area redraw_display_area(); m_bView = 0; if ((err = DI_ContReadPort(m_hCard, 0, &m_wDIBufId1, m_dwDISamples, m_fSampleRate, ASYNCH_OP)) != NoError) { DI_ContBufferReset(m_hCard); strMsg.Format( TEXT("DI_ContReadPort Error = %d\n"), err); AfxMessageBox( strMsg ); return; } if ((err = DO_ContWritePort(m_hCard, 0, &m_wDOBufferId, 5120/*10240*/, 1, m_fSampleRate, ASYNCH_OP)) != NoError) { DO_ContBufferReset(m_hCard); strMsg.Format( TEXT("DO_ContWritePort Error = %d") ,err ); AfxMessageBox( strMsg ); return; } if( m_nTimer == -1 ) { m_nTimer = SetTimer( PCI9222_DIO_STATUS_TIMER, 20, NULL ); } GetDlgItem( IDC_BT_START )->EnableWindow( FALSE ); GetDlgItem( IDC_BT_STOP )->EnableWindow( TRUE ); } void C9222WavDlg::OnStop() { // TODO: Add your control notification handler code here U32 count; DI_AsyncClear(m_hCard, &count); DI_ContBufferReset(m_hCard); DO_AsyncClear(m_hCard, &count); DO_ContBufferReset( m_hCard ); GetDlgItem( IDC_BT_START )->EnableWindow( TRUE ); GetDlgItem( IDC_BT_STOP )->EnableWindow( FALSE); } void C9222WavDlg::OnTimer(UINT_PTR nIDEvent) { // TODO: Add your message handler code here and/or call default BOOLEAN HalfReady; U16 OverrunFlag; I16 err; err = DI_AsyncDblBufferHalfReady(m_hCard, &HalfReady); if( HalfReady ) // half-buffer ready { if(m_bView == 0) { display_buffer_data( m_pDIAddr1 ); m_bView = 1; } else { display_buffer_data( m_pDIAddr2 ); m_bView= 0; } DI_AsyncDblBufferHandled( m_hCard ); DI_AsyncDblBufferOverrun( m_hCard, 0, &OverrunFlag); if(OverrunFlag) { m_dwOverrunCnt++; DI_AsyncDblBufferOverrun( m_hCard, 1, NULL ); // SetDlgItemInt( IDC_EDIT_OVERRUN, (UINT)(m_dwOverrunCnt) ); } } CDialog::OnTimer(nIDEvent); } void C9222WavDlg::display_buffer_data(U16* pBuf) { CClientDC dc_plot(this); int nDraw_Bottom; int newY, oldY = -1; int newX, oldX = -1; U32 index; U16 di_data; // redraw the background dc_plot.FillSolidRect( plotRect, RGB(0,0,0) ); dc_plot.SelectObject( &GreenPen ); nDraw_Bottom = ( plotRect.bottom - 5 ); for( index=0; index 2000000.0 ) m_fSampleRate = 2000000.0; break; case SB_LINEDOWN: m_fSampleRate -= 50000; if (m_fSampleRate < 50000 ) m_fSampleRate = 50000; break; } SetDlgItemInt( IDC_EDIT_SAMPLE_RATE, (UINT)(m_fSampleRate/1000) ); CDialog::OnVScroll(nSBCode, nPos, pScrollBar); }