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
OrientationSensor()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);
45 }
46
process(sensors_event_t * outEvent,const sensors_event_t & event)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;
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;
66 outEvent->sensor = '_ypr';
67 outEvent->type = SENSOR_TYPE_ORIENTATION;
68 return true;
69 }
70 }
71 return false;
72 }
73
activate(void * ident,bool enabled)74 status_t OrientationSensor::activate(void* ident, bool enabled) {
75 return mSensorFusion.activate(FUSION_9AXIS, ident, enabled);
76 }
77
setDelay(void * ident,int,int64_t ns)78 status_t OrientationSensor::setDelay(void* ident, int /*handle*/, int64_t ns) {
79 return mSensorFusion.setDelay(FUSION_9AXIS, ident, ns);
80 }
81
82 // ---------------------------------------------------------------------------
83 }; // namespace android
84
85