xref: /aosp_15_r20/frameworks/native/libs/vibrator/include/vibrator/ExternalVibrationUtils.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright (C) 2020 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 
17 #ifndef ANDROID_EXTERNAL_VIBRATION_UTILS_H
18 #define ANDROID_EXTERNAL_VIBRATION_UTILS_H
19 
20 #include <cstring>
21 #include <sstream>
22 #include <string>
23 
24 namespace android::os {
25 
26 enum class HapticLevel : int32_t {
27     MUTE = -100,
28     VERY_LOW = -2,
29     LOW = -1,
30     NONE = 0,
31     HIGH = 1,
32     VERY_HIGH = 2,
33 };
34 
35 class HapticScale {
36 private:
37 HapticLevel mLevel = HapticLevel::NONE;
38 float mScaleFactor = -1.0f; // undefined, use haptic level to define scale factor
39 float mAdaptiveScaleFactor = 1.0f;
40 
41 public:
HapticScale(HapticLevel level,float scaleFactor,float adaptiveScaleFactor)42     explicit HapticScale(HapticLevel level, float scaleFactor, float adaptiveScaleFactor)
43           : mLevel(level), mScaleFactor(scaleFactor), mAdaptiveScaleFactor(adaptiveScaleFactor) {}
HapticScale(HapticLevel level)44     explicit HapticScale(HapticLevel level) : mLevel(level) {}
HapticScale()45     constexpr HapticScale() {}
46 
getLevel()47     HapticLevel getLevel() const { return mLevel; }
getScaleFactor()48     float getScaleFactor() const { return mScaleFactor; }
getAdaptiveScaleFactor()49     float getAdaptiveScaleFactor() const { return mAdaptiveScaleFactor; }
50 
51     bool operator==(const HapticScale& other) const {
52         return mLevel == other.mLevel && mScaleFactor == other.mScaleFactor &&
53                 mAdaptiveScaleFactor == other.mAdaptiveScaleFactor;
54     }
55 
isScaleNone()56 bool isScaleNone() const {
57     return (mLevel == HapticLevel::NONE || mScaleFactor == 1.0f) && mAdaptiveScaleFactor == 1.0f;
58 }
59 
isScaleMute()60 bool isScaleMute() const {
61     return mLevel == HapticLevel::MUTE || mScaleFactor == 0 || mAdaptiveScaleFactor == 0;
62 }
63 
toString()64 std::string toString() const {
65     std::ostringstream os;
66     os << "HapticScale { level: " << static_cast<int>(mLevel);
67     os << ", scaleFactor: " << mScaleFactor;
68     os << ", adaptiveScaleFactor: " << mAdaptiveScaleFactor;
69     os << "}";
70     return os.str();
71 }
72 
mute()73 static HapticScale mute() { return os::HapticScale(os::HapticLevel::MUTE); }
74 
none()75 static HapticScale none() { return os::HapticScale(os::HapticLevel::NONE); }
76 };
77 
78 bool isValidHapticScale(HapticScale scale);
79 
80 /* Scales the haptic data in given buffer using the selected HapticScaleLevel and ensuring no
81  * absolute value will be larger than the absolute of given limit.
82  * The limit will be ignored if it is NaN or zero.
83  */
84 void scaleHapticData(float* buffer, size_t length, HapticScale scale, float limit);
85 
86 } // namespace android::os
87 
88 #endif // ANDROID_EXTERNAL_VIBRATION_UTILS_H
89