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 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 32 | OrientationSensor::OrientationSensor() { |
| 33 | const sensor_t sensor = { |
| 34 | .name = "Orientation Sensor", |
| 35 | .vendor = "AOSP", |
| 36 | .version = 1, |
| 37 | .handle = '_ypr', |
| 38 | .type = SENSOR_TYPE_ORIENTATION, |
| 39 | .maxRange = 360.0f, |
| 40 | .resolution = 1.0f/256.0f, // FIXME: real value here |
| 41 | .power = mSensorFusion.getPowerUsage(), |
| 42 | .minDelay = mSensorFusion.getMinDelay(), |
| 43 | }; |
| 44 | mSensor = Sensor(&sensor); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | bool OrientationSensor::process(sensors_event_t* outEvent, |
| 48 | const sensors_event_t& event) |
| 49 | { |
| 50 | if (event.type == SENSOR_TYPE_ACCELEROMETER) { |
| 51 | if (mSensorFusion.hasEstimate()) { |
| 52 | vec3_t g; |
| 53 | const float rad2deg = 180 / M_PI; |
| 54 | const mat33_t R(mSensorFusion.getRotationMatrix()); |
| 55 | g[0] = atan2f(-R[1][0], R[0][0]) * rad2deg; |
| 56 | g[1] = atan2f(-R[2][1], R[2][2]) * rad2deg; |
| 57 | g[2] = asinf ( R[2][0]) * rad2deg; |
| 58 | if (g[0] < 0) |
| 59 | g[0] += 360; |
| 60 | |
| 61 | *outEvent = event; |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 62 | outEvent->orientation.azimuth = g.x; |
| 63 | outEvent->orientation.pitch = g.y; |
| 64 | outEvent->orientation.roll = g.z; |
| 65 | outEvent->orientation.status = SENSOR_STATUS_ACCURACY_HIGH; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 66 | outEvent->sensor = '_ypr'; |
| 67 | outEvent->type = SENSOR_TYPE_ORIENTATION; |
| 68 | return true; |
| 69 | } |
| 70 | } |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | status_t OrientationSensor::activate(void* ident, bool enabled) { |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 75 | return mSensorFusion.activate(FUSION_9AXIS, ident, enabled); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 78 | status_t OrientationSensor::setDelay(void* ident, int /*handle*/, int64_t ns) { |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 79 | return mSensorFusion.setDelay(FUSION_9AXIS, ident, ns); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 82 | // --------------------------------------------------------------------------- |
| 83 | }; // namespace android |
| 84 | |