universal7904: vibrator: Switch from 0-255 to 0.0f-1.0f range for amplitude
* This was missed when converting to AIDL
* Fixes strength control for effects
Change-Id: Iedcdbb455b67b9466a76e53ca9273cbcdcede059
diff --git a/aidl/vibrator/Vibrator.cpp b/aidl/vibrator/Vibrator.cpp
index 97a850c..6958040 100644
--- a/aidl/vibrator/Vibrator.cpp
+++ b/aidl/vibrator/Vibrator.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2021 The LineageOS Project
+ * Copyright (C) 2021-2023 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -101,7 +101,7 @@
ndk::ScopedAStatus Vibrator::perform(Effect effect, EffectStrength strength, const std::shared_ptr<IVibratorCallback>& callback, int32_t* _aidl_return) {
ndk::ScopedAStatus status;
- uint32_t amplitude = strengthToAmplitude(strength, &status);
+ float amplitude = strengthToAmplitude(strength, &status);
uint32_t ms = 1000;
if (!status.isOk())
@@ -152,20 +152,13 @@
ndk::ScopedAStatus Vibrator::setAmplitude(float amplitude) {
uint32_t intensity;
- if (amplitude == 0) {
- return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
+ if (amplitude <= 0.0f || amplitude > 1.0f) {
+ return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_ILLEGAL_ARGUMENT));
}
- LOG(DEBUG) << "Setting amplitude: " << (uint32_t)amplitude;
+ LOG(DEBUG) << "Setting amplitude: " << amplitude;
- intensity = std::lround((amplitude - 1) * INTENSITY_MAX / 254.0);
- if (intensity > INTENSITY_MAX) {
- intensity = INTENSITY_MAX;
- }
-
- if (intensity == 0) {
- return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
- }
+ intensity = amplitude * INTENSITY_MAX;
LOG(DEBUG) << "Setting intensity: " << intensity;
@@ -265,16 +258,16 @@
return writeNode(VIBRATOR_TIMEOUT_PATH, timeoutMs);
}
-uint8_t Vibrator::strengthToAmplitude(EffectStrength strength, ndk::ScopedAStatus* status) {
+float Vibrator::strengthToAmplitude(EffectStrength strength, ndk::ScopedAStatus* status) {
*status = ndk::ScopedAStatus::ok();
switch (strength) {
case EffectStrength::LIGHT:
- return 64;
+ return AMPLITUDE_LIGHT;
case EffectStrength::MEDIUM:
- return 128;
+ return AMPLITUDE_MEDIUM;
case EffectStrength::STRONG:
- return 255;
+ return AMPLITUDE_STRONG;
}
*status = ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
diff --git a/aidl/vibrator/Vibrator.h b/aidl/vibrator/Vibrator.h
index 785ad9d..06f5166 100644
--- a/aidl/vibrator/Vibrator.h
+++ b/aidl/vibrator/Vibrator.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2021 The LineageOS Project
+ * Copyright (C) 2021-2023 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -12,6 +12,10 @@
#define INTENSITY_MAX 10000
#define INTENSITY_DEFAULT INTENSITY_MAX
+#define AMPLITUDE_LIGHT 0.25
+#define AMPLITUDE_MEDIUM 0.5
+#define AMPLITUDE_STRONG 1
+
#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"
@@ -60,7 +64,7 @@
private:
ndk::ScopedAStatus activate(uint32_t ms);
static uint32_t effectToMs(Effect effect, ndk::ScopedAStatus* status);
- static uint8_t strengthToAmplitude(EffectStrength strength, ndk::ScopedAStatus* status);
+ static float strengthToAmplitude(EffectStrength strength, ndk::ScopedAStatus* status);
bool mEnabled{false};
bool mExternalControl{false};