VelocityControl.cpp 3.5 KB

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