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 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} 74status_t GravitySensor::activate(void* ident, bool enabled) { 75 status_t err = mSensorDevice.activate(this, mAccelerometer.getHandle(), enabled); 76 if (err == NO_ERROR) { 77 if (enabled) { 78 mAccTime = 0; 79 } 80 } 81 return err; 82} 83 84status_t GravitySensor::setDelay(void* ident, int handle, int64_t ns) 85{ 86 return mSensorDevice.setDelay(this, mAccelerometer.getHandle(), ns); 87} 88 89Sensor GravitySensor::getSensor() const { 90 sensor_t hwSensor; 91 hwSensor.name = "Gravity Sensor"; 92 hwSensor.vendor = "Google Inc."; 93 hwSensor.version = 1; 94 hwSensor.handle = '_grv'; 95 hwSensor.type = SENSOR_TYPE_GRAVITY; 96 hwSensor.maxRange = mAccelerometer.getMaxValue(); 97 hwSensor.resolution = mAccelerometer.getResolution(); 98 hwSensor.power = mAccelerometer.getPowerUsage(); 99 hwSensor.minDelay = mAccelerometer.getMinDelay(); 100 Sensor sensor(&hwSensor); 101 return sensor; 102} 103 104// --------------------------------------------------------------------------- 105}; // namespace android 106 107