diff options
author | Harpreet "Eli" Sangha <eliptus@google.com> | 2020-03-23 01:34:07 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2020-03-23 01:34:07 +0000 |
commit | 815e67c54c581e2b56b2106219adda8424d0c8dc (patch) | |
tree | bd6cd43f82e294d3e566fb84b9e0938f3fe6e46c | |
parent | b2b295dfe5982ba2e3757db9baa62d7258e487f1 (diff) | |
parent | 2395a8b3d3012861a7bf60e5192c59809fb14886 (diff) | |
download | pixel-815e67c54c581e2b56b2106219adda8424d0c8dc.tar.gz |
Merge "vibrator: cs40l25: Implement Minimum "Feelability"" into rvc-dev
-rw-r--r-- | vibrator/cs40l25/Vibrator.cpp | 124 | ||||
-rw-r--r-- | vibrator/cs40l25/Vibrator.h | 11 | ||||
-rw-r--r-- | vibrator/cs40l25/tests/test-vibrator.cpp | 98 |
3 files changed, 131 insertions, 102 deletions
diff --git a/vibrator/cs40l25/Vibrator.cpp b/vibrator/cs40l25/Vibrator.cpp index 97baa434..e53a8221 100644 --- a/vibrator/cs40l25/Vibrator.cpp +++ b/vibrator/cs40l25/Vibrator.cpp @@ -39,17 +39,9 @@ namespace vibrator { static constexpr uint32_t BASE_CONTINUOUS_EFFECT_OFFSET = 32768; -static constexpr uint32_t WAVEFORM_SIMPLE_EFFECT_INDEX = 2; - -static constexpr uint32_t WAVEFORM_TEXTURE_TICK_EFFECT_LEVEL = 0; - -static constexpr uint32_t WAVEFORM_TICK_EFFECT_LEVEL = 1; - -static constexpr uint32_t WAVEFORM_CLICK_EFFECT_LEVEL = 2; - -static constexpr uint32_t WAVEFORM_HEAVY_CLICK_EFFECT_LEVEL = 3; - -static constexpr uint32_t WAVEFORM_EFFECT_MAX_LEVEL = 4; +static constexpr uint32_t WAVEFORM_EFFECT_0_20_LEVEL = 0; +static constexpr uint32_t WAVEFORM_EFFECT_1_00_LEVEL = 4; +static constexpr uint32_t WAVEFORM_EFFECT_LEVEL_MINIMUM = 4; static constexpr uint32_t WAVEFORM_DOUBLE_CLICK_SILENCE_MS = 100; @@ -57,6 +49,7 @@ static constexpr uint32_t WAVEFORM_LONG_VIBRATION_EFFECT_INDEX = 0; static constexpr uint32_t WAVEFORM_LONG_VIBRATION_THRESHOLD_MS = 50; static constexpr uint32_t WAVEFORM_SHORT_VIBRATION_EFFECT_INDEX = 3 + BASE_CONTINUOUS_EFFECT_OFFSET; +static constexpr uint32_t WAVEFORM_CLICK_INDEX = 2; static constexpr uint32_t WAVEFORM_QUICK_RISE_INDEX = 6; static constexpr uint32_t WAVEFORM_SLOW_RISE_INDEX = 7; static constexpr uint32_t WAVEFORM_QUICK_FALL_INDEX = 8; @@ -93,6 +86,7 @@ Vibrator::Vibrator(std::unique_ptr<HwApi> hwapi, std::unique_ptr<HwCal> hwcal) : mHwApi(std::move(hwapi)), mHwCal(std::move(hwcal)), mAsyncHandle(std::async([] {})) { uint32_t caldata; uint32_t effectCount; + std::array<uint32_t, 6> volLevels; if (!mHwApi->setState(true)) { ALOGE("Failed to set state (%d): %s", errno, strerror(errno)); @@ -107,7 +101,19 @@ Vibrator::Vibrator(std::unique_ptr<HwApi> hwapi, std::unique_ptr<HwCal> hwcal) if (mHwCal->getQ(&caldata)) { mHwApi->setQ(caldata); } - mHwCal->getVolLevels(&mVolLevels); + + mHwCal->getVolLevels(&volLevels); + /* + * Given voltage levels for two intensities, assuming a linear function, + * solve for 'f(0)' in 'v = f(i) = a + b * i' (i.e 'v0 - (v1 - v0) / ((i1 - i0) / i0)'). + */ + mEffectVolMin = std::max(std::lround(volLevels[WAVEFORM_EFFECT_0_20_LEVEL] - + (volLevels[WAVEFORM_EFFECT_1_00_LEVEL] - + volLevels[WAVEFORM_EFFECT_0_20_LEVEL]) / + 4.0f), + static_cast<long>(WAVEFORM_EFFECT_LEVEL_MINIMUM)); + mEffectVolMax = volLevels[WAVEFORM_EFFECT_1_00_LEVEL]; + mGlobalVolMax = volLevels[VOLTAGE_GLOBAL_SCALE_LEVEL]; mHwApi->getEffectCount(&effectCount); mEffectDurations.resize(effectCount); @@ -218,10 +224,10 @@ ndk::ScopedAStatus Vibrator::getSupportedPrimitives(std::vector<CompositePrimiti ndk::ScopedAStatus Vibrator::getPrimitiveDuration(CompositePrimitive primitive, int32_t *durationMs) { ndk::ScopedAStatus status; - uint32_t effectIndex, volLevel; + uint32_t effectIndex; if (primitive != CompositePrimitive::NOOP) { - status = getPrimitiveDetails(primitive, 1.0f, &effectIndex, &volLevel); + status = getPrimitiveDetails(primitive, &effectIndex); if (!status.isOk()) { return status; } @@ -245,6 +251,10 @@ ndk::ScopedAStatus Vibrator::compose(const std::vector<CompositeEffect> &composi } for (auto &e : composite) { + if (e.scale < 0.0f || e.scale > 1.0f) { + return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); + } + if (e.delayMs) { if (e.delayMs > COMPOSE_DELAY_MAX_MS) { return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); @@ -254,14 +264,13 @@ ndk::ScopedAStatus Vibrator::compose(const std::vector<CompositeEffect> &composi if (e.primitive != CompositePrimitive::NOOP) { ndk::ScopedAStatus status; uint32_t effectIndex; - uint32_t volLevel; - status = getPrimitiveDetails(e.primitive, e.scale, &effectIndex, &volLevel); + status = getPrimitiveDetails(e.primitive, &effectIndex); if (!status.isOk()) { return status; } - effectBuilder << effectIndex << "." << volLevel << ","; + effectBuilder << effectIndex << "." << intensityToVolLevel(e.scale) << ","; } } @@ -304,7 +313,7 @@ ndk::ScopedAStatus Vibrator::setEffectAmplitude(float amplitude, float maximum) } ndk::ScopedAStatus Vibrator::setGlobalAmplitude(bool set) { - uint8_t amplitude = set ? mVolLevels[VOLTAGE_GLOBAL_SCALE_LEVEL] : VOLTAGE_SCALE_MAX; + uint8_t amplitude = set ? mGlobalVolMax : VOLTAGE_SCALE_MAX; int32_t scale = amplitudeToScale(amplitude, VOLTAGE_SCALE_MAX); if (!mHwApi->setGlobalScale(scale)) { @@ -322,11 +331,12 @@ ndk::ScopedAStatus Vibrator::getSupportedAlwaysOnEffects(std::vector<Effect> *_a ndk::ScopedAStatus Vibrator::alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) { ndk::ScopedAStatus status; + uint32_t effectIndex; uint32_t timeMs; uint32_t volLevel; uint32_t scale; - status = getSimpleDetails(effect, strength, &timeMs, &volLevel); + status = getSimpleDetails(effect, strength, &effectIndex, &timeMs, &volLevel); if (!status.isOk()) { return status; } @@ -335,11 +345,11 @@ ndk::ScopedAStatus Vibrator::alwaysOnEnable(int32_t id, Effect effect, EffectStr switch (static_cast<AlwaysOnId>(id)) { case AlwaysOnId::GPIO_RISE: - mHwApi->setGpioRiseIndex(WAVEFORM_SIMPLE_EFFECT_INDEX); + mHwApi->setGpioRiseIndex(effectIndex); mHwApi->setGpioRiseScale(scale); return ndk::ScopedAStatus::ok(); case AlwaysOnId::GPIO_FALL: - mHwApi->setGpioFallIndex(WAVEFORM_SIMPLE_EFFECT_INDEX); + mHwApi->setGpioFallIndex(effectIndex); mHwApi->setGpioFallScale(scale); return ndk::ScopedAStatus::ok(); } @@ -376,11 +386,10 @@ binder_status_t Vibrator::dump(int fd, const char **args, uint32_t numArgs) { dprintf(fd, "AIDL:\n"); - dprintf(fd, " Voltage Levels:"); - for (auto v : mVolLevels) { - dprintf(fd, " %" PRIu32, v); - } - dprintf(fd, "\n"); + dprintf(fd, " Voltage Levels:\n"); + dprintf(fd, " Effect Min: %" PRIu32 "\n", mEffectVolMin); + dprintf(fd, " Effect Max: %" PRIu32 "\n", mEffectVolMax); + dprintf(fd, " Global Max: %" PRIu32 "\n", mGlobalVolMax); dprintf(fd, " Effect Durations:"); for (auto d : mEffectDurations) { @@ -401,21 +410,22 @@ binder_status_t Vibrator::dump(int fd, const char **args, uint32_t numArgs) { } ndk::ScopedAStatus Vibrator::getSimpleDetails(Effect effect, EffectStrength strength, - uint32_t *outTimeMs, uint32_t *outVolLevel) { + uint32_t *outEffectIndex, uint32_t *outTimeMs, + uint32_t *outVolLevel) { + uint32_t effectIndex; uint32_t timeMs; + float intensity; uint32_t volLevel; - uint32_t volIndex; - int8_t volOffset; switch (strength) { case EffectStrength::LIGHT: - volOffset = -1; + intensity = 0.5f; break; case EffectStrength::MEDIUM: - volOffset = 0; + intensity = 0.7f; break; case EffectStrength::STRONG: - volOffset = 1; + intensity = 1.0f; break; default: return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); @@ -423,26 +433,29 @@ ndk::ScopedAStatus Vibrator::getSimpleDetails(Effect effect, EffectStrength stre switch (effect) { case Effect::TEXTURE_TICK: - volIndex = WAVEFORM_TEXTURE_TICK_EFFECT_LEVEL; - volOffset = 0; + effectIndex = WAVEFORM_LIGHT_TICK_INDEX; + intensity *= 0.5f; break; case Effect::TICK: - volIndex = WAVEFORM_TICK_EFFECT_LEVEL; - volOffset = 0; + effectIndex = WAVEFORM_LIGHT_TICK_INDEX; + intensity *= 0.7f; break; case Effect::CLICK: - volIndex = WAVEFORM_CLICK_EFFECT_LEVEL; + effectIndex = WAVEFORM_CLICK_INDEX; + intensity *= 0.7f; break; case Effect::HEAVY_CLICK: - volIndex = WAVEFORM_HEAVY_CLICK_EFFECT_LEVEL; + effectIndex = WAVEFORM_CLICK_INDEX; + intensity *= 1.0f; break; default: return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); } - volLevel = mVolLevels[volIndex + volOffset]; - timeMs = mEffectDurations[WAVEFORM_SIMPLE_EFFECT_INDEX] + MAX_COLD_START_LATENCY_MS; + volLevel = intensityToVolLevel(intensity); + timeMs = mEffectDurations[effectIndex] + MAX_COLD_START_LATENCY_MS; + *outEffectIndex = effectIndex; *outTimeMs = timeMs; *outVolLevel = volLevel; @@ -455,6 +468,7 @@ ndk::ScopedAStatus Vibrator::getCompoundDetails(Effect effect, EffectStrength st ndk::ScopedAStatus status; uint32_t timeMs; std::ostringstream effectBuilder; + uint32_t thisEffectIndex; uint32_t thisTimeMs; uint32_t thisVolLevel; @@ -462,11 +476,12 @@ ndk::ScopedAStatus Vibrator::getCompoundDetails(Effect effect, EffectStrength st case Effect::DOUBLE_CLICK: timeMs = 0; - status = getSimpleDetails(Effect::CLICK, strength, &thisTimeMs, &thisVolLevel); + status = getSimpleDetails(Effect::CLICK, strength, &thisEffectIndex, &thisTimeMs, + &thisVolLevel); if (!status.isOk()) { return status; } - effectBuilder << WAVEFORM_SIMPLE_EFFECT_INDEX << "." << thisVolLevel; + effectBuilder << thisEffectIndex << "." << thisVolLevel; timeMs += thisTimeMs; effectBuilder << ","; @@ -476,11 +491,12 @@ ndk::ScopedAStatus Vibrator::getCompoundDetails(Effect effect, EffectStrength st effectBuilder << ","; - status = getSimpleDetails(Effect::HEAVY_CLICK, strength, &thisTimeMs, &thisVolLevel); + status = getSimpleDetails(Effect::HEAVY_CLICK, strength, &thisEffectIndex, &thisTimeMs, + &thisVolLevel); if (!status.isOk()) { return status; } - effectBuilder << WAVEFORM_SIMPLE_EFFECT_INDEX << "." << thisVolLevel; + effectBuilder << thisEffectIndex << "." << thisVolLevel; timeMs += thisTimeMs; break; @@ -494,19 +510,15 @@ ndk::ScopedAStatus Vibrator::getCompoundDetails(Effect effect, EffectStrength st return ndk::ScopedAStatus::ok(); } -ndk::ScopedAStatus Vibrator::getPrimitiveDetails(CompositePrimitive primitive, float scale, - uint32_t *outEffectIndex, uint32_t *outVolLevel) { +ndk::ScopedAStatus Vibrator::getPrimitiveDetails(CompositePrimitive primitive, + uint32_t *outEffectIndex) { uint32_t effectIndex; - if (scale < 0.0f || scale > 1.0f) { - return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); - } - switch (primitive) { case CompositePrimitive::NOOP: return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); case CompositePrimitive::CLICK: - effectIndex = WAVEFORM_SIMPLE_EFFECT_INDEX; + effectIndex = WAVEFORM_CLICK_INDEX; break; case CompositePrimitive::QUICK_RISE: effectIndex = WAVEFORM_QUICK_RISE_INDEX; @@ -525,7 +537,6 @@ ndk::ScopedAStatus Vibrator::getPrimitiveDetails(CompositePrimitive primitive, f } *outEffectIndex = effectIndex; - *outVolLevel = std::lround(scale * (mVolLevels[WAVEFORM_EFFECT_MAX_LEVEL] - 1)) + 1; return ndk::ScopedAStatus::ok(); } @@ -544,6 +555,7 @@ ndk::ScopedAStatus Vibrator::performEffect(Effect effect, EffectStrength strengt const std::shared_ptr<IVibratorCallback> &callback, int32_t *outTimeMs) { ndk::ScopedAStatus status; + uint32_t effectIndex; uint32_t timeMs = 0; uint32_t volLevel; std::string effectQueue; @@ -556,7 +568,7 @@ ndk::ScopedAStatus Vibrator::performEffect(Effect effect, EffectStrength strengt case Effect::CLICK: // fall-through case Effect::HEAVY_CLICK: - status = getSimpleDetails(effect, strength, &timeMs, &volLevel); + status = getSimpleDetails(effect, strength, &effectIndex, &timeMs, &volLevel); break; case Effect::DOUBLE_CLICK: status = getCompoundDetails(effect, strength, &timeMs, &volLevel, &effectQueue); @@ -569,7 +581,7 @@ ndk::ScopedAStatus Vibrator::performEffect(Effect effect, EffectStrength strengt goto exit; } - status = performEffect(WAVEFORM_SIMPLE_EFFECT_INDEX, volLevel, &effectQueue, callback); + status = performEffect(effectIndex, volLevel, &effectQueue, callback); exit: @@ -606,6 +618,10 @@ void Vibrator::waitForComplete(std::shared_ptr<IVibratorCallback> &&callback) { } } +uint32_t Vibrator::intensityToVolLevel(float intensity) { + return std::lround(intensity * (mEffectVolMax - mEffectVolMin)) + mEffectVolMin; +} + } // namespace vibrator } // namespace hardware } // namespace android diff --git a/vibrator/cs40l25/Vibrator.h b/vibrator/cs40l25/Vibrator.h index fa7264c2..3d154067 100644 --- a/vibrator/cs40l25/Vibrator.h +++ b/vibrator/cs40l25/Vibrator.h @@ -146,14 +146,14 @@ class Vibrator : public BnVibrator { ndk::ScopedAStatus setEffectAmplitude(float amplitude, float maximum); ndk::ScopedAStatus setGlobalAmplitude(bool set); // 'simple' effects are those precompiled and loaded into the controller - ndk::ScopedAStatus getSimpleDetails(Effect effect, EffectStrength strength, uint32_t *outTimeMs, + ndk::ScopedAStatus getSimpleDetails(Effect effect, EffectStrength strength, + uint32_t *outEffectIndex, uint32_t *outTimeMs, uint32_t *outVolLevel); // 'compound' effects are those composed by stringing multiple 'simple' effects ndk::ScopedAStatus getCompoundDetails(Effect effect, EffectStrength strength, uint32_t *outTimeMs, uint32_t *outVolLevel, std::string *outEffectQueue); - ndk::ScopedAStatus getPrimitiveDetails(CompositePrimitive primitive, float scale, - uint32_t *outEffectIndex, uint32_t *outVolLevel); + ndk::ScopedAStatus getPrimitiveDetails(CompositePrimitive primitive, uint32_t *outEffectIndex); ndk::ScopedAStatus setEffectQueue(const std::string &effectQueue); ndk::ScopedAStatus performEffect(Effect effect, EffectStrength strength, const std::shared_ptr<IVibratorCallback> &callback, @@ -163,10 +163,13 @@ class Vibrator : public BnVibrator { const std::shared_ptr<IVibratorCallback> &callback); bool isUnderExternalControl(); void waitForComplete(std::shared_ptr<IVibratorCallback> &&callback); + uint32_t intensityToVolLevel(float intensity); std::unique_ptr<HwApi> mHwApi; std::unique_ptr<HwCal> mHwCal; - std::array<uint32_t, 6> mVolLevels; + uint32_t mEffectVolMin; + uint32_t mEffectVolMax; + uint32_t mGlobalVolMax; std::vector<uint32_t> mEffectDurations; std::future<void> mAsyncHandle; }; diff --git a/vibrator/cs40l25/tests/test-vibrator.cpp b/vibrator/cs40l25/tests/test-vibrator.cpp index 4fc124d6..4d0a5ad8 100644 --- a/vibrator/cs40l25/tests/test-vibrator.cpp +++ b/vibrator/cs40l25/tests/test-vibrator.cpp @@ -61,6 +61,9 @@ static EffectQueue Queue(const QueueDelay &delay); template <typename T, typename U, typename... Args> static EffectQueue Queue(const T &first, const U &second, Args... rest); +static EffectLevel Level(float intensity); +static EffectScale Scale(float intensity); + // Constants With Arbitrary Values static constexpr std::array<EffectLevel, 6> V_LEVELS{40, 50, 60, 70, 80, 90}; @@ -69,37 +72,43 @@ static constexpr std::array<EffectDuration, 9> EFFECT_DURATIONS{0, 0, 15, 0 // Constants With Prescribed Values -static constexpr EffectIndex EFFECT_INDEX{2}; +static const std::map<Effect, EffectIndex> EFFECT_INDEX{ + {Effect::CLICK, 2}, + {Effect::TICK, 9}, + {Effect::HEAVY_CLICK, 2}, + {Effect::TEXTURE_TICK, 9}, +}; + static constexpr EffectIndex QUEUE_INDEX{65534}; static const EffectScale ON_GLOBAL_SCALE{levelToScale(V_LEVELS[5])}; static const EffectIndex ON_EFFECT_INDEX{0}; static const std::map<EffectTuple, EffectScale> EFFECT_SCALE{ - {{Effect::CLICK, EffectStrength::LIGHT}, levelToScale(V_LEVELS[1])}, - {{Effect::CLICK, EffectStrength::MEDIUM}, levelToScale(V_LEVELS[2])}, - {{Effect::CLICK, EffectStrength::STRONG}, levelToScale(V_LEVELS[3])}, - {{Effect::TICK, EffectStrength::LIGHT}, levelToScale(V_LEVELS[1])}, - {{Effect::TICK, EffectStrength::MEDIUM}, levelToScale(V_LEVELS[1])}, - {{Effect::TICK, EffectStrength::STRONG}, levelToScale(V_LEVELS[1])}, - {{Effect::HEAVY_CLICK, EffectStrength::LIGHT}, levelToScale(V_LEVELS[2])}, - {{Effect::HEAVY_CLICK, EffectStrength::MEDIUM}, levelToScale(V_LEVELS[3])}, - {{Effect::HEAVY_CLICK, EffectStrength::STRONG}, levelToScale(V_LEVELS[4])}, - {{Effect::TEXTURE_TICK, EffectStrength::LIGHT}, levelToScale(V_LEVELS[0])}, - {{Effect::TEXTURE_TICK, EffectStrength::MEDIUM}, levelToScale(V_LEVELS[0])}, - {{Effect::TEXTURE_TICK, EffectStrength::STRONG}, levelToScale(V_LEVELS[0])}, + {{Effect::CLICK, EffectStrength::LIGHT}, Scale(0.7f * 0.5f)}, + {{Effect::CLICK, EffectStrength::MEDIUM}, Scale(0.7f * 0.7f)}, + {{Effect::CLICK, EffectStrength::STRONG}, Scale(0.7f * 1.0f)}, + {{Effect::TICK, EffectStrength::LIGHT}, Scale(0.7f * 0.5f)}, + {{Effect::TICK, EffectStrength::MEDIUM}, Scale(0.7f * 0.7f)}, + {{Effect::TICK, EffectStrength::STRONG}, Scale(0.7f * 1.0f)}, + {{Effect::HEAVY_CLICK, EffectStrength::LIGHT}, Scale(1.0f * 0.5f)}, + {{Effect::HEAVY_CLICK, EffectStrength::MEDIUM}, Scale(1.0f * 0.7f)}, + {{Effect::HEAVY_CLICK, EffectStrength::STRONG}, Scale(1.0f * 1.0f)}, + {{Effect::TEXTURE_TICK, EffectStrength::LIGHT}, Scale(0.5f * 0.5f)}, + {{Effect::TEXTURE_TICK, EffectStrength::MEDIUM}, Scale(0.5f * 0.7f)}, + {{Effect::TEXTURE_TICK, EffectStrength::STRONG}, Scale(0.5f * 1.0f)}, }; static const std::map<EffectTuple, EffectQueue> EFFECT_QUEUE{ {{Effect::DOUBLE_CLICK, EffectStrength::LIGHT}, - Queue(QueueEffect{EFFECT_INDEX, V_LEVELS[1]}, 100, - QueueEffect{EFFECT_INDEX, V_LEVELS[2]})}, + Queue(QueueEffect{EFFECT_INDEX.at(Effect::CLICK), Level(0.7f * 0.5f)}, 100, + QueueEffect{EFFECT_INDEX.at(Effect::CLICK), Level(1.0f * 0.5f)})}, {{Effect::DOUBLE_CLICK, EffectStrength::MEDIUM}, - Queue(QueueEffect{EFFECT_INDEX, V_LEVELS[2]}, 100, - QueueEffect{EFFECT_INDEX, V_LEVELS[3]})}, + Queue(QueueEffect{EFFECT_INDEX.at(Effect::CLICK), Level(0.7f * 0.7f)}, 100, + QueueEffect{EFFECT_INDEX.at(Effect::CLICK), Level(1.0f * 0.7f)})}, {{Effect::DOUBLE_CLICK, EffectStrength::STRONG}, - Queue(QueueEffect{EFFECT_INDEX, V_LEVELS[3]}, 100, - QueueEffect{EFFECT_INDEX, V_LEVELS[4]})}, + Queue(QueueEffect{EFFECT_INDEX.at(Effect::CLICK), Level(0.7f * 1.0f)}, 100, + QueueEffect{EFFECT_INDEX.at(Effect::CLICK), Level(1.0f * 1.0f)})}, }; EffectQueue Queue(const QueueEffect &effect) { @@ -124,6 +133,16 @@ EffectQueue Queue(const T &first, const U &second, Args... rest) { return {string, duration}; } +static EffectLevel Level(float intensity) { + auto vMin = std::max(V_LEVELS[0] - (V_LEVELS[4] - V_LEVELS[0]) / 4.0f, 4.0f); + auto vMax = V_LEVELS[4]; + return std::lround(intensity * (vMax - vMin)) + vMin; +} + +static EffectScale Scale(float intensity) { + return levelToScale(Level(intensity)); +} + class VibratorTest : public Test { public: void SetUp() override { @@ -426,7 +445,8 @@ TEST_P(EffectsTest, perform) { if (scale != EFFECT_SCALE.end()) { duration = EFFECT_DURATIONS[2]; - eSetup += EXPECT_CALL(*mMockApi, setEffectIndex(EFFECT_INDEX)).WillOnce(DoDefault()); + eSetup += EXPECT_CALL(*mMockApi, setEffectIndex(EFFECT_INDEX.at(effect))) + .WillOnce(DoDefault()); eSetup += EXPECT_CALL(*mMockApi, setEffectScale(scale->second)).WillOnce(Return(true)); } else if (queue != EFFECT_QUEUE.end()) { duration = std::get<1>(queue->second); @@ -471,7 +491,7 @@ TEST_P(EffectsTest, alwaysOnEnable) { bool supported = (scale != EFFECT_SCALE.end()); if (supported) { - EXPECT_CALL(*mMockApi, setGpioRiseIndex(EFFECT_INDEX)).WillOnce(Return(true)); + EXPECT_CALL(*mMockApi, setGpioRiseIndex(EFFECT_INDEX.at(effect))).WillOnce(Return(true)); EXPECT_CALL(*mMockApi, setGpioRiseScale(scale->second)).WillOnce(Return(true)); } @@ -507,7 +527,6 @@ class PrimitiveTest : public VibratorTest, public WithParamInterface<PrimitivePa const std::vector<PrimitiveParam> kPrimitiveParams = { {CompositePrimitive::NOOP, 0}, {CompositePrimitive::CLICK, 2}, - {CompositePrimitive::THUD, 4}, {CompositePrimitive::SPIN, 5}, {CompositePrimitive::QUICK_RISE, 6}, {CompositePrimitive::SLOW_RISE, 7}, {CompositePrimitive::QUICK_FALL, 8}, }; @@ -566,30 +585,19 @@ TEST_P(ComposeTest, compose) { } const std::vector<ComposeParam> kComposeParams = { - {"click", - {{0, CompositePrimitive::CLICK, 1.0f}}, - Queue(QueueEffect(2, 1.0f * V_LEVELS[4]), 0)}, - {"thud", - {{1, CompositePrimitive::THUD, 0.8f}}, - Queue(1, QueueEffect(4, 0.8f * V_LEVELS[4]), 0)}, - {"spin", - {{2, CompositePrimitive::SPIN, 0.6f}}, - Queue(2, QueueEffect(5, 0.6f * V_LEVELS[4]), 0)}, + {"click", {{0, CompositePrimitive::CLICK, 1.0f}}, Queue(QueueEffect(2, Level(1.0f)), 0)}, {"quick_rise", {{3, CompositePrimitive::QUICK_RISE, 0.4f}}, - Queue(3, QueueEffect(6, 0.4f * V_LEVELS[4]), 0)}, + Queue(3, QueueEffect(6, Level(0.4f)), 0)}, {"slow_rise", - {{4, CompositePrimitive::SLOW_RISE, 0.2f}}, - Queue(4, QueueEffect(7, 0.2f * V_LEVELS[4]), 0)}, + {{4, CompositePrimitive::SLOW_RISE, 0.0f}}, + Queue(4, QueueEffect(7, Level(0.0f)), 0)}, {"quick_fall", {{5, CompositePrimitive::QUICK_FALL, 1.0f}}, - Queue(5, QueueEffect(8, V_LEVELS[4]), 0)}, - {"pop", - {{6, CompositePrimitive::SLOW_RISE, 1.0f}, {50, CompositePrimitive::THUD, 1.0f}}, - Queue(6, QueueEffect(7, V_LEVELS[4]), 50, QueueEffect(4, V_LEVELS[4]), 0)}, + Queue(5, QueueEffect(8, Level(1.0f)), 0)}, {"snap", {{7, CompositePrimitive::QUICK_RISE, 1.0f}, {0, CompositePrimitive::QUICK_FALL, 1.0f}}, - Queue(7, QueueEffect(6, V_LEVELS[4]), QueueEffect(8, V_LEVELS[4]), 0)}, + Queue(7, QueueEffect(6, Level(1.0f)), QueueEffect(8, Level(1.0f)), 0)}, }; INSTANTIATE_TEST_CASE_P(VibratorTests, ComposeTest, @@ -609,20 +617,22 @@ TEST_P(AlwaysOnTest, alwaysOnEnable) { std::advance(scale, std::rand() % EFFECT_SCALE.size()); + auto effect = std::get<0>(scale->first); + auto strength = std::get<1>(scale->first); + switch (param) { case 0: - EXPECT_CALL(*mMockApi, setGpioRiseIndex(EFFECT_INDEX)).WillOnce(Return(true)); + EXPECT_CALL(*mMockApi, setGpioRiseIndex(EFFECT_INDEX.at(effect))) + .WillOnce(Return(true)); EXPECT_CALL(*mMockApi, setGpioRiseScale(scale->second)).WillOnce(Return(true)); break; case 1: - EXPECT_CALL(*mMockApi, setGpioFallIndex(EFFECT_INDEX)).WillOnce(Return(true)); + EXPECT_CALL(*mMockApi, setGpioFallIndex(EFFECT_INDEX.at(effect))) + .WillOnce(Return(true)); EXPECT_CALL(*mMockApi, setGpioFallScale(scale->second)).WillOnce(Return(true)); break; } - auto effect = std::get<0>(scale->first); - auto strength = std::get<1>(scale->first); - ndk::ScopedAStatus status = mVibrator->alwaysOnEnable(param, effect, strength); EXPECT_EQ(EX_NONE, status.getExceptionCode()); } |