qtmobility/plugins/sensors/symbian/compasssym.cpp
changeset 4 90517678cc4f
child 11 06b8e2af4411
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "compasssym.h"
       
    43 
       
    44 /**
       
    45  * set the id of the accelerometer sensor
       
    46  */
       
    47 const char *CCompassSym::id("sym.compass");
       
    48 
       
    49 /**
       
    50  * Factory function, this is used to create the compass object
       
    51  * @return CCompassSym if successful, leaves on failure
       
    52  */
       
    53 CCompassSym* CCompassSym::NewL(QSensor *sensor)
       
    54     {
       
    55     CCompassSym* self = new (ELeave) CCompassSym(sensor);
       
    56     CleanupStack::PushL(self);
       
    57     self->ConstructL();
       
    58     CleanupStack::Pop();
       
    59     return self;    
       
    60     }
       
    61 
       
    62 /**
       
    63  * Destructor
       
    64  * Closes the backend resources
       
    65  */
       
    66 CCompassSym::~CCompassSym()
       
    67     {
       
    68     delete iMagnetometer;
       
    69     Close();
       
    70     }
       
    71 
       
    72 /**
       
    73  * Default constructor
       
    74  */
       
    75 CCompassSym::CCompassSym(QSensor *sensor):CSensorBackendSym(sensor)
       
    76     {
       
    77     setReading<QCompassReading>(&iReading);
       
    78     iBackendData.iSensorType = KSensrvChannelTypeIdMagneticNorthData;
       
    79     }
       
    80 
       
    81 /**
       
    82  * start() overrides CSensorBackendSym::start()
       
    83  * This is to allow starting magnetometer before stopping the compass 
       
    84  */
       
    85 void CCompassSym::start()
       
    86     {
       
    87     // start the magnetometer
       
    88     iMagnetometer->start();
       
    89     // start the compass backend
       
    90     CSensorBackendSym::start();
       
    91     }
       
    92 
       
    93 /**
       
    94  * stop() overrides CSensorBackendSym::stop()
       
    95  * This is to allow stopping magnetometer before stopping the compass 
       
    96  */
       
    97 void CCompassSym::stop()
       
    98     {
       
    99     // stop the magnetometer
       
   100     iMagnetometer->stop();
       
   101     // start the compass backend
       
   102     CSensorBackendSym::stop();
       
   103     }
       
   104 
       
   105 /*
       
   106  * RecvData is used to retrieve the sensor reading from sensor server
       
   107  * It is implemented here to handle compass sensor specific
       
   108  * reading data and provides conversion and utility code
       
   109  */  
       
   110 void CCompassSym::RecvData(CSensrvChannel &aChannel)
       
   111     {
       
   112     TPckg<TSensrvMagneticNorthData> proxpkg( iData );
       
   113     TInt ret = aChannel.GetData( proxpkg );
       
   114     if(KErrNone != ret)
       
   115         {
       
   116         // If there is no reading available, return without setting
       
   117         return;
       
   118         }
       
   119     // Get a lock on the reading data
       
   120     iBackendData.iReadingLock.Wait();    
       
   121     iReading.setAzimuth(iData.iAngleFromMagneticNorth);
       
   122     // Retrieve and set the calibration level
       
   123     iReading.setCalibrationLevel(iMagnetometer->GetCalibrationLevel());
       
   124     // Set the timestamp
       
   125     iReading.setTimestamp(iData.iTimeStamp.Int64());
       
   126     // Release the lock
       
   127     iBackendData.iReadingLock.Signal();
       
   128     }
       
   129 
       
   130 /**
       
   131  * Second phase constructor
       
   132  * Initialize the backend resources
       
   133  */
       
   134 void CCompassSym::ConstructL()
       
   135     {
       
   136     // Initialize the backend
       
   137     InitializeL();
       
   138     // Create magnetometer, this is required as in sensor server,
       
   139     // calibration data is available only for magnetometer
       
   140     iMagnetometer = CMagnetometerSensorSym::NewL(NULL);
       
   141     // Listen only for property change on magnetometer as we are
       
   142     // interested only in calibration property
       
   143     iMagnetometer->SetListening(EFalse, ETrue);
       
   144     }
       
   145