examples/SFExamples/RecipeEx/src/SensorListener.cpp

00001 // Symbian Foundation Example Code
00002 // 
00003 // This software is in the public domain. No copyright is claimed, and you 
00004 // may use it for any purpose without license from the Symbian Foundation.
00005 // No warranty for any purpose is expressed or implied by the authors or
00006 // the Symbian Foundation. 
00007 
00008 
00009 #include <SensrvDataListener.h> 
00010 #include <sensrvchannelfinder.h> 
00011 #include <sensrvchannel.h> 
00012 #include <sensrvaccelerometersensor.h>
00013 #include <e32math.h>
00014 
00015 #include "SensorListener.h"     
00016 #include "LogContainer.h"       
00017 
00018 // Constants
00019 const TReal KIntervalBetweenShakes = 0.5;
00020 const TReal KShakeThreshold = 100;
00021 
00025 CSensorListener* CSensorListener::NewL(CLogContainer* aLogView)
00026         {
00027         CSensorListener* self = CSensorListener::NewLC(aLogView);
00028         CleanupStack::Pop(self);
00029         return self;
00030         }
00031 
00035 CSensorListener* CSensorListener::NewLC(CLogContainer* aLogView)
00036         {
00037         CSensorListener* self = new (ELeave) CSensorListener(aLogView);
00038         CleanupStack::PushL(self);
00039         self->ConstructL();
00040         return self;
00041         }
00042 
00044 CSensorListener::~CSensorListener()
00045         {
00046         if(iSensrvChannel)
00047         {
00048                 iSensrvChannel->StopDataListening();
00049                 iSensrvChannel->CloseChannel();
00050         }
00051         
00052         delete iSensrvChannel;
00053         iChannelInfoList.Reset();
00054         delete iSensrvChannelFinder;
00055         }
00056 
00060 CSensorListener::CSensorListener(CLogContainer* aLogView) : iLogView(aLogView)
00061         {
00062         }
00063 
00065 void CSensorListener::ConstructL()
00066         {
00067         _LIT(KSensorNotSupported, "Sensor not supported!");
00068         _LIT(KIntroduction, "Shake detector...");
00069 
00070         iLogView->LogEntryL(KIntroduction);
00071         
00072         iSensrvChannelFinder = CSensrvChannelFinder::NewL(); 
00073         
00074         TSensrvChannelInfo searchConditions; // match for AccelerometerXYZ
00075         searchConditions.iChannelType = KSensrvChannelTypeIdAccelerometerXYZAxisData;
00076         iSensrvChannelFinder->FindChannelsL(iChannelInfoList,searchConditions);
00077                                 
00078         if (iChannelInfoList.Count()<1)
00079                 {
00080                 // The sensor is not supported on this device
00081                 iLogView->LogEntryL(KSensorNotSupported);
00082                 }
00083         else
00084                 {
00085                 // Create the sensor channel using the first returned in the search
00086                 iSensrvChannel = CSensrvChannel::NewL(iChannelInfoList[0]);
00087                 iSensrvChannel->OpenChannelL();
00088                 
00089                 // Start Listening for data. 
00090                 iSensrvChannel->StartDataListeningL( 
00091                                 this,   
00092                                 1,              /* The desired and maximum number of measured data items */ 
00093                                                 /* before a callback. A value of 1 means we will not miss a single item. */
00094                                 1,
00095                                 0);
00096                 
00097                 // Set the current univeral time
00098                 iLastTime.UniversalTime();
00099                 }
00100         
00101         }
00102 
00119 void CSensorListener::DataReceived( CSensrvChannel& aChannel, 
00120                                                    TInt aCount, 
00121                                                    TInt /*aDataLost*/ )
00122         {
00123         _LIT(KShaken, "%S: Shake Detected");
00124         
00125         TTime currentTime;
00126         currentTime.UniversalTime();
00127         
00128         TTimeIntervalSeconds timeDiff;
00129         currentTime.SecondsFrom(iLastTime, timeDiff);
00130         
00131         if (timeDiff.Int()>KIntervalBetweenShakes)
00132                 {
00133                 // Only respond to data from the channel type we are interested in.
00134                 if ( aChannel.GetChannelInfo().iChannelType ==  KSensrvChannelTypeIdAccelerometerXYZAxisData )
00135                         {       
00136                         TSensrvAccelerometerAxisData data;
00137                         for( TInt i = 0; i < aCount; i++ )
00138                                 {
00139                                 TPckgBuf<TSensrvAccelerometerAxisData> dataBuf;
00140                                 aChannel.GetData( dataBuf );
00141                                 data = dataBuf();
00142 
00143                                 TInt x = data.iAxisX;
00144                                 TInt y = data.iAxisY;
00145                                 TInt z = data.iAxisZ;
00146                                 
00147                                 TReal source = TReal(x * x + y * y + z * z);
00148                                 TReal result(0.0);
00149                                 
00150                                 // Calculate the violence of current acceleration 
00151                             Math::Sqrt(result, source);
00152                             
00153                             // Log a shake if the violence exceeds our threshold
00154                             if (result>KShakeThreshold)
00155                                 {
00156                                         // Log the timestamp and XYZ data
00157                                         _LIT(KTimeFormat,"%H:%T:%S");
00158                                         TBuf <50> formattedTime;
00159                                         TRAP_IGNORE(data.iTimeStamp.FormatL(formattedTime, KTimeFormat));
00160                                         TRAP_IGNORE(iLogView->LogEntryL(KShaken, &formattedTime));
00161                                 }
00162                                 }
00163                         }
00164                 iLastTime.UniversalTime();
00165                 }
00166         }
00167 
00177 void CSensorListener::DataError( CSensrvChannel& aChannel, 
00178                                                 TSensrvErrorSeverity aError )
00179         {
00180         // Sensor error severity 
00181         _LIT(KSeverityNotDefined, "Not Defined");
00182         _LIT(KSeverityMinor, "Minor");
00183         _LIT(KSeverityFatal, "Fatal");
00184         _LIT(KSensorDataError, "Sensor error, severity: %S");
00185         
00186         // Only log errors for the channel we are interested in.
00187         if (aChannel.GetChannelInfo().iChannelType == KSensrvChannelTypeIdAccelerometerXYZAxisData)
00188                 {
00189                 switch (aError)
00190                         {
00191                         case ESensrvErrorSeverityMinor:
00192                                 {
00193                                 TRAP_IGNORE(iLogView->LogEntryL(KSensorDataError, &KSeverityMinor));
00194                                 break;
00195                                 }
00196                         case ESensrvErrorSeverityFatal:                 
00197                                 {
00198                                 TRAP_IGNORE(iLogView->LogEntryL(KSensorDataError, &KSeverityFatal));
00199                                 break;
00200                                 }
00201                         case ESensrvErrorSeverityNotDefined:
00202                         default:                
00203                                 {
00204                                 TRAP_IGNORE(iLogView->LogEntryL(KSensorDataError, &KSeverityNotDefined));
00205                                 break;
00206                                 }
00207                         }
00208                 }
00209         }
00210 
00219 void CSensorListener::GetDataListenerInterfaceL( TUid /*aInterfaceUid*/, TAny*& /*aInterface*/ )
00220         {
00221         // Not implemented
00222         }
00223 
00224 // End of File
00225 

Generated by  doxygen 1.6.2