GravitySensor.cpp revision 0cc8f809924706c7d683da30605f432635dd5bb6
1/*
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"
26#include "SensorDevice.h"
27#include "SensorFusion.h"
28
29namespace android {
30// ---------------------------------------------------------------------------
31
32GravitySensor::GravitySensor(sensor_t const* list, size_t count)
33    : mSensorDevice(SensorDevice::getInstance()),
34      mSensorFusion(SensorFusion::getInstance())
35{
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    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);
54}
55
56bool GravitySensor::process(sensors_event_t* outEvent,
57        const sensors_event_t& event)
58{
59    if (event.type == SENSOR_TYPE_ACCELEROMETER) {
60        vec3_t g;
61        if (!mSensorFusion.hasEstimate(FUSION_NOMAG))
62            return false;
63        const mat33_t R(mSensorFusion.getRotationMatrix(FUSION_NOMAG));
64        // 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
69        *outEvent = event;
70        outEvent->data[0] = g.x;
71        outEvent->data[1] = g.y;
72        outEvent->data[2] = g.z;
73        outEvent->sensor = '_grv';
74        outEvent->type = SENSOR_TYPE_GRAVITY;
75        return true;
76    }
77    return false;
78}
79
80status_t GravitySensor::activate(void* ident, bool enabled) {
81    return mSensorFusion.activate(FUSION_NOMAG, ident, enabled);
82}
83
84status_t GravitySensor::setDelay(void* ident, int /*handle*/, int64_t ns) {
85    return mSensorFusion.setDelay(FUSION_NOMAG, ident, ns);
86}
87
88const Sensor& GravitySensor::getSensor() const {
89    return mSensor;
90}
91
92// ---------------------------------------------------------------------------
93}; // namespace android
94
95