plugin/poi/landmarks/overlay/src/Accelerometer.cpp
author David Caabeiro <david.caabeiro@seqpoint.com>
Fri, 25 Jun 2010 12:50:05 +0200
changeset 0 c316ab048e9d
permissions -rw-r--r--
First public commit
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
     1
/*
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
     2
 * Name        : Accelerometer.cpp
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
     3
 * Description : Accelerometer helper class
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
     4
 * Project     : This file is part of OpenMAR, an Open Mobile Augmented Reality browser
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
     5
 * Website     : http://OpenMAR.org
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
     6
 *
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
     7
 * Copyright (c) 2010 David Caabeiro
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
     8
 *
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
     9
 * All rights reserved. This program and the accompanying materials are made available 
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    10
 * under the terms of the Eclipse Public License v1.0 which accompanies this 
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    11
 * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    12
 *
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    13
 */
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    14
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    15
#include "Accelerometer.h"
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    16
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    17
#include <SensrvAccelerometerSensor.h>
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    18
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    19
#include <SensrvChannel.h>
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    20
#include <SensrvChannelFinder.h>
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    21
#include <SensrvChannelInfo.h>
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    22
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    23
CAccelerometer* CAccelerometer::NewL()
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    24
{
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    25
    CAccelerometer* self = new(ELeave) CAccelerometer;
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    26
    CleanupStack::PushL(self);
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    27
    self->ConstructL();
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    28
    CleanupStack::Pop(self);
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    29
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    30
    return self;
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    31
}
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    32
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    33
CAccelerometer::~CAccelerometer()
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    34
{
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    35
    iSensorChannel->CloseChannel();
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    36
}
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    37
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    38
CAccelerometer::CAccelerometer()
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    39
{}
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    40
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    41
void CAccelerometer::ConstructL()
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    42
{
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    43
    CSensrvChannelFinder* sensorChannelFinder = CSensrvChannelFinder::NewLC();
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    44
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    45
    RSensrvChannelInfoList channelInfoList;
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    46
    CleanupClosePushL(channelInfoList);
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    47
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    48
    TSensrvChannelInfo mySearchConditions;
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    49
    mySearchConditions.iChannelType = KSensrvChannelTypeIdAccelerometerXYZAxisData;
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    50
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    51
    sensorChannelFinder->FindChannelsL(channelInfoList, mySearchConditions);
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    52
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    53
    TSensrvChannelInfo channelInfo;
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    54
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    55
    if (channelInfoList.Count() > 0)
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    56
        channelInfo = channelInfoList[0];
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    57
    else
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    58
        User::Leave(KErrNotFound);
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    59
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    60
    CleanupStack::PopAndDestroy(&channelInfoList);
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    61
    CleanupStack::PopAndDestroy(sensorChannelFinder);
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    62
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    63
    iSensorChannel = CSensrvChannel::NewL(channelInfo);
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    64
    iSensorChannel->OpenChannelL();
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    65
}
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    66
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    67
void CAccelerometer::StartL()
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    68
{
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    69
    iSensorChannel->StartDataListeningL(this, 1, 8, 0);
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    70
}
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    71
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    72
void CAccelerometer::Stop()
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    73
{
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    74
    iSensorChannel->StopDataListening();
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    75
}
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    76
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    77
void CAccelerometer::DataReceived(CSensrvChannel& aChannel, TInt aCount, TInt aDataLost)
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    78
{
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    79
    if (aChannel.GetChannelInfo().iChannelType ==  KSensrvChannelTypeIdAccelerometerXYZAxisData)
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    80
    {
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    81
        TPckgBuf<TSensrvAccelerometerAxisData> dataBuffer;
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    82
        TSensrvAccelerometerAxisData data;
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    83
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    84
        for (TInt i = 0; i < aCount; ++i)
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    85
        {
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    86
            aChannel.GetData(dataBuffer);
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    87
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    88
            data = dataBuffer();
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    89
            // Do something with the date in data variable
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    90
            // data.iTimeStamp
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    91
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    92
            // Compensate axis for landscape mode
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    93
            TInt x = -data.iAxisY;
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    94
            TInt y =  data.iAxisX;
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    95
            TInt z =  data.iAxisZ;
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    96
#if 0
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    97
            TInt x = data.iAxisX;
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    98
            TInt y = data.iAxisY;
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
    99
            TInt z = data.iAxisZ;
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   100
#endif
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   101
            const TReal K128 = 0.0078125;   // 1/128
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   102
            const TReal KPole1 = 0.97;      // Pole of lowpass filter applied to X and Y
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   103
            const TReal KPole2 = 0.90;      // Pole of lowpass filter applied to Z
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   104
            const TReal KPole3 = 0.75;      // Pole of highpass filter applied to Z
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   105
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   106
            // Single-pole lowpass-filtering of X and Y
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   107
            iX1 = KPole1 * iX1 + ( 1.0 - KPole1 ) * ( K128 * x );
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   108
            iY1 = KPole1 * iY1 + ( 1.0 - KPole1 ) * ( K128 * y );
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   109
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   110
            // Single-pole lowpass-filtering of Z
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   111
            iZ0 = KPole2 * iZ1 + ( 1.0 - KPole2 ) * ( K128 * z );
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   112
            // 1st order highpass-filtering of Z (zero at 1)
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   113
            iZ2 = KPole3 * iZ2 + ( 1.0 - KPole3 ) * ( iZ0 - iZ1 );
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   114
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   115
            // Update variables for use at next sample
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   116
            iZ1 = iZ0;
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   117
        }
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   118
    }
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   119
}
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   120
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   121
void CAccelerometer::DataError(CSensrvChannel& aChannel, TSensrvErrorSeverity aError)
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   122
{
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   123
}
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   124
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   125
void CAccelerometer::GetDataListenerInterfaceL(TUid aInterfaceUid, TAny*& aInterface)
c316ab048e9d First public commit
David Caabeiro <david.caabeiro@seqpoint.com>
parents:
diff changeset
   126
{}