GravitySensor.cpp revision 87c9dbb728febe9ce035874796c58f308043879d
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
27namespace android {
28// ---------------------------------------------------------------------------
29
30GravitySensor::GravitySensor(sensor_t const* list, size_t count)
31    : mSensorDevice(SensorDevice::getInstance()),
32      mEnabled(false), mAccTime(0),
33      mLowPass(M_SQRT1_2, 1.5f),
34      mX(mLowPass), mY(mLowPass), mZ(mLowPass)
35
36{
37    for (size_t i=0 ; i<count ; i++) {
38        if (list[i].type == SENSOR_TYPE_ACCELEROMETER) {
39            mAccelerometer = Sensor(list + i);
40            break;
41        }
42    }
43}
44
45bool GravitySensor::process(sensors_event_t* outEvent,
46        const sensors_event_t& event)
47{
48    const static double NS2S = 1.0 / 1000000000.0;
49    if (event.type == SENSOR_TYPE_ACCELEROMETER) {
50        float x, y, z;
51        const double now = event.timestamp * NS2S;
52        if (mAccTime == 0) {
53            x = mX.init(event.acceleration.x);
54            y = mY.init(event.acceleration.y);
55            z = mZ.init(event.acceleration.z);
56        } else {
57            double dT = now - mAccTime;
58            mLowPass.setSamplingPeriod(dT);
59            x = mX(event.acceleration.x);
60            y = mY(event.acceleration.y);
61            z = mZ(event.acceleration.z);
62        }
63        mAccTime = now;
64        *outEvent = event;
65        outEvent->data[0] = x;
66        outEvent->data[1] = y;
67        outEvent->data[2] = z;
68        outEvent->sensor = '_grv';
69        outEvent->type = SENSOR_TYPE_GRAVITY;
70        return true;
71    }
72    return false;
73}
74
75bool GravitySensor::isEnabled() const {
76    return mEnabled;
77}
78
79status_t GravitySensor::activate(void* ident, bool enabled) {
80    status_t err = mSensorDevice.activate(this, mAccelerometer.getHandle(), enabled);
81    if (err == NO_ERROR) {
82        mEnabled = enabled;
83        if (enabled) {
84            mAccTime = 0;
85        }
86    }
87    return err;
88}
89
90status_t GravitySensor::setDelay(void* ident, int handle, int64_t ns)
91{
92    return mSensorDevice.setDelay(this, mAccelerometer.getHandle(), ns);
93}
94
95Sensor GravitySensor::getSensor() const {
96    sensor_t hwSensor;
97    hwSensor.name       = "Gravity Sensor";
98    hwSensor.vendor     = "Google Inc.";
99    hwSensor.version    = 1;
100    hwSensor.handle     = '_grv';
101    hwSensor.type       = SENSOR_TYPE_GRAVITY;
102    hwSensor.maxRange   = mAccelerometer.getMaxValue();
103    hwSensor.resolution = mAccelerometer.getResolution();
104    hwSensor.power      = mAccelerometer.getPowerUsage();
105    hwSensor.minDelay   = mAccelerometer.getMinDelay();
106    Sensor sensor(&hwSensor);
107    return sensor;
108}
109
110// ---------------------------------------------------------------------------
111}; // namespace android
112
113