OrientationSensor.cpp revision 984826cc158193e61e3a00359ef4f6699c7d748a
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdint.h>
18#include <math.h>
19#include <sys/types.h>
20
21#include <utils/Errors.h>
22
23#include <hardware/sensors.h>
24
25#include "OrientationSensor.h"
26#include "SensorDevice.h"
27#include "SensorFusion.h"
28
29namespace android {
30// ---------------------------------------------------------------------------
31
32OrientationSensor::OrientationSensor()
33    : mSensorDevice(SensorDevice::getInstance()),
34      mSensorFusion(SensorFusion::getInstance())
35{
36}
37
38bool OrientationSensor::process(sensors_event_t* outEvent,
39        const sensors_event_t& event)
40{
41    if (event.type == SENSOR_TYPE_ACCELEROMETER) {
42        if (mSensorFusion.hasEstimate()) {
43            vec3_t g;
44            const float rad2deg = 180 / M_PI;
45            const mat33_t R(mSensorFusion.getRotationMatrix());
46            g[0] = atan2f(-R[1][0], R[0][0])    * rad2deg;
47            g[1] = atan2f(-R[2][1], R[2][2])    * rad2deg;
48            g[2] = asinf ( R[2][0])             * rad2deg;
49            if (g[0] < 0)
50                g[0] += 360;
51
52            *outEvent = event;
53            outEvent->data[0] = g.x;
54            outEvent->data[1] = g.y;
55            outEvent->data[2] = g.z;
56            outEvent->sensor = '_ypr';
57            outEvent->type = SENSOR_TYPE_ORIENTATION;
58            return true;
59        }
60    }
61    return false;
62}
63
64status_t OrientationSensor::activate(void* ident, bool enabled) {
65    return mSensorFusion.activate(this, enabled);
66}
67
68status_t OrientationSensor::setDelay(void* ident, int handle, int64_t ns) {
69    return mSensorFusion.setDelay(this, ns);
70}
71
72Sensor OrientationSensor::getSensor() const {
73    sensor_t hwSensor;
74    hwSensor.name       = "Orientation Sensor";
75    hwSensor.vendor     = "Google Inc.";
76    hwSensor.version    = 1;
77    hwSensor.handle     = '_ypr';
78    hwSensor.type       = SENSOR_TYPE_ORIENTATION;
79    hwSensor.maxRange   = 360.0f;
80    hwSensor.resolution = 1.0f/256.0f; // FIXME: real value here
81    hwSensor.power      = mSensorFusion.getPowerUsage();
82    hwSensor.minDelay   = mSensorFusion.getMinDelay();
83    Sensor sensor(&hwSensor);
84    return sensor;
85}
86
87// ---------------------------------------------------------------------------
88}; // namespace android
89
90