Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Detecting accelerometer sensor on Symbian C++ (Hourglass)

From 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 05:21.
286 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×