|
1 /* |
|
2 * Copyright (c) 2013, Linux Foundation. All rights reserved |
|
3 * |
|
4 * Copyright (C) 2008 The Android Open Source Project |
|
5 * |
|
6 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
7 * you may not use this file except in compliance with the License. |
|
8 * You may obtain a copy of the License at |
|
9 * |
|
10 * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 * |
|
12 * Unless required by applicable law or agreed to in writing, software |
|
13 * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 * See the License for the specific language governing permissions and |
|
16 * limitations under the License. |
|
17 */ |
|
18 |
|
19 #ifndef ProcessOrientation_h |
|
20 #define ProcessOrientation_h |
|
21 |
|
22 #include "mozilla/Hal.h" |
|
23 |
|
24 namespace mozilla { |
|
25 |
|
26 // History of observed tilt angles. |
|
27 #define TILT_HISTORY_SIZE 40 |
|
28 |
|
29 class ProcessOrientation { |
|
30 public: |
|
31 ProcessOrientation() {}; |
|
32 ~ProcessOrientation() {}; |
|
33 |
|
34 int OnSensorChanged(const mozilla::hal::SensorData& event, int deviceCurrentRotation); |
|
35 int Reset(); |
|
36 |
|
37 private: |
|
38 int GetProposedRotation(); |
|
39 |
|
40 // Returns true if the tilt angle is acceptable for a given predicted |
|
41 // rotation. |
|
42 bool IsTiltAngleAcceptable(int rotation, int tiltAngle); |
|
43 |
|
44 // Returns true if the orientation angle is acceptable for a given predicted |
|
45 // rotation. This function takes into account the gap between adjacent |
|
46 // orientations for hysteresis. |
|
47 bool IsOrientationAngleAcceptable(int rotation, int orientationAngle, |
|
48 int currentRotation); |
|
49 |
|
50 // Returns true if the predicted rotation is ready to be advertised as a |
|
51 // proposed rotation. |
|
52 bool IsPredictedRotationAcceptable(long now); |
|
53 |
|
54 void ClearPredictedRotation(); |
|
55 void UpdatePredictedRotation(long now, int rotation); |
|
56 bool IsAccelerating(float magnitude); |
|
57 void ClearTiltHistory(); |
|
58 void AddTiltHistoryEntry(long now, float tilt); |
|
59 bool IsFlat(long now); |
|
60 bool IsSwinging(long now, float tilt); |
|
61 int NextTiltHistoryIndex(int index); |
|
62 float RemainingMS(long now, long until); |
|
63 |
|
64 // The tilt angle range in degrees for each orientation. Beyond these tilt |
|
65 // angles, we don't even consider transitioning into the specified orientation. |
|
66 // We place more stringent requirements on unnatural orientations than natural |
|
67 // ones to make it less likely to accidentally transition into those states. |
|
68 // The first value of each pair is negative so it applies a limit when the |
|
69 // device is facing down (overhead reading in bed). The second value of each |
|
70 // pair is positive so it applies a limit when the device is facing up |
|
71 // (resting on a table). The ideal tilt angle is 0 (when the device is vertical) |
|
72 // so the limits establish how close to vertical the device must be in order |
|
73 // to change orientation. |
|
74 static const int tiltTolerance[][4]; |
|
75 |
|
76 // Timestamp and value of the last accelerometer sample. |
|
77 long mLastFilteredTimestampNanos; |
|
78 float mLastFilteredX, mLastFilteredY, mLastFilteredZ; |
|
79 |
|
80 // The last proposed rotation, -1 if unknown. |
|
81 int mProposedRotation; |
|
82 |
|
83 // Value of the current predicted rotation, -1 if unknown. |
|
84 int mPredictedRotation; |
|
85 |
|
86 // Timestamp of when the predicted rotation most recently changed. |
|
87 long mPredictedRotationTimestampNanos; |
|
88 |
|
89 // Timestamp when the device last appeared to be flat for sure (the flat delay |
|
90 // elapsed). |
|
91 long mFlatTimestampNanos; |
|
92 |
|
93 // Timestamp when the device last appeared to be swinging. |
|
94 long mSwingTimestampNanos; |
|
95 |
|
96 // Timestamp when the device last appeared to be undergoing external |
|
97 // acceleration. |
|
98 long mAccelerationTimestampNanos; |
|
99 |
|
100 struct { |
|
101 struct { |
|
102 float tiltAngle; |
|
103 long timestampNanos; |
|
104 } history[TILT_HISTORY_SIZE]; |
|
105 int index; |
|
106 } mTiltHistory; |
|
107 }; |
|
108 |
|
109 } // namespace mozilla |
|
110 #endif |