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 #include "VibrationElement.h"
18
19 #include <android-base/stringprintf.h>
20
21 #include <algorithm>
22 #include <cinttypes>
23
24 using android::base::StringPrintf;
25
26 namespace android {
27 // VibrationElement implementations
VibrationElement(size_t channelNum)28 VibrationElement::VibrationElement(size_t channelNum) {
29 channels.reserve(channelNum);
30 }
31
VibrationElement(const VibrationElement & other)32 VibrationElement::VibrationElement(const VibrationElement& other) {
33 duration = other.duration;
34 channels.resize(other.channels.size());
35 for (size_t i = 0; i < other.channels.size(); i++) {
36 channels[i].first = other.channels[i].first;
37 channels[i].second = other.channels[i].second;
38 }
39 }
40
toString() const41 const std::string VibrationElement::toString() const {
42 std::string dump;
43 dump += StringPrintf("[duration=%lldms, channels=[", duration.count());
44
45 for (auto it = channels.begin(); it != channels.end(); ++it) {
46 dump += std::to_string(it->first);
47 dump += " : ";
48 dump += std::to_string(it->second);
49 if (std::next(it) != channels.end()) {
50 dump += ", ";
51 }
52 }
53
54 dump += "]]";
55 return dump;
56 }
57
getMagnitude(int32_t vibratorId) const58 uint16_t VibrationElement::getMagnitude(int32_t vibratorId) const {
59 auto it =
60 std::find_if(channels.begin(), channels.end(),
61 [vibratorId](const std::pair<int32_t /*vibratorId*/, uint8_t /*amplitude*/>
62 pair) { return pair.first == vibratorId; });
63 if (it == channels.end()) {
64 return 0;
65 }
66 // convert range [0,255] to [0,65535] (android framework to linux ff ranges)
67 return static_cast<uint16_t>(it->second) << 8;
68 }
69
isOn() const70 bool VibrationElement::isOn() const {
71 return std::any_of(channels.begin(), channels.end(),
72 [](const auto& channel) { return channel.second != 0; });
73 }
74
addChannel(int32_t vibratorId,uint8_t amplitude)75 void VibrationElement::addChannel(int32_t vibratorId, uint8_t amplitude) {
76 channels.push_back(std::make_pair(vibratorId, amplitude));
77 }
78
operator ==(const VibrationElement & other) const79 bool VibrationElement::operator==(const VibrationElement& other) const {
80 if (duration != other.duration || channels.size() != other.channels.size()) {
81 return false;
82 }
83 for (size_t i = 0; i < CHANNEL_SIZE; i++) {
84 if (channels[i] != other.channels[i]) {
85 return false;
86 }
87 }
88 return true;
89 }
90
operator !=(const VibrationElement & other) const91 bool VibrationElement::operator!=(const VibrationElement& other) const {
92 return !(*this == other);
93 }
94
95 // VibrationSequence implementations
VibrationSequence(size_t length)96 VibrationSequence::VibrationSequence(size_t length) {
97 pattern.reserve(length);
98 }
99
operator =(const VibrationSequence & other)100 void VibrationSequence::operator=(const VibrationSequence& other) {
101 pattern = other.pattern;
102 }
103
operator ==(const VibrationSequence & other) const104 bool VibrationSequence::operator==(const VibrationSequence& other) const {
105 if (pattern.size() != other.pattern.size()) {
106 return false;
107 }
108 for (size_t i = 0; i < pattern.size(); i++) {
109 if (pattern[i] != other.pattern[i]) {
110 return false;
111 }
112 }
113 return true;
114 }
115
addElement(VibrationElement element)116 void VibrationSequence::addElement(VibrationElement element) {
117 pattern.push_back(element);
118 }
119
toString() const120 const std::string VibrationSequence::toString() const {
121 std::string dump;
122 dump += "[";
123
124 for (const auto& element : pattern) {
125 dump += element.toString();
126 dump += " ";
127 }
128
129 dump += "]";
130 return dump;
131 }
132
133 } // namespace android
134