Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 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 { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 27 | // VibrationElement implementations |
| 28 | VibrationElement::VibrationElement(size_t channelNum) { |
| 29 | channels.reserve(channelNum); |
| 30 | } |
| 31 | |
| 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 | } |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 40 | |
Chris Ye | 6393a26 | 2020-08-04 19:41:36 -0700 | [diff] [blame] | 41 | const std::string VibrationElement::toString() const { |
| 42 | std::string dump; |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 43 | dump += StringPrintf("[duration=%lldms, channels=[", duration.count()); |
| 44 | |
Chris Ye | 6393a26 | 2020-08-04 19:41:36 -0700 | [diff] [blame] | 45 | for (auto it = channels.begin(); it != channels.end(); ++it) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 46 | dump += std::to_string(it->first); |
| 47 | dump += " : "; |
| 48 | dump += std::to_string(it->second); |
Chris Ye | 6393a26 | 2020-08-04 19:41:36 -0700 | [diff] [blame] | 49 | if (std::next(it) != channels.end()) { |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 50 | dump += ", "; |
Chris Ye | 6393a26 | 2020-08-04 19:41:36 -0700 | [diff] [blame] | 51 | } |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 52 | } |
Chris Ye | 6393a26 | 2020-08-04 19:41:36 -0700 | [diff] [blame] | 53 | |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 54 | dump += "]]"; |
Chris Ye | 6393a26 | 2020-08-04 19:41:36 -0700 | [diff] [blame] | 55 | return dump; |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 58 | 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()) { |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 64 | return 0; |
| 65 | } |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 66 | // convert range [0,255] to [0,65535] (android framework to linux ff ranges) |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 67 | return static_cast<uint16_t>(it->second) << 8; |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | bool VibrationElement::isOn() const { |
| 71 | return std::any_of(channels.begin(), channels.end(), |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 72 | [](const auto& channel) { return channel.second != 0; }); |
| 73 | } |
| 74 | |
| 75 | void VibrationElement::addChannel(int32_t vibratorId, uint8_t amplitude) { |
| 76 | channels.push_back(std::make_pair(vibratorId, amplitude)); |
| 77 | } |
| 78 | |
| 79 | 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 | |
| 91 | bool VibrationElement::operator!=(const VibrationElement& other) const { |
| 92 | return !(*this == other); |
| 93 | } |
| 94 | |
| 95 | // VibrationSequence implementations |
| 96 | VibrationSequence::VibrationSequence(size_t length) { |
| 97 | pattern.reserve(length); |
| 98 | } |
| 99 | |
| 100 | void VibrationSequence::operator=(const VibrationSequence& other) { |
| 101 | pattern = other.pattern; |
| 102 | } |
| 103 | |
| 104 | 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 | |
| 116 | void VibrationSequence::addElement(VibrationElement element) { |
| 117 | pattern.push_back(element); |
| 118 | } |
| 119 | |
| 120 | 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; |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | } // namespace android |