qtmobility/plugins/sensors/s60_sensor_api/qs60sensorapiaccelerometer.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 //Symbian
       
    42 #include <e32std.h>
       
    43 #include <rrsensorapi.h>
       
    44 
       
    45 // Local
       
    46 #include "qs60sensorapiaccelerometer.h"
       
    47 
       
    48 // Constants
       
    49 const int KAccelerometerSensorUID = 0x10273024;
       
    50 
       
    51 const char *QS60SensorApiAccelerometer::id("s60sensorapi.accelerometer");
       
    52 
       
    53 QS60SensorApiAccelerometer::QS60SensorApiAccelerometer(QSensor *sensor)
       
    54     : QSensorBackend(sensor)
       
    55     , m_nativeSensor(NULL)
       
    56     , m_sampleFactor(0.0f)
       
    57 {
       
    58     TRAPD(err, findAndCreateNativeSensorL());
       
    59     if(err != KErrNone) {
       
    60         sensorStopped();
       
    61         sensorError(err);
       
    62     }
       
    63     
       
    64     setReading<QAccelerometerReading>(&m_reading);
       
    65     
       
    66     // http://www.st.com/stonline/products/literature/ds/12726/lis302dl.pdf
       
    67     // That 3D accelerometer inside N95 , N93i or N82 is from STMicroelectronics (type LIS302DL).
       
    68     // http://wiki.forum.nokia.com/index.php/Nokia_Sensor_APIs.
       
    69     // Sensor is set to 100Hz 2G mode and no public interface to switch it to 8G
       
    70     
       
    71     // 2G - mode
       
    72     addDataRate(100, 100);
       
    73     sensor->setDataRate(100);
       
    74     addOutputRange(-22.418, 22.418, 0.17651);
       
    75     setDescription(QLatin1String("lis302dl"));
       
    76     
       
    77     //Synbian interface gives values between -680 - 680
       
    78     m_sampleFactor =  this->sensor()->outputRanges()[0].maximum / 680.0f;
       
    79 }
       
    80 
       
    81 QS60SensorApiAccelerometer::~QS60SensorApiAccelerometer()
       
    82 {
       
    83     stop();
       
    84     delete m_nativeSensor;
       
    85     m_nativeSensor = NULL;
       
    86 }
       
    87 
       
    88 void QS60SensorApiAccelerometer::start()
       
    89 {
       
    90     if(!m_nativeSensor)
       
    91         return;
       
    92     
       
    93     m_nativeSensor->AddDataListener(this);    
       
    94 }
       
    95 
       
    96 void QS60SensorApiAccelerometer::stop()
       
    97 {
       
    98     if(!m_nativeSensor)
       
    99         return;
       
   100     
       
   101     m_nativeSensor->RemoveDataListener();
       
   102 }
       
   103 
       
   104 void QS60SensorApiAccelerometer::poll()
       
   105 {
       
   106     //empty implementation
       
   107 }
       
   108 
       
   109 
       
   110 void QS60SensorApiAccelerometer::HandleDataEventL(TRRSensorInfo aSensor, TRRSensorEvent aEvent)
       
   111 {
       
   112     if (aSensor.iSensorId != KAccelerometerSensorUID) 
       
   113         return; 
       
   114     
       
   115     TTime time;
       
   116     time.UniversalTime();
       
   117     m_reading.setTimestamp(time.Int64());
       
   118     m_reading.setX((qreal)aEvent.iSensorData2 * m_sampleFactor);
       
   119     m_reading.setY((qreal)aEvent.iSensorData1 * -m_sampleFactor);
       
   120     m_reading.setZ((qreal)aEvent.iSensorData3 * -m_sampleFactor);
       
   121     newReadingAvailable();
       
   122 }
       
   123 
       
   124 void QS60SensorApiAccelerometer::findAndCreateNativeSensorL()
       
   125 {
       
   126     if(m_nativeSensor)
       
   127         return;
       
   128     
       
   129     RArray<TRRSensorInfo> sensorList;
       
   130     CRRSensorApi::FindSensorsL(sensorList);
       
   131     CleanupClosePushL(sensorList);
       
   132 
       
   133     TInt index = 0;
       
   134     do {
       
   135         if (sensorList[index].iSensorId == KAccelerometerSensorUID)
       
   136             m_nativeSensor = CRRSensorApi::NewL(sensorList[index]);
       
   137     } while(!m_nativeSensor && ++index < sensorList.Count());
       
   138         
       
   139     if (!m_nativeSensor)
       
   140         User::Leave(KErrHardwareNotAvailable);
       
   141     
       
   142     CleanupStack::PopAndDestroy(&sensorList);
       
   143 }