Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 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" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 26 | #include "SensorDevice.h" |
| 27 | #include "SensorFusion.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | // --------------------------------------------------------------------------- |
| 31 | |
| 32 | GravitySensor::GravitySensor(sensor_t const* list, size_t count) |
| 33 | : mSensorDevice(SensorDevice::getInstance()), |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 34 | mSensorFusion(SensorFusion::getInstance()) |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 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 | |
| 44 | bool GravitySensor::process(sensors_event_t* outEvent, |
| 45 | const sensors_event_t& event) |
| 46 | { |
| 47 | const static double NS2S = 1.0 / 1000000000.0; |
| 48 | if (event.type == SENSOR_TYPE_ACCELEROMETER) { |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 49 | vec3_t g; |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 50 | if (!mSensorFusion.hasEstimate()) |
| 51 | return false; |
| 52 | const mat33_t R(mSensorFusion.getRotationMatrix()); |
| 53 | // FIXME: we need to estimate the length of gravity because |
| 54 | // the accelerometer may have a small scaling error. This |
| 55 | // translates to an offset in the linear-acceleration sensor. |
| 56 | g = R[2] * GRAVITY_EARTH; |
| 57 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 58 | *outEvent = event; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 59 | outEvent->data[0] = g.x; |
| 60 | outEvent->data[1] = g.y; |
| 61 | outEvent->data[2] = g.z; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 62 | outEvent->sensor = '_grv'; |
| 63 | outEvent->type = SENSOR_TYPE_GRAVITY; |
| 64 | return true; |
| 65 | } |
| 66 | return false; |
| 67 | } |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 68 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 69 | status_t GravitySensor::activate(void* ident, bool enabled) { |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 70 | return mSensorFusion.activate(this, enabled); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 73 | status_t GravitySensor::setDelay(void* ident, int handle, int64_t ns) { |
| 74 | return mSensorFusion.setDelay(this, ns); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | Sensor GravitySensor::getSensor() const { |
| 78 | sensor_t hwSensor; |
| 79 | hwSensor.name = "Gravity Sensor"; |
| 80 | hwSensor.vendor = "Google Inc."; |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 81 | hwSensor.version = 3; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 82 | hwSensor.handle = '_grv'; |
| 83 | hwSensor.type = SENSOR_TYPE_GRAVITY; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 84 | hwSensor.maxRange = GRAVITY_EARTH * 2; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 85 | hwSensor.resolution = mAccelerometer.getResolution(); |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 86 | hwSensor.power = mSensorFusion.getPowerUsage(); |
| 87 | hwSensor.minDelay = mSensorFusion.getMinDelay(); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 88 | Sensor sensor(&hwSensor); |
| 89 | return sensor; |
| 90 | } |
| 91 | |
| 92 | // --------------------------------------------------------------------------- |
| 93 | }; // namespace android |
| 94 | |