1984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian/*
2984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * Copyright (C) 2011 The Android Open Source Project
3984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian *
4984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * you may not use this file except in compliance with the License.
6984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * You may obtain a copy of the License at
7984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian *
8984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian *
10984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * Unless required by applicable law or agreed to in writing, software
11984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * See the License for the specific language governing permissions and
14984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian * limitations under the License.
15984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian */
16984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
17984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#ifndef ANDROID_SENSOR_FUSION_H
18984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#define ANDROID_SENSOR_FUSION_H
19984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
20984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include <stdint.h>
21984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include <sys/types.h>
22984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
23984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include <utils/SortedVector.h>
24984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include <utils/Singleton.h>
25984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include <utils/String8.h>
26984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
27984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include <gui/Sensor.h>
28984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
29984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include "Fusion.h"
30984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
31984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian// ---------------------------------------------------------------------------
32984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
33984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopiannamespace android {
34984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian// ---------------------------------------------------------------------------
35984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
36984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianclass SensorDevice;
37984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
38984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianclass SensorFusion : public Singleton<SensorFusion> {
39984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    friend class Singleton<SensorFusion>;
40724d91d778e71c8186399f4955de14b54812b3edAravind Akella    static const nsecs_t DEFAULT_EVENTS_PERIOD = 200000000;  //  5 Hz
41984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
42984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    SensorDevice& mSensorDevice;
43984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    Sensor mAcc;
44984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    Sensor mMag;
45984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    Sensor mGyro;
46984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    Fusion mFusion;
47984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    bool mEnabled;
482e2a560c4b60c24258e0eaadc1189eb9dcc1a0b4Mathias Agopian    float mEstimatedGyroRate;
49984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    nsecs_t mTargetDelayNs;
50984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    nsecs_t mGyroTime;
513301542828febc768e1df42892cfac4992c35474Mathias Agopian    vec4_t mAttitude;
52984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    SortedVector<void*> mClients;
53984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
54984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    SensorFusion();
55984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
56984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopianpublic:
57984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    void process(const sensors_event_t& event);
58984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
59984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    bool isEnabled() const { return mEnabled; }
603301542828febc768e1df42892cfac4992c35474Mathias Agopian    bool hasEstimate() const { return mFusion.hasEstimate(); }
613301542828febc768e1df42892cfac4992c35474Mathias Agopian    mat33_t getRotationMatrix() const { return mFusion.getRotationMatrix(); }
623301542828febc768e1df42892cfac4992c35474Mathias Agopian    vec4_t getAttitude() const { return mAttitude; }
63984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    vec3_t getGyroBias() const { return mFusion.getBias(); }
642e2a560c4b60c24258e0eaadc1189eb9dcc1a0b4Mathias Agopian    float getEstimatedRate() const { return mEstimatedGyroRate; }
65984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
66984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    status_t activate(void* ident, bool enabled);
67984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    status_t setDelay(void* ident, int64_t ns);
68984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
69984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    float getPowerUsage() const;
70984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian    int32_t getMinDelay() const;
71984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
72ba02cd2f6cc3f59adf66cb2b9176bfe6c9e382d1Mathias Agopian    void dump(String8& result);
73984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian};
74984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
75984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
76984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian// ---------------------------------------------------------------------------
77984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian}; // namespace android
78984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
79984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#endif // ANDROID_SENSOR_FUSION_H
80