michael@0: /* michael@0: * Copyright (c) 2013, Linux Foundation. All rights reserved michael@0: * michael@0: * Copyright (C) 2008 The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #ifndef ProcessOrientation_h michael@0: #define ProcessOrientation_h michael@0: michael@0: #include "mozilla/Hal.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: // History of observed tilt angles. michael@0: #define TILT_HISTORY_SIZE 40 michael@0: michael@0: class ProcessOrientation { michael@0: public: michael@0: ProcessOrientation() {}; michael@0: ~ProcessOrientation() {}; michael@0: michael@0: int OnSensorChanged(const mozilla::hal::SensorData& event, int deviceCurrentRotation); michael@0: int Reset(); michael@0: michael@0: private: michael@0: int GetProposedRotation(); michael@0: michael@0: // Returns true if the tilt angle is acceptable for a given predicted michael@0: // rotation. michael@0: bool IsTiltAngleAcceptable(int rotation, int tiltAngle); michael@0: michael@0: // Returns true if the orientation angle is acceptable for a given predicted michael@0: // rotation. This function takes into account the gap between adjacent michael@0: // orientations for hysteresis. michael@0: bool IsOrientationAngleAcceptable(int rotation, int orientationAngle, michael@0: int currentRotation); michael@0: michael@0: // Returns true if the predicted rotation is ready to be advertised as a michael@0: // proposed rotation. michael@0: bool IsPredictedRotationAcceptable(long now); michael@0: michael@0: void ClearPredictedRotation(); michael@0: void UpdatePredictedRotation(long now, int rotation); michael@0: bool IsAccelerating(float magnitude); michael@0: void ClearTiltHistory(); michael@0: void AddTiltHistoryEntry(long now, float tilt); michael@0: bool IsFlat(long now); michael@0: bool IsSwinging(long now, float tilt); michael@0: int NextTiltHistoryIndex(int index); michael@0: float RemainingMS(long now, long until); michael@0: michael@0: // The tilt angle range in degrees for each orientation. Beyond these tilt michael@0: // angles, we don't even consider transitioning into the specified orientation. michael@0: // We place more stringent requirements on unnatural orientations than natural michael@0: // ones to make it less likely to accidentally transition into those states. michael@0: // The first value of each pair is negative so it applies a limit when the michael@0: // device is facing down (overhead reading in bed). The second value of each michael@0: // pair is positive so it applies a limit when the device is facing up michael@0: // (resting on a table). The ideal tilt angle is 0 (when the device is vertical) michael@0: // so the limits establish how close to vertical the device must be in order michael@0: // to change orientation. michael@0: static const int tiltTolerance[][4]; michael@0: michael@0: // Timestamp and value of the last accelerometer sample. michael@0: long mLastFilteredTimestampNanos; michael@0: float mLastFilteredX, mLastFilteredY, mLastFilteredZ; michael@0: michael@0: // The last proposed rotation, -1 if unknown. michael@0: int mProposedRotation; michael@0: michael@0: // Value of the current predicted rotation, -1 if unknown. michael@0: int mPredictedRotation; michael@0: michael@0: // Timestamp of when the predicted rotation most recently changed. michael@0: long mPredictedRotationTimestampNanos; michael@0: michael@0: // Timestamp when the device last appeared to be flat for sure (the flat delay michael@0: // elapsed). michael@0: long mFlatTimestampNanos; michael@0: michael@0: // Timestamp when the device last appeared to be swinging. michael@0: long mSwingTimestampNanos; michael@0: michael@0: // Timestamp when the device last appeared to be undergoing external michael@0: // acceleration. michael@0: long mAccelerationTimestampNanos; michael@0: michael@0: struct { michael@0: struct { michael@0: float tiltAngle; michael@0: long timestampNanos; michael@0: } history[TILT_HISTORY_SIZE]; michael@0: int index; michael@0: } mTiltHistory; michael@0: }; michael@0: michael@0: } // namespace mozilla michael@0: #endif