blob: 20b49be2841979c41db1b14240a35d504b188e48 [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
32OrientationSensor::OrientationSensor()
33 : mSensorDevice(SensorDevice::getInstance()),
34 mSensorFusion(SensorFusion::getInstance())
35{
Mathias Agopian03193062013-05-10 19:32:39 -070036 // 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.
Peng Xu0cc8f802016-04-05 23:46:03 -070039 sensor_t hwSensor;
40 hwSensor.name = "Orientation Sensor";
41 hwSensor.vendor = "AOSP";
42 hwSensor.version = 1;
43 hwSensor.handle = '_ypr';
44 hwSensor.type = SENSOR_TYPE_ORIENTATION;
45 hwSensor.maxRange = 360.0f;
46 hwSensor.resolution = 1.0f/256.0f; // FIXME: real value here
47 hwSensor.power = mSensorFusion.getPowerUsage();
48 hwSensor.minDelay = mSensorFusion.getMinDelay();
49 mSensor = Sensor(&hwSensor);
Mathias Agopian984826c2011-05-17 22:54:42 -070050}
51
52bool OrientationSensor::process(sensors_event_t* outEvent,
53 const sensors_event_t& event)
54{
55 if (event.type == SENSOR_TYPE_ACCELEROMETER) {
56 if (mSensorFusion.hasEstimate()) {
57 vec3_t g;
58 const float rad2deg = 180 / M_PI;
59 const mat33_t R(mSensorFusion.getRotationMatrix());
60 g[0] = atan2f(-R[1][0], R[0][0]) * rad2deg;
61 g[1] = atan2f(-R[2][1], R[2][2]) * rad2deg;
62 g[2] = asinf ( R[2][0]) * rad2deg;
63 if (g[0] < 0)
64 g[0] += 360;
65
66 *outEvent = event;
Mathias Agopian33015422011-05-27 18:18:13 -070067 outEvent->orientation.azimuth = g.x;
68 outEvent->orientation.pitch = g.y;
69 outEvent->orientation.roll = g.z;
70 outEvent->orientation.status = SENSOR_STATUS_ACCURACY_HIGH;
Mathias Agopian984826c2011-05-17 22:54:42 -070071 outEvent->sensor = '_ypr';
72 outEvent->type = SENSOR_TYPE_ORIENTATION;
73 return true;
74 }
75 }
76 return false;
77}
78
79status_t OrientationSensor::activate(void* ident, bool enabled) {
Peng Xuf66684a2015-07-23 11:41:53 -070080 return mSensorFusion.activate(FUSION_9AXIS, ident, enabled);
Mathias Agopian984826c2011-05-17 22:54:42 -070081}
82
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070083status_t OrientationSensor::setDelay(void* ident, int /*handle*/, int64_t ns) {
Peng Xuf66684a2015-07-23 11:41:53 -070084 return mSensorFusion.setDelay(FUSION_9AXIS, ident, ns);
Mathias Agopian984826c2011-05-17 22:54:42 -070085}
86
Peng Xu0cc8f802016-04-05 23:46:03 -070087const Sensor& OrientationSensor::getSensor() const {
88 return mSensor;
Mathias Agopian984826c2011-05-17 22:54:42 -070089}
90
91// ---------------------------------------------------------------------------
92}; // namespace android
93