summaryrefslogtreecommitdiffstats
path: root/vibrator/1.0-lineage/Vibrator.cpp
blob: 50ca82eb6345435d5b89cc3b282b18d36559f58f (plain)
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
/*
 * Copyright (C) 2017 The LineageOS Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "VibratorService"

#include <log/log.h>

#include "Vibrator.h"

#include <cmath>
#include <fstream>

namespace android {
namespace hardware {
namespace vibrator {
namespace V1_0 {
namespace implementation {

#define VIBRATOR "/sys/devices/virtual/timed_output/vibrator/"

#define ENABLE      "enable"
#define VTG_LEVEL   "vtg_level"
#define VTG_MIN     "vtg_min"
#define VTG_MAX     "vtg_max"

#define CLICK_TIMING_MS 20

#define DEFAULT_MIN_VTG 0
#define DEFAULT_MAX_VTG 255

static int get(std::string path, int defaultValue) {
    int value = defaultValue;
    std::ifstream file(path);

    if (!file) {
        ALOGE("Failed to open %s", path.c_str());
        return value;
    }

    file >> value;

    if (!file) {
        ALOGE("Failed to read value from %s", path.c_str());
    }

    return value;
}

static int set(std::string path, int value) {
    std::ofstream file(path);

    if (!file) {
        ALOGE("Failed to open %s", path.c_str());
        return -1;
    }

    file << value;

    if (!file) {
        ALOGE("Failed to write %d to %s", value, path.c_str());
        return -1;
    }

    return 0;
}

Vibrator::Vibrator() {
    minVoltage = get(VIBRATOR VTG_MIN, DEFAULT_MIN_VTG);
    maxVoltage = get(VIBRATOR VTG_MAX, DEFAULT_MAX_VTG);
}

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 voltage =
            std::lround((amplitude - 1) / 254.0 * (maxVoltage - minVoltage) + minVoltage);

    if (set(VIBRATOR VTG_LEVEL, voltage)) {
        return Status::UNKNOWN_ERROR;
    }

    ALOGI("Voltage set to: %u", voltage);

    return Status::OK;
}

Return<void> Vibrator::perform(Effect effect, EffectStrength strength, perform_cb _hidl_cb) {
    switch (effect) {
    case Effect::CLICK:
        uint8_t amplitude;

        switch (strength) {
        case EffectStrength::LIGHT:
            amplitude = 36;
            break;
        case EffectStrength::MEDIUM:
            amplitude = 128;
            break;
        case EffectStrength::STRONG:
            amplitude = 255;
            break;
        default:
            _hidl_cb(Status::UNSUPPORTED_OPERATION, 0);
            return Void();
        }

        on(CLICK_TIMING_MS);
        setAmplitude(amplitude);
        _hidl_cb(Status::OK, CLICK_TIMING_MS);
        break;
    default:
        _hidl_cb(Status::UNSUPPORTED_OPERATION, 0);
        break;
    }

    return Void();
}

} // namespace implementation
}  // namespace V1_0
}  // namespace vibrator
}  // namespace hardware
}  // namespace android