Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 08:45, 7 April 2008.

CS000888 - Listening for accelerometer sensor data events

From Forum Nokia Wiki


ID CS000888 Creation date April 7, 2008
Platform S60 3rd Edition, FP1 Tested on devices Nokia N95 8GB
Category Symbian C++ Subcategory Hardware, Sensor


Keywords (APIs, classes, methods, functions): CRRSensorApi, TRRSensorInfo, CRRSensorApi::NewL(), CRRSensorApi::AddDataListener(), CRRSensorApi::RemoveDataListener(), MRRSensorDataListener::HandleDataEventL()

Overview

This snippet demonstrates how to listen for data events produced by the accelerometer sensor of the device. In practice, the control implements the MRRSensorDataListener interface which enables it to listen to the data events. Whenever a data event occurs, the MRRSensorDataListener::HandleDataEventL() function is called.

Note: In order to use the code, you need to install the sensor plug-in for your SDK.

This snippet can be self-signed.

MMP file

The following libraries are required:

LIBRARY  RRSensorApi.lib

Header file

#include <RRSensorApi.h>
// Accelerometer sensor UID
const TInt KAccelerometerSensorUID = 0x10273024;
class CMyControl : public CCoeControl, public MRRSensorDataListener
    {
    // ...
    
    private:  // Functions from base classes
        /**
         * From MRRSensorDataListener.
         * Callback function for receiving sensor data events.
         *
         * @param aSensor identifies the sensor that created the event.
         * @param aEvent contains data about created event.
         */
        void HandleDataEventL(TRRSensorInfo aSensor, TRRSensorEvent aEvent);
 
    private:
        /**
         * Initializes and registers accelerometer sensors.
         */
        void RegisterSensors();
 
        /**
         * Unregisters accelerometer sensors.
         */
        void UnregisterSensors();
 
    private:  // Data
        CRRSensorApi* iAccelerometerSensor;
    }

Source file

#include <RRSensorApi.h>
void CMyControl::ConstructL(const TRect& aRect)
    {
    // ...
 
    // Initialize and register accelerometer sensors
    RegisterSensors();
    
    // ...
    }
CMyControl::~CMyControl()
    {
    // ...
 
    // Unregister accelerometer sensors
    UnregisterSensors();
    
    // ...
    }
/**
 * Initializes and registers accelerometer sensors.
 */
void CMyControl::RegisterSensors()
    {
    RArray<TRRSensorInfo> sensorList;
    CleanupClosePushL(sensorList);
    
    // Retrieve list of available sensors
    CRRSensorApi::FindSensorsL(sensorList);
    
    // Get number of sensors available
    TInt sensorCount = sensorList.Count();
    
    for (TInt i = 0; i < sensorCount; i++)
        {
        // We are interested only in the accelerometer sensor now
        if (sensorList[i].iSensorId == KAccelerometerSensorUID)
            {
            iAccelerometerSensor = CRRSensorApi::NewL(sensorList[i]);
            
            // Register this control as accelerometer data listener
            iAccelerometerSensor->AddDataListener(this);
            
            break;
            }
        }
    
    CleanupStack::PopAndDestroy();  // sensorList
    }
/**
 * Unregisters accelerometer sensors.
 */
void CMyControl::UnregisterSensors()
    {
    // Unregister accelerometer data listener
    iAccelerometerSensor->RemoveDataListener();
    delete iAccelerometerSensor;
    iAccelerometerSensor = NULL;
    }
/**
 * Gets called whenever a data event occurs.
 */
void CMyControl::HandleDataEventL(TRRSensorInfo aSensor, TRRSensorEvent aEvent)
    {
    // A data event occurred. Handle it.
    // ...
    }

Postconditions

Whenever a data event occurs, CMyControl::HandleDataEventL() function is called.

Related Discussions
Thread Thread Starter Forum Replies Last Post
Gesture recognition djsid Python 9 2008-08-12 21:14
proximity light sensor in Nokia 7650 MuSymbian Symbian Networking & Messaging 4 2004-05-31 08:39
[announce] Creepy Invaders game haviital Python 10 2008-04-12 21:24
Not getting EMsvEntriesCreated event for SMS in MDL kiranpuranik General Symbian C++ 1 2006-03-01 05:35
Device loan? tote_b5 Developer Resources Feedback (Documentation, Examples, Training) 2 2007-06-26 20:43
 
Powered by MediaWiki