1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2020 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker #include "VibrationElement.h"
18*38e8c45fSAndroid Build Coastguard Worker
19*38e8c45fSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
20*38e8c45fSAndroid Build Coastguard Worker
21*38e8c45fSAndroid Build Coastguard Worker #include <algorithm>
22*38e8c45fSAndroid Build Coastguard Worker #include <cinttypes>
23*38e8c45fSAndroid Build Coastguard Worker
24*38e8c45fSAndroid Build Coastguard Worker using android::base::StringPrintf;
25*38e8c45fSAndroid Build Coastguard Worker
26*38e8c45fSAndroid Build Coastguard Worker namespace android {
27*38e8c45fSAndroid Build Coastguard Worker // VibrationElement implementations
VibrationElement(size_t channelNum)28*38e8c45fSAndroid Build Coastguard Worker VibrationElement::VibrationElement(size_t channelNum) {
29*38e8c45fSAndroid Build Coastguard Worker channels.reserve(channelNum);
30*38e8c45fSAndroid Build Coastguard Worker }
31*38e8c45fSAndroid Build Coastguard Worker
VibrationElement(const VibrationElement & other)32*38e8c45fSAndroid Build Coastguard Worker VibrationElement::VibrationElement(const VibrationElement& other) {
33*38e8c45fSAndroid Build Coastguard Worker duration = other.duration;
34*38e8c45fSAndroid Build Coastguard Worker channels.resize(other.channels.size());
35*38e8c45fSAndroid Build Coastguard Worker for (size_t i = 0; i < other.channels.size(); i++) {
36*38e8c45fSAndroid Build Coastguard Worker channels[i].first = other.channels[i].first;
37*38e8c45fSAndroid Build Coastguard Worker channels[i].second = other.channels[i].second;
38*38e8c45fSAndroid Build Coastguard Worker }
39*38e8c45fSAndroid Build Coastguard Worker }
40*38e8c45fSAndroid Build Coastguard Worker
toString() const41*38e8c45fSAndroid Build Coastguard Worker const std::string VibrationElement::toString() const {
42*38e8c45fSAndroid Build Coastguard Worker std::string dump;
43*38e8c45fSAndroid Build Coastguard Worker dump += StringPrintf("[duration=%lldms, channels=[", duration.count());
44*38e8c45fSAndroid Build Coastguard Worker
45*38e8c45fSAndroid Build Coastguard Worker for (auto it = channels.begin(); it != channels.end(); ++it) {
46*38e8c45fSAndroid Build Coastguard Worker dump += std::to_string(it->first);
47*38e8c45fSAndroid Build Coastguard Worker dump += " : ";
48*38e8c45fSAndroid Build Coastguard Worker dump += std::to_string(it->second);
49*38e8c45fSAndroid Build Coastguard Worker if (std::next(it) != channels.end()) {
50*38e8c45fSAndroid Build Coastguard Worker dump += ", ";
51*38e8c45fSAndroid Build Coastguard Worker }
52*38e8c45fSAndroid Build Coastguard Worker }
53*38e8c45fSAndroid Build Coastguard Worker
54*38e8c45fSAndroid Build Coastguard Worker dump += "]]";
55*38e8c45fSAndroid Build Coastguard Worker return dump;
56*38e8c45fSAndroid Build Coastguard Worker }
57*38e8c45fSAndroid Build Coastguard Worker
getMagnitude(int32_t vibratorId) const58*38e8c45fSAndroid Build Coastguard Worker uint16_t VibrationElement::getMagnitude(int32_t vibratorId) const {
59*38e8c45fSAndroid Build Coastguard Worker auto it =
60*38e8c45fSAndroid Build Coastguard Worker std::find_if(channels.begin(), channels.end(),
61*38e8c45fSAndroid Build Coastguard Worker [vibratorId](const std::pair<int32_t /*vibratorId*/, uint8_t /*amplitude*/>
62*38e8c45fSAndroid Build Coastguard Worker pair) { return pair.first == vibratorId; });
63*38e8c45fSAndroid Build Coastguard Worker if (it == channels.end()) {
64*38e8c45fSAndroid Build Coastguard Worker return 0;
65*38e8c45fSAndroid Build Coastguard Worker }
66*38e8c45fSAndroid Build Coastguard Worker // convert range [0,255] to [0,65535] (android framework to linux ff ranges)
67*38e8c45fSAndroid Build Coastguard Worker return static_cast<uint16_t>(it->second) << 8;
68*38e8c45fSAndroid Build Coastguard Worker }
69*38e8c45fSAndroid Build Coastguard Worker
isOn() const70*38e8c45fSAndroid Build Coastguard Worker bool VibrationElement::isOn() const {
71*38e8c45fSAndroid Build Coastguard Worker return std::any_of(channels.begin(), channels.end(),
72*38e8c45fSAndroid Build Coastguard Worker [](const auto& channel) { return channel.second != 0; });
73*38e8c45fSAndroid Build Coastguard Worker }
74*38e8c45fSAndroid Build Coastguard Worker
addChannel(int32_t vibratorId,uint8_t amplitude)75*38e8c45fSAndroid Build Coastguard Worker void VibrationElement::addChannel(int32_t vibratorId, uint8_t amplitude) {
76*38e8c45fSAndroid Build Coastguard Worker channels.push_back(std::make_pair(vibratorId, amplitude));
77*38e8c45fSAndroid Build Coastguard Worker }
78*38e8c45fSAndroid Build Coastguard Worker
operator ==(const VibrationElement & other) const79*38e8c45fSAndroid Build Coastguard Worker bool VibrationElement::operator==(const VibrationElement& other) const {
80*38e8c45fSAndroid Build Coastguard Worker if (duration != other.duration || channels.size() != other.channels.size()) {
81*38e8c45fSAndroid Build Coastguard Worker return false;
82*38e8c45fSAndroid Build Coastguard Worker }
83*38e8c45fSAndroid Build Coastguard Worker for (size_t i = 0; i < CHANNEL_SIZE; i++) {
84*38e8c45fSAndroid Build Coastguard Worker if (channels[i] != other.channels[i]) {
85*38e8c45fSAndroid Build Coastguard Worker return false;
86*38e8c45fSAndroid Build Coastguard Worker }
87*38e8c45fSAndroid Build Coastguard Worker }
88*38e8c45fSAndroid Build Coastguard Worker return true;
89*38e8c45fSAndroid Build Coastguard Worker }
90*38e8c45fSAndroid Build Coastguard Worker
operator !=(const VibrationElement & other) const91*38e8c45fSAndroid Build Coastguard Worker bool VibrationElement::operator!=(const VibrationElement& other) const {
92*38e8c45fSAndroid Build Coastguard Worker return !(*this == other);
93*38e8c45fSAndroid Build Coastguard Worker }
94*38e8c45fSAndroid Build Coastguard Worker
95*38e8c45fSAndroid Build Coastguard Worker // VibrationSequence implementations
VibrationSequence(size_t length)96*38e8c45fSAndroid Build Coastguard Worker VibrationSequence::VibrationSequence(size_t length) {
97*38e8c45fSAndroid Build Coastguard Worker pattern.reserve(length);
98*38e8c45fSAndroid Build Coastguard Worker }
99*38e8c45fSAndroid Build Coastguard Worker
operator =(const VibrationSequence & other)100*38e8c45fSAndroid Build Coastguard Worker void VibrationSequence::operator=(const VibrationSequence& other) {
101*38e8c45fSAndroid Build Coastguard Worker pattern = other.pattern;
102*38e8c45fSAndroid Build Coastguard Worker }
103*38e8c45fSAndroid Build Coastguard Worker
operator ==(const VibrationSequence & other) const104*38e8c45fSAndroid Build Coastguard Worker bool VibrationSequence::operator==(const VibrationSequence& other) const {
105*38e8c45fSAndroid Build Coastguard Worker if (pattern.size() != other.pattern.size()) {
106*38e8c45fSAndroid Build Coastguard Worker return false;
107*38e8c45fSAndroid Build Coastguard Worker }
108*38e8c45fSAndroid Build Coastguard Worker for (size_t i = 0; i < pattern.size(); i++) {
109*38e8c45fSAndroid Build Coastguard Worker if (pattern[i] != other.pattern[i]) {
110*38e8c45fSAndroid Build Coastguard Worker return false;
111*38e8c45fSAndroid Build Coastguard Worker }
112*38e8c45fSAndroid Build Coastguard Worker }
113*38e8c45fSAndroid Build Coastguard Worker return true;
114*38e8c45fSAndroid Build Coastguard Worker }
115*38e8c45fSAndroid Build Coastguard Worker
addElement(VibrationElement element)116*38e8c45fSAndroid Build Coastguard Worker void VibrationSequence::addElement(VibrationElement element) {
117*38e8c45fSAndroid Build Coastguard Worker pattern.push_back(element);
118*38e8c45fSAndroid Build Coastguard Worker }
119*38e8c45fSAndroid Build Coastguard Worker
toString() const120*38e8c45fSAndroid Build Coastguard Worker const std::string VibrationSequence::toString() const {
121*38e8c45fSAndroid Build Coastguard Worker std::string dump;
122*38e8c45fSAndroid Build Coastguard Worker dump += "[";
123*38e8c45fSAndroid Build Coastguard Worker
124*38e8c45fSAndroid Build Coastguard Worker for (const auto& element : pattern) {
125*38e8c45fSAndroid Build Coastguard Worker dump += element.toString();
126*38e8c45fSAndroid Build Coastguard Worker dump += " ";
127*38e8c45fSAndroid Build Coastguard Worker }
128*38e8c45fSAndroid Build Coastguard Worker
129*38e8c45fSAndroid Build Coastguard Worker dump += "]";
130*38e8c45fSAndroid Build Coastguard Worker return dump;
131*38e8c45fSAndroid Build Coastguard Worker }
132*38e8c45fSAndroid Build Coastguard Worker
133*38e8c45fSAndroid Build Coastguard Worker } // namespace android
134