// sample1Dlg.cpp : implementation file // #include "stdafx.h" #include "sample1.h" #include "sample1Dlg.h" #include "DetectDialog.h" #include "AIOUSB.H" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSample1Dlg dialog CSample1Dlg::CSample1Dlg(CWnd* pParent /*=NULL*/) : CDialog(CSample1Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CSample1Dlg) m_instructions = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); unsigned long Status; DeviceIndex = diOnly; Status = QueryDeviceInfo(DeviceIndex, NULL, NULL, NULL, NULL, NULL); if (Status != ERROR_SUCCESS) { DetectDialog dlg; if(dlg.DoModal() != IDOK) { exit(0); } DeviceIndex = dlg.index; } outMask = 0xFFFFFFFF; inMask = 0; DIO_WriteAll(DeviceIndex, &outMask); /******************************************* ** being able to reach the LEDs through an array ** allows us to loop through the process of setting ** their colors in the paint procedure *******************************************/ outArray[0] = &m_outA0; outArray[1] = &m_outA1; outArray[2] = &m_outA2; outArray[3] = &m_outA3; outArray[4] = &m_outA4; outArray[5] = &m_outA5; outArray[6] = &m_outA6; outArray[7] = &m_outA7; outArray[8] = &m_outB0; outArray[9] = &m_outB1; outArray[10] = &m_outB2; outArray[11] = &m_outB3; outArray[12] = &m_outB4; outArray[13] = &m_outB5; outArray[14] = &m_outB6; outArray[15] = &m_outB7; inArray[0] = &m_inA0; inArray[1] = &m_inA1; inArray[2] = &m_inA2; inArray[3] = &m_inA3; inArray[4] = &m_inA4; inArray[5] = &m_inA5; inArray[6] = &m_inA6; inArray[7] = &m_inA7; inArray[8] = &m_inB0; inArray[9] = &m_inB1; inArray[10] = &m_inB2; inArray[11] = &m_inB3; inArray[12] = &m_inB4; inArray[13] = &m_inB5; inArray[14] = &m_inB6; inArray[15] = &m_inB7; m_instructions = "Clicking on the \"Read Port A\" and \"Read Port B\" buttons will read the inputs from the card\r\n"; m_instructions += "Clicking on the \"Write Port A\" and \"Write Port B\" will write the values entered into the edit boxes to the relays\r\n"; m_instructions += "Clicking on the lights above the edit boxes will change the relays one at a time."; } void CSample1Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSample1Dlg) DDX_Control(pDX, IDC_RED, m_red); DDX_Control(pDX, IDC_GREEN, m_green); DDX_Control(pDX, IDC_IN_A0, m_inA0); DDX_Control(pDX, IDC_IN_A1, m_inA1); DDX_Control(pDX, IDC_IN_A2, m_inA2); DDX_Control(pDX, IDC_IN_A3, m_inA3); DDX_Control(pDX, IDC_IN_A4, m_inA4); DDX_Control(pDX, IDC_IN_A5, m_inA5); DDX_Control(pDX, IDC_IN_A6, m_inA6); DDX_Control(pDX, IDC_IN_A7, m_inA7); DDX_Control(pDX, IDC_IN_B0, m_inB0); DDX_Control(pDX, IDC_IN_B1, m_inB1); DDX_Control(pDX, IDC_IN_B2, m_inB2); DDX_Control(pDX, IDC_IN_B3, m_inB3); DDX_Control(pDX, IDC_IN_B4, m_inB4); DDX_Control(pDX, IDC_IN_B5, m_inB5); DDX_Control(pDX, IDC_IN_B6, m_inB6); DDX_Control(pDX, IDC_IN_B7, m_inB7); DDX_Control(pDX, IDC_OUT_A0, m_outA0); DDX_Control(pDX, IDC_OUT_A1, m_outA1); DDX_Control(pDX, IDC_OUT_A2, m_outA2); DDX_Control(pDX, IDC_OUT_A3, m_outA3); DDX_Control(pDX, IDC_OUT_A4, m_outA4); DDX_Control(pDX, IDC_OUT_A5, m_outA5); DDX_Control(pDX, IDC_OUT_A6, m_outA6); DDX_Control(pDX, IDC_OUT_A7, m_outA7); DDX_Control(pDX, IDC_OUT_B0, m_outB0); DDX_Control(pDX, IDC_OUT_B1, m_outB1); DDX_Control(pDX, IDC_OUT_B2, m_outB2); DDX_Control(pDX, IDC_OUT_B3, m_outB3); DDX_Control(pDX, IDC_OUT_B4, m_outB4); DDX_Control(pDX, IDC_OUT_B5, m_outB5); DDX_Control(pDX, IDC_OUT_B6, m_outB6); DDX_Control(pDX, IDC_OUT_B7, m_outB7); DDX_Control(pDX, IDC_EDIT_OUTB, m_outB); DDX_Control(pDX, IDC_EDIT_OUTA, m_outA); DDX_Text(pDX, IDC_INSTRUCTIONS, m_instructions); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CSample1Dlg, CDialog) //{{AFX_MSG_MAP(CSample1Dlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_EN_UPDATE(IDC_EDIT_OUTA, OnUpdateEditOuta) ON_BN_CLICKED(IDC_WRITEA, OnWritea) ON_EN_UPDATE(IDC_EDIT_OUTB, OnUpdateEditOutb) ON_BN_CLICKED(IDC_WRITEB, OnWriteb) ON_BN_CLICKED(IDC_READA_BTN, OnReadaBtn) ON_BN_CLICKED(IDC_READB, OnReadb) ON_BN_CLICKED(IDC_OUT_A7, OnOutA7) ON_BN_CLICKED(IDC_OUT_A6, OnOutA6) ON_BN_CLICKED(IDC_OUT_A5, OnOutA5) ON_BN_CLICKED(IDC_OUT_A4, OnOutA4) ON_BN_CLICKED(IDC_OUT_A3, OnOutA3) ON_BN_CLICKED(IDC_OUT_A2, OnOutA2) ON_BN_CLICKED(IDC_OUT_A1, OnOutA1) ON_BN_CLICKED(IDC_OUT_A0, OnOutA0) ON_BN_CLICKED(IDC_OUT_B7, OnOutB7) ON_BN_CLICKED(IDC_OUT_B6, OnOutB6) ON_BN_CLICKED(IDC_OUT_B5, OnOutB5) ON_BN_CLICKED(IDC_OUT_B4, OnOutB4) ON_BN_CLICKED(IDC_OUT_B3, OnOutB3) ON_BN_CLICKED(IDC_OUT_B2, OnOutB2) ON_BN_CLICKED(IDC_OUT_B1, OnOutB1) ON_BN_CLICKED(IDC_OUT_B0, OnOutB0) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSample1Dlg message handlers BOOL CSample1Dlg::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); } } // 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 m_outA.SetWindowText("00"); m_outB.SetWindowText("00"); return TRUE; // return TRUE unless you set the focus to a control } void CSample1Dlg::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 CSample1Dlg::OnPaint() { CDC *pDC; //this is going to point to each of the "LEDs" contexts so they can be filled RECT rect; //this is going to hold the dimensions of each of the "LEDs" CBrush onBrush(RGB(0x00, 0xff, 0x00)); //the on brush is green CBrush offBrush(RGB(0xff, 0x00, 0x00)); //the off brush is red pDC = m_green.GetDC(); m_green.GetClientRect(&rect); pDC->FillRect(&rect, &onBrush); pDC = m_red.GetDC(); m_red.GetClientRect(&rect); pDC->FillRect(&rect, &offBrush); for (int i = 0; i < 16; i++) //cycle through the inputs and outputs and set them to the right color { pDC = inArray[i]->GetDC(); inArray[i]->GetClientRect(&rect); if ((inMask >> i) % 2) pDC->FillRect(&rect, &onBrush); else pDC->FillRect(&rect, &offBrush); pDC = outArray[i]->GetDC(); outArray[i]->GetClientRect(&rect); if ((outMask >> i) % 2) pDC->FillRect(&rect, &onBrush); else pDC->FillRect(&rect, &offBrush); } 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::OnUpdateEditOuta() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function to send the EM_SETEVENTMASK message to the control // with the ENM_UPDATE flag ORed into the lParam mask. // TODO: Add your control notification handler code here //MessageBox("UPDATE"); if (GetAsyncKeyState(VK_RETURN) & 0x8000) OnWritea(); } void CSample1Dlg::OnWritea() { // TODO: Add your control notification handler code here // unsigned char mask; CString data; char *buf; DWORD mask; UpdateData(true); m_outA.GetWindowText(data); buf = data.GetBuffer(3); sscanf(buf, "%x", &mask); data.ReleaseBuffer(); mask &= 0xff; outMask &= 0xff00; outMask |= mask; DIO_WriteAll(DeviceIndex, &outMask); Invalidate(0); } void CSample1Dlg::OnUpdateEditOutb() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function to send the EM_SETEVENTMASK message to the control // with the ENM_UPDATE flag ORed into the lParam mask. // TODO: Add your control notification handler code here if (GetAsyncKeyState(VK_RETURN) & 0x8000) OnWriteb(); } void CSample1Dlg::OnWriteb() { // TODO: Add your control notification handler code here CString data; char *buf; DWORD mask; UpdateData(true); m_outB.GetWindowText(data); buf = data.GetBuffer(3); sscanf(buf, "%x", &mask); data.ReleaseBuffer(); mask &= 0xff; outMask &= 0x00ff; outMask |= mask << 8; DIO_WriteAll(DeviceIndex, &outMask); Invalidate(0); } void CSample1Dlg::OnReadaBtn() { // TODO: Add your control notification handler code here unsigned char input; DIO_Read8(DeviceIndex, 2, &input); inMask &= 0xff00; inMask |= input; Invalidate(0); } void CSample1Dlg::OnReadb() { // TODO: Add your control notification handler code here unsigned char input; DIO_Read8(DeviceIndex, 3, &input); inMask &= 0x00ff; inMask |= input << 8; Invalidate(0); } void CSample1Dlg::OnOutA7() { // TODO: Add your control notification handler code here outMask ^= 0x80; //we could just call DIO_WriteAll here, but we are going to demonstrate DIO_Write1 instead DIO_Write1(DeviceIndex, 7, (outMask & 0x80) ? 1 : 0); Invalidate(0); } void CSample1Dlg::OnOutA6() { // TODO: Add your control notification handler code here outMask ^= 0x40; //we could just call DIO_WriteAll here, but we are going to demonstrate DIO_Write1 instead DIO_Write1(DeviceIndex, 6, (outMask & 0x40) ? 1 : 0); Invalidate(0); } void CSample1Dlg::OnOutA5() { // TODO: Add your control notification handler code here outMask ^= 0x20; //we could just call DIO_WriteAll here, but we are going to demonstrate DIO_Write1 instead DIO_Write1(DeviceIndex, 5, (outMask & 0x20) ? 1 : 0); Invalidate(0); } void CSample1Dlg::OnOutA4() { // TODO: Add your control notification handler code here outMask ^= 0x10; //we could just call DIO_WriteAll here, but we are going to demonstrate DIO_Write1 instead DIO_Write1(DeviceIndex, 4, (outMask & 0x10) ? 1 : 0); Invalidate(0); } void CSample1Dlg::OnOutA3() { // TODO: Add your control notification handler code here outMask ^= 0x08; //we could just call DIO_WriteAll here, but we are going to demonstrate DIO_Write1 instead DIO_Write1(DeviceIndex, 3, (outMask & 0x08) ? 1 : 0); Invalidate(0); } void CSample1Dlg::OnOutA2() { // TODO: Add your control notification handler code here outMask ^= 0x04; //we could just call DIO_WriteAll here, but we are going to demonstrate DIO_Write1 instead DIO_Write1(DeviceIndex, 2, (outMask & 0x04) ? 1 : 0); Invalidate(0); } void CSample1Dlg::OnOutA1() { // TODO: Add your control notification handler code here outMask ^= 0x02; //we could just call DIO_WriteAll here, but we are going to demonstrate DIO_Write1 instead DIO_Write1(DeviceIndex, 1, (outMask & 0x02) ? 1 : 0); Invalidate(0); } void CSample1Dlg::OnOutA0() { // TODO: Add your control notification handler code here outMask ^= 0x01; //we could just call DIO_WriteAll here, but we are going to demonstrate DIO_Write1 instead DIO_Write1(DeviceIndex, 0, (outMask & 0x01) ? 1 : 0); Invalidate(0); } void CSample1Dlg::OnOutB7() { // TODO: Add your control notification handler code here outMask ^= 0x8000; //we could just call DIO_WriteAll here, but we are going to demonstrate DIO_Write1 instead DIO_Write1(DeviceIndex, 15, (outMask & 0x8000) ? 1 : 0); Invalidate(0); } void CSample1Dlg::OnOutB6() { // TODO: Add your control notification handler code here outMask ^= 0x4000; //we could just call DIO_WriteAll here, but we are going to demonstrate DIO_Write1 instead DIO_Write1(DeviceIndex, 14, (outMask & 0x4000) ? 1 : 0); Invalidate(0); } void CSample1Dlg::OnOutB5() { // TODO: Add your control notification handler code here outMask ^= 0x2000; //we could just call DIO_WriteAll here, but we are going to demonstrate DIO_Write1 instead DIO_Write1(DeviceIndex, 13, (outMask & 0x2000) ? 1 : 0); Invalidate(0); } void CSample1Dlg::OnOutB4() { // TODO: Add your control notification handler code here outMask ^= 0x1000; //we could just call DIO_WriteAll here, but we are going to demonstrate DIO_Write1 instead DIO_Write1(DeviceIndex, 12, (outMask & 0x1000) ? 1 : 0); Invalidate(0); } void CSample1Dlg::OnOutB3() { // TODO: Add your control notification handler code here outMask ^= 0x0800; //we could just call DIO_WriteAll here, but we are going to demonstrate DIO_Write1 instead DIO_Write1(DeviceIndex, 11, (outMask & 0x0800) ? 1 : 0); Invalidate(0); } void CSample1Dlg::OnOutB2() { // TODO: Add your control notification handler code here outMask ^= 0x0400; //we could just call DIO_WriteAll here, but we are going to demonstrate DIO_Write1 instead DIO_Write1(DeviceIndex, 10, (outMask & 0x0400) ? 1 : 0); Invalidate(0); } void CSample1Dlg::OnOutB1() { // TODO: Add your control notification handler code here outMask ^= 0x0200; //we could just call DIO_WriteAll here, but we are going to demonstrate DIO_Write1 instead DIO_Write1(DeviceIndex, 9, (outMask & 0x0200) ? 1 : 0); Invalidate(0); } void CSample1Dlg::OnOutB0() { // TODO: Add your control notification handler code here outMask ^= 0x0100; //we could just call DIO_WriteAll here, but we are going to demonstrate DIO_Write1 instead DIO_Write1(DeviceIndex, 8, (outMask & 0x0100) ? 1 : 0); Invalidate(0); }