CPowerThread更新至v1.1.0

本文介绍了一个用于Windows CE平台的电源状态监控类CPowerThread的实现细节,包括如何获取电池电量百分比及电源状态,并提供了线程处理、消息队列创建及通知等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//========================================================================
//TITLE:
// CPowerThread更新至v1.1.0
//AUTHOR:
// norains
//DATE:
// Tuesday 25-December-2007
//Environment:
// EVC4.0 + Windows CE 5.0 Standard SDK
// EVC4.0 + SDK-WINCE5.0-MIPSII
// VS2005 + SDK-WINCE5.0-MIPSII
//========================================================================

相对于v1.0.0版本,有如下变动:

1.去掉GetInstance()函数,不用获取对象实例,可以直接声明对象实例使用

2.改进该类以令其在继承之后能正常工作.

3.增加OnNotifyPower()函数,可以不设置回调函数而通过重载该函数监视电源的变化.

v1.0.0版本:http://blog.youkuaiyun.com/norains/archive/2007/07/20/1700980.aspx


//////////////////////////////////////////////////////////////////////
//PowerThread.h:interfacefortheCPowerThreadclass.
//
//Version:
//1.1.0
//Date:
//2007.11.15
//////////////////////////////////////////////////////////////////////

#ifndefPOWERTHREAD_H
#definePOWERTHREAD_H



#include
"Pm.h"



//-----------------------------------------------------------------
//Enumdatatype
enumPowerStatusType
{
POW_UNKNOW,
//Unknowthestatus
POW_CHARGING,//It'schargingnow
POW_CHARGEFULL,//Fullcharge
POW_VLOW,//Thebatterylevelisverylow
POW_LOW,
POW_NORMAL,
POW_HIGH,
POW_VHIGH
//Thebatterylevelisveryhigh
};
//----------------------------------------------------------------

classCPowerThread
{
public:
BOOLResumnSign();
//Notsupportnow
voidGetCallbackFunction(void(**pCallbackFunc)(PowerStatusTypepowStatus,intiBatteryPercent));
voidSetCallbackFunction(void(*pCallbackFunc)(PowerStatusTypepowStatus,intiBatteryPercent));
voidStopCapture();
BOOLStartCapture();
BOOLGetRunStatus();
voidSetTimeout(ULONGulTime);

CPowerThread();
virtual~CPowerThread();


protected:
virtualvoidOnNotifyPower(PowerStatusTypepowStatus,intiBatteryPercent);


//Thecriticalsectionfunction
inlinevoidInitLock(){InitializeCriticalSection(&m_csLock);}
inline
voidLockThis(){EnterCriticalSection(&m_csLock);}
inline
voidUnLockThis(){LeaveCriticalSection(&m_csLock);}
inline
voidDelLock(){DeleteCriticalSection(&m_csLock);}


private:
PowerStatusTypeGetPowerStatus(PPOWER_BROADCASTpPowerInfo,
int*piPercent);
staticDWORDWINAPIPowerThread(PVOIDpArg);

BOOLm_bExitThread;
ULONGm_ulWaitTime;
BOOLm_bRunning;
CRITICAL_SECTIONm_csLock;

//Thisisforcallbackfunction.
void(*m_pNotifyPower)(PowerStatusTypepowStatus,intiBatteryPercent);
};

#endif//#ifndefPOWERTHREAD_H


//////////////////////////////////////////////////////////////////////
//PowerThread.cpp:implementationoftheCPowerThreadclass.
//
//////////////////////////////////////////////////////////////////////

#include
"stdafx.h"
#include
"PowerThread.h"
#include
"Msgqueue.h"


//--------------------------------------------------------------------
//Macrodefine
#defineDEFAULT_TIMEOUT1000//1000ms

//----------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////
//Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPowerThread::CPowerThread():
m_bExitThread(TRUE),
m_ulWaitTime(DEFAULT_TIMEOUT),
m_bRunning(FALSE),
m_pNotifyPower(NULL)
{
InitLock();
}

CPowerThread::
~CPowerThread()
{
DelLock();
}



//------------------------------------------------------------------
//Description:
//GetthelevelofpowerfromthePPOWER_BROADCASTstruct
//
//Parameters:
//pPowerInfo:[in]Thestructincludesthepowerinformation
//piPercent:[out]Thebatterylifepercent.
//
//ReturnValues:
//Thepowerstatus
//----------------------------------------------------------------
PowerStatusTypeCPowerThread::GetPowerStatus(PPOWER_BROADCASTpPowerInfo,int*piPercent)
{
PowerStatusTypepowStatus
=POW_UNKNOW;

if(!pPowerInfo)
{
returnPOW_UNKNOW;
}

PPOWER_BROADCAST_POWER_INFOppbpi
=(PPOWER_BROADCAST_POWER_INFO)pPowerInfo->SystemPowerState;
if(!ppbpi)
{
returnPOW_UNKNOW;
}

*piPercent=ppbpi->bBatteryLifePercent;

if(ppbpi->bACLineStatus==AC_LINE_ONLINE)
{
if(ppbpi->bBatteryFlag==BATTERY_FLAG_CHARGING)
{
//Charging
powStatus=POW_CHARGING;
}
else
{
//Maybefullcharging,ormaybenobattery
powStatus=POW_CHARGEFULL;
}
}
else
{
//Usebattery

if(0<=ppbpi->bBatteryLifePercent&&ppbpi->bBatteryLifePercent<=20)
{
powStatus
=POW_VLOW;
}
elseif(20<ppbpi->bBatteryLifePercent&&ppbpi->bBatteryLifePercent<=40)
{
powStatus
=POW_LOW;
}
elseif(40<ppbpi->bBatteryLifePercent&&ppbpi->bBatteryLifePercent<=60)
{
powStatus
=POW_NORMAL;
}
elseif(60<ppbpi->bBatteryLifePercent&&ppbpi->bBatteryLifePercent<=80)
{
powStatus
=POW_HIGH;
}
elseif(80<ppbpi->bBatteryLifePercent&&ppbpi->bBatteryLifePercent<=100)
{
powStatus
=POW_VHIGH;
}
else
{
powStatus
=POW_UNKNOW;
}
}

returnpowStatus;
}




//------------------------------------------------------------------
//Description:
//Threadtogetthepowerstatus
//----------------------------------------------------------------
DWORDWINAPICPowerThread::PowerThread(PVOIDpArg)
{
CPowerThread
*pObject=(CPowerThread*)pArg;

pObject
->m_bRunning=TRUE;

BYTEpbMsgBuf[
sizeof(POWER_BROADCAST)+sizeof(POWER_BROADCAST_POWER_INFO)];
PPOWER_BROADCASTppb
=(PPOWER_BROADCAST)pbMsgBuf;
MSGQUEUEOPTIONSmsgopts;

//Createourmessagequeue
memset(&msgopts,0,sizeof(msgopts));
msgopts.dwSize
=sizeof(msgopts);
msgopts.dwFlags
=0;
msgopts.dwMaxMessages
=0;
msgopts.cbMaxMessage
=sizeof(pbMsgBuf);
msgopts.bReadAccess
=TRUE;

HANDLErghWaits[
1]={NULL};
rghWaits[
0]=CreateMsgQueue(NULL,&msgopts);
if(!rghWaits[0])
{
//erro
return0x10;

}

HANDLEhReq
=NULL;
//Requestnotifications
hReq=RequestPowerNotifications(rghWaits[0],PBT_POWERINFOCHANGE);
if(!hReq)
{
CloseHandle(rghWaits[
0]);
//erro
return0x15;

}



while(pObject->m_bExitThread==FALSE)
{


DWORDdwWaitCode
=MsgWaitForMultipleObjectsEx(1,rghWaits,pObject->m_ulWaitTime,QS_ALLINPUT,MWMO_INPUTAVAILABLE);
if(dwWaitCode==WAIT_OBJECT_0)
{
DWORDdwSize,dwFlags;
BOOLbReadResult
=ReadMsgQueue(rghWaits[0],ppb,sizeof(pbMsgBuf),&dwSize,0,&dwFlags);
if(bReadResult==TRUE)
{
intiPowPercent;
PowerStatusTypepowStatus
=pObject->GetPowerStatus(ppb,&iPowPercent);

pObject
->LockThis();
if(pObject->m_pNotifyPower!=NULL)
{
pObject
->m_pNotifyPower(powStatus,iPowPercent);
}
pObject
->OnNotifyPower(powStatus,iPowPercent);
pObject
->UnLockThis();

}
else
{
//Weshouldnevergethere
break;
}
}


}
pObject
->m_bRunning=FALSE;

return0;
}




//------------------------------------------------------------------
//Description:
//Setthetimeoutforthewaitthread.ItisonlyfortheMsgWaitForMultipleObjectsEx()
//ThedefaultvalueisDEFAULT_TIMEOUT
//----------------------------------------------------------------
voidCPowerThread::SetTimeout(ULONGulTime)
{
m_ulWaitTime
=ulTime;
}


//------------------------------------------------------------------
//Description:
//Getthestatusofthread
//
//ReturnValues:
//TRUE:Thethreadisrunningforcapturingthepowerstatus.
//FALSE:Nothreadrunning.
//----------------------------------------------------------------
BOOLCPowerThread::GetRunStatus()
{
returnm_bRunning;
}


//------------------------------------------------------------------
//Description:
//startcapturingthepowerstatus.Ifthereisthreadrunning,
//itwillreturnFALSE;
//
//------------------------------------------------------------------
BOOLCPowerThread::StartCapture()
{
if(m_bRunning==TRUE)
{
returnFALSE;
}

m_bExitThread
=FALSE;

//Createthethreadforbattersampled
DWORDdwPwrThdID;
HANDLEhdThrd
=CreateThread(NULL,0,PowerThread,(void*)this,0,&dwPwrThdID);
if(hdThrd==NULL)
{
returnFALSE;
}
CloseHandle(hdThrd);

returnTRUE;
}



//-----------------------------------------------------------------------------
//Description:
//Stopcapturing.
//
//--------------------------------------------------------------------------------
voidCPowerThread::StopCapture()
{
m_bExitThread
=TRUE;
}


//------------------------------------------------------------------
//Description:
//Setthecallbackfunctionforreceivethepowerstatus
//------------------------------------------------------------------
voidCPowerThread::SetCallbackFunction(void(*pCallbackFunc)(PowerStatusTypepowStatus,intiBatteryPercent))
{
LockThis();
m_pNotifyPower
=pCallbackFunc;
UnLockThis();
}



//------------------------------------------------------------------
//Description:
//Getthecallbackfunction
//------------------------------------------------------------------
voidCPowerThread::GetCallbackFunction(void(**pCallbackFunc)(PowerStatusTypepowStatus,intiBatteryPercent))
{
LockThis();
*pCallbackFunc=m_pNotifyPower;
UnLockThis();
}


//------------------------------------------------------------------
//Description:
//Resumnthesignordertogetthecurrentpowerstatus,orthepower
//statuswillreturnuntillthepowerstatuschanged.
//------------------------------------------------------------------
BOOLCPowerThread::ResumnSign()
{
/*
BYTEpbMsgBuf[sizeof(POWER_BROADCAST)+sizeof(POWER_BROADCAST_POWER_INFO)];
PPOWER_BROADCASTppb=(PPOWER_BROADCAST)pbMsgBuf;
MSGQUEUEOPTIONSmsgopts;

//Createourmessagequeue
memset(&msgopts,0,sizeof(msgopts));
msgopts.dwSize=sizeof(msgopts);
msgopts.dwFlags=0;
msgopts.dwMaxMessages=0;
msgopts.cbMaxMessage=sizeof(pbMsgBuf);
msgopts.bReadAccess=TRUE;

HANDLErghWaits[1]={NULL};
rghWaits[0]=CreateMsgQueue(NULL,&msgopts);
if(!rghWaits[0])
{
//erro
returnFALSE;

}


//Requestnotifications
HANDLEhReq=RequestPowerNotifications(rghWaits[0],PBT_POWERINFOCHANGE);
if(!hReq)
{
CloseHandle(rghWaits[0]);
//erro
returnFALSE;

}




returnTRUE;
*/


returnFALSE;
}



//------------------------------------------------------------------
//Description:
//Notifythepower.It'susedascallbackfuntion.
//------------------------------------------------------------------
voidCPowerThread::OnNotifyPower(PowerStatusTypepowStatus,intiBatteryPercent)
{
return;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值