// USB_WINDlg.cpp : implementation file // #include "stdafx.h" #include "USB_WIN.h" #include "USB_WINDlg.h" #include "UsbCode.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define USBIODS_GUID_STR "{B5157D69-75F8-11d3-8CE0-00207815E611}" // --- Globals Variables --- CUsbCode CUsb; // Global Usb function class ///////////////////////////////////////////////////////////////////////////// // 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() ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// // CUSB_WINDlg MAIN dialog CUSB_WINDlg::CUSB_WINDlg(CWnd* pParent /*=NULL*/) : CDialog(CUSB_WINDlg::IDD, pParent) { //{{AFX_DATA_INIT(CUSB_WINDlg) // 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); } void CUSB_WINDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CUSB_WINDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CUSB_WINDlg, CDialog) //{{AFX_MSG_MAP(CUSB_WINDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_LED_P10, OnLEDP10) ON_WM_DESTROY() ON_BN_CLICKED(IDC_LED_P11, OnLedP11) ON_BN_CLICKED(IDC_LED_P12, OnLedP12) ON_BN_CLICKED(IDC_LED_P13, OnLedP13) ON_BN_CLICKED(IDC_LED_P14, OnLedP14) ON_BN_CLICKED(IDC_LED_P15, OnLedP15) ON_BN_CLICKED(IDC_LED_P16, OnLedP16) ON_BN_CLICKED(IDC_LED_P17, OnLedP17) ON_LBN_SELCHANGE(IDC_DEVICELIST, OnSelchangeDevicelist) ON_WM_TIMER() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CUSB_WINDlg message handlers BOOL CUSB_WINDlg::OnInitDialog() { int i=0; CDialog::OnInitDialog(); // call the base class first // 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 // Scan for USB Devices and fill the list box CString Devices[127]; Devices[0] = USBIODS_GUID_STR; // get the guid CListBox* pListBox = (CListBox*) GetDlgItem(IDC_DEVICELIST); int found = CUsb.Scan( Devices ); // scans for all the device matching the guid if( !found) { MessageBox("Unable to enumerate the Usb device", "Enumeration Error"); pListBox->AddString("USB Device Not Found!"); } else{ i=0; while(iAddString(Devices[i++]); // add all the device to the list box } pListBox->SetCurSel(0); // default to the first entry. // If a USBIODS device exists try to open the first device found. if(found) if( CUsb.Open( Devices[0]) ) // try to open the device { // Start the timer ReadVersion(); // Display the Firmware infomation if( !SetTimer(ID_TIMER,100,NULL)) AfxMessageBox("Unable to setup timer resource",MB_OK); } else AfxMessageBox("Unable to open device",MB_OK); return TRUE; } void CUSB_WINDlg::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 CUSB_WINDlg::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 CUSB_WINDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CUSB_WINDlg::OnDestroy() { CDialog::OnDestroy(); KillTimer(ID_TIMER); CUsb.Close(); } void CUSB_WINDlg::OnLEDP10() { CButton *pLEDP10 = (CButton*) GetDlgItem( IDC_LED_P10 ); CUsb.Packet.MajorCmd = 10; CUsb.Packet.MinorCmd = 12; CUsb.Packet.DataMSB = 0; CUsb.Packet.DataLSB = 0; if( pLEDP10->GetCheck() ) CUsb.Packet.DataLSB = 0x01; else CUsb.Packet.DataMSB = 0x01; CUsb.SendPacket(&CUsb.Packet); } void CUSB_WINDlg::OnLedP11() { CButton *pLEDP10 = (CButton*) GetDlgItem( IDC_LED_P11 ); CUsb.Packet.MajorCmd = 10; CUsb.Packet.MinorCmd = 12; CUsb.Packet.DataMSB = 0; CUsb.Packet.DataLSB = 0; if( pLEDP10->GetCheck() ) CUsb.Packet.DataLSB = 0x02; else CUsb.Packet.DataMSB = 0x02; CUsb.SendPacket(&CUsb.Packet); } void CUSB_WINDlg::OnLedP12() { CButton *pLEDP10 = (CButton*) GetDlgItem( IDC_LED_P12 ); CUsb.Packet.MajorCmd = 10; CUsb.Packet.MinorCmd = 12; CUsb.Packet.DataMSB = 0; CUsb.Packet.DataLSB = 0; if( pLEDP10->GetCheck() ) CUsb.Packet.DataLSB = 0x04; else CUsb.Packet.DataMSB = 0x04; CUsb.SendPacket(&CUsb.Packet); } void CUSB_WINDlg::OnLedP13() { CButton *pLEDP10 = (CButton*) GetDlgItem( IDC_LED_P13 ); CUsb.Packet.MajorCmd = 10; CUsb.Packet.MinorCmd = 12; CUsb.Packet.DataMSB = 0; CUsb.Packet.DataLSB = 0; if( pLEDP10->GetCheck() ) CUsb.Packet.DataLSB = 0x08; else CUsb.Packet.DataMSB = 0x08; CUsb.SendPacket(&CUsb.Packet); } void CUSB_WINDlg::OnLedP14() { CButton *pLEDP10 = (CButton*) GetDlgItem( IDC_LED_P14 ); CUsb.Packet.MajorCmd = 10; CUsb.Packet.MinorCmd = 12; CUsb.Packet.DataMSB = 0; CUsb.Packet.DataLSB = 0; if( pLEDP10->GetCheck() ) CUsb.Packet.DataLSB = 0x10; else CUsb.Packet.DataMSB = 0x10; CUsb.SendPacket(&CUsb.Packet); } void CUSB_WINDlg::OnLedP15() { CButton *pLEDP10 = (CButton*) GetDlgItem( IDC_LED_P15 ); CUsb.Packet.MajorCmd = 10; CUsb.Packet.MinorCmd = 12; CUsb.Packet.DataMSB = 0; CUsb.Packet.DataLSB = 0; if( pLEDP10->GetCheck() ) CUsb.Packet.DataLSB = 0x20; else CUsb.Packet.DataMSB = 0x20; CUsb.SendPacket(&CUsb.Packet); } void CUSB_WINDlg::OnLedP16() { CButton *pLEDP10 = (CButton*) GetDlgItem( IDC_LED_P16 ); CUsb.Packet.MajorCmd = 10; CUsb.Packet.MinorCmd = 12; CUsb.Packet.DataMSB = 0; CUsb.Packet.DataLSB = 0; if( pLEDP10->GetCheck() ) CUsb.Packet.DataLSB = 0x40; else CUsb.Packet.DataMSB = 0x40; CUsb.SendPacket(&CUsb.Packet); } void CUSB_WINDlg::OnLedP17() { CButton *pLEDP10 = (CButton*) GetDlgItem( IDC_LED_P17 ); CUsb.Packet.MajorCmd = 10; CUsb.Packet.MinorCmd = 12; CUsb.Packet.DataMSB = 0; CUsb.Packet.DataLSB = 0; if( pLEDP10->GetCheck() ) CUsb.Packet.DataLSB = 0x80; else CUsb.Packet.DataMSB = 0x80; CUsb.SendPacket(&CUsb.Packet); } void CUSB_WINDlg::OnSelchangeDevicelist() { CString DeviceName; KillTimer(ID_TIMER); CUsb.Close(); CListBox* pListBox = (CListBox*) GetDlgItem(IDC_DEVICELIST); pListBox->GetText(pListBox->GetCurSel(),DeviceName); // get the device name if( CUsb.Open( DeviceName ) ) // try to open the device { ReadVersion(); // Display the Firmware infomation if( !SetTimer(ID_TIMER,100,NULL)) // Re-enble Timer AfxMessageBox("Unable to setup timer resource",MB_OK); } else AfxMessageBox("Unable to open device",MB_OK); } void CUSB_WINDlg::OnTimer(UINT nIDEvent) { ReadPorts(); // read the current ports & set the dialog boxs CDialog::OnTimer(nIDEvent); // pass the timer event down } void CUSB_WINDlg::ReadPorts() { CUsb.Packet.MajorCmd = 11; CUsb.Packet.MinorCmd = 0; CUsb.Packet.DataMSB = 0; CUsb.Packet.DataLSB = 0; CUsb.SendPacket( &CUsb.Packet ); // update dip switches CButton *pDIPSW = (CButton*) GetDlgItem( IDC_DIPSW_P00 ); if(CUsb.Packet.Recipient & 0x01 ) pDIPSW->SetCheck(1); else pDIPSW->SetCheck(0); pDIPSW = (CButton*) GetDlgItem( IDC_DIPSW_P01 ); if(CUsb.Packet.Recipient & 0x02 ) pDIPSW->SetCheck(1); else pDIPSW->SetCheck(0); pDIPSW = (CButton*) GetDlgItem( IDC_DIPSW_P02 ); if(CUsb.Packet.Recipient & 0x04 ) pDIPSW->SetCheck(1); else pDIPSW->SetCheck(0); pDIPSW = (CButton*) GetDlgItem( IDC_DIPSW_P03 ); if(CUsb.Packet.Recipient & 0x08 ) pDIPSW->SetCheck(1); else pDIPSW->SetCheck(0); pDIPSW = (CButton*) GetDlgItem( IDC_DIPSW_P04 ); if(CUsb.Packet.Recipient & 0x10 ) pDIPSW->SetCheck(1); else pDIPSW->SetCheck(0); pDIPSW = (CButton*) GetDlgItem( IDC_DIPSW_P05 ); if(CUsb.Packet.Recipient & 0x20 ) pDIPSW->SetCheck(1); else pDIPSW->SetCheck(0); pDIPSW = (CButton*) GetDlgItem( IDC_DIPSW_P06 ); if(CUsb.Packet.Recipient & 0x40 ) pDIPSW->SetCheck(1); else pDIPSW->SetCheck(0); pDIPSW = (CButton*) GetDlgItem( IDC_DIPSW_P07 ); if(CUsb.Packet.Recipient & 0x80 ) pDIPSW->SetCheck(1); else pDIPSW->SetCheck(0); // update leds pDIPSW = (CButton*) GetDlgItem( IDC_LED_P10 ); if(CUsb.Packet.DeviceModel & 0x01 ) pDIPSW->SetCheck(0); else pDIPSW->SetCheck(1); pDIPSW = (CButton*) GetDlgItem( IDC_LED_P11 ); if(CUsb.Packet.DeviceModel & 0x02 ) pDIPSW->SetCheck(0); else pDIPSW->SetCheck(1); pDIPSW = (CButton*) GetDlgItem( IDC_LED_P12 ); if(CUsb.Packet.DeviceModel & 0x04 ) pDIPSW->SetCheck(0); else pDIPSW->SetCheck(1); pDIPSW = (CButton*) GetDlgItem( IDC_LED_P13 ); if(CUsb.Packet.DeviceModel & 0x08 ) pDIPSW->SetCheck(0); else pDIPSW->SetCheck(1); pDIPSW = (CButton*) GetDlgItem( IDC_LED_P14 ); if(CUsb.Packet.DeviceModel & 0x10 ) pDIPSW->SetCheck(0); else pDIPSW->SetCheck(1); pDIPSW = (CButton*) GetDlgItem( IDC_LED_P15 ); if(CUsb.Packet.DeviceModel & 0x20 ) pDIPSW->SetCheck(0); else pDIPSW->SetCheck(1); pDIPSW = (CButton*) GetDlgItem( IDC_LED_P16 ); if(CUsb.Packet.DeviceModel & 0x40 ) pDIPSW->SetCheck(0); else pDIPSW->SetCheck(1); pDIPSW = (CButton*) GetDlgItem( IDC_LED_P17 ); if(CUsb.Packet.DeviceModel & 0x80 ) pDIPSW->SetCheck(0); else pDIPSW->SetCheck(1); } void CUSB_WINDlg::ReadVersion() { struct FirmwareInfoStruct{ unsigned int SerialNumber; unsigned char Version; unsigned char Date; unsigned char Month; unsigned char Year; } *pFirmwareInfo; CString str; CUsb.Packet.MajorCmd = 11; CUsb.Packet.MinorCmd = 10; CUsb.Packet.DataMSB = 0; CUsb.Packet.DataLSB = 0; CUsb.SendPacket( &CUsb.Packet ); pFirmwareInfo = (FirmwareInfoStruct*)&CUsb.Packet; str.Format("Serial Number: %u",pFirmwareInfo->SerialNumber); SetDlgItemText(IDC_SERIAL,(LPCTSTR)str); str.Format("Version: %u %u/%u/%u", pFirmwareInfo->Version, pFirmwareInfo->Month, pFirmwareInfo->Date, 2000+pFirmwareInfo->Year); SetDlgItemText(IDC_VERSION,(LPCTSTR)str); }