// SelectDP5.cpp : implementation file // #include "stdafx.h" #include "DppWinUSB.h" #include "SelectDP5.h" #include "SelectDP5Res.h" #include ".\selectdp5.h" #include "SProfile.h" #define HIDD_DP5_CONNECT 0x20191 BEGIN_MESSAGE_MAP(CSelectDP5, CDialog) ON_WM_DESTROY() ON_BN_CLICKED(IDC_RADIO_COMMTYPE2, OnBnClickedRadioCommtype2) ON_BN_CLICKED(IDC_BUTTON_FIND_USB_DEVICE, OnBnClickedButtonFindUsbDevice) ON_BN_CLICKED(IDC_CONNECTOK, OnBnClickedConnectOK) ON_BN_CLICKED(IDOK, OnOK) ON_BN_CLICKED(IDC_BUTTON_COUNT_DEVICES, OnBnClickedButtonCountDevices) ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_SELECT_DEVICE, OnDeltaposSpinSelectDevice) ON_BN_CLICKED(IDC_BUTTON_CLOSE_DEVICE, OnBnClickedButtonCloseDevice) ON_EN_CHANGE(IDC_SPECTRUM_REFRESH_TIME, OnEnChangeSpectrumRefreshTime) ON_BN_CLICKED(IDC_BUTTON_SPECTRUM_REFRESH_DEFAULT, OnBnClickedButtonSpectrumRefreshDefault) ON_WM_HELPINFO() END_MESSAGE_MAP() // CSelectDP5 dialog //IDD_DP5_CONNECT //IMPLEMENT_DYNAMIC(CSelectDP5, CDialog) //CSelectDP5::CSelectDP5(CWnd* pParent /*=NULL*/) // : CDialog(CSelectDP5::IDD, pParent) //CSelectDP5::CSelectDP5(CWnd* pParent /*=NULL*/) // : CDialog(_T("IDD_DP5_CONNECT"), pParent) //CSelectDP5::CSelectDP5(CWnd* pParent /*=NULL*/) // : CDialog("IDD_DP5_CONNECT") CSelectDP5::CSelectDP5() : CDialog(_T("IDD_DP5_CONNECT")) { // } //CSelectDP5::~CSelectDP5() //{ //} void CSelectDP5::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSpectrumDlg) DDX_Control(pDX, IDC_STATIC_FOUND2, lblDppFound[1]); DDX_Control(pDX, IDC_SPIN_SELECT_DEVICE, m_SpinSelectDevice); DDX_Control(pDX, IDC_SPECTRUM_REFRESH_TIME, m_SpectrumRefreshTimeCtrl); //}}AFX_DATA_MAP } BOOL CSelectDP5::OnInitDialog() { CDialog::OnInitDialog(); m_bModified = FALSE; CString cstrDataRefresh; long lDataRefresh; lDataRefresh = Profile.data_refresh; if ((lDataRefresh < 500) || (lDataRefresh > 20000)) { // 500-20,000 milli seconds lDataRefresh = 1000; Profile.data_refresh = 1000; } cstrDataRefresh.Format("%d",Profile.data_refresh); m_SpectrumRefreshTimeCtrl.SetWindowText(cstrDataRefresh); DppWinUSB.NumDevices = 0; GetDlgItem(IDC_STATIC_NUM_DEVICES)->SetWindowText("0"); DppWinUSB.CurrentDevice = 1; m_SpinSelectDevice.SetRange(1,1); m_SpinSelectDevice.SetPos(1); GetDlgItem(IDC_STATIC_NUM_DEVICES2)->SetWindowText("1"); lblDppFound[USB] .SetFontName("Arial") .SetFontSize(14) .SetTextColor(colorLightSlateGray) .SetBkColor(colorLightSteelBlue) .SetFontBold(TRUE); if (DppWinUSB.devInfo.deviceConnected) { lblDppFound[USB].ShowWindow(SW_SHOW); } else { lblDppFound[USB].ShowWindow(SW_HIDE); } CheckRadioButton(IDC_RADIO_COMMTYPE2, IDC_RADIO_COMMTYPE2, IDC_RADIO_COMMTYPE2); EnableCommFunctions(USB); // initialize interface selection OnBnClickedButtonCountDevices(); OnBnClickedButtonFindUsbDevice(); CenterWindow(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } BOOL CSelectDP5::DestroyWindow() { return CDialog::DestroyWindow(); } void CSelectDP5::OnBnClickedRadioCommtype2() { EnableCommFunctions(USB); OnBnClickedButtonCountDevices(); OnBnClickedButtonFindUsbDevice(); } void CSelectDP5::OnBnClickedButtonFindUsbDevice() { TRANSMIT_PACKET_TYPE XmtCmd; BOOL HaveBuffer; BOOL SentPkt; string strUSBStatus; DPP_USB_STATUS USBStatus; //0=do nothing, 1=new connect, 2=cannot connect long pktLen; BYTE buffer[4096] = {0}; OnBnClickedButtonCloseDevice(); USBStatus = DppWinUSB.FindUSBDevice(DppWinUSB.CurrentDevice - 1); if (USBStatus != dusbStatNoAction) { strUSBStatus = DppWinUSB.USBStatusString(USBStatus); GetDlgItem(IDC_EDIT_STATUS2)->SetWindowText(strUSBStatus.c_str()); if (USBStatus == dusbStatNewConnect) { // new usb connection } } if (DppWinUSB.devInfo.deviceConnected) { lblDppFound[USB].ShowWindow(SW_SHOW); DppWinUSB.isDppFound = TRUE; XmtCmd = XMTPT_SEND_NETFINDER_PACKET; HaveBuffer = SndCmd.DP5_CMD(DP5Proto.BufferOUT, XmtCmd); if (HaveBuffer) { SentPkt = DppWinUSB.SendPacketUSB(DP5Proto.BufferOUT, DppWinUSB.devInfo, DP5Proto.PacketIn); } if (SentPkt) { FindDppUSB.InitAllEntries(); pktLen = (long)((DP5Proto.PacketIn[4] * 256) + DP5Proto.PacketIn[5] + 8); // temporarily save the length field for (int i=0; i< pktLen;i++) { buffer[i] = DP5Proto.PacketIn[i+6]; } FindDppUSB.AddEntry(&FindDppUSB.DppEntries[0], buffer, 0); CString cstrStatus; CString cstrDeviceInfo; cstrDeviceInfo.Format("USB Device %d of %d", DppWinUSB.CurrentDevice, DppWinUSB.NumDevices); cstrStatus = FindDppUSB.EntryToStrUSB(&FindDppUSB.DppEntries[0], cstrDeviceInfo); GetDlgItem(IDC_EDIT_STATUS2)->SetWindowText(cstrStatus); } } else { lblDppFound[USB].ShowWindow(SW_HIDE); DppWinUSB.isDppFound = FALSE; } } void CSelectDP5::EnableCommFunctions(int ControlGroup) { BOOL isDUSBEnabled; int ShowDUSBEnabled; isDUSBEnabled = FALSE; ShowDUSBEnabled = SW_HIDE; switch (ControlGroup) { case USB: isDUSBEnabled = TRUE; ShowDUSBEnabled = isDUSBEnabled * SW_SHOW; break; } // WinUsb GetDlgItem(IDC_BUTTON_FIND_USB_DEVICE)->EnableWindow(isDUSBEnabled); GetDlgItem(IDC_EDIT_STATUS2)->EnableWindow(isDUSBEnabled); GetDlgItem(IDC_BUTTON_COUNT_DEVICES)->EnableWindow(isDUSBEnabled); GetDlgItem(IDC_STATIC_NUM_DEVICES)->EnableWindow(isDUSBEnabled); GetDlgItem(IDC_STATIC_SELECT_USB_DEVICE_LABEL)->EnableWindow(isDUSBEnabled); GetDlgItem(IDC_SPIN_SELECT_DEVICE)->EnableWindow(isDUSBEnabled); GetDlgItem(IDC_STATIC_NUM_DEVICES2)->EnableWindow(isDUSBEnabled); GetDlgItem(IDC_BUTTON_CLOSE_DEVICE)->EnableWindow(isDUSBEnabled); GetDlgItem(IDC_EDIT_STATUS2)->ShowWindow(ShowDUSBEnabled); if (ControlGroup != USB) { lblDppFound[USB].SetNullColor(); } else { lblDppFound[USB].SetLastColor(); } GetDlgItem(IDC_RADIO_COMMTYPE2)->EnableWindow(TRUE); // WinUsb } void CSelectDP5::OnBnClickedOk() { // TODO: Add your control notification handler code here OnOK(); } void CSelectDP5::OnBnClickedButtonCountDevices() { CDppWinUSB DppWinUSB1; CString cstrNumDevices; DppWinUSB.NumDevices = DppWinUSB1.CountDP5WinusbDevices(); if (DppWinUSB.CurrentDevice > DppWinUSB.NumDevices) { DppWinUSB.CurrentDevice = 1; m_SpinSelectDevice.SetPos(1); GetDlgItem(IDC_STATIC_NUM_DEVICES)->SetWindowText("1"); } if (DppWinUSB.NumDevices > 1) { m_SpinSelectDevice.SetRange(1,DppWinUSB.NumDevices); m_SpinSelectDevice.EnableWindow(TRUE); } else { DppWinUSB.CurrentDevice = 1; m_SpinSelectDevice.SetPos(1); m_SpinSelectDevice.SetRange(1,1); GetDlgItem(IDC_STATIC_NUM_DEVICES)->SetWindowText("1"); m_SpinSelectDevice.EnableWindow(FALSE); } cstrNumDevices.Format("%d",DppWinUSB.NumDevices); GetDlgItem(IDC_STATIC_NUM_DEVICES)->SetWindowText(cstrNumDevices); UpdateData(FALSE); // DDEX from var to cntrl, ie sends var value to control } void CSelectDP5::OnDeltaposSpinSelectDevice(NMHDR *pNMHDR, LRESULT *pResult) { LPNMUPDOWN pNMUpDown = reinterpret_cast(pNMHDR); CString cstrPos; int iMax = 1; int iMin = 1; int iVal = 1; // old value int iPos = 1; // new value (if updated) int iDelta = 0; // direction of change int iNewPos = 1; // final value m_SpinSelectDevice.GetRange(iMin, iMax); if ((iMin != 1) || (iMax < 1)) { return; } // max/min value errors if (iMax == 1) { // 0 or 1 devices iNewPos = 1; } else { // max must be greater than 1 GetDlgItem(IDC_STATIC_NUM_DEVICES2)->GetWindowText(cstrPos); iVal = atoi(cstrPos); iPos = pNMUpDown->iPos; iDelta = pNMUpDown->iDelta; if (iVal != iPos) { // value was updated iNewPos = iPos; } else { // control just enabled, has delta but pos not updated iNewPos = iPos + iDelta; // update value if (iNewPos > iMax) { // do wrap if needed iNewPos = iMin; } else if (iNewPos < iMin) { iNewPos = iMax; } } } //cstrPos.Format("iVal %d iPos %d iDelta %d iNewPos %d", iVal, iPos, iDelta, iNewPos); cstrPos.Format("%d", iNewPos); //AfxMessageBox(cstrPos); DppWinUSB.CurrentDevice = iNewPos; GetDlgItem(IDC_STATIC_NUM_DEVICES2)->SetWindowText(cstrPos); *pResult = 0; } void CSelectDP5::OnBnClickedButtonCloseDevice() { DppWinUSB.devInfo.deviceConnected = FALSE; DppWinUSB.CloseWinUsbDevice(); WinusbNotify.RemoveDeviceNotification(); GetDlgItem(IDC_EDIT_STATUS2)->SetWindowText("Device Connection Closed"); lblDppFound[USB].ShowWindow(SW_HIDE); DppWinUSB.isDppFound = FALSE; } void CSelectDP5::OnBnClickedConnectOK() { CString cstrDataRefresh; long lDataRefresh; if (m_bModified) { m_SpectrumRefreshTimeCtrl.GetWindowText(cstrDataRefresh); lDataRefresh = atoi(cstrDataRefresh); if ((lDataRefresh < 500) || (lDataRefresh > 20000)) { // 500-20,000 milli seconds lDataRefresh = 1000; Profile.data_refresh = 1000; } else { Profile.data_refresh = lDataRefresh; } } CDialog::OnOK(); } void CSelectDP5::OnOK() { //DISABLE DEFAULT } void CSelectDP5::CopyTextToClip(CString strTitle, CString strDesc) { CString cstrText; cstrText = strTitle + "\r\n" + "\r\n" + strDesc + "\r\n" ; AfxGetApp()->m_pMainWnd->OpenClipboard() ; EmptyClipboard(); CSharedFile sf(GMEM_MOVEABLE|GMEM_DDESHARE|GMEM_ZEROINIT); sf.Write(cstrText, cstrText.GetLength()); HGLOBAL hMem = sf.Detach(); if (!hMem) return; SetClipboardData(CF_TEXT, hMem); CloseClipboard(); } void CSelectDP5::SetModified(BOOL bChanged) { m_bModified = bChanged; } void CSelectDP5::OnEnChangeSpectrumRefreshTime() { SetModified(TRUE); } void CSelectDP5::OnBnClickedButtonSpectrumRefreshDefault() { m_SpectrumRefreshTimeCtrl.SetWindowText("1000"); Profile.data_refresh = 1000; SetModified(FALSE); } BOOL CSelectDP5::OnHelpInfo(HELPINFO* pHelpInfo) { CString cstrHelp; if (pHelpInfo->iContextType == HELPINFO_WINDOW) { if (pHelpInfo->dwContextId == 0) { ::HtmlHelp(this->GetSafeHwnd(), theApp.m_pszHelpFilePath, HH_HELP_CONTEXT, HIDD_DP5_CONNECT); } else { // } } return true; }