Resources | developer.brewmp.com Resources | developer.brewmp.com

Developer

resources

Example: Using the IBattery interface

An example of how the IBattery and related interfaces can be used to obtain information about the state of the battery.

Note: This example is intended to illustrate the interfaces, and is not intended to be compiled and executed.

#include "AEEBattery.h"
#include "AEEIModel.h"

// In the event handler

IShell *piShell;
IBattery *piBattery;
AEEBatteryStatus bs;
uint32 dwBatteryLevel;
AEEBatteryChargerStatus cs;
boolean bExtPwr;
IModel *piModel;
ModelListener sBatteryModelListener;
int nErr;
myapp *pme;

// assume pIShell points to a valid IShell
nErr = ISHELL_CreateInstance(pIShell, AEECLSID_BATTERY, (void**)&piBattery);

if(AEE_SUCCESS != nErr) {
   return nErr;
}

nErr = IBATTERY_GetBatteryStatus(piBattery, &bs);
if(AEE_SUCCESS == nErr) {
		 // Battery Status is available in bs
}

nErr = IBATTERY_GetBatteryLevel(piBattery, &dwBatteryLevel);
         
if(AEE_SUCCESS == nErr) {
		 uint16 BatteryLevel, BatteryScale;

		 // Battery Level is available in dwBatteryLevel
		 BatteryLevel = GETBATTERYLEVEL(dwBatteryLevel);
		 BatteryScale = GETBATTERYSCALE(dwBatteryLevel);
}

nErr = IBATTERY_GetBatteryChargerStatus(piBattery, &cs);

if(AEE_SUCCESS == nErr){
		 // Battery Charger Status is available in cs
}

nErr = IBATTERY_IsExternalPowerPresent(piBattery, &bExtPwr);
if(AEE_SUCCESS == nErr){
		 // Battery External Power Present Status is in bExtPwr
}

nErr = IBATTERY_QueryInterface(piBattery, AEEIID_MODEL, (void**)&piModel);

if(AEE_SUCCESS == nErr) {
   IMODEL_AddListenerEx(piModel, sBatteryModelListener, (PFNLISTENER)SampleApp_Battery_ModelListener, pme);
   IMODEL_Release(piModel);
}

//... other code


//===========================================================================
//
// ModelListener for IBattery model events
//
//===========================================================================
static void SampleApp_Battery_ModelListener(myapp *pme, ModelEvent *pEvent)
{
   if (EVT_MDL_BATTERY_STATUS_CHANGE == pEvent->evCode) {
		    // status change model event received
   }
	  else if (EVT_MDL_BATTERY_LEVEL_CHANGE == pEvent->evCode) {
		    // level change model event received
	  }
  	else if (EVT_MDL_BATTERY_CHARGERSTATUS_CHANGE == pEvent->evCode) {
		    // charger status change model event received
  	}
	  else if (EVT_MDL_BATTERY_EXTPWR_CHANGE == pEvent->evCode) {
    		if (TRUE == (uint32)pEvent->dwParam) {
      			// External power present model event received
		    }
    		else {
			      // External power not present model event received
		    }
	  }
}