×
Namespaces

Variants
Actions

Detecting accelerometer sensor on Symbian C++ (Hourglass)

From Nokia Developer Wiki
Jump to: navigation, search

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)
Featured Article
07 Sep
2008

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);
}
 
}

Application

Newnewamp.JPG

This page was last modified on 28 October 2011, at 08:21.
178 page views in the last 30 days.
×