xref: /aosp_15_r20/frameworks/native/services/inputflinger/include/VibrationElement.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2020 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #pragma once
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #include <array>
20*38e8c45fSAndroid Build Coastguard Worker #include <chrono>
21*38e8c45fSAndroid Build Coastguard Worker #include <cstdint>
22*38e8c45fSAndroid Build Coastguard Worker #include <string>
23*38e8c45fSAndroid Build Coastguard Worker #include <vector>
24*38e8c45fSAndroid Build Coastguard Worker 
25*38e8c45fSAndroid Build Coastguard Worker namespace android {
26*38e8c45fSAndroid Build Coastguard Worker 
27*38e8c45fSAndroid Build Coastguard Worker // evdev FF_RUMBLE effect only supports two channels of vibration.
28*38e8c45fSAndroid Build Coastguard Worker constexpr size_t CHANNEL_SIZE = 2;
29*38e8c45fSAndroid Build Coastguard Worker /*
30*38e8c45fSAndroid Build Coastguard Worker  * Describes a rumble effect
31*38e8c45fSAndroid Build Coastguard Worker  */
32*38e8c45fSAndroid Build Coastguard Worker struct VibrationElement {
33*38e8c45fSAndroid Build Coastguard Worker     std::chrono::milliseconds duration;
34*38e8c45fSAndroid Build Coastguard Worker     // Channel amplitude range 0-255.
35*38e8c45fSAndroid Build Coastguard Worker     std::vector<std::pair<int32_t /*vibratorId*/, uint8_t /*amplitude*/>> channels;
36*38e8c45fSAndroid Build Coastguard Worker 
37*38e8c45fSAndroid Build Coastguard Worker     explicit VibrationElement(size_t channelNum);
38*38e8c45fSAndroid Build Coastguard Worker 
39*38e8c45fSAndroid Build Coastguard Worker     VibrationElement(const VibrationElement& other);
40*38e8c45fSAndroid Build Coastguard Worker 
41*38e8c45fSAndroid Build Coastguard Worker     bool operator==(const VibrationElement& other) const;
42*38e8c45fSAndroid Build Coastguard Worker 
43*38e8c45fSAndroid Build Coastguard Worker     bool operator!=(const VibrationElement& other) const;
44*38e8c45fSAndroid Build Coastguard Worker 
45*38e8c45fSAndroid Build Coastguard Worker     void addChannel(int32_t vibratorId, uint8_t amplitude);
46*38e8c45fSAndroid Build Coastguard Worker 
47*38e8c45fSAndroid Build Coastguard Worker     const std::string toString() const;
48*38e8c45fSAndroid Build Coastguard Worker 
49*38e8c45fSAndroid Build Coastguard Worker     uint16_t getMagnitude(int32_t vibratorId) const;
50*38e8c45fSAndroid Build Coastguard Worker 
51*38e8c45fSAndroid Build Coastguard Worker     bool isOn() const;
52*38e8c45fSAndroid Build Coastguard Worker };
53*38e8c45fSAndroid Build Coastguard Worker 
54*38e8c45fSAndroid Build Coastguard Worker /*
55*38e8c45fSAndroid Build Coastguard Worker  * Describes a sequence of rumble effect
56*38e8c45fSAndroid Build Coastguard Worker  */
57*38e8c45fSAndroid Build Coastguard Worker struct VibrationSequence {
58*38e8c45fSAndroid Build Coastguard Worker     // Pattern of vibration elements
59*38e8c45fSAndroid Build Coastguard Worker     std::vector<VibrationElement> pattern;
60*38e8c45fSAndroid Build Coastguard Worker 
61*38e8c45fSAndroid Build Coastguard Worker     explicit VibrationSequence(size_t length);
62*38e8c45fSAndroid Build Coastguard Worker 
63*38e8c45fSAndroid Build Coastguard Worker     void operator=(const VibrationSequence& other);
64*38e8c45fSAndroid Build Coastguard Worker 
65*38e8c45fSAndroid Build Coastguard Worker     bool operator==(const VibrationSequence& other) const;
66*38e8c45fSAndroid Build Coastguard Worker 
67*38e8c45fSAndroid Build Coastguard Worker     void addElement(VibrationElement element);
68*38e8c45fSAndroid Build Coastguard Worker 
69*38e8c45fSAndroid Build Coastguard Worker     const std::string toString() const;
70*38e8c45fSAndroid Build Coastguard Worker };
71*38e8c45fSAndroid Build Coastguard Worker 
72*38e8c45fSAndroid Build Coastguard Worker } // namespace android
73