blob: a165a5b70182f5222a4542e7fad876f14f544e42 [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
32GravitySensor::GravitySensor(sensor_t const* list, size_t count)
33 : mSensorDevice(SensorDevice::getInstance()),
Mathias Agopian33015422011-05-27 18:18:13 -070034 mSensorFusion(SensorFusion::getInstance())
Mathias Agopianf001c922010-11-11 17:58:51 -080035{
36 for (size_t i=0 ; i<count ; i++) {
37 if (list[i].type == SENSOR_TYPE_ACCELEROMETER) {
38 mAccelerometer = Sensor(list + i);
39 break;
40 }
41 }
42}
43
44bool GravitySensor::process(sensors_event_t* outEvent,
45 const sensors_event_t& event)
46{
Mathias Agopianf001c922010-11-11 17:58:51 -080047 if (event.type == SENSOR_TYPE_ACCELEROMETER) {
Mathias Agopian984826c2011-05-17 22:54:42 -070048 vec3_t g;
Peng Xuf66684a2015-07-23 11:41:53 -070049 if (!mSensorFusion.hasEstimate(FUSION_NOMAG))
Mathias Agopian33015422011-05-27 18:18:13 -070050 return false;
Peng Xuf66684a2015-07-23 11:41:53 -070051 const mat33_t R(mSensorFusion.getRotationMatrix(FUSION_NOMAG));
Mathias Agopian33015422011-05-27 18:18:13 -070052 // FIXME: we need to estimate the length of gravity because
53 // the accelerometer may have a small scaling error. This
54 // translates to an offset in the linear-acceleration sensor.
55 g = R[2] * GRAVITY_EARTH;
56
Mathias Agopianf001c922010-11-11 17:58:51 -080057 *outEvent = event;
Mathias Agopian984826c2011-05-17 22:54:42 -070058 outEvent->data[0] = g.x;
59 outEvent->data[1] = g.y;
60 outEvent->data[2] = g.z;
Mathias Agopianf001c922010-11-11 17:58:51 -080061 outEvent->sensor = '_grv';
62 outEvent->type = SENSOR_TYPE_GRAVITY;
63 return true;
64 }
65 return false;
66}
Mathias Agopian984826c2011-05-17 22:54:42 -070067
Mathias Agopianf001c922010-11-11 17:58:51 -080068status_t GravitySensor::activate(void* ident, bool enabled) {
Peng Xuf66684a2015-07-23 11:41:53 -070069 return mSensorFusion.activate(FUSION_NOMAG, ident, enabled);
Mathias Agopianf001c922010-11-11 17:58:51 -080070}
71
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070072status_t GravitySensor::setDelay(void* ident, int /*handle*/, int64_t ns) {
Peng Xuf66684a2015-07-23 11:41:53 -070073 return mSensorFusion.setDelay(FUSION_NOMAG, ident, ns);
Mathias Agopianf001c922010-11-11 17:58:51 -080074}
75
76Sensor GravitySensor::getSensor() const {
77 sensor_t hwSensor;
78 hwSensor.name = "Gravity Sensor";
Mathias Agopian03193062013-05-10 19:32:39 -070079 hwSensor.vendor = "AOSP";
Mathias Agopian33015422011-05-27 18:18:13 -070080 hwSensor.version = 3;
Mathias Agopianf001c922010-11-11 17:58:51 -080081 hwSensor.handle = '_grv';
82 hwSensor.type = SENSOR_TYPE_GRAVITY;
Mathias Agopian984826c2011-05-17 22:54:42 -070083 hwSensor.maxRange = GRAVITY_EARTH * 2;
Mathias Agopianf001c922010-11-11 17:58:51 -080084 hwSensor.resolution = mAccelerometer.getResolution();
Mathias Agopian33015422011-05-27 18:18:13 -070085 hwSensor.power = mSensorFusion.getPowerUsage();
86 hwSensor.minDelay = mSensorFusion.getMinDelay();
Mathias Agopianf001c922010-11-11 17:58:51 -080087 Sensor sensor(&hwSensor);
88 return sensor;
89}
90
91// ---------------------------------------------------------------------------
92}; // namespace android
93