LinearAccelerationSensor.cpp revision 50b66767f6c5635430483393e17d15969dfe2f05
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
56status_t LinearAccelerationSensor::activate(void* ident, bool enabled) {
57    return mGravitySensor.activate(ident, enabled);
58}
59
60status_t LinearAccelerationSensor::setDelay(void* ident, int handle, int64_t ns) {
61    return mGravitySensor.setDelay(ident, handle, ns);
62}
63
64Sensor LinearAccelerationSensor::getSensor() const {
65    Sensor gsensor(mGravitySensor.getSensor());
66    sensor_t hwSensor;
67    hwSensor.name       = "Linear Acceleration Sensor";
68    hwSensor.vendor     = "Google Inc.";
69    hwSensor.version    = 1;
70    hwSensor.handle     = '_lin';
71    hwSensor.type       = SENSOR_TYPE_LINEAR_ACCELERATION;
72    hwSensor.maxRange   = gsensor.getMaxValue();
73    hwSensor.resolution = gsensor.getResolution();
74    hwSensor.power      = gsensor.getPowerUsage();
75    hwSensor.minDelay   = gsensor.getMinDelay();
76    Sensor sensor(&hwSensor);
77    return sensor;
78}
79
80// ---------------------------------------------------------------------------
81}; // namespace android
82
83