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 32984826cc158193e61e3a00359ef4f6699c7d748aMathias AgopianOrientationSensor::OrientationSensor() 33984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian : mSensorDevice(SensorDevice::getInstance()), 34984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian mSensorFusion(SensorFusion::getInstance()) 35984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian{ 360319306670b0344da99efa606b6f172dde575a39Mathias Agopian // FIXME: instead of using the SensorFusion code, we should use 370319306670b0344da99efa606b6f172dde575a39Mathias Agopian // the SENSOR_TYPE_ROTATION_VECTOR instead. This way we could use the 380319306670b0344da99efa606b6f172dde575a39Mathias Agopian // HAL's implementation. 39984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 40984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 41984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianbool OrientationSensor::process(sensors_event_t* outEvent, 42984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian const sensors_event_t& event) 43984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian{ 44984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (event.type == SENSOR_TYPE_ACCELEROMETER) { 45984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (mSensorFusion.hasEstimate()) { 46984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian vec3_t g; 47984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian const float rad2deg = 180 / M_PI; 48984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian const mat33_t R(mSensorFusion.getRotationMatrix()); 49984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian g[0] = atan2f(-R[1][0], R[0][0]) * rad2deg; 50984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian g[1] = atan2f(-R[2][1], R[2][2]) * rad2deg; 51984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian g[2] = asinf ( R[2][0]) * rad2deg; 52984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (g[0] < 0) 53984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian g[0] += 360; 54984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 55984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian *outEvent = event; 563301542828febc768e1df42892cfac4992c35474Mathias Agopian outEvent->orientation.azimuth = g.x; 573301542828febc768e1df42892cfac4992c35474Mathias Agopian outEvent->orientation.pitch = g.y; 583301542828febc768e1df42892cfac4992c35474Mathias Agopian outEvent->orientation.roll = g.z; 593301542828febc768e1df42892cfac4992c35474Mathias Agopian outEvent->orientation.status = SENSOR_STATUS_ACCURACY_HIGH; 60984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian outEvent->sensor = '_ypr'; 61984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian outEvent->type = SENSOR_TYPE_ORIENTATION; 62984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian return true; 63984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 64984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 65984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian return false; 66984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 67984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 68984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianstatus_t OrientationSensor::activate(void* ident, bool enabled) { 69bf72deea2f9982a09c6a95f94cfa1654bc8c684fAravind Akella return mSensorFusion.activate(ident, enabled); 70984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 71984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 7292dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzynstatus_t OrientationSensor::setDelay(void* ident, int /*handle*/, int64_t ns) { 73bf72deea2f9982a09c6a95f94cfa1654bc8c684fAravind Akella return mSensorFusion.setDelay(ident, ns); 74984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 75984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 76984826cc158193e61e3a00359ef4f6699c7d748aMathias AgopianSensor OrientationSensor::getSensor() const { 77984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian sensor_t hwSensor; 78984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian hwSensor.name = "Orientation Sensor"; 790319306670b0344da99efa606b6f172dde575a39Mathias Agopian hwSensor.vendor = "AOSP"; 80984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian hwSensor.version = 1; 81984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian hwSensor.handle = '_ypr'; 82984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian hwSensor.type = SENSOR_TYPE_ORIENTATION; 83984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian hwSensor.maxRange = 360.0f; 84984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian hwSensor.resolution = 1.0f/256.0f; // FIXME: real value here 85984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian hwSensor.power = mSensorFusion.getPowerUsage(); 86984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian hwSensor.minDelay = mSensorFusion.getMinDelay(); 87984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian Sensor sensor(&hwSensor); 88984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian return sensor; 89984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 90984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 91984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian// --------------------------------------------------------------------------- 92984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian}; // namespace android 93984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 94