1984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian/*
2984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * Copyright (C) 2011 The Android Open Source Project
3984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian *
4984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * you may not use this file except in compliance with the License.
6984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * You may obtain a copy of the License at
7984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian *
8984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian *
10984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * Unless required by applicable law or agreed to in writing, software
11984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * See the License for the specific language governing permissions and
14984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * limitations under the License.
15984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian */
16984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
17984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include <stdint.h>
18984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include <math.h>
19984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include <sys/types.h>
20984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
21984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include <utils/Errors.h>
22984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
23984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include <hardware/sensors.h>
24984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
25984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include "OrientationSensor.h"
26984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include "SensorDevice.h"
27984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include "SensorFusion.h"
28984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
29984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopiannamespace android {
30984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian// ---------------------------------------------------------------------------
31984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
32755c451c7861a029e26e5f16e319b629169e656dPeng XuOrientationSensor::OrientationSensor() {
33755c451c7861a029e26e5f16e319b629169e656dPeng Xu    const sensor_t sensor = {
34755c451c7861a029e26e5f16e319b629169e656dPeng Xu        .name       = "Orientation Sensor",
35755c451c7861a029e26e5f16e319b629169e656dPeng Xu        .vendor     = "AOSP",
36755c451c7861a029e26e5f16e319b629169e656dPeng Xu        .version    = 1,
37755c451c7861a029e26e5f16e319b629169e656dPeng Xu        .handle     = '_ypr',
38755c451c7861a029e26e5f16e319b629169e656dPeng Xu        .type       = SENSOR_TYPE_ORIENTATION,
39755c451c7861a029e26e5f16e319b629169e656dPeng Xu        .maxRange   = 360.0f,
40755c451c7861a029e26e5f16e319b629169e656dPeng Xu        .resolution = 1.0f/256.0f, // FIXME: real value here
41755c451c7861a029e26e5f16e319b629169e656dPeng Xu        .power      = mSensorFusion.getPowerUsage(),
42755c451c7861a029e26e5f16e319b629169e656dPeng Xu        .minDelay   = mSensorFusion.getMinDelay(),
43755c451c7861a029e26e5f16e319b629169e656dPeng Xu    };
44755c451c7861a029e26e5f16e319b629169e656dPeng Xu    mSensor = Sensor(&sensor);
45984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian}
46984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
47984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianbool OrientationSensor::process(sensors_event_t* outEvent,
48984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian        const sensors_event_t& event)
49984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian{
50984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    if (event.type == SENSOR_TYPE_ACCELEROMETER) {
51984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian        if (mSensorFusion.hasEstimate()) {
52984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian            vec3_t g;
53984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian            const float rad2deg = 180 / M_PI;
54984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian            const mat33_t R(mSensorFusion.getRotationMatrix());
55984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian            g[0] = atan2f(-R[1][0], R[0][0])    * rad2deg;
56984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian            g[1] = atan2f(-R[2][1], R[2][2])    * rad2deg;
57984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian            g[2] = asinf ( R[2][0])             * rad2deg;
58984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian            if (g[0] < 0)
59984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian                g[0] += 360;
60984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
61984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian            *outEvent = event;
623301542828febc768e1df42892cfac4992c35474Mathias Agopian            outEvent->orientation.azimuth = g.x;
633301542828febc768e1df42892cfac4992c35474Mathias Agopian            outEvent->orientation.pitch   = g.y;
643301542828febc768e1df42892cfac4992c35474Mathias Agopian            outEvent->orientation.roll    = g.z;
653301542828febc768e1df42892cfac4992c35474Mathias Agopian            outEvent->orientation.status  = SENSOR_STATUS_ACCURACY_HIGH;
66984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian            outEvent->sensor = '_ypr';
67984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian            outEvent->type = SENSOR_TYPE_ORIENTATION;
68984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian            return true;
69984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian        }
70984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    }
71984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    return false;
72984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian}
73984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
74984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianstatus_t OrientationSensor::activate(void* ident, bool enabled) {
75f66684a6fb2a2991e84a085673629db2a0494fc6Peng Xu    return mSensorFusion.activate(FUSION_9AXIS, ident, enabled);
76984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian}
77984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
7892dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzynstatus_t OrientationSensor::setDelay(void* ident, int /*handle*/, int64_t ns) {
79f66684a6fb2a2991e84a085673629db2a0494fc6Peng Xu    return mSensorFusion.setDelay(FUSION_9AXIS, ident, ns);
80984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian}
81984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
82984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian// ---------------------------------------------------------------------------
83984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian}; // namespace android
84984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
85