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 namespace android::os {
21
22 enum class HapticLevel {
23 MUTE = -100,
24 VERY_LOW = -2,
25 LOW = -1,
26 NONE = 0,
27 HIGH = 1,
28 VERY_HIGH = 2,
29 };
30
31 class HapticScale {
32 private:
33 HapticLevel mLevel = HapticLevel::NONE;
34 float mAdaptiveScaleFactor = 1.0f;
35
36 public:
HapticScale(HapticLevel level,float adaptiveScaleFactor)37 constexpr HapticScale(HapticLevel level, float adaptiveScaleFactor)
38 : mLevel(level), mAdaptiveScaleFactor(adaptiveScaleFactor) {}
HapticScale(HapticLevel level)39 constexpr HapticScale(HapticLevel level) : mLevel(level) {}
HapticScale()40 constexpr HapticScale() {}
41
getLevel()42 HapticLevel getLevel() const { return mLevel; }
getAdaptiveScaleFactor()43 float getAdaptiveScaleFactor() const { return mAdaptiveScaleFactor; }
44
45 bool operator==(const HapticScale& other) const {
46 return mLevel == other.mLevel && mAdaptiveScaleFactor == other.mAdaptiveScaleFactor;
47 }
48
isScaleNone()49 bool isScaleNone() const {
50 return mLevel == HapticLevel::NONE && mAdaptiveScaleFactor == 1.0f;
51 }
52
isScaleMute()53 bool isScaleMute() const {
54 return mLevel == HapticLevel::MUTE;
55 }
56
mute()57 static HapticScale mute() {
58 return {/*level=*/os::HapticLevel::MUTE};
59 }
60 };
61
62 bool isValidHapticScale(HapticScale scale);
63
64 /* Scales the haptic data in given buffer using the selected HapticScaleLevel and ensuring no
65 * absolute value will be larger than the absolute of given limit.
66 * The limit will be ignored if it is NaN or zero.
67 */
68 void scaleHapticData(float* buffer, size_t length, HapticScale scale, float limit);
69
70 } // namespace android::os
71
72 #endif // ANDROID_EXTERNAL_VIBRATION_UTILS_H
73