| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1 | /* | 
|  | 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. | 
| dingxiaobo | 70dc879 | 2021-08-23 11:25:30 +0800 | [diff] [blame] | 21 | static constexpr bool DEBUG_ACCELERATION = false; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 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 |  | 
|  | 30 | namespace android { | 
|  | 31 |  | 
|  | 32 | // --- VelocityControl --- | 
|  | 33 |  | 
|  | 34 | const nsecs_t VelocityControl::STOP_TIME; | 
|  | 35 |  | 
|  | 36 | VelocityControl::VelocityControl() { | 
|  | 37 | reset(); | 
|  | 38 | } | 
|  | 39 |  | 
| Zixuan Qu | 79b76d1 | 2022-11-11 04:47:51 +0000 | [diff] [blame] | 40 | VelocityControlParameters& VelocityControl::getParameters() { | 
|  | 41 | return mParameters; | 
|  | 42 | } | 
|  | 43 |  | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 44 | void VelocityControl::setParameters(const VelocityControlParameters& parameters) { | 
|  | 45 | mParameters = parameters; | 
|  | 46 | reset(); | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | void VelocityControl::reset() { | 
|  | 50 | mLastMovementTime = LLONG_MIN; | 
| Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 51 | mRawPositionX = 0; | 
|  | 52 | mRawPositionY = 0; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 53 | mVelocityTracker.clear(); | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | void VelocityControl::move(nsecs_t eventTime, float* deltaX, float* deltaY) { | 
|  | 57 | if ((deltaX && *deltaX) || (deltaY && *deltaY)) { | 
|  | 58 | if (eventTime >= mLastMovementTime + STOP_TIME) { | 
| dingxiaobo | 70dc879 | 2021-08-23 11:25:30 +0800 | [diff] [blame] | 59 | if (DEBUG_ACCELERATION && mLastMovementTime != LLONG_MIN) { | 
|  | 60 | ALOGD("VelocityControl: stopped, last movement was %0.3fms ago", | 
|  | 61 | (eventTime - mLastMovementTime) * 0.000001f); | 
|  | 62 | } | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 63 | reset(); | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | mLastMovementTime = eventTime; | 
|  | 67 | if (deltaX) { | 
| Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 68 | mRawPositionX += *deltaX; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 69 | } | 
|  | 70 | if (deltaY) { | 
| Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 71 | mRawPositionY += *deltaY; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 72 | } | 
| Siarhei Vishniakou | 8d23203 | 2023-01-11 08:17:21 -0800 | [diff] [blame] | 73 | mVelocityTracker.addMovement(eventTime, /*pointerId=*/0, AMOTION_EVENT_AXIS_X, | 
|  | 74 | mRawPositionX); | 
|  | 75 | mVelocityTracker.addMovement(eventTime, /*pointerId=*/0, AMOTION_EVENT_AXIS_Y, | 
|  | 76 | mRawPositionY); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 77 |  | 
| Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 78 | std::optional<float> vx = mVelocityTracker.getVelocity(AMOTION_EVENT_AXIS_X, 0); | 
|  | 79 | std::optional<float> vy = mVelocityTracker.getVelocity(AMOTION_EVENT_AXIS_Y, 0); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 80 | float scale = mParameters.scale; | 
| Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 81 | if (vx && vy) { | 
|  | 82 | float speed = hypotf(*vx, *vy) * scale; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 83 | if (speed >= mParameters.highThreshold) { | 
|  | 84 | // Apply full acceleration above the high speed threshold. | 
|  | 85 | scale *= mParameters.acceleration; | 
|  | 86 | } else if (speed > mParameters.lowThreshold) { | 
|  | 87 | // Linearly interpolate the acceleration to apply between the low and high | 
|  | 88 | // speed thresholds. | 
|  | 89 | scale *= 1 + (speed - mParameters.lowThreshold) | 
|  | 90 | / (mParameters.highThreshold - mParameters.lowThreshold) | 
|  | 91 | * (mParameters.acceleration - 1); | 
|  | 92 | } | 
|  | 93 |  | 
| dingxiaobo | 70dc879 | 2021-08-23 11:25:30 +0800 | [diff] [blame] | 94 | if (DEBUG_ACCELERATION) { | 
|  | 95 | ALOGD("VelocityControl(%0.3f, %0.3f, %0.3f, %0.3f): " | 
| Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 96 | "vx=%0.3f, vy=%0.3f, speed=%0.3f, accel=%0.3f", | 
|  | 97 | mParameters.scale, mParameters.lowThreshold, mParameters.highThreshold, | 
|  | 98 | mParameters.acceleration, *vx, *vy, speed, scale / mParameters.scale); | 
| dingxiaobo | 70dc879 | 2021-08-23 11:25:30 +0800 | [diff] [blame] | 99 | } | 
|  | 100 |  | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 101 | } else { | 
| dingxiaobo | 70dc879 | 2021-08-23 11:25:30 +0800 | [diff] [blame] | 102 | if (DEBUG_ACCELERATION) { | 
|  | 103 | ALOGD("VelocityControl(%0.3f, %0.3f, %0.3f, %0.3f): unknown velocity", | 
|  | 104 | mParameters.scale, mParameters.lowThreshold, mParameters.highThreshold, | 
|  | 105 | mParameters.acceleration); | 
|  | 106 | } | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 107 | } | 
|  | 108 |  | 
|  | 109 | if (deltaX) { | 
|  | 110 | *deltaX *= scale; | 
|  | 111 | } | 
|  | 112 | if (deltaY) { | 
|  | 113 | *deltaY *= scale; | 
|  | 114 | } | 
|  | 115 | } | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | } // namespace android |