-
Notifications
You must be signed in to change notification settings - Fork 5
/
Vibrator.cpp
171 lines (135 loc) · 4.22 KB
/
Vibrator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#define LOG_TAG "VibratorService"
#include <cmath>
#include <fstream>
#include <android-base/logging.h>
#include "Vibrator.h"
namespace android {
namespace hardware {
namespace vibrator {
namespace V1_1 {
namespace implementation {
#define VIBRATOR "/sys/devices/virtual/timed_output/vibrator/"
#define ENABLE "enable"
#define VTG_DEFAULT "vtg_default"
#define VTG_MIN "vtg_min"
#define VTG_MAX "vtg_max"
#define VTG_LEVEL "vtg_level"
#define CLICK_TIMING_MS 20
#define TICK_TIMING_MS 20
#define DEFAULT_MIN_VTG 0
#define DEFAULT_MAX_VTG 255
using Status = ::android::hardware::vibrator::V1_0::Status;
using EffectStrength = ::android::hardware::vibrator::V1_0::EffectStrength;
static int get(std::string path, int defaultValue) {
int value = defaultValue;
std::ifstream file(path);
if (!file) {
LOG(ERROR) << "Failed to open " << path;
return value;
}
file >> value;
if (!file) {
LOG(ERROR) << "Failed to read value from " << path;
}
return value;
}
static int set(std::string path, int value) {
std::ofstream file(path);
if (!file) {
LOG(ERROR) << "Failed to open " << path;
return -1;
}
file << value;
if (!file) {
LOG(ERROR) << "Failed to write " << value << " to " << path;
return -1;
}
return 0;
}
Vibrator::Vibrator() {
minVoltage = get(VIBRATOR VTG_MIN, DEFAULT_MIN_VTG);
maxVoltage = get(VIBRATOR VTG_MAX, DEFAULT_MAX_VTG);
}
// Methods from ::android::hardware::vibrator::V1_0::IVibrator follow.
Return<Status> Vibrator::on(uint32_t timeout_ms) {
if (set(VIBRATOR ENABLE, timeout_ms)) {
return Status::UNKNOWN_ERROR;
}
return Status::OK;
}
Return<Status> Vibrator::off() {
if (set(VIBRATOR ENABLE, 0)) {
return Status::UNKNOWN_ERROR;
}
return Status::OK;
}
Return<bool> Vibrator::supportsAmplitudeControl() {
return true;
}
Return<Status> Vibrator::setAmplitude(uint8_t amplitude) {
if (amplitude == 0) {
return Status::BAD_VALUE;
}
/*
* Scale the voltage such that an amplitude of 1 is minVoltage
* and an amplitude of 255 is maxVoltage.
*/
uint32_t ledge = std::lround((maxVoltage - minVoltage) / 4);
uint32_t level = get(VIBRATOR VTG_LEVEL, (maxVoltage - ledge));
uint32_t loVoltage = ((level > ledge) && ((level - ledge) > minVoltage)) ? (level - ledge) : minVoltage;
uint32_t hiVoltage = ((level + ledge) < maxVoltage) ? (level + ledge) : maxVoltage;
uint32_t voltage =
std::lround((amplitude - 1) / 254.0 * (hiVoltage - loVoltage) + loVoltage);
if (set(VIBRATOR VTG_DEFAULT, voltage)) {
return Status::UNKNOWN_ERROR;
}
LOG(INFO) << "setAmplitude " << +amplitude << " -> got level " << level << " -> voltage set to " << voltage;
return Status::OK;
}
static uint8_t convertEffectStrength(EffectStrength strength) {
uint8_t amplitude;
switch (strength) {
case EffectStrength::LIGHT:
amplitude = 1;
break;
case EffectStrength::MEDIUM:
amplitude = 128;
break;
default:
case EffectStrength::STRONG:
amplitude = 255;
break;
}
return amplitude;
}
Return<void> Vibrator::perform(Effect effect, EffectStrength strength,
perform_cb _hidl_cb) {
if (effect == Effect::CLICK) {
on(CLICK_TIMING_MS);
setAmplitude(convertEffectStrength(strength));
_hidl_cb(Status::OK, CLICK_TIMING_MS);
} else {
_hidl_cb(Status::UNSUPPORTED_OPERATION, 0);
}
return Void();
}
// Methods from ::android::hardware::vibrator::V1_1::IVibrator follow.
Return<void> Vibrator::perform_1_1(Effect_1_1 effect, EffectStrength strength,
perform_cb _hidl_cb) {
if (effect == Effect_1_1::TICK) {
on(TICK_TIMING_MS);
setAmplitude(convertEffectStrength(strength));
_hidl_cb(Status::OK, TICK_TIMING_MS);
return Void();
} else if (effect < Effect_1_1::TICK) {
return perform(static_cast<Effect>(effect), strength, _hidl_cb);
} else {
_hidl_cb(Status::UNSUPPORTED_OPERATION, 0);
return Void();
}
}
} // namespace implementation
} // namespace V1_1
} // namespace vibrator
} // namespace hardware
} // namespace android