camerauis/cameraxui/cxengine/src/sensor/xqdeviceorientation_p.cpp
branchRCL_3
changeset 24 bac7acad7cb3
parent 23 61bc0f252b2b
child 25 2c87b2808fd7
equal deleted inserted replaced
23:61bc0f252b2b 24:bac7acad7cb3
     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:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 #include "xqdeviceorientation_p.h"
       
    18 #include <e32math.h>
       
    19 
       
    20 const TInt KDefaultResolution = 15;
       
    21 
       
    22 XQDeviceOrientationPrivate::XQDeviceOrientationPrivate(
       
    23         XQDeviceOrientation& qq) : q(qq), iResolution(KDefaultResolution)
       
    24 {
       
    25     iXRotation = 0;
       
    26     iYRotation = 0;
       
    27     iZRotation = 0;
       
    28 }
       
    29 
       
    30 XQDeviceOrientationPrivate::~XQDeviceOrientationPrivate()
       
    31 {
       
    32     close();
       
    33 }
       
    34 
       
    35 void XQDeviceOrientationPrivate::open()
       
    36 {
       
    37     iAccelerationSensor.addFilter(*this);
       
    38     iAccelerationSensor.addFilter(iAccelerationPostFilter);
       
    39 
       
    40     iAccelerationSensor.open();
       
    41     iAccelerationSensor.startReceiving();
       
    42 }
       
    43 
       
    44 void XQDeviceOrientationPrivate::close()
       
    45 {
       
    46     iAccelerationSensor.stopReceiving();
       
    47     iAccelerationSensor.close();
       
    48     iAccelerationSensor.filters().removeOne(this);
       
    49 }
       
    50 
       
    51 void XQDeviceOrientationPrivate::setResolution(TInt resolution)
       
    52 {
       
    53     iResolution = resolution;
       
    54 }
       
    55 
       
    56 TInt XQDeviceOrientationPrivate::resolution() const
       
    57 {
       
    58     return iResolution;
       
    59 }
       
    60 
       
    61 int XQDeviceOrientationPrivate::xRotation() const
       
    62 {
       
    63     return iXRotation;
       
    64 }
       
    65 
       
    66 int XQDeviceOrientationPrivate::yRotation() const
       
    67 {
       
    68     return iYRotation;
       
    69 }
       
    70 
       
    71 int XQDeviceOrientationPrivate::zRotation() const
       
    72 {
       
    73     return iZRotation;
       
    74 }
       
    75 
       
    76 XQDeviceOrientation::DisplayOrientation
       
    77         XQDeviceOrientationPrivate::orientation() const
       
    78 {
       
    79     return iOrientation;
       
    80 }
       
    81 
       
    82 bool XQDeviceOrientationPrivate::filter(int& xAcceleration, 
       
    83     int& yAcceleration, int& zAcceleration)
       
    84 {
       
    85     TReal xAcc = xAcceleration;
       
    86     TReal yAcc = yAcceleration;
       
    87     TReal zAcc = zAcceleration;
       
    88 
       
    89     TReal angle = 0;
       
    90 
       
    91     //axis X rotation
       
    92     if (yAcc != 0) {
       
    93         Math::ATan(angle, Abs(zAcc / yAcc));
       
    94         angle *= 180 / KPi;  //rad to degrees
       
    95         if (zAcc > 0 && yAcc > 0) {
       
    96             //do nothing
       
    97         } else if (zAcc > 0 && yAcc < 0) {
       
    98             angle = 90 + (90 - angle);
       
    99         } else if (zAcc < 0 && yAcc < 0) {
       
   100             angle += 180;
       
   101         } else {
       
   102             angle = 270 + (90 - angle);
       
   103         }
       
   104     } else {
       
   105         if (zAcceleration > 0) {
       
   106             angle = 90;
       
   107         } else {
       
   108             angle = 270;
       
   109         }
       
   110     }
       
   111     TInt xRotation = RoundAngle(angle);
       
   112 
       
   113     //axis Y rotation
       
   114     if (xAcc != 0) {
       
   115         Math::ATan(angle, Abs(zAcc / xAcc));
       
   116         angle *= 180 / KPi;  //rad to degrees
       
   117         if (zAcc > 0 && xAcc > 0) {
       
   118             angle += 90;
       
   119         } else if (zAcc > 0 && xAcc < 0) {
       
   120             angle = 270 - angle;
       
   121         } else if (zAcc < 0 && xAcc < 0) {
       
   122             angle += 270;
       
   123         } else {
       
   124             angle = 90 - angle;
       
   125         }
       
   126     } else {
       
   127         if (zAcceleration > 0) {
       
   128             angle = 180;
       
   129         } else {
       
   130             angle = 0;
       
   131         }
       
   132     }
       
   133     TInt yRotation = RoundAngle(angle);
       
   134 
       
   135     //axis Z rotation
       
   136     if (yAcc != 0) {
       
   137         Math::ATan(angle, Abs(xAcc / yAcc));
       
   138         angle *= 180 / KPi;  //rad to degrees
       
   139         if (xAcc > 0 && yAcc > 0) {
       
   140             angle = 360 - angle;
       
   141         } else if (xAcc > 0 && yAcc < 0) {
       
   142             angle += 180;
       
   143         } else if (xAcc <= 0 && yAcc <= 0) {
       
   144             angle = 180 - angle;
       
   145         } else {
       
   146             //Do nothing
       
   147         }
       
   148     } else {
       
   149         if (xAcceleration > 0) {
       
   150             angle = 270;
       
   151         } else {
       
   152             angle = 90;
       
   153         }
       
   154     }
       
   155     TInt zRotation = RoundAngle(angle);
       
   156 
       
   157     iXRotation = xRotation;
       
   158     iYRotation = yRotation;
       
   159     iZRotation = zRotation;
       
   160 
       
   161     XQDeviceOrientation::DisplayOrientation orientation =
       
   162             XQDeviceOrientation::OrientationUndefined;
       
   163     if (((315 < xRotation && xRotation < 360) ||
       
   164             (0 <= xRotation && xRotation <= 45)) &&
       
   165          ((315 < zRotation && zRotation < 360) ||
       
   166             (0 <= zRotation && zRotation <= 45))) {
       
   167         orientation = XQDeviceOrientation::OrientationDisplayUp;
       
   168     } else if (135 < xRotation && xRotation <= 225 && 135 < zRotation &&
       
   169             zRotation <= 225) {
       
   170         orientation = XQDeviceOrientation::OrientationDisplayDown;
       
   171     } else if (225 < yRotation && yRotation <= 315 && 45 < zRotation &&
       
   172             zRotation <= 135) {
       
   173         orientation = XQDeviceOrientation::OrientationDisplayLeftUp;
       
   174     } else if (45 < yRotation && yRotation <= 135 && 225 < zRotation &&
       
   175             zRotation <= 315) {
       
   176         orientation = XQDeviceOrientation::OrientationDisplayRightUp;
       
   177     } else if (45 < xRotation && xRotation <= 135 && 135 < yRotation &&
       
   178             yRotation <= 225) {
       
   179         orientation = XQDeviceOrientation::OrientationDisplayUpwards;
       
   180     } else if (225 < xRotation && xRotation <= 315 &&
       
   181         ((315 < yRotation && yRotation < 360) || (0 <= yRotation &&
       
   182             yRotation <= 45))) {
       
   183         orientation = XQDeviceOrientation::OrientationDisplayDownwards;
       
   184     }
       
   185 
       
   186     emit q.rotationChanged(xRotation, yRotation, zRotation);
       
   187 
       
   188     if (orientation != iOrientation) {
       
   189         if (orientation != XQDeviceOrientation::OrientationUndefined) {
       
   190             emit q.orientationChanged(orientation);
       
   191         }
       
   192         iOrientation = orientation;
       
   193     }
       
   194 
       
   195     return false;
       
   196 }
       
   197 
       
   198 TInt XQDeviceOrientationPrivate::RoundAngle(TReal aAngle) const
       
   199 {
       
   200     return ((TInt(aAngle) + iResolution / 2) / iResolution * iResolution) % 360;
       
   201 }
       
   202 
       
   203 // End of file