// sample1Dlg.cpp : implementation file // #include "stdafx.h" #include "sample1.h" #include "sample1Dlg.h" #include "AIOUSB.h" #include "DetectDialog.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CSample1Dlg dialog CSample1Dlg::CSample1Dlg(CWnd* pParent /*=NULL*/) : CDialog(CSample1Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CSample1Dlg) //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CSample1Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSample1Dlg) DDX_Control(pDX, IDC_EDIT_PORTC, m_editPortC); DDX_Control(pDX, IDC_EDIT_PORTB, m_editPortB); DDX_Control(pDX, IDC_EDIT_PORTA, m_editPortA); DDX_Control(pDX, IDC_CHECK_PORTB, m_chkPortB); DDX_Control(pDX, IDC_CHECK_PORTA, m_chkPortA); DDX_Control(pDX, IDC_BUTTON_PORTC, m_btnPortC); DDX_Control(pDX, IDC_BUTTON_PORTB, m_btnPortB); DDX_Control(pDX, IDC_BUTTON_PORTA, m_btnPortA); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CSample1Dlg, CDialog) //{{AFX_MSG_MAP(CSample1Dlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_CHECK_PORTA, OnCheckPorta) ON_BN_CLICKED(IDC_CHECK_PORTB, OnCheckPortb) ON_BN_CLICKED(IDC_BUTTON_PORTA, OnButtonPorta) ON_BN_CLICKED(IDC_BUTTON_PORTB, OnButtonPortb) ON_BN_CLICKED(IDC_BUTTON_PORTC, OnButtonPortc) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSample1Dlg message handlers BOOL CSample1Dlg::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 btnArray[0] = &m_btnPortA; btnArray[1] = &m_btnPortB; btnArray[2] = &m_btnPortC; checkArray[0] = &m_chkPortA; checkArray[1] = &m_chkPortB; editArray[0] = &m_editPortA; editArray[1] = &m_editPortB; editArray[2] = &m_editPortC; deviceIndex = diOnly; if (DIO_Configure(deviceIndex, false, &outMask, data) != ERROR_SUCCESS) { DetectDialog box; box.DoModal(); deviceIndex=box.index; if (deviceIndex == -1) //no valid device was selected exit(0); } 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 CSample1Dlg::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 CSample1Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CSample1Dlg::chkClicked(int index) { if (checkArray[index]->GetCheck() == BST_CHECKED) { outMask |= 1 << index; btnArray[index]->SetWindowText("Write"); } else { outMask &= (0xFF ^ (1 << index)); btnArray[index]->SetWindowText("Read"); } DIO_Configure(deviceIndex, false, &outMask, data); } void CSample1Dlg::OnCheckPorta() { chkClicked(0); } void CSample1Dlg::OnCheckPortb() { chkClicked(1); } int StrToInt(char *S) { int Result; sscanf(S, "%X", &Result); return Result; } void CSample1Dlg::grabData() { char buf[3]; UpdateData(true); for (int count = 0; count < 2; count++) { CString InterBuf; editArray[count]->GetWindowText(buf, 3); data[count] = StrToInt(buf); } } void CSample1Dlg::buttonClicked(int index) { if (checkArray[index]->GetCheck() == BST_CHECKED) { //we are outputting so get the data from the form grabData(); DIO_Write8(deviceIndex, index, data[index]); } else { CString received; int status; //we are recieving so put information on the form status = DIO_Read8(deviceIndex, index, &(data[index])); received.Format("%02X", data[index]); editArray[index]->SetWindowText(received); } } void CSample1Dlg::OnButtonPorta() { buttonClicked(0); } void CSample1Dlg::OnButtonPortb() { buttonClicked(1); } void CSample1Dlg::OnButtonPortc() { buttonClicked(2); }