⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maindlg.cpp

📁 在WINCE 5.0 上基于Maxim 17040 电池采样测试程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    }
	if(!InitMFPRM())
    {
        RETAILMSG(1, (_T("Init MFP Resoruce Manage ERROR\r\n")));
        return FALSE;
    }
	
	if(!I2CInit())
	{
		RETAILMSG(1, (TEXT("MaxInitialize : Initialize I2C failed")));
		return FALSE;
	}
	
	m_dwLogTimer = 0;
	m_dwSampleTimer = 0;
	m_uTrayID = 1000;
	
	BYTE byPer = 100;
	sampleADC(&byPer, &m_dwVol);
	m_dwPercent = byPer;
	
	m_bFlash = TRUE;
	::SendMessage(GetDlgItem(IDC_RADIO1), BM_SETCHECK, BST_CHECKED, 0);
	::SendMessage(GetDlgItem(IDC_RADIO2), BM_SETCHECK, BST_UNCHECKED, 0);
	
	m_dwSampleTimer = SetTimer(SAMPLETIMER, 1000);

	DoDataExchange(FALSE);
	return TRUE;
}

LRESULT CMainDlg::OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CAboutDlg dlg;
	dlg.DoModal();
	return 0;
}

LRESULT CMainDlg::OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	// TODO: Add validation code 
	CloseDialog(wID);
	return 0;
}

LRESULT CMainDlg::OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CloseDialog(wID);
	return 0;
}

LRESULT CMainDlg::OnTimer(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	if((DWORD)wParam == m_dwLogTimer)
	{
		LogVolPercent2File();
	}
	else if((DWORD)wParam == SAMPLETIMER)
	{
		BYTE byPer = 100;
		sampleADC(&byPer, &m_dwVol);
		m_dwPercent = byPer;
		DoDataExchange(FALSE);	
	}

	return 0;
}

LRESULT CMainDlg::OnDestory(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	
	if (gs_pi2cregs)
    {
        MmUnmapIoSpace((PVOID)gs_pi2cregs, sizeof(XLLP_VUINT32_T));
        mfp_reg = NULL;
    }
	if (gs_pOstRegs)
    {
        MmUnmapIoSpace((PVOID)gs_pOstRegs, sizeof(XLLP_VUINT32_T));
        mfp_reg = NULL;
    }
	if (gs_pClockRegs)
    {
        MmUnmapIoSpace((PVOID)gs_pClockRegs, sizeof(XLLP_VUINT32_T));
        mfp_reg = NULL;
    }
	if (mfp_reg)
    {
        MmUnmapIoSpace((PVOID)mfp_reg, sizeof(XLLP_VUINT32_T));
        mfp_reg = NULL;
    }
	
	DeInitMFPRM();	
	CloseHandle(g_hI2CMutex);

	return 0;
}

LRESULT CMainDlg::OnSize(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
{
	if(wParam == SIZE_MINIMIZED)
	{
		NOTIFYICONDATA nid;
		nid.cbSize = sizeof(NOTIFYICONDATA);
		nid.hWnd = m_hWnd;
		nid.uFlags = NIF_MESSAGE | NIF_ICON;
		nid.uCallbackMessage = WM_TRAYICON_NOTIFY;
		nid.hIcon = NULL;
		nid.szTip[0] = 0;
		nid.uID = m_uTrayID;

		nid.hIcon = (HICON)LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
		Shell_NotifyIcon(NIM_ADD, &nid);
		
		ShowWindow(SW_HIDE);
	}
	bHandled = FALSE;	
	return 0;
}
LRESULT CMainDlg::OnTrayIconNotify(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
	NOTIFYICONDATA nid;
	nid.cbSize = sizeof(NOTIFYICONDATA);
    nid.hWnd = m_hWnd;
    nid.uFlags = NIF_MESSAGE | NIF_ICON;
    nid.uCallbackMessage = WM_TRAYICON_NOTIFY;
    nid.hIcon = NULL;
    nid.szTip[0] = 0;
    nid.uID = m_uTrayID;

	if (wParam == m_uTrayID)
    {
        if (lParam == WM_LBUTTONDBLCLK)
        {
            ShowWindow(SW_SHOW);
			::SetForegroundWindow(m_hWnd);
			
			Shell_NotifyIcon(NIM_DELETE, &nid);
			return TRUE;
		}
    }
	return 0;
}

LRESULT CMainDlg::OnHide(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	NOTIFYICONDATA nid;
	nid.cbSize = sizeof(NOTIFYICONDATA);
    nid.hWnd = m_hWnd;
    nid.uFlags = NIF_MESSAGE | NIF_ICON;
    nid.uCallbackMessage = WM_TRAYICON_NOTIFY;
    nid.hIcon = NULL;
    nid.szTip[0] = 0;
    nid.uID = m_uTrayID;

	nid.hIcon = (HICON)LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	Shell_NotifyIcon(NIM_ADD, &nid);
	
	ShowWindow(SW_HIDE);
	return 0;
}

LRESULT CMainDlg::OnStartloging(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	TCHAR szLogfile[MAX_PATH] = {0};

	SYSTEMTIME st = {0};
	::GetLocalTime(&st);

	_stprintf(szLogfile, _T("%sBattTest%04d%02d%02d%02d%02d.txt"), m_bFlash ? g_szFlashPath : g_szSDMMCPath, st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute);
	if(!CreateLog(szLogfile, TRUE))
	{
		MessageBox(_T("创建LOG文件失败,请查看是否有FLASH或者SD/MMC 卡!"));
		return 0;
	}
	
	if(g_byPrePercent == 0)
		g_byPrePercent = m_dwPercent;		
	LogVolPercent2File();
	m_dwLogTimer = SetTimer(LOGINGTIMER, 10*1000);
	
	::EnableWindow(GetDlgItem(IDC_RADIO1), FALSE);
	::EnableWindow(GetDlgItem(IDC_RADIO2), FALSE);
	::EnableWindow(GetDlgItem(IDC_BUTTON2), FALSE);	

	NOTIFYICONDATA nid;
	nid.cbSize = sizeof(NOTIFYICONDATA);
    nid.hWnd = m_hWnd;
    nid.uFlags = NIF_MESSAGE | NIF_ICON;
    nid.uCallbackMessage = WM_TRAYICON_NOTIFY;
    nid.hIcon = NULL;
    nid.szTip[0] = 0;
    nid.uID = m_uTrayID;

	nid.hIcon = (HICON)LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	Shell_NotifyIcon(NIM_ADD, &nid);
	
	ShowWindow(SW_HIDE);
	return 0;
}

	
LRESULT CMainDlg::OnRadioButton(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	switch(wID)
	{
	case IDC_RADIO2:
		::SendMessage(GetDlgItem(IDC_RADIO1), BM_SETCHECK, BST_UNCHECKED, 0);
		m_bFlash = FALSE;
		break;
	case IDC_RADIO1:
		::SendMessage(GetDlgItem(IDC_RADIO2), BM_SETCHECK, BST_UNCHECKED, 0);
		m_bFlash = TRUE;
		break;
	default:
		break;
	}
	return 0;
}
void CMainDlg::CloseDialog(int nVal)
{
	if(m_dwLogTimer)
		KillTimer(m_dwLogTimer);
	if(m_dwSampleTimer)
		KillTimer(m_dwSampleTimer);

	DeleteLog();
	DestroyWindow();
	::PostQuitMessage(nVal);
}

void CMainDlg::LogVolPercent2File()
{
	TCHAR buf[MAX_PATH] = {0};	
	
	if(m_dwPercent != g_byPrePercent)			// 如果百分比变化了,则计算上次百分比对应电压的平均值
	{
		DWORD dwAvgVol = 0;
		for(int i=0; i<g_dwCnt && i<50; i++)
		{
			dwAvgVol += g_dwVOl[i];
		}
		if(g_dwCnt >= 50)
			dwAvgVol = dwAvgVol/50;
		else if(g_dwCnt)
			dwAvgVol = dwAvgVol/g_dwCnt;
		
		_stprintf(buf, _T("Average------------------{%dmV, %d%%}"), dwAvgVol, g_byPrePercent);
		WriteLogInfo(buf, FALSE);
		g_dwCnt = 0;
		g_byPrePercent = m_dwPercent;
	}
	g_dwVOl[g_dwCnt++] = m_dwVol;
	_stprintf(buf, _T("{%dmV, %d%%}"), m_dwVol, m_dwPercent);
	WriteLogInfo(buf, FALSE);
}

HRESULT CMainDlg::ActivatePreviousInstance(HINSTANCE hInstance)
{		
	const TCHAR* pszClass = _T("BatteryTest");
	
	// Orginally 500ms in SmartPhone 2003 App Wizard generated code
	// A lower value will result in a more responsive start-up of 
	// the existing instance or termination of this instance.
	const DWORD dRetryInterval = 100; 
	
	// Orginally 5 in SmartPhone 2003 App Wizard generated code
	// Multiplied by 5, since wait time was divided by 5.
	const int iMaxRetries = 25;
	
	for(int i = 0; i < iMaxRetries; ++i)
	{
		// Don't need ownership of the mutex
		HANDLE hMutex = CreateMutex(NULL, FALSE, pszClass);
		
		DWORD dw = GetLastError();
		
		if(NULL == hMutex)
		{
			HRESULT hr;
			
			switch(dw)
			{
			case ERROR_INVALID_HANDLE:
				// A non-mutext object with this name already exists.
				hr = E_INVALIDARG;
				break;
			default:
				// This should never happen...
				hr = E_FAIL;
			}
			
			return hr;
		}
		
		// If the mutex already exists, then there should be another instance running
		if(ERROR_ALREADY_EXISTS == dw)
		{
			// Just needed the error result, in this case, so close the handle.
			CloseHandle(hMutex);
			
			// Try to find the other instance, don't need to close HWND's.
			// Don't check title in case it is changed by app after init.
			HWND hwnd = FindWindow(_T("Dialog"), pszClass);
			
			if(NULL == hwnd)
			{
				// It's possible that the other istance is in the process of starting up or shutting down.
				// So wait a bit and try again.
				Sleep(dRetryInterval);
				continue;
			}
			else
			{
				// Set the previous instance as the foreground window
// 				::ShowWindow(hwnd, SW_SHOWNORMAL);
				
				// The "| 0x1" in the code below activates the correct owned window 
				// of the previous instance's main window according to the SmartPhone 2003
				// wizard generated code.
//				if(0 != SetForegroundWindow(reinterpret_cast<HWND>(reinterpret_cast<ULONG>(hwnd) | 0x1)))
//				{
//					// S_FALSE indicates that another instance was activated, so this instance should terminate.
//					return S_FALSE;
// 				}
				::PostMessage(hwnd, WM_TRAYICON_NOTIFY, 1000, WM_LBUTTONDBLCLK);
				return S_FALSE;
			}
		}
		else
		{
			// This is the first istance, so return S_OK.
			// Don't close the mutext handle here.
			// Do it on app shutdown instead.
			return S_OK;
		}
		
		// 增加这个延时,以解决启动时会启动两个窗口的问题
		Sleep(dRetryInterval);
	}
	
	// The mutex was created by another instance, but it's window couldn't be brought
	// to the foreground, so ssume  it's not a invalid instance (not this app, hung, etc.)
	// and let this one start.
	return S_OK;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -