Detecting accelerometer sensor on Symbian C++ (Hourglass)
This code snippet shows how to detect device sensors in Symbian C++.
Article Metadata
Tested with
Devices(s): Nokia N95
Article
Created: Diego Soares Lopes
(29 Aug 2008)
Last edited: hamishwillee
(28 Oct 2011)
Contents |
Overview
This code snippet shows how to detect device sensors in Symbian C++.
The example taken from an application in which the device accelerometer sensor is used to simulate a real hourglass on the Nokia N95. Every time you change device's position, an observer updates the acceleration values in each axis, ranging from -360 to 360.
Header
//SensorApi header
#include <rrsensorapi.h>
//CGravitation class inherits an interface MRRSensorDataListener
class CGravitationAppView : public CHourglass,...,MRRSensorDataListener
{
public:
...
//Virtual function inherits from MRRSensorDataListener
void HandleDataEventL (TRRSensorInfo aSensor, TRRSensorEvent aEvent);
...
private:
//Instance of CRRSensorApi
CRRSensorApi* iSensorApi;
Source
void CGravitationAppView::ConstructL( const TRect& aRect )
{
...
//Creating an array of sensors
RArray< TRRSensorInfo > sensorInfoArray;
//Using FindSensorsL to find all sensors in the device and
//fill the array with the available sensors
CRRSensorApi::FindSensorsL(sensorInfoArray);
if(sensorInfoArray.Count() > 0)
{
//Constructs the iSensorApi with an sensor in the array
iSensorApi = CRRSensorApi::NewL(sensorInfoArray[1]);
iSensorApi -> AddDataListener(this);
}
else
User::Leave(KErrNotSupported);
...
}
CgravitationAppView::~CGravitationAppView()
{
...
//releases memory allocated
if(iSensorApi)
{
delete iSensorApi;
iSensorApi = NULL;
}
...
}
//HandleDataEventL's implementation, Callback that updates the acceleration
void CGravitationAppView::HandleDataEventL (TRRSensorInfo aSensor, TRRSensorEvent aEvent)
{
TInt i;
for(i = 0; i < iGrains.Count(); i++)
{
iGrains[i]->SetAcelXY(-aEvent.iSensorData2, -aEvent.iSensorData1);
}
}

