diff options
Diffstat (limited to 'vibrator/cs40l25/Vibrator.cpp')
-rw-r--r-- | vibrator/cs40l25/Vibrator.cpp | 54 |
1 files changed, 51 insertions, 3 deletions
diff --git a/vibrator/cs40l25/Vibrator.cpp b/vibrator/cs40l25/Vibrator.cpp index ba4efad..b868cfd 100644 --- a/vibrator/cs40l25/Vibrator.cpp +++ b/vibrator/cs40l25/Vibrator.cpp @@ -65,7 +65,6 @@ static constexpr uint32_t WAVEFORM_LOW_TICK_INDEX = 10; static constexpr uint32_t WAVEFORM_UNSAVED_TRIGGER_QUEUE_INDEX = 65529; static constexpr uint32_t WAVEFORM_TRIGGER_QUEUE_INDEX = 65534; - static constexpr uint32_t VOLTAGE_GLOBAL_SCALE_LEVEL = 5; static constexpr uint8_t VOLTAGE_SCALE_MAX = 100; @@ -77,6 +76,7 @@ static constexpr float AMP_ATTENUATE_STEP_SIZE = 0.125f; static constexpr float EFFECT_FREQUENCY_KHZ = 48.0f; static constexpr auto ASYNC_COMPLETION_TIMEOUT = std::chrono::milliseconds(100); +static constexpr auto POLLING_TIMEOUT = 20; static constexpr int32_t COMPOSE_DELAY_MAX_MS = 10000; static constexpr int32_t COMPOSE_SIZE_MAX = 127; @@ -306,6 +306,7 @@ ndk::ScopedAStatus Vibrator::getCapabilities(int32_t *_aidl_return) { ndk::ScopedAStatus Vibrator::off() { ATRACE_NAME("Vibrator::off"); + ALOGD("Vibrator::off"); setGlobalAmplitude(false); mHwApi->setF0Offset(0); if (!mHwApi->setActivate(0)) { @@ -319,6 +320,7 @@ ndk::ScopedAStatus Vibrator::off() { ndk::ScopedAStatus Vibrator::on(int32_t timeoutMs, const std::shared_ptr<IVibratorCallback> &callback) { ATRACE_NAME("Vibrator::on"); + ALOGD("Vibrator::on"); const uint32_t index = timeoutMs < WAVEFORM_LONG_VIBRATION_THRESHOLD_MS ? WAVEFORM_SHORT_VIBRATION_EFFECT_INDEX : WAVEFORM_LONG_VIBRATION_EFFECT_INDEX; @@ -334,6 +336,7 @@ ndk::ScopedAStatus Vibrator::perform(Effect effect, EffectStrength strength, const std::shared_ptr<IVibratorCallback> &callback, int32_t *_aidl_return) { ATRACE_NAME("Vibrator::perform"); + ALOGD("Vibrator::perform"); return performEffect(effect, strength, callback, _aidl_return); } @@ -443,6 +446,7 @@ ndk::ScopedAStatus Vibrator::getPrimitiveDuration(CompositePrimitive primitive, ndk::ScopedAStatus Vibrator::compose(const std::vector<CompositeEffect> &composite, const std::shared_ptr<IVibratorCallback> &callback) { ATRACE_NAME("Vibrator::compose"); + ALOGD("Vibrator::compose"); std::ostringstream effectBuilder; std::string effectQueue; @@ -450,6 +454,11 @@ ndk::ScopedAStatus Vibrator::compose(const std::vector<CompositeEffect> &composi return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); } + // Reset the mTotalDuration + { + const std::scoped_lock<std::mutex> lock(mTotalDurationMutex); + mTotalDuration = 0; + } for (auto &e : composite) { if (e.scale < 0.0f || e.scale > 1.0f) { return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); @@ -460,6 +469,10 @@ ndk::ScopedAStatus Vibrator::compose(const std::vector<CompositeEffect> &composi return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); } effectBuilder << e.delayMs << ","; + { + const std::scoped_lock<std::mutex> lock(mTotalDurationMutex); + mTotalDuration += e.delayMs; + } } if (e.primitive != CompositePrimitive::NOOP) { ndk::ScopedAStatus status; @@ -471,6 +484,10 @@ ndk::ScopedAStatus Vibrator::compose(const std::vector<CompositeEffect> &composi } effectBuilder << effectIndex << "." << intensityToVolLevel(e.scale, effectIndex) << ","; + { + const std::scoped_lock<std::mutex> lock(mTotalDurationMutex); + mTotalDuration += mEffectDurations[effectIndex]; + } } } @@ -497,9 +514,17 @@ ndk::ScopedAStatus Vibrator::on(uint32_t timeoutMs, uint32_t effectIndex, return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); } + ALOGD("Vibrator::on"); mHwApi->setEffectIndex(effectIndex); mHwApi->setDuration(timeoutMs); mHwApi->setActivate(1); + // Using the mToalDuration for composed effect. + // For composed effect, we set the UINT32_MAX to the duration sysfs node, + // but it not a practical way to use it to monitor the total duration time. + if (timeoutMs != UINT32_MAX) { + const std::scoped_lock<std::mutex> lock(mTotalDurationMutex); + mTotalDuration = timeoutMs; + } mActiveId = effectIndex; @@ -897,7 +922,10 @@ ndk::ScopedAStatus Vibrator::composePwle(const std::vector<PrimitivePwle> &compo totalDuration += MAX_COLD_START_LATENCY_MS; mHwApi->setDuration(totalDuration); - + { + const std::scoped_lock<std::mutex> lock(mTotalDurationMutex); + mTotalDuration = totalDuration; + } mHwApi->setActivate(1); mAsyncHandle = std::async(&Vibrator::waitForComplete, this, callback); @@ -993,6 +1021,10 @@ ndk::ScopedAStatus Vibrator::getSimpleDetails(Effect effect, EffectStrength stre volLevel = intensityToVolLevel(intensity, effectIndex); timeMs = mEffectDurations[effectIndex] + MAX_COLD_START_LATENCY_MS; + { + const std::scoped_lock<std::mutex> lock(mTotalDurationMutex); + mTotalDuration = timeMs; + } *outEffectIndex = effectIndex; *outTimeMs = timeMs; @@ -1037,6 +1069,10 @@ ndk::ScopedAStatus Vibrator::getCompoundDetails(Effect effect, EffectStrength st } effectBuilder << thisEffectIndex << "." << thisVolLevel; timeMs += thisTimeMs; + { + const std::scoped_lock<std::mutex> lock(mTotalDurationMutex); + mTotalDuration = timeMs; + } break; default: @@ -1155,7 +1191,19 @@ ndk::ScopedAStatus Vibrator::performEffect(uint32_t effectIndex, uint32_t volLev } void Vibrator::waitForComplete(std::shared_ptr<IVibratorCallback> &&callback) { - mHwApi->pollVibeState(false); + ALOGD("Vibrator::waitForComplete"); + uint32_t duration; + { + const std::scoped_lock<std::mutex> lock(mTotalDurationMutex); + duration = ((mTotalDuration + POLLING_TIMEOUT) < UINT32_MAX) + ? mTotalDuration + POLLING_TIMEOUT + : UINT32_MAX; + } + if (!mHwApi->pollVibeState(false, duration)) { + ALOGE("Timeout(%u)! Fail to poll STOP state", duration); + } else { + ALOGD("Vibrator::waitForComplete: Get STOP! Set active to 0."); + } mHwApi->setActivate(false); if (callback) { |