blob: 17e1ad4d15b1d508d31ea550eea0f6d3bbfca0ba [file] [log] [blame]
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +00001/*
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
24using android::base::StringPrintf;
25
26namespace android {
Chris Ye87143712020-11-10 05:05:58 +000027// VibrationElement implementations
28VibrationElement::VibrationElement(size_t channelNum) {
29 channels.reserve(channelNum);
30}
31
32VibrationElement::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}
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000040
Chris Ye6393a262020-08-04 19:41:36 -070041const std::string VibrationElement::toString() const {
42 std::string dump;
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000043 dump += StringPrintf("[duration=%lldms, channels=[", duration.count());
44
Chris Ye6393a262020-08-04 19:41:36 -070045 for (auto it = channels.begin(); it != channels.end(); ++it) {
Chris Ye87143712020-11-10 05:05:58 +000046 dump += std::to_string(it->first);
47 dump += " : ";
48 dump += std::to_string(it->second);
Chris Ye6393a262020-08-04 19:41:36 -070049 if (std::next(it) != channels.end()) {
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000050 dump += ", ";
Chris Ye6393a262020-08-04 19:41:36 -070051 }
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000052 }
Chris Ye6393a262020-08-04 19:41:36 -070053
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000054 dump += "]]";
Chris Ye6393a262020-08-04 19:41:36 -070055 return dump;
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000056}
57
Chris Ye87143712020-11-10 05:05:58 +000058uint16_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()) {
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000064 return 0;
65 }
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000066 // convert range [0,255] to [0,65535] (android framework to linux ff ranges)
Chris Ye87143712020-11-10 05:05:58 +000067 return static_cast<uint16_t>(it->second) << 8;
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000068}
69
70bool VibrationElement::isOn() const {
71 return std::any_of(channels.begin(), channels.end(),
Chris Ye87143712020-11-10 05:05:58 +000072 [](const auto& channel) { return channel.second != 0; });
73}
74
75void VibrationElement::addChannel(int32_t vibratorId, uint8_t amplitude) {
76 channels.push_back(std::make_pair(vibratorId, amplitude));
77}
78
79bool 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
91bool VibrationElement::operator!=(const VibrationElement& other) const {
92 return !(*this == other);
93}
94
95// VibrationSequence implementations
96VibrationSequence::VibrationSequence(size_t length) {
97 pattern.reserve(length);
98}
99
100void VibrationSequence::operator=(const VibrationSequence& other) {
101 pattern = other.pattern;
102}
103
104bool 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
116void VibrationSequence::addElement(VibrationElement element) {
117 pattern.push_back(element);
118}
119
120const 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;
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000131}
132
133} // namespace android