widget/gonk/libui/VelocityControl.cpp

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * Copyright (C) 2012 The Android Open Source Project
michael@0 3 *
michael@0 4 * Licensed under the Apache License, Version 2.0 (the "License");
michael@0 5 * you may not use this file except in compliance with the License.
michael@0 6 * You may obtain a copy of the License at
michael@0 7 *
michael@0 8 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 9 *
michael@0 10 * Unless required by applicable law or agreed to in writing, software
michael@0 11 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 13 * See the License for the specific language governing permissions and
michael@0 14 * limitations under the License.
michael@0 15 */
michael@0 16
michael@0 17 #define LOG_TAG "VelocityControl"
michael@0 18 //#define LOG_NDEBUG 0
michael@0 19
michael@0 20 // Log debug messages about acceleration.
michael@0 21 #define DEBUG_ACCELERATION 0
michael@0 22
michael@0 23 #include <math.h>
michael@0 24 #include <limits.h>
michael@0 25
michael@0 26 #include "VelocityControl.h"
michael@0 27 #include <utils/BitSet.h>
michael@0 28 #include <utils/Timers.h>
michael@0 29
michael@0 30 namespace android {
michael@0 31
michael@0 32 // --- VelocityControl ---
michael@0 33
michael@0 34 const nsecs_t VelocityControl::STOP_TIME;
michael@0 35
michael@0 36 VelocityControl::VelocityControl() {
michael@0 37 reset();
michael@0 38 }
michael@0 39
michael@0 40 void VelocityControl::setParameters(const VelocityControlParameters& parameters) {
michael@0 41 mParameters = parameters;
michael@0 42 reset();
michael@0 43 }
michael@0 44
michael@0 45 void VelocityControl::reset() {
michael@0 46 mLastMovementTime = LLONG_MIN;
michael@0 47 mRawPosition.x = 0;
michael@0 48 mRawPosition.y = 0;
michael@0 49 mVelocityTracker.clear();
michael@0 50 }
michael@0 51
michael@0 52 void VelocityControl::move(nsecs_t eventTime, float* deltaX, float* deltaY) {
michael@0 53 if ((deltaX && *deltaX) || (deltaY && *deltaY)) {
michael@0 54 if (eventTime >= mLastMovementTime + STOP_TIME) {
michael@0 55 #if DEBUG_ACCELERATION
michael@0 56 ALOGD("VelocityControl: stopped, last movement was %0.3fms ago",
michael@0 57 (eventTime - mLastMovementTime) * 0.000001f);
michael@0 58 #endif
michael@0 59 reset();
michael@0 60 }
michael@0 61
michael@0 62 mLastMovementTime = eventTime;
michael@0 63 if (deltaX) {
michael@0 64 mRawPosition.x += *deltaX;
michael@0 65 }
michael@0 66 if (deltaY) {
michael@0 67 mRawPosition.y += *deltaY;
michael@0 68 }
michael@0 69 mVelocityTracker.addMovement(eventTime, BitSet32(BitSet32::valueForBit(0)), &mRawPosition);
michael@0 70
michael@0 71 float vx, vy;
michael@0 72 float scale = mParameters.scale;
michael@0 73 if (mVelocityTracker.getVelocity(0, &vx, &vy)) {
michael@0 74 float speed = hypotf(vx, vy) * scale;
michael@0 75 if (speed >= mParameters.highThreshold) {
michael@0 76 // Apply full acceleration above the high speed threshold.
michael@0 77 scale *= mParameters.acceleration;
michael@0 78 } else if (speed > mParameters.lowThreshold) {
michael@0 79 // Linearly interpolate the acceleration to apply between the low and high
michael@0 80 // speed thresholds.
michael@0 81 scale *= 1 + (speed - mParameters.lowThreshold)
michael@0 82 / (mParameters.highThreshold - mParameters.lowThreshold)
michael@0 83 * (mParameters.acceleration - 1);
michael@0 84 }
michael@0 85
michael@0 86 #if DEBUG_ACCELERATION
michael@0 87 ALOGD("VelocityControl(%0.3f, %0.3f, %0.3f, %0.3f): "
michael@0 88 "vx=%0.3f, vy=%0.3f, speed=%0.3f, accel=%0.3f",
michael@0 89 mParameters.scale, mParameters.lowThreshold, mParameters.highThreshold,
michael@0 90 mParameters.acceleration,
michael@0 91 vx, vy, speed, scale / mParameters.scale);
michael@0 92 #endif
michael@0 93 } else {
michael@0 94 #if DEBUG_ACCELERATION
michael@0 95 ALOGD("VelocityControl(%0.3f, %0.3f, %0.3f, %0.3f): unknown velocity",
michael@0 96 mParameters.scale, mParameters.lowThreshold, mParameters.highThreshold,
michael@0 97 mParameters.acceleration);
michael@0 98 #endif
michael@0 99 }
michael@0 100
michael@0 101 if (deltaX) {
michael@0 102 *deltaX *= scale;
michael@0 103 }
michael@0 104 if (deltaY) {
michael@0 105 *deltaY *= scale;
michael@0 106 }
michael@0 107 }
michael@0 108 }
michael@0 109
michael@0 110 } // namespace android

mercurial