blob: 6d85cca0f66ccb62919d3fd1fe3c8c1cb73db4c3 [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.
Mathias Agopian984826c2011-05-17 22:54:42 -070039}
40
41bool 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 Agopian33015422011-05-27 18:18:13 -070056 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 Agopian984826c2011-05-17 22:54:42 -070060 outEvent->sensor = '_ypr';
61 outEvent->type = SENSOR_TYPE_ORIENTATION;
62 return true;
63 }
64 }
65 return false;
66}
67
68status_t OrientationSensor::activate(void* ident, bool enabled) {
Aravind Akellabf72dee2013-09-16 15:37:41 -070069 return mSensorFusion.activate(ident, enabled);
Mathias Agopian984826c2011-05-17 22:54:42 -070070}
71
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070072status_t OrientationSensor::setDelay(void* ident, int /*handle*/, int64_t ns) {
Aravind Akellabf72dee2013-09-16 15:37:41 -070073 return mSensorFusion.setDelay(ident, ns);
Mathias Agopian984826c2011-05-17 22:54:42 -070074}
75
76Sensor OrientationSensor::getSensor() const {
77 sensor_t hwSensor;
78 hwSensor.name = "Orientation Sensor";
Mathias Agopian03193062013-05-10 19:32:39 -070079 hwSensor.vendor = "AOSP";
Mathias Agopian984826c2011-05-17 22:54:42 -070080 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