LinearAccelerationSensor.cpp revision f001c92436b4a66eb7687286325ced7f10c9f917
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 "LinearAccelerationSensor.h"
26
27namespace android {
28// ---------------------------------------------------------------------------
29
30LinearAccelerationSensor::LinearAccelerationSensor(sensor_t const* list, size_t count)
31    : mSensorDevice(SensorDevice::getInstance()),
32      mGravitySensor(list, count)
33{
34    mData[0] = mData[1] = mData[2] = 0;
35}
36
37bool LinearAccelerationSensor::process(sensors_event_t* outEvent,
38        const sensors_event_t& event)
39{
40    bool result = mGravitySensor.process(outEvent, event);
41    if (result) {
42        if (event.type == SENSOR_TYPE_ACCELEROMETER) {
43            mData[0] = event.acceleration.x;
44            mData[1] = event.acceleration.y;
45            mData[2] = event.acceleration.z;
46        }
47        outEvent->data[0] = mData[0] - outEvent->data[0];
48        outEvent->data[1] = mData[1] - outEvent->data[1];
49        outEvent->data[2] = mData[2] - outEvent->data[2];
50        outEvent->sensor = '_lin';
51        outEvent->type = SENSOR_TYPE_LINEAR_ACCELERATION;
52    }
53    return result;
54}
55
56bool LinearAccelerationSensor::isEnabled() const {
57    return mGravitySensor.isEnabled();
58}
59
60status_t LinearAccelerationSensor::activate(void* ident, bool enabled) {
61    return mGravitySensor.activate(ident, enabled);
62}
63
64status_t LinearAccelerationSensor::setDelay(void* ident, int handle, int64_t ns) {
65    return mGravitySensor.setDelay(ident, handle, ns);
66}
67
68Sensor LinearAccelerationSensor::getSensor() const {
69    Sensor gsensor(mGravitySensor.getSensor());
70    sensor_t hwSensor;
71    hwSensor.name       = "Linear Acceleration Sensor";
72    hwSensor.vendor     = "Google Inc.";
73    hwSensor.version    = 1;
74    hwSensor.handle     = '_lin';
75    hwSensor.type       = SENSOR_TYPE_LINEAR_ACCELERATION;
76    hwSensor.maxRange   = gsensor.getMaxValue();
77    hwSensor.resolution = gsensor.getResolution();
78    hwSensor.power      = gsensor.getPowerUsage();
79    hwSensor.minDelay   = gsensor.getMinDelay();
80    Sensor sensor(&hwSensor);
81    return sensor;
82}
83
84// ---------------------------------------------------------------------------
85}; // namespace android
86
87