Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 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 | |
| 29 | namespace android { |
| 30 | // --------------------------------------------------------------------------- |
| 31 | |
| 32 | OrientationSensor::OrientationSensor() |
| 33 | : mSensorDevice(SensorDevice::getInstance()), |
| 34 | mSensorFusion(SensorFusion::getInstance()) |
| 35 | { |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 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. |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | bool 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; |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 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; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 60 | outEvent->sensor = '_ypr'; |
| 61 | outEvent->type = SENSOR_TYPE_ORIENTATION; |
| 62 | return true; |
| 63 | } |
| 64 | } |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | status_t OrientationSensor::activate(void* ident, bool enabled) { |
Aravind Akella | bf72dee | 2013-09-16 15:37:41 -0700 | [diff] [blame] | 69 | return mSensorFusion.activate(ident, enabled); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame^] | 72 | status_t OrientationSensor::setDelay(void* ident, int /*handle*/, int64_t ns) { |
Aravind Akella | bf72dee | 2013-09-16 15:37:41 -0700 | [diff] [blame] | 73 | return mSensorFusion.setDelay(ident, ns); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | Sensor OrientationSensor::getSensor() const { |
| 77 | sensor_t hwSensor; |
| 78 | hwSensor.name = "Orientation Sensor"; |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 79 | hwSensor.vendor = "AOSP"; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 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 | |