blob: 0c3ad633f6e10ca8396d1ee547009648e8f76e2b [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
2 * Copyright (C) 2012 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#define LOG_TAG "VelocityControl"
18//#define LOG_NDEBUG 0
19
20// Log debug messages about acceleration.
21#define DEBUG_ACCELERATION 0
22
23#include <math.h>
24#include <limits.h>
25
26#include <input/VelocityControl.h>
27#include <utils/BitSet.h>
28#include <utils/Timers.h>
29
30namespace android {
31
32// --- VelocityControl ---
33
34const nsecs_t VelocityControl::STOP_TIME;
35
36VelocityControl::VelocityControl() {
37 reset();
38}
39
40void VelocityControl::setParameters(const VelocityControlParameters& parameters) {
41 mParameters = parameters;
42 reset();
43}
44
45void VelocityControl::reset() {
46 mLastMovementTime = LLONG_MIN;
47 mRawPosition.x = 0;
48 mRawPosition.y = 0;
49 mVelocityTracker.clear();
50}
51
52void VelocityControl::move(nsecs_t eventTime, float* deltaX, float* deltaY) {
53 if ((deltaX && *deltaX) || (deltaY && *deltaY)) {
54 if (eventTime >= mLastMovementTime + STOP_TIME) {
55#if DEBUG_ACCELERATION
dingxiaobo595c9112021-08-20 11:19:54 +080056 if (mLastMovementTime != LLONG_MIN) {
57 ALOGD("VelocityControl: stopped, last movement was %0.3fms ago",
58 (eventTime - mLastMovementTime) * 0.000001f);
59 }
Jeff Brown5912f952013-07-01 19:10:31 -070060#endif
61 reset();
62 }
63
64 mLastMovementTime = eventTime;
65 if (deltaX) {
66 mRawPosition.x += *deltaX;
67 }
68 if (deltaY) {
69 mRawPosition.y += *deltaY;
70 }
71 mVelocityTracker.addMovement(eventTime, BitSet32(BitSet32::valueForBit(0)), &mRawPosition);
72
73 float vx, vy;
74 float scale = mParameters.scale;
75 if (mVelocityTracker.getVelocity(0, &vx, &vy)) {
76 float speed = hypotf(vx, vy) * scale;
77 if (speed >= mParameters.highThreshold) {
78 // Apply full acceleration above the high speed threshold.
79 scale *= mParameters.acceleration;
80 } else if (speed > mParameters.lowThreshold) {
81 // Linearly interpolate the acceleration to apply between the low and high
82 // speed thresholds.
83 scale *= 1 + (speed - mParameters.lowThreshold)
84 / (mParameters.highThreshold - mParameters.lowThreshold)
85 * (mParameters.acceleration - 1);
86 }
87
88#if DEBUG_ACCELERATION
89 ALOGD("VelocityControl(%0.3f, %0.3f, %0.3f, %0.3f): "
90 "vx=%0.3f, vy=%0.3f, speed=%0.3f, accel=%0.3f",
91 mParameters.scale, mParameters.lowThreshold, mParameters.highThreshold,
92 mParameters.acceleration,
93 vx, vy, speed, scale / mParameters.scale);
94#endif
95 } else {
96#if DEBUG_ACCELERATION
97 ALOGD("VelocityControl(%0.3f, %0.3f, %0.3f, %0.3f): unknown velocity",
98 mParameters.scale, mParameters.lowThreshold, mParameters.highThreshold,
99 mParameters.acceleration);
100#endif
101 }
102
103 if (deltaX) {
104 *deltaX *= scale;
105 }
106 if (deltaY) {
107 *deltaY *= scale;
108 }
109 }
110}
111
112} // namespace android