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 { |
| 27 | |
Chris Ye | 6393a26 | 2020-08-04 19:41:36 -0700 | [diff] [blame^] | 28 | const std::string VibrationElement::toString() const { |
| 29 | std::string dump; |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 30 | dump += StringPrintf("[duration=%lldms, channels=[", duration.count()); |
| 31 | |
Chris Ye | 6393a26 | 2020-08-04 19:41:36 -0700 | [diff] [blame^] | 32 | for (auto it = channels.begin(); it != channels.end(); ++it) { |
| 33 | dump += std::to_string(*it); |
| 34 | if (std::next(it) != channels.end()) { |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 35 | dump += ", "; |
Chris Ye | 6393a26 | 2020-08-04 19:41:36 -0700 | [diff] [blame^] | 36 | } |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 37 | } |
Chris Ye | 6393a26 | 2020-08-04 19:41:36 -0700 | [diff] [blame^] | 38 | |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 39 | dump += "]]"; |
Chris Ye | 6393a26 | 2020-08-04 19:41:36 -0700 | [diff] [blame^] | 40 | return dump; |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Chris Ye | 6393a26 | 2020-08-04 19:41:36 -0700 | [diff] [blame^] | 43 | uint16_t VibrationElement::getMagnitude(size_t channelIdx) const { |
| 44 | if (channelIdx >= channels.size()) { |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 45 | return 0; |
| 46 | } |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 47 | // convert range [0,255] to [0,65535] (android framework to linux ff ranges) |
Chris Ye | 6393a26 | 2020-08-04 19:41:36 -0700 | [diff] [blame^] | 48 | return static_cast<uint16_t>(channels[channelIdx]) << 8; |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | bool VibrationElement::isOn() const { |
| 52 | return std::any_of(channels.begin(), channels.end(), |
| 53 | [](uint16_t channel) { return channel != 0; }); |
| 54 | } |
| 55 | |
| 56 | } // namespace android |