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 _VIBRATION_ELEMENT_H
18 #define _VIBRATION_ELEMENT_H
19 
20 #include <array>
21 #include <chrono>
22 #include <cstdint>
23 #include <string>
24 #include <vector>
25 
26 namespace android {
27 
28 // evdev FF_RUMBLE effect only supports two channels of vibration.
29 constexpr size_t CHANNEL_SIZE = 2;
30 /*
31  * Describes a rumble effect
32  */
33 struct VibrationElement {
34     std::chrono::milliseconds duration;
35     // Channel amplitude range 0-255.
36     std::vector<std::pair<int32_t /*vibratorId*/, uint8_t /*amplitude*/>> channels;
37 
38     explicit VibrationElement(size_t channelNum);
39 
40     VibrationElement(const VibrationElement& other);
41 
42     bool operator==(const VibrationElement& other) const;
43 
44     bool operator!=(const VibrationElement& other) const;
45 
46     void addChannel(int32_t vibratorId, uint8_t amplitude);
47 
48     const std::string toString() const;
49 
50     uint16_t getMagnitude(int32_t vibratorId) const;
51 
52     bool isOn() const;
53 };
54 
55 /*
56  * Describes a sequence of rumble effect
57  */
58 struct VibrationSequence {
59     // Pattern of vibration elements
60     std::vector<VibrationElement> pattern;
61 
62     explicit VibrationSequence(size_t length);
63 
64     void operator=(const VibrationSequence& other);
65 
66     bool operator==(const VibrationSequence& other) const;
67 
68     void addElement(VibrationElement element);
69 
70     const std::string toString() const;
71 };
72 
73 } // namespace android
74 
75 #endif // _VIBRATION_ELEMENT_H
76