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    // FIXME: instead of using the SensorFusion code, we should use
37    // the SENSOR_TYPE_ROTATION_VECTOR instead. This way we could use the
38    // HAL's implementation.
39}
40
41bool OrientationSensor::process(sensors_event_t* outEvent,
42        const sensors_event_t& event)
43{
44    if (event.type == SENSOR_TYPE_ACCELEROMETER) {
45        if (mSensorFusion.hasEstimate()) {
46            vec3_t g;
47            const float rad2deg = 180 / M_PI;
48            const mat33_t R(mSensorFusion.getRotationMatrix());
49            g[0] = atan2f(-R[1][0], R[0][0])    * rad2deg;
50            g[1] = atan2f(-R[2][1], R[2][2])    * rad2deg;
51            g[2] = asinf ( R[2][0])             * rad2deg;
52            if (g[0] < 0)
53                g[0] += 360;
54
55            *outEvent = event;
56            outEvent->orientation.azimuth = g.x;
57            outEvent->orientation.pitch   = g.y;
58            outEvent->orientation.roll    = g.z;
59            outEvent->orientation.status  = SENSOR_STATUS_ACCURACY_HIGH;
60            outEvent->sensor = '_ypr';
61            outEvent->type = SENSOR_TYPE_ORIENTATION;
62            return true;
63        }
64    }
65    return false;
66}
67
68status_t OrientationSensor::activate(void* ident, bool enabled) {
69    return mSensorFusion.activate(ident, enabled);
70}
71
72status_t OrientationSensor::setDelay(void* ident, int /*handle*/, int64_t ns) {
73    return mSensorFusion.setDelay(ident, ns);
74}
75
76Sensor OrientationSensor::getSensor() const {
77    sensor_t hwSensor;
78    hwSensor.name       = "Orientation Sensor";
79    hwSensor.vendor     = "AOSP";
80    hwSensor.version    = 1;
81    hwSensor.handle     = '_ypr';
82    hwSensor.type       = SENSOR_TYPE_ORIENTATION;
83    hwSensor.maxRange   = 360.0f;
84    hwSensor.resolution = 1.0f/256.0f; // FIXME: real value here
85    hwSensor.power      = mSensorFusion.getPowerUsage();
86    hwSensor.minDelay   = mSensorFusion.getMinDelay();
87    Sensor sensor(&hwSensor);
88    return sensor;
89}
90
91// ---------------------------------------------------------------------------
92}; // namespace android
93
94