From e37874d4a34f8ffa4238b6adcddc3794c165b47f Mon Sep 17 00:00:00 2001 From: SamarV-121 Date: Mon, 8 May 2023 16:17:17 +0530 Subject: [PATCH] vibrator: Add support for duration based amplitude control * Similar to what samsung is doing on devices without hardware intensity control. * Add `$(call soong_config_set,samsungVibratorVars,duration_amplitude,true)` in BoardConfig.mk to enable. Change-Id: I87a767e00363c36f8bd4d61cb6dd23b5c033a3d3 --- aidl/vibrator/Android.bp | 22 ++++++++++++++++++- aidl/vibrator/Vibrator.cpp | 44 ++++++++++++++++++++++++++++++++++---- aidl/vibrator/Vibrator.h | 11 ++++++++++ 3 files changed, 72 insertions(+), 5 deletions(-) diff --git a/aidl/vibrator/Android.bp b/aidl/vibrator/Android.bp index 0a255a18..bcd6a66a 100644 --- a/aidl/vibrator/Android.bp +++ b/aidl/vibrator/Android.bp @@ -1,11 +1,31 @@ // -// Copyright (C) 2021 The LineageOS Project +// Copyright (C) 2021-2023 The LineageOS Project // // SPDX-License-Identifier: Apache-2.0 // +soong_config_module_type { + name: "samsung_vibrator", + module_type: "cc_defaults", + config_namespace: "samsungVibratorVars", + bool_variables: ["duration_amplitude"], + properties: ["cflags"], +} + +samsung_vibrator { + name: "samsung_vibrator_defaults", + soong_config_variables: { + duration_amplitude: { + cflags: [ + "-DVIBRATOR_SUPPORTS_DURATION_AMPLITUDE_CONTROL", + ], + }, + }, +} + cc_binary { name: "android.hardware.vibrator-service.samsung", + defaults: ["samsung_vibrator_defaults"], relative_install_path: "hw", init_rc: ["android.hardware.vibrator-service.samsung.rc"], vintf_fragments: ["android.hardware.vibrator-service.samsung.xml"], diff --git a/aidl/vibrator/Vibrator.cpp b/aidl/vibrator/Vibrator.cpp index c2159a35..6818f77f 100644 --- a/aidl/vibrator/Vibrator.cpp +++ b/aidl/vibrator/Vibrator.cpp @@ -31,6 +31,14 @@ static std::map CP_TRIGGER_EFFECTS { { Effect::TICK, 50 } }; +#ifdef VIBRATOR_SUPPORTS_DURATION_AMPLITUDE_CONTROL +static std::map DURATION_AMPLITUDE = { + { EffectStrength::LIGHT, DURATION_AMPLITUDE_LIGHT }, + { EffectStrength::MEDIUM, DURATION_AMPLITUDE_MEDIUM }, + { EffectStrength::STRONG, DURATION_AMPLITUDE_STRONG } +}; +#endif + /* * Write value to path and close file. */ @@ -73,10 +81,12 @@ ndk::ScopedAStatus Vibrator::getCapabilities(int32_t* _aidl_return) { IVibrator::CAP_EXTERNAL_CONTROL /*| IVibrator::CAP_COMPOSE_EFFECTS | IVibrator::CAP_ALWAYS_ON_CONTROL*/; - if (mHasTimedOutIntensity) { - *_aidl_return = *_aidl_return | IVibrator::CAP_AMPLITUDE_CONTROL | - IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL; - } +#ifdef VIBRATOR_SUPPORTS_DURATION_AMPLITUDE_CONTROL + *_aidl_return |= IVibrator::CAP_AMPLITUDE_CONTROL | IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL; +#else + if (mHasTimedOutIntensity) + *_aidl_return |= IVibrator::CAP_AMPLITUDE_CONTROL | IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL; +#endif return ndk::ScopedAStatus::ok(); } @@ -91,6 +101,10 @@ ndk::ScopedAStatus Vibrator::on(int32_t timeoutMs, const std::shared_ptr= 0.5) { + return DURATION_AMPLITUDE_MEDIUM; + } + + return DURATION_AMPLITUDE_LIGHT; +} +#endif + } // namespace vibrator } // namespace hardware } // namespace android diff --git a/aidl/vibrator/Vibrator.h b/aidl/vibrator/Vibrator.h index db91b983..09e88d39 100644 --- a/aidl/vibrator/Vibrator.h +++ b/aidl/vibrator/Vibrator.h @@ -16,6 +16,12 @@ #define AMPLITUDE_MEDIUM 0.5 #define AMPLITUDE_STRONG 1 +#ifdef VIBRATOR_SUPPORTS_DURATION_AMPLITUDE_CONTROL +#define DURATION_AMPLITUDE_LIGHT 0.65 +#define DURATION_AMPLITUDE_MEDIUM 0.8 +#define DURATION_AMPLITUDE_STRONG 1 +#endif + #define VIBRATOR_TIMEOUT_PATH "/sys/class/timed_output/vibrator/enable" #define VIBRATOR_INTENSITY_PATH "/sys/class/timed_output/vibrator/intensity" #define VIBRATOR_CP_TRIGGER_PATH "/sys/class/timed_output/vibrator/cp_trigger_index" @@ -66,6 +72,11 @@ private: uint32_t effectToMs(Effect effect, ndk::ScopedAStatus* status); static float strengthToAmplitude(EffectStrength strength, ndk::ScopedAStatus* status); +#ifdef VIBRATOR_SUPPORTS_DURATION_AMPLITUDE_CONTROL + static float durationAmplitude(float amplitude); + float mDurationAmplitude; +#endif + bool mEnabled{false}; bool mExternalControl{false}; std::mutex mMutex;