// Sample0Dlg.cpp : implementation file // #include "stdafx.h" #include "Sample0.h" #include "Sample0Dlg.h" #include "AIOUSB.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif const unsigned long TimerPeriod =100; // value in msecs, 10 Hz ///////////////////////////////////////////////////////////////////////////// // 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() ///////////////////////////////////////////////////////////////////////////// // CSample0Dlg dialog CSample0Dlg::CSample0Dlg(CWnd* pParent /*=NULL*/) : CDialog(CSample0Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CSample0Dlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); Bmp0.LoadBitmap( IDB_BITMAP0 ); Bmp1.LoadBitmap( IDB_BITMAP1 ); BmpND.LoadBitmap( IDB_BITMAP_ND ); m_Instructions = "Clicking on the \"Write Port \" 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 CSample0Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSample0Dlg) // NOTE: the ClassWizard will add DDX and DDV calls here DDX_Control(pDX, IDC_STATUS, m_Status); DDX_Control(pDX, IDC_OUT_A0, m_BitControl[ 0 ] ); DDX_Control(pDX, IDC_OUT_A1, m_BitControl[ 1 ] ); DDX_Control(pDX, IDC_OUT_A2, m_BitControl[ 2 ] ); DDX_Control(pDX, IDC_OUT_A3, m_BitControl[ 3 ] ); DDX_Control(pDX, IDC_IN_A0, m_BitControl[ 8 ] ); DDX_Control(pDX, IDC_IN_A1, m_BitControl[ 9 ] ); DDX_Control(pDX, IDC_IN_A2, m_BitControl[ 10 ] ); DDX_Control(pDX, IDC_IN_A3, m_BitControl[ 11 ] ); DDX_Control(pDX, IDC_IN_A4, m_BitControl[ 12 ] ); DDX_Control(pDX, IDC_IN_A5, m_BitControl[ 13 ] ); DDX_Control(pDX, IDC_IN_A6, m_BitControl[ 14 ] ); DDX_Control(pDX, IDC_IN_A7, m_BitControl[ 15 ] ); DDX_Control(pDX, IDC_WRITE_A, m_ButtonControl[ 0 ] ); DDX_Control(pDX, IDC_EDIT_OUT_A, m_Out[ 0 ] ); DDX_Text(pDX, IDC_INSTRUCTIONS, m_Instructions ); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CSample0Dlg, CDialog) //{{AFX_MSG_MAP(CSample0Dlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_WRITE_A, OnWriteA) ON_BN_CLICKED(IDC_OUT_A0, OnBit0a) ON_BN_CLICKED(IDC_OUT_A1, OnBit1a) ON_BN_CLICKED(IDC_OUT_A2, OnBit2a) ON_BN_CLICKED(IDC_OUT_A3, OnBit3a) ON_WM_TIMER() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSample0Dlg message handlers BOOL CSample0Dlg::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 // set to initial values m_DeviceDetected = false; m_DeviceIndex = 0; if( DetectDevice() ) { InitializeDevice(); ReadAll(); } UpdateDisplay(); SetTimer( 0, TimerPeriod, NULL ); return TRUE; // return TRUE unless you set the focus to a control } void CSample0Dlg::OnExit() { KillTimer( 0 ); } void CSample0Dlg::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 CSample0Dlg::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 CSample0Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CSample0Dlg::OnCancel() { // TODO: Add extra cleanup here CDialog::OnCancel(); } void CSample0Dlg::OnBit0a() { BitClicked( 0 ); } void CSample0Dlg::OnBit1a() { BitClicked( 1 ); } void CSample0Dlg::OnBit2a() { BitClicked( 2 ); } void CSample0Dlg::OnBit3a() { BitClicked( 3 ); } void CSample0Dlg::OnWriteA() { // grab data from the Edit Window and send // it to the port CString strText; char *bufPtr; DWORD userData; UpdateData( true ); // store window's text in strText m_Out[ 0 ].GetWindowText( strText ); // copy text into location pointed by bufPtr bufPtr = strText.GetBuffer( 3 ); // read data from the location indicated by bufPtr and store // into userData location in Hex format sscanf( bufPtr, "%x", &userData ); // release string buffer strText.ReleaseBuffer(); // extract 8 least significant bits userData &= 0xff; // save user enter data m_Data[ 0 ] = userData; // send to the port DIO_Write8( m_DeviceIndex, 0, m_Data[ 0 ] ); UpdateDisplay(); } void CSample0Dlg::BitClicked( int bit ) { m_Data[ 0 ] ^= ( 1 << bit ); bool newValue = ( m_Data[ 0 ] & (1<< bit) ) != 0; DIO_Write1( diOnly, bit, newValue ); UpdateDisplay(); } bool CSample0Dlg::DetectDevice() { unsigned long nameSize, DioSizeInBytes, numOfCounters, PID; char buf[ 256 ]; CString str; if( GetDevices() == 0 ) { str = "No device detected or driver is not installed properly."; m_Status.SetWindowText( str ); m_DeviceDetected = false; return false; } m_DeviceIndex = diOnly; nameSize = sizeof( buf ) - 1; if( QueryDeviceInfo( m_DeviceIndex, &PID, &nameSize, buf, &DioSizeInBytes, &numOfCounters ) != ERROR_SUCCESS ) { str = "No device detected"; m_Status.SetWindowText( str ); m_DeviceDetected = false; return false; } //----------------------- // device was detected // verify PID //------------------------ buf[ nameSize ] = '\0'; if( (PID & 0xFFFE) != 0x8036 ) { str = "Detected device: "; str += _T( buf ); str += " not supported."; m_Status.SetWindowText( str ); m_DeviceDetected = false; return false; } str = ""; m_Status.SetWindowText( str ); m_DeviceDetected = true; return true; } void CSample0Dlg::OnTimer( UINT nIDEvent ) { if( m_DeviceDetected ) { ReadAll(); } else { if( DetectDevice() ) { InitializeDevice(); } } UpdateDisplay(); SetTimer( 0, TimerPeriod, NULL ); } void CSample0Dlg::UpdateDisplay() { int port, bitInPort; for( int bit = 0; bit < 16; ++bit ) { if ( bit == 4 ) bit = 8; //Skip excess out bits. port = bit/8; bitInPort = bit%8; if( m_DeviceDetected ) { if( m_Data[ port ] & ( 1 << bitInPort ) ) { m_BitControl[ bit ].SetBitmap( Bmp1 ); } else { m_BitControl[ bit ].SetBitmap( Bmp0 ); } } else { m_BitControl[ bit ].SetBitmap( BmpND ); } } // port 0 is outputs if( m_DeviceDetected == false ) { m_Out[ 0 ].ShowWindow(SW_HIDE ); m_ButtonControl[ 0 ].ShowWindow(SW_HIDE ); } else { m_Out[ 0 ].ShowWindow(SW_SHOW); m_ButtonControl[ 0 ].ShowWindow(SW_SHOW); m_ButtonControl[ 0 ].SetWindowText( "Write Port" ); } } void CSample0Dlg::ReadAll() { unsigned char tmp[ 2 ]; unsigned long retVal = DIO_ReadAll( m_DeviceIndex, ( char *)tmp ); if( retVal != ERROR_SUCCESS ) { m_DeviceDetected = false; } else { for( int port = 0; port < 2; ++port ) { if( m_OutMask & ( 1 << port ) ) { // port is output // TBD - check read-out against m_Data } else { m_Data[ port ] = tmp[ port ]; } } } } void CSample0Dlg::InitializeDevice( void ) { m_Out[ 0 ].SetWindowText( "FF" ); m_Out[ 1 ].SetWindowText( "FF" ); // port 0 is outputs, 1 is inputs m_OutMask = 1; //Initialize all ports to ones for( int port = 0; port < 1; ++port ) { m_Data[ port ] = 0xff; } DIO_WriteAll( m_DeviceIndex, m_Data ); }