blob: 0b5f8e5091210007fac5a1aa30a1a2f68ab39ed8 [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.
dingxiaobo70dc8792021-08-23 11:25:30 +080021static constexpr bool DEBUG_ACCELERATION = false;
Jeff Brown5912f952013-07-01 19:10:31 -070022
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
Zixuan Quad074412022-11-23 19:20:58 +000040const VelocityControlParameters& VelocityControl::getParameters() const{
Zixuan Qu79b76d12022-11-11 04:47:51 +000041 return mParameters;
42}
43
Jeff Brown5912f952013-07-01 19:10:31 -070044void VelocityControl::setParameters(const VelocityControlParameters& parameters) {
45 mParameters = parameters;
46 reset();
47}
48
49void VelocityControl::reset() {
50 mLastMovementTime = LLONG_MIN;
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000051 mRawPositionX = 0;
52 mRawPositionY = 0;
Jeff Brown5912f952013-07-01 19:10:31 -070053 mVelocityTracker.clear();
54}
55
56void VelocityControl::move(nsecs_t eventTime, float* deltaX, float* deltaY) {
57 if ((deltaX && *deltaX) || (deltaY && *deltaY)) {
58 if (eventTime >= mLastMovementTime + STOP_TIME) {
dingxiaobo70dc8792021-08-23 11:25:30 +080059 if (DEBUG_ACCELERATION && mLastMovementTime != LLONG_MIN) {
60 ALOGD("VelocityControl: stopped, last movement was %0.3fms ago",
61 (eventTime - mLastMovementTime) * 0.000001f);
62 }
Jeff Brown5912f952013-07-01 19:10:31 -070063 reset();
64 }
65
66 mLastMovementTime = eventTime;
67 if (deltaX) {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000068 mRawPositionX += *deltaX;
Jeff Brown5912f952013-07-01 19:10:31 -070069 }
70 if (deltaY) {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000071 mRawPositionY += *deltaY;
Jeff Brown5912f952013-07-01 19:10:31 -070072 }
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000073 mVelocityTracker.addMovement(eventTime, BitSet32(BitSet32::valueForBit(0)),
74 {{AMOTION_EVENT_AXIS_X, {mRawPositionX}},
75 {AMOTION_EVENT_AXIS_Y, {mRawPositionY}}});
Jeff Brown5912f952013-07-01 19:10:31 -070076
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000077 std::optional<float> vx = mVelocityTracker.getVelocity(AMOTION_EVENT_AXIS_X, 0);
78 std::optional<float> vy = mVelocityTracker.getVelocity(AMOTION_EVENT_AXIS_Y, 0);
Jeff Brown5912f952013-07-01 19:10:31 -070079 float scale = mParameters.scale;
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000080 if (vx && vy) {
81 float speed = hypotf(*vx, *vy) * scale;
Jeff Brown5912f952013-07-01 19:10:31 -070082 if (speed >= mParameters.highThreshold) {
83 // Apply full acceleration above the high speed threshold.
84 scale *= mParameters.acceleration;
85 } else if (speed > mParameters.lowThreshold) {
86 // Linearly interpolate the acceleration to apply between the low and high
87 // speed thresholds.
88 scale *= 1 + (speed - mParameters.lowThreshold)
89 / (mParameters.highThreshold - mParameters.lowThreshold)
90 * (mParameters.acceleration - 1);
91 }
92
dingxiaobo70dc8792021-08-23 11:25:30 +080093 if (DEBUG_ACCELERATION) {
94 ALOGD("VelocityControl(%0.3f, %0.3f, %0.3f, %0.3f): "
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000095 "vx=%0.3f, vy=%0.3f, speed=%0.3f, accel=%0.3f",
96 mParameters.scale, mParameters.lowThreshold, mParameters.highThreshold,
97 mParameters.acceleration, *vx, *vy, speed, scale / mParameters.scale);
dingxiaobo70dc8792021-08-23 11:25:30 +080098 }
99
Jeff Brown5912f952013-07-01 19:10:31 -0700100 } else {
dingxiaobo70dc8792021-08-23 11:25:30 +0800101 if (DEBUG_ACCELERATION) {
102 ALOGD("VelocityControl(%0.3f, %0.3f, %0.3f, %0.3f): unknown velocity",
103 mParameters.scale, mParameters.lowThreshold, mParameters.highThreshold,
104 mParameters.acceleration);
105 }
Jeff Brown5912f952013-07-01 19:10:31 -0700106 }
107
108 if (deltaX) {
109 *deltaX *= scale;
110 }
111 if (deltaY) {
112 *deltaY *= scale;
113 }
114 }
115}
116
117} // namespace android