Namespaces
Variants
Actions
Revision as of 08:21, 28 October 2011 by hamishwillee (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Detecting accelerometer sensor on Symbian C++ (Hourglass)

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)
{{{width}}}
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

521 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved