blob: aaf583401ac1788fb3aa518234e382a5aa59ebff [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 {
27
Chris Ye6393a262020-08-04 19:41:36 -070028const std::string VibrationElement::toString() const {
29 std::string dump;
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000030 dump += StringPrintf("[duration=%lldms, channels=[", duration.count());
31
Chris Ye6393a262020-08-04 19:41:36 -070032 for (auto it = channels.begin(); it != channels.end(); ++it) {
33 dump += std::to_string(*it);
34 if (std::next(it) != channels.end()) {
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000035 dump += ", ";
Chris Ye6393a262020-08-04 19:41:36 -070036 }
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000037 }
Chris Ye6393a262020-08-04 19:41:36 -070038
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000039 dump += "]]";
Chris Ye6393a262020-08-04 19:41:36 -070040 return dump;
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000041}
42
Chris Ye6393a262020-08-04 19:41:36 -070043uint16_t VibrationElement::getMagnitude(size_t channelIdx) const {
44 if (channelIdx >= channels.size()) {
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000045 return 0;
46 }
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000047 // convert range [0,255] to [0,65535] (android framework to linux ff ranges)
Chris Ye6393a262020-08-04 19:41:36 -070048 return static_cast<uint16_t>(channels[channelIdx]) << 8;
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000049}
50
51bool VibrationElement::isOn() const {
52 return std::any_of(channels.begin(), channels.end(),
53 [](uint16_t channel) { return channel != 0; });
54}
55
56} // namespace android