blob: 0e80f1617adc06b3490f5f9a76fd737dd21441a7 [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 }
Peng Xu0cc8f802016-04-05 23:46:03 -070042
43 sensor_t hwSensor;
44 hwSensor.name = "Gravity Sensor";
45 hwSensor.vendor = "AOSP";
46 hwSensor.version = 3;
47 hwSensor.handle = '_grv';
48 hwSensor.type = SENSOR_TYPE_GRAVITY;
49 hwSensor.maxRange = GRAVITY_EARTH * 2;
50 hwSensor.resolution = mAccelerometer.getResolution();
51 hwSensor.power = mSensorFusion.getPowerUsage();
52 hwSensor.minDelay = mSensorFusion.getMinDelay();
53 mSensor = Sensor(&hwSensor);
Mathias Agopianf001c922010-11-11 17:58:51 -080054}
55
56bool GravitySensor::process(sensors_event_t* outEvent,
57 const sensors_event_t& event)
58{
Mathias Agopianf001c922010-11-11 17:58:51 -080059 if (event.type == SENSOR_TYPE_ACCELEROMETER) {
Mathias Agopian984826c2011-05-17 22:54:42 -070060 vec3_t g;
Peng Xuf66684a2015-07-23 11:41:53 -070061 if (!mSensorFusion.hasEstimate(FUSION_NOMAG))
Mathias Agopian33015422011-05-27 18:18:13 -070062 return false;
Peng Xuf66684a2015-07-23 11:41:53 -070063 const mat33_t R(mSensorFusion.getRotationMatrix(FUSION_NOMAG));
Mathias Agopian33015422011-05-27 18:18:13 -070064 // FIXME: we need to estimate the length of gravity because
65 // the accelerometer may have a small scaling error. This
66 // translates to an offset in the linear-acceleration sensor.
67 g = R[2] * GRAVITY_EARTH;
68
Mathias Agopianf001c922010-11-11 17:58:51 -080069 *outEvent = event;
Mathias Agopian984826c2011-05-17 22:54:42 -070070 outEvent->data[0] = g.x;
71 outEvent->data[1] = g.y;
72 outEvent->data[2] = g.z;
Mathias Agopianf001c922010-11-11 17:58:51 -080073 outEvent->sensor = '_grv';
74 outEvent->type = SENSOR_TYPE_GRAVITY;
75 return true;
76 }
77 return false;
78}
Mathias Agopian984826c2011-05-17 22:54:42 -070079
Mathias Agopianf001c922010-11-11 17:58:51 -080080status_t GravitySensor::activate(void* ident, bool enabled) {
Peng Xuf66684a2015-07-23 11:41:53 -070081 return mSensorFusion.activate(FUSION_NOMAG, ident, enabled);
Mathias Agopianf001c922010-11-11 17:58:51 -080082}
83
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070084status_t GravitySensor::setDelay(void* ident, int /*handle*/, int64_t ns) {
Peng Xuf66684a2015-07-23 11:41:53 -070085 return mSensorFusion.setDelay(FUSION_NOMAG, ident, ns);
Mathias Agopianf001c922010-11-11 17:58:51 -080086}
87
Peng Xu0cc8f802016-04-05 23:46:03 -070088const Sensor& GravitySensor::getSensor() const {
89 return mSensor;
Mathias Agopianf001c922010-11-11 17:58:51 -080090}
91
92// ---------------------------------------------------------------------------
93}; // namespace android
94