1 /*
2  * Copyright (C) 2011 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 "CorrectedGyroSensor.h"
26 #include "SensorDevice.h"
27 #include "SensorFusion.h"
28 
29 namespace android {
30 // ---------------------------------------------------------------------------
31 
CorrectedGyroSensor(sensor_t const * list,size_t count)32 CorrectedGyroSensor::CorrectedGyroSensor(sensor_t const* list, size_t count)
33     : VirtualSensor() {
34     for (size_t i=0 ; i<count ; i++) {
35         if (list[i].type == SENSOR_TYPE_GYROSCOPE) {
36             mGyro = Sensor(list + i);
37             break;
38         }
39     }
40 
41     const sensor_t sensor = {
42             .name       = "Corrected Gyroscope Sensor",
43             .vendor     = "AOSP",
44             .version    = 1,
45             .handle     = '_cgy',
46             .type       = SENSOR_TYPE_GYROSCOPE,
47             .maxRange   = mGyro.getMaxValue(),
48             .resolution = mGyro.getResolution(),
49             .power      = mSensorFusion.getPowerUsage(),
50             .minDelay   = mGyro.getMinDelay(),
51     };
52     mSensor = Sensor(&sensor);
53 }
54 
process(sensors_event_t * outEvent,const sensors_event_t & event)55 bool CorrectedGyroSensor::process(sensors_event_t* outEvent,
56         const sensors_event_t& event)
57 {
58     if (event.type == SENSOR_TYPE_GYROSCOPE) {
59         const vec3_t bias(mSensorFusion.getGyroBias());
60         *outEvent = event;
61         outEvent->data[0] -= bias.x;
62         outEvent->data[1] -= bias.y;
63         outEvent->data[2] -= bias.z;
64         outEvent->sensor = '_cgy';
65         return true;
66     }
67     return false;
68 }
69 
activate(void * ident,bool enabled)70 status_t CorrectedGyroSensor::activate(void* ident, bool enabled) {
71     mSensorDevice.activate(ident, mGyro.getHandle(), enabled);
72     return mSensorFusion.activate(FUSION_9AXIS, ident, enabled);
73 }
74 
setDelay(void * ident,int,int64_t ns)75 status_t CorrectedGyroSensor::setDelay(void* ident, int /*handle*/, int64_t ns) {
76     mSensorDevice.setDelay(ident, mGyro.getHandle(), ns);
77     return mSensorFusion.setDelay(FUSION_9AXIS, ident, ns);
78 }
79 
80 // ---------------------------------------------------------------------------
81 }; // namespace android
82 
83