// dacSampDlg.cpp : implementation file // #include "stdafx.h" #include "dacSamp.h" #include "dacSampDlg.h" #include "DetectDialog.h" #include "aiousb.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CDacSampDlg dialog CDacSampDlg::CDacSampDlg(CWnd* pParent /*=NULL*/) : CDialog(CDacSampDlg::IDD, pParent) { //{{AFX_DATA_INIT(CDacSampDlg) m_rateLBL = _T(""); m_blurb = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CDacSampDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDacSampDlg) DDX_Control(pDX, IDC_SLIDER1, m_slider); DDX_Control(pDX, IDC_LIST1, m_listBox); DDX_Control(pDX, IDC_GO_BTN, m_goButton); DDX_Control(pDX, IDC_FILE_BTN, m_fileButton); DDX_Text(pDX, IDC_RATE_LBL, m_rateLBL); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDacSampDlg, CDialog) //{{AFX_MSG_MAP(CDacSampDlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_FILE_BTN, OnFileBtn) ON_BN_CLICKED(IDC_GO_BTN, OnGoBtn) ON_WM_CLOSE() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDacSampDlg message handlers BOOL CDacSampDlg::OnInitDialog() { CDialog::OnInitDialog(); // 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 DetectDialog box; if (box.DoModal() == IDOK) DeviceIndex = box.index; else exit(1); m_slider.SetRange(1, 100); m_slider.SetPos(10); m_listBox.InsertString(0, "This program will read data for the DACs from a text file with the following format:"); m_listBox.InsertString(1, " "); m_listBox.InsertString(2, "x,x,x,x,x,x,x,x"); m_listBox.InsertString(3, "x,x,x,x,,,,"); m_listBox.InsertString(4, " "); m_listBox.InsertString(5, "Each x represents a value to be placed on the corresponding DAC"); m_listBox.InsertString(6, " "); m_listBox.InsertString(7, "Note that there are always 7 commas in a line of the file."); m_listBox.InsertString(8, "If a DAC's entry is blank, all the following DAC entries for that line must be blank."); m_listBox.InsertString(9, "The program will then play the file continiously on the selected USB-DA12-8A until "); m_listBox.InsertString(10, "it is exited."); return TRUE; // return TRUE unless you set the focus to a control } // 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 CDacSampDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) 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(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CDacSampDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CDacSampDlg::OnFileBtn() { CFileDialog box(true); if (box.DoModal() == IDOK) { fName = box.GetPathName(); m_goButton.EnableWindow(true); m_listBox.AddString(fName); } } BOOL CDacSampDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) { NMHDR *message; message = (NMHDR *)lParam; switch (wParam) { case IDC_SLIDER1: m_rateLBL.Format("%d KHz", m_slider.GetPos()); UpdateData(false); break; } return CDialog::OnNotify(wParam, lParam, pResult); } void CDacSampDlg::OnGoBtn() { CString listString, currentLine, formatString; unsigned short FrameData[8192]; unsigned long FramePoints, status; int numDacs, oldDacs, index; CStdioFile theFile; int count; char *lineBuf; double clock; bool loopFile = false; m_fileButton.EnableWindow(false); m_slider.EnableWindow(false); theFile.Open(fName, CFile::modeRead); done = false; index = FramePoints = 0; oldDacs = 8; clock = m_slider.GetPos() * 1000.0; DACOutputOpen(DeviceIndex, &clock); DACOutputSetCount(DeviceIndex, oldDacs); do { if (theFile.ReadString(currentLine) == false) { theFile.SeekToBegin(); theFile.ReadString(currentLine); } lineBuf = currentLine.GetBuffer(0); numDacs = sscanf(lineBuf, "%d, %d, %d, %d, %d, %d, %d, %d", &(FrameData[index]), &(FrameData[index + 1]), &(FrameData[index + 2]), &(FrameData[index + 3]), &(FrameData[index + 4]), &(FrameData[index + 5]), &(FrameData[index + 6]), &(FrameData[index + 7])); currentLine.ReleaseBuffer(-1); if (numDacs == oldDacs) { FramePoints++; index += numDacs; if (index + numDacs > 8191) { DACOutputSetCount(DeviceIndex, numDacs); do { status = DACOutputFrame(DeviceIndex, FramePoints, FrameData); }while (status == ERROR_NOT_READY); index = FramePoints = 0; } } else { DACOutputSetCount(DeviceIndex, oldDacs); do { status = DACOutputFrame(DeviceIndex, FramePoints, FrameData); }while (status == ERROR_NOT_READY); for (count = 0; count < numDacs; count++) { FrameData[count] = FrameData[count + index]; } index = numDacs; FramePoints = 1; oldDacs = numDacs; } while (ProcessDialogMessages() == TRUE); }while (done == false); /* m_fileButton.EnableWindow(false); m_slider.EnableWindow(false); theFile.Open(fName, CFile::modeRead); theFile.ReadString(currentLine); lineIndex = 0; numDacs = 1; lineBuf = currentLine.GetBuffer(0); do { if(lineBuf[lineIndex] == ',') numDacs++; lineIndex++; }while (lineBuf[lineIndex] != '\0'); currentLine.ReleaseBuffer(-1); listString.Format("File is for %d DACs", numDacs); m_listBox.AddString(listString); clock = m_slider.GetPos() * 1000.0; DACOutputOpen(DeviceIndex, &clock); DACOutputSetCount(DeviceIndex, numDacs); listString.Format("Actual clock rate is %.2f Hz", clock); m_listBox.AddString(listString); formatString = "%d"; //sending an index longer than the string for Insert() makes it act like a strcat for (count = 1; count < numDacs; count++) formatString.Insert(255, ", %d"); lineIndex = FramePoints = dataIndex = 0; done = false; do { do { lineBuf = currentLine.GetBuffer(0); sscanf(lineBuf, formatString, &(FrameData[dataIndex]), &(FrameData[dataIndex + 1]), &(FrameData[dataIndex + 2]), &(FrameData[dataIndex + 3]), &(FrameData[dataIndex + 4]), &(FrameData[dataIndex + 5]), &(FrameData[dataIndex + 6]), &(FrameData[dataIndex + 7])); dataIndex += numDacs; FramePoints++; currentLine.ReleaseBuffer(-1); }while ((dataIndex + numDacs < 8192) && (theFile.ReadString(currentLine) != false)); if (dataIndex + numDacs >= 8192) loopFile = true; if (loopFile == false) { m_listBox.AddString("The file has less than 8k data points."); do { while (ProcessDialogMessages() == TRUE); DACOutputFrame(DeviceIndex, FramePoints, FrameData); }while (done == false); } else { do { status = DACOutputFrame(DeviceIndex, FramePoints, FrameData); }while (status == ERROR_NOT_READY); FramePoints = dataIndex = 0; if (theFile.ReadString(currentLine) == false) { theFile.SeekToBegin(); //SeekToBegin sounds like some ascetic priest's proverb, but it just puts us at //the beginning of a file theFile.ReadString(currentLine); } } while (ProcessDialogMessages() == TRUE); }while (done == false); DACOutputClose(DeviceIndex, 0); theFile.Close(); */ } BOOL CDacSampDlg::ProcessDialogMessages() { Sleep(0); // Yield to other // system processes // Process the message queue MSG dlgMsg; if (::PeekMessage (&dlgMsg, NULL, 0, 0, PM_NOREMOVE)) // If there are // pending messages { if (!AfxGetApp ()->PumpMessage ()) { // Allow them to be // processed ::PostQuitMessage (0); // When no more // messages exist, // return WM_QUIT // should return 0 return TRUE; // Return the caller, // indicating that // we just processed // a Message and that // there may be more } } // Simulate idle processing. LONG lIdle = 0; while (AfxGetApp ()->OnIdle (lIdle++)); return FALSE; // Return to Caller, // saying that we // did not process // any messages } void CDacSampDlg::OnClose() { done = true; CDialog::OnClose(); }