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{ 36984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 37984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 38984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianbool OrientationSensor::process(sensors_event_t* outEvent, 39984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian const sensors_event_t& event) 40984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian{ 41984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (event.type == SENSOR_TYPE_ACCELEROMETER) { 42984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (mSensorFusion.hasEstimate()) { 43984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian vec3_t g; 44984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian const float rad2deg = 180 / M_PI; 45984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian const mat33_t R(mSensorFusion.getRotationMatrix()); 46984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian g[0] = atan2f(-R[1][0], R[0][0]) * rad2deg; 47984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian g[1] = atan2f(-R[2][1], R[2][2]) * rad2deg; 48984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian g[2] = asinf ( R[2][0]) * rad2deg; 49984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (g[0] < 0) 50984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian g[0] += 360; 51984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 52984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian *outEvent = event; 533301542828febc768e1df42892cfac4992c35474Mathias Agopian outEvent->orientation.azimuth = g.x; 543301542828febc768e1df42892cfac4992c35474Mathias Agopian outEvent->orientation.pitch = g.y; 553301542828febc768e1df42892cfac4992c35474Mathias Agopian outEvent->orientation.roll = g.z; 563301542828febc768e1df42892cfac4992c35474Mathias Agopian outEvent->orientation.status = SENSOR_STATUS_ACCURACY_HIGH; 57984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian outEvent->sensor = '_ypr'; 58984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian outEvent->type = SENSOR_TYPE_ORIENTATION; 59984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian return true; 60984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 61984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 62984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian return false; 63984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 64984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 65984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianstatus_t OrientationSensor::activate(void* ident, bool enabled) { 66984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian return mSensorFusion.activate(this, enabled); 67984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 68984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 69984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianstatus_t OrientationSensor::setDelay(void* ident, int handle, int64_t ns) { 70984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian return mSensorFusion.setDelay(this, ns); 71984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 72984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 73984826cc158193e61e3a00359ef4f6699c7d748aMathias AgopianSensor OrientationSensor::getSensor() const { 74984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian sensor_t hwSensor; 75984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian hwSensor.name = "Orientation Sensor"; 76984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian hwSensor.vendor = "Google Inc."; 77984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian hwSensor.version = 1; 78984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian hwSensor.handle = '_ypr'; 79984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian hwSensor.type = SENSOR_TYPE_ORIENTATION; 80984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian hwSensor.maxRange = 360.0f; 81984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian hwSensor.resolution = 1.0f/256.0f; // FIXME: real value here 82984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian hwSensor.power = mSensorFusion.getPowerUsage(); 83984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian hwSensor.minDelay = mSensorFusion.getMinDelay(); 84984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian Sensor sensor(&hwSensor); 85984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian return sensor; 86984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian} 87984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 88984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian// --------------------------------------------------------------------------- 89984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian}; // namespace android 90984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 91