blob: 9d8add169fec5e007eacd43f9e3b83777cc405c6 [file] [log] [blame]
Mathias Agopianf001c922010-11-11 17:58:51 -08001/*
2 * Copyright (C) 2010 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 "GravitySensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070026#include "SensorDevice.h"
27#include "SensorFusion.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080028
29namespace android {
30// ---------------------------------------------------------------------------
31
Peng Xu755c4512016-04-07 23:15:14 -070032GravitySensor::GravitySensor(sensor_t const* list, size_t count) {
Mathias Agopianf001c922010-11-11 17:58:51 -080033 for (size_t i=0 ; i<count ; i++) {
34 if (list[i].type == SENSOR_TYPE_ACCELEROMETER) {
35 mAccelerometer = Sensor(list + i);
36 break;
37 }
38 }
Peng Xu0cc8f802016-04-05 23:46:03 -070039
Peng Xu755c4512016-04-07 23:15:14 -070040 const sensor_t sensor = {
41 .name = "Gravity Sensor",
42 .vendor = "AOSP",
43 .version = 3,
44 .handle = '_grv',
45 .type = SENSOR_TYPE_GRAVITY,
46 .maxRange = GRAVITY_EARTH * 2,
47 .resolution = mAccelerometer.getResolution(),
48 .power = mSensorFusion.getPowerUsage(),
49 .minDelay = mSensorFusion.getMinDelay(),
50 };
51 mSensor = Sensor(&sensor);
Mathias Agopianf001c922010-11-11 17:58:51 -080052}
53
54bool GravitySensor::process(sensors_event_t* outEvent,
55 const sensors_event_t& event)
56{
Mathias Agopianf001c922010-11-11 17:58:51 -080057 if (event.type == SENSOR_TYPE_ACCELEROMETER) {
Mathias Agopian984826c2011-05-17 22:54:42 -070058 vec3_t g;
Peng Xuf66684a2015-07-23 11:41:53 -070059 if (!mSensorFusion.hasEstimate(FUSION_NOMAG))
Mathias Agopian33015422011-05-27 18:18:13 -070060 return false;
Peng Xuf66684a2015-07-23 11:41:53 -070061 const mat33_t R(mSensorFusion.getRotationMatrix(FUSION_NOMAG));
Mathias Agopian33015422011-05-27 18:18:13 -070062 // FIXME: we need to estimate the length of gravity because
63 // the accelerometer may have a small scaling error. This
64 // translates to an offset in the linear-acceleration sensor.
65 g = R[2] * GRAVITY_EARTH;
66
Mathias Agopianf001c922010-11-11 17:58:51 -080067 *outEvent = event;
Mathias Agopian984826c2011-05-17 22:54:42 -070068 outEvent->data[0] = g.x;
69 outEvent->data[1] = g.y;
70 outEvent->data[2] = g.z;
Mathias Agopianf001c922010-11-11 17:58:51 -080071 outEvent->sensor = '_grv';
72 outEvent->type = SENSOR_TYPE_GRAVITY;
73 return true;
74 }
75 return false;
76}
Mathias Agopian984826c2011-05-17 22:54:42 -070077
Mathias Agopianf001c922010-11-11 17:58:51 -080078status_t GravitySensor::activate(void* ident, bool enabled) {
Peng Xuf66684a2015-07-23 11:41:53 -070079 return mSensorFusion.activate(FUSION_NOMAG, ident, enabled);
Mathias Agopianf001c922010-11-11 17:58:51 -080080}
81
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070082status_t GravitySensor::setDelay(void* ident, int /*handle*/, int64_t ns) {
Peng Xuf66684a2015-07-23 11:41:53 -070083 return mSensorFusion.setDelay(FUSION_NOMAG, ident, ns);
Mathias Agopianf001c922010-11-11 17:58:51 -080084}
85
Mathias Agopianf001c922010-11-11 17:58:51 -080086// ---------------------------------------------------------------------------
87}; // namespace android
88