Compensating Sensor Data for Display Orientation

You can use the sensor data compensator APIs to correct the display and device orientation data.

Before you begin using the sensor data compensator APIs, you must do the following:

  1. Open a Sensor Channel for communication. For details, see Using Sensor Channels APIs

  2. Implement the data listener interface. For details see, Receiving Data from Sensors

  3. Register as a listener with any of the sensor types using an instance of CSensrvChannel.

  4. Ensure that the data received from the sensor channel is not already compensated using the CSensrvChannel::GetPropertyL() function, as shown in the following example:

    // 'iChannel' contains an open channel
    TSensrvProperty property;
    TRAPD( err, iChannel->GetPropertyL( KSensrvPropIdChannelDataCompensation, KSensrvItemIndexNone, property ) );
    if ( err == KErrNone && property.PropertyType() == ESensrvIntProperty )
         {
         // Channel data is compensated, check the compensation type
         TInt compensationType;
         property.GetValue( compensationType );
         // 'compensationType' contains now a value from TSensorCompensationType enumeration declared in sensordatacompensationtypes.h
         }
    else
         {
         // Channel data is not compensated
         }

    For more information, see Retrieving channel properties .

The sensor data compensator APIs allows you to compensate sensor data values, based on:

  • Display Orientation, when the display is changed from portrait to landscape.

  • Device Orientation, when the keyboard is opened, resulting in the display being set at an angle to the keyboard. For example, N97.

  1. Create an instance of CSensorDataCompensator.
    iCompensator = CSensorDataCompensator::NewL(TSensrvAccelerometerAxisData::KDataTypeId,ESensorCompensateDeviceAndUIOrientation);
  2. Use the CSensorDataCompensator::Compensate() function to correct the sensor data received from the following channel types and related classes:
    Sensor Channel Type Data Class
    Accelerometer axis data TSensrvAccelerometerAxisData
    Magnetometer axis data TSensrvMagnetometerAxisData
    Accelerometer double-tap TSensrvTappingData

Example

The following example illustrates the usage of sensor data compensator APIs to correct axis data received from accelerometer sensor.

#include <sensordatacompensator.h> // link against sensordatacompensator.lib

void CMyClass::ConstructL()

   {
   // iSensorChannel already instantiated and registered
   if ( !AlreadyCompensated() )
          {
          iCompensator = CSensorDataCompensator::NewL( 
          TSensrvAccelerometerAxisData::KDataTypeId, 
           ESensorCompensateDeviceAndUIOrientation );
          }
   }

CMyClass::~CMyClass()

   {
   delete iCompensator;
   }

void CMyClass::DataReceived( CSensrvChannel& aChannel,

                            TInt /*aCount*/, 
                            TInt /*aDataLost*/ )
   {
    
   TPckgBuf  <TSensrvAccelerometerAxisData> dataBuf;
   iSensorChannel -> GetData( dataBuf );

   if ( iCompensator )
       {
       if ( iCompensator->Compensate( dataBuf ) == KErrNone )
           {
           // Now use the compensated data.
           }
       }

End the session with the sensor channel using the CSensrvChannel::CloseChannel() function.