blob: ea5dbc9044aa37417dc0e2b50e8da0026251596e [file] [log] [blame]
Mathias Agopian984826c2011-05-17 22:54:42 -07001/*
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
29namespace android {
30// ---------------------------------------------------------------------------
31
Peng Xu755c4512016-04-07 23:15:14 -070032OrientationSensor::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 Agopian984826c2011-05-17 22:54:42 -070045}
46
47bool 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 Agopian33015422011-05-27 18:18:13 -070062 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 Agopian984826c2011-05-17 22:54:42 -070066 outEvent->sensor = '_ypr';
67 outEvent->type = SENSOR_TYPE_ORIENTATION;
68 return true;
69 }
70 }
71 return false;
72}
73
74status_t OrientationSensor::activate(void* ident, bool enabled) {
Peng Xuf66684a2015-07-23 11:41:53 -070075 return mSensorFusion.activate(FUSION_9AXIS, ident, enabled);
Mathias Agopian984826c2011-05-17 22:54:42 -070076}
77
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070078status_t OrientationSensor::setDelay(void* ident, int /*handle*/, int64_t ns) {
Peng Xuf66684a2015-07-23 11:41:53 -070079 return mSensorFusion.setDelay(FUSION_9AXIS, ident, ns);
Mathias Agopian984826c2011-05-17 22:54:42 -070080}
81
Mathias Agopian984826c2011-05-17 22:54:42 -070082// ---------------------------------------------------------------------------
83}; // namespace android
84