EngSrc/IESensorMonitor.cpp
changeset 3 93fff7023be8
equal deleted inserted replaced
2:e1e28b0273b0 3:93fff7023be8
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors: Juha Kauppinen, Mika Hokkanen
       
    13 * 
       
    14 * Description: Photo Browser
       
    15 *
       
    16 */
       
    17 
       
    18 #include "IESensorMonitor.h"
       
    19 #include "debug.h"
       
    20 
       
    21 #ifdef _ACCELEROMETER_SUPPORTED_
       
    22 
       
    23 #ifdef _S60_3x_ACCELEROMETER_
       
    24 const TInt KAccelerometerSensorUID = 0x10273024;
       
    25 #endif
       
    26 
       
    27 CIESensorMonitor* CIESensorMonitor::NewL(MIESensorMonitorObserver& aSensorObserver)
       
    28 {
       
    29     DP0_IMAGIC(_L("CIESensorMonitor::NewL++"));
       
    30     CIESensorMonitor* self=new (ELeave) CIESensorMonitor(aSensorObserver);
       
    31     CleanupStack::PushL(self);
       
    32     self->ConstructL();
       
    33     CleanupStack::Pop();
       
    34     
       
    35     DP0_IMAGIC(_L("CIESensorMonitor::NewL--"));
       
    36     return self;
       
    37 }
       
    38 
       
    39 
       
    40 void CIESensorMonitor::ConstructL()
       
    41 {
       
    42     DP0_IMAGIC(_L("CIESensorMonitor::ConstructL++"));
       
    43 
       
    44 #ifdef _S60_3x_ACCELEROMETER_
       
    45     // Noise filter for the accelerometer;
       
    46     iAccSensorDataX = 0;
       
    47     iAccSensorDataY = 0;
       
    48     iAccSensorDataZ = 0;
       
    49 
       
    50 #ifdef SENSOR_API_LOAD_DYNAMICALLY
       
    51     _LIT( KSensorApiDll, "RRSensorApi" );
       
    52     TUidType dllUid( KDynamicLibraryUid );
       
    53     TInt error = iSensorApi.Load( KSensorApiDll, dllUid );
       
    54     User::LeaveIfError(error);
       
    55 #endif    
       
    56     
       
    57     iSensorDataFilterX = CIESensorDataFilter::NewL();
       
    58     iSensorDataFilterY = CIESensorDataFilter::NewL();
       
    59     iSensorDataFilterZ = CIESensorDataFilter::NewL();
       
    60     
       
    61 #ifdef SENSOR_API_LOAD_DYNAMICALLY
       
    62     // If Sensor API library is dynamically linked
       
    63     typedef void ( *TFindSensorsLFunction )( RArray<TRRSensorInfo>& ); 
       
    64     TFindSensorsLFunction findSensorsLFunction = ( TFindSensorsLFunction )iSensorApi.Lookup( 1 );
       
    65     findSensorsLFunction( iSensorList );
       
    66 #else
       
    67     TRAPD( error , CRRSensorApi::FindSensorsL(iSensorList));
       
    68     if (error)
       
    69     {
       
    70     // Error found in sensors  
       
    71     }
       
    72 #endif    
       
    73     
       
    74     TInt sensorCount = iSensorList.Count();
       
    75     
       
    76     for (TInt i = 0; i < sensorCount; i++ )
       
    77     {
       
    78         if (iSensorList[i].iSensorId == KAccelerometerSensorUID)
       
    79         {
       
    80             iAccelerometerSensorIndex = i;       
       
    81             break;
       
    82         }
       
    83     }
       
    84 #endif _S60_3x_ACCELEROMETER_
       
    85 #ifdef _S60_5x_ACCELEROMETER_
       
    86     
       
    87     DP0_IMAGIC(_L("CIESensorMonitor::ConstructL - create CSensrvChannelFinder"));
       
    88     iSensrvChannelFinder = CSensrvChannelFinder::NewL();
       
    89     DP1_IMAGIC(_L("CIESensorMonitor::ConstructL - CSensrvChannelFinder created: %d"),iSensrvChannelFinder);
       
    90         
       
    91     iChannelInfoList.Reset();
       
    92     TSensrvChannelInfo mySearchConditions; // none, so matches all.
       
    93     DP0_IMAGIC(_L("CIESensorMonitor::ConstructL - iSensrvChannelFinder->FindChannelsL"));
       
    94     TRAPD(err, iSensrvChannelFinder->FindChannelsL(iChannelInfoList, mySearchConditions));
       
    95     if(err != KErrNone)
       
    96         {
       
    97         DP1_IMAGIC(_L("CIESensorMonitor::ConstructL - iSensrvChannelFinder->FindChannelsL ERROR: %d"), err);
       
    98         User::Leave(err);
       
    99         }
       
   100     DP0_IMAGIC(_L("CIESensorMonitor::ConstructL - iSensrvChannelFinder->FindChannelsL - OK"));
       
   101     
       
   102     TInt senIndex(0); // Sensor Selection 
       
   103     
       
   104     TBuf<256> text;
       
   105     text.Append(_L(" ----------------------------FOUND SENSOR=" ));
       
   106     text.AppendNum(iChannelInfoList.Count());
       
   107     DP0_IMAGIC(text);
       
   108     
       
   109     if(senIndex >= 0 && senIndex < iChannelInfoList.Count())
       
   110     {
       
   111         DP0_IMAGIC(_L("CIESensorMonitor::ConstructL++"));
       
   112         iSensrvSensorChannel = CSensrvChannel::NewL( iChannelInfoList[senIndex] );
       
   113         iSensrvSensorChannel->OpenChannelL();
       
   114     }
       
   115     
       
   116 #endif //_S60_5x_ACCELEROMETER_
       
   117     
       
   118     DP0_IMAGIC(_L("CIESensorMonitor::ConstructL++"));
       
   119 }
       
   120 
       
   121 
       
   122 CIESensorMonitor::CIESensorMonitor(MIESensorMonitorObserver& aSensorObserver)
       
   123     :iSensorObserver(aSensorObserver)
       
   124     {
       
   125     }
       
   126 
       
   127 
       
   128 CIESensorMonitor::~CIESensorMonitor()
       
   129 {
       
   130     DP0_IMAGIC(_L("CIESensorMonitor::~CIESensorMonitor"));
       
   131     
       
   132     StopMonitoring();
       
   133     
       
   134 #ifdef _S60_3x_ACCELEROMETER_ 
       
   135 
       
   136 #ifdef SENSOR_API_LOAD_DYNAMICALLY
       
   137     // Close dynamically loaded library
       
   138     iSensorApi.Close();
       
   139 #endif //SENSOR_API_LOAD_DYNAMICALLY    
       
   140     
       
   141     delete iAccelerometerSensor;
       
   142     iAccelerometerSensor = NULL;
       
   143 #endif
       
   144 #ifdef _S60_5x_ACCELEROMETER_ 
       
   145 
       
   146     if(iSensrvSensorChannel)
       
   147 	    iSensrvSensorChannel->CloseChannel();
       
   148     
       
   149     delete iSensrvSensorChannel;
       
   150     
       
   151     iChannelInfoList.Reset();
       
   152     delete iSensrvChannelFinder;
       
   153     
       
   154 #endif
       
   155 }
       
   156 
       
   157 void CIESensorMonitor::StartMonitoring()
       
   158 {
       
   159     DP0_IMAGIC(_L("CIESensorMonitor::StartMonitoring+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"));
       
   160 
       
   161 #ifdef _S60_3x_ACCELEROMETER_
       
   162     
       
   163 #ifdef SENSOR_API_LOAD_DYNAMICALLY
       
   164     // If Sensor API library is dynamically linked
       
   165     typedef CRRSensorApi* ( *TNewLFunction )( TRRSensorInfo ); 
       
   166     TNewLFunction newLFunction = ( TNewLFunction )iSensorApi.Lookup( 2 );
       
   167     iAccelerometerSensor = newLFunction( iSensorList[iAccelerometerSensorIndex] );
       
   168 #else    
       
   169     iAccelerometerSensor = CRRSensorApi::NewL(iSensorList[iAccelerometerSensorIndex]);
       
   170 #endif
       
   171 
       
   172     if (iAccelerometerSensor)
       
   173         iAccelerometerSensor->AddDataListener(this);
       
   174 #endif
       
   175 #ifdef _S60_5x_ACCELEROMETER_
       
   176     if(iSensrvSensorChannel)
       
   177         iSensrvSensorChannel->StartDataListeningL( this, 1,1,iUpdateInterval);         
       
   178 #endif
       
   179 }
       
   180 
       
   181 void CIESensorMonitor::StopMonitoring()
       
   182 {
       
   183     DP0_IMAGIC(_L("CSensorMonitor::StopMonitoring++"));
       
   184     
       
   185 #ifdef _S60_3x_ACCELEROMETER_
       
   186     if(iAccelerometerSensor)
       
   187         iAccelerometerSensor->RemoveDataListener();
       
   188 #endif
       
   189 #ifdef _S60_5x_ACCELEROMETER_
       
   190     if(iSensrvSensorChannel)
       
   191         iSensrvSensorChannel->StopDataListening();
       
   192     
       
   193 #endif
       
   194 }
       
   195 
       
   196 #ifdef _S60_3x_ACCELEROMETER_
       
   197 
       
   198 void CIESensorMonitor::HandleDataEventL(TRRSensorInfo aSensor, TRRSensorEvent aEvent)
       
   199     {
       
   200     TImagicDeviceOrientation deviceOrientation;
       
   201      // Axis Data
       
   202     switch (aSensor.iSensorId)
       
   203         {
       
   204         case KAccelerometerSensorUID:
       
   205             {
       
   206             iAccSensorDataX = iSensorDataFilterX->FilterSensorData(aEvent.iSensorData1); // X 
       
   207             iAccSensorDataY = iSensorDataFilterY->FilterSensorData(aEvent.iSensorData2); // Y
       
   208             iAccSensorDataZ = iSensorDataFilterZ->FilterSensorData(aEvent.iSensorData3); // Z
       
   209             
       
   210             TInt x = Abs(iAccSensorDataX);
       
   211             TInt y = Abs(iAccSensorDataY);
       
   212             TInt z = Abs(iAccSensorDataZ);
       
   213             
       
   214             // Calculate the orientation of the screen
       
   215             if (x>z && x>z) // Landscape
       
   216                 {
       
   217                 if (iAccSensorDataX > 0)
       
   218                     deviceOrientation = EOrientationDisplayRigthUp;
       
   219                 else
       
   220                     deviceOrientation = EOrientationDisplayLeftUp;
       
   221                 }
       
   222             if (y>x && y>z) // Portrait Mode
       
   223                 {
       
   224                 if (iAccSensorDataY > 0)
       
   225                     deviceOrientation = EOrientationDisplayUp;
       
   226                 else
       
   227                     deviceOrientation = EOrientationDisplayDown;
       
   228                 
       
   229                 }
       
   230             //if (z>x && z>y)  
       
   231             //    {
       
   232             //    if (iAccSensorDataZ)
       
   233                   //Not used  deviceOrientation = EOrientationDisplayDownwards;
       
   234             //    else
       
   235                   //Not used  deviceOrientation = EOrientationDisplayUpwards;
       
   236             //    } 
       
   237             
       
   238             iSensorObserver.SensorDataAvailable(deviceOrientation, EFalse);
       
   239             
       
   240             }
       
   241             break;
       
   242         default:
       
   243             break;
       
   244         }
       
   245     }
       
   246 #endif
       
   247 #ifdef _S60_5x_ACCELEROMETER_
       
   248 
       
   249  _LIT( KTimeString, "%-B%:0%J%:1%T%:2%S%.%*C4%:3%+B" );
       
   250  
       
   251 void CIESensorMonitor::DataReceived( CSensrvChannel& aChannel, TInt aCount, TInt aDataLost )
       
   252 {
       
   253     DP0_IMAGIC(_L("CSensorMonitor::DataReceived"));
       
   254     
       
   255     TBuf<250> progressBuf;
       
   256 	
       
   257 	TInt errErr(KErrNone);
       
   258 	
       
   259 	//iDataLostCount = iDataLostCount + aDataLost;
       
   260 	//iDataCount = iDataCount + aCount;
       
   261 
       
   262 	if( aChannel.GetChannelInfo().iChannelType == KSensrvChannelTypeIdOrientationData )
       
   263 	{
       
   264 		TSensrvOrientationData data;
       
   265 		
       
   266 		//TRAP(errErr,
       
   267 		for( TInt i = 0; i < aCount; i++ )
       
   268 	    {
       
   269 	    	TPckgBuf<TSensrvOrientationData> dataBuf;
       
   270 	    	aChannel.GetData( dataBuf );
       
   271 	    	data = dataBuf();
       
   272 	    	data.iTimeStamp.FormatL(progressBuf, KTimeString );
       
   273 	    }
       
   274 
       
   275 	    if(errErr != KErrNone)
       
   276 	    {
       
   277 	    	progressBuf.Zero();
       
   278 	    }
       
   279 	
       
   280     	switch ( data.iDeviceOrientation )
       
   281         {
       
   282         case EOrientationDisplayUp:
       
   283             {
       
   284             progressBuf.Append( _L( "Display up" ) );
       
   285             DP1_IMAGIC( _L( "Display up: %d" ),data.iDeviceOrientation );
       
   286             break;
       
   287             }
       
   288         case EOrientationDisplayDown:
       
   289             {
       
   290             progressBuf.Append( _L( "Display down" ) );
       
   291             DP1_IMAGIC( _L( "Display down: %d" ),data.iDeviceOrientation );
       
   292             break;
       
   293             }
       
   294         case EOrientationDisplayLeftUp:
       
   295             {
       
   296             progressBuf.Append( _L( "Display left up" ) );
       
   297             DP1_IMAGIC( _L( "Display left up: %d" ),data.iDeviceOrientation );
       
   298             break;
       
   299             }
       
   300         case EOrientationDisplayRigthUp:
       
   301             {
       
   302             progressBuf.Append( _L( "Display right up" ) );
       
   303             DP1_IMAGIC( _L( "Display right up: %d" ),data.iDeviceOrientation );
       
   304             break;
       
   305             }
       
   306         default:
       
   307             {
       
   308             progressBuf.Append( _L( "Unknown orientation" ) );
       
   309             DP1_IMAGIC( _L( "Unknown orientation: %d" ),data.iDeviceOrientation );
       
   310             break;
       
   311             }
       
   312         }
       
   313     	iSensorObserver.SensorDataAvailable(TImagicDeviceOrientation(data.iDeviceOrientation), EFalse);
       
   314 	}
       
   315 	else
       
   316 	{
       
   317 		progressBuf.Copy(_L("Channel = " ));
       
   318 	    progressBuf.AppendNum(aChannel.GetChannelInfo().iChannelType,EHex);
       
   319 	}
       
   320 	
       
   321 	DP0_IMAGIC(progressBuf);
       
   322 	
       
   323 }
       
   324 
       
   325 void CIESensorMonitor::DataError( CSensrvChannel& /*aChannel*/, TSensrvErrorSeverity /*aError*/)
       
   326 {
       
   327     DP0_IMAGIC(_L("CIESensorMonitor::DataReceived"));
       
   328 }
       
   329 
       
   330 #endif //_S60_5x_ACCELEROMETER_
       
   331 
       
   332 #endif //_ACCELEROMETER_SUPPORTED_
       
   333 // End of File
       
   334