blob: a69f5d06c5e2839e987b2f5384841bbdc4a5b9e1 [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
28// The sentinel to use the default amplitude
29static const int DEFAULT_AMPLITUDE = -1;
30
31// The vibration magnitude for the "DEFAULT_AMPLITUDE" magnitude constant.
32static const uint16_t DEFAULT_MAGNITUDE = 0xc000;
33
34void VibrationElement::dump(std::string& dump) const {
35 dump += StringPrintf("[duration=%lldms, channels=[", duration.count());
36
37 if (channels.size()) {
38 dump += std::to_string(channels[0]);
39 std::for_each(channels.begin() + 1, channels.end(), [&dump](int channel) {
40 dump += ", ";
41 dump += std::to_string(channel);
42 });
43 }
44 dump += "]]";
45}
46
47uint16_t VibrationElement::getChannel(int id) const {
48 if (id >= (int)channels.size()) {
49 return 0;
50 }
51
52 // android framework uses DEFAULT_AMPLITUDE to signal that the vibration
53 // should use some built-in default value, denoted here as DEFAULT_MAGNITUDE
54 if (channels[id] == DEFAULT_AMPLITUDE) {
55 return DEFAULT_MAGNITUDE;
56 }
57
58 // convert range [0,255] to [0,65535] (android framework to linux ff ranges)
59 return ((uint16_t)channels[id]) << 8;
60}
61
62bool VibrationElement::isOn() const {
63 return std::any_of(channels.begin(), channels.end(),
64 [](uint16_t channel) { return channel != 0; });
65}
66
67} // namespace android