summaryrefslogtreecommitdiff
path: root/vibrator/cs40l25/Hardware.h
blob: e2222798ec5c2f05a6ac7c9f3187ddd40b36bbd5 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * Copyright (C) 2019 The Android Open Source 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.
 */
#pragma once

#include "HardwareBase.h"
#include "Vibrator.h"

namespace aidl {
namespace android {
namespace hardware {
namespace vibrator {

class HwApi : public Vibrator::HwApi, private HwApiBase {
  public:
    HwApi() {
        open("device/f0_stored", &mF0);
        open("device/f0_offset", &mF0Offset);
        open("device/redc_stored", &mRedc);
        open("device/q_stored", &mQ);
        open("activate", &mActivate);
        open("duration", &mDuration);
        open("state", &mState);
        open("device/cp_trigger_duration", &mEffectDuration);
        open("device/cp_trigger_index", &mEffectIndex);
        open("device/cp_trigger_queue", &mEffectQueue);
        open("device/cp_dig_scale", &mEffectScale);
        open("device/dig_scale", &mGlobalScale);
        open("device/asp_enable", &mAspEnable);
        open("device/gpio1_fall_index", &mGpioFallIndex);
        open("device/gpio1_fall_dig_scale", &mGpioFallScale);
        open("device/gpio1_rise_index", &mGpioRiseIndex);
        open("device/gpio1_rise_dig_scale", &mGpioRiseScale);
        open("device/vibe_state", &mVibeState);
        open("device/num_waves", &mEffectCount);
        open("device/clab_enable", &mClabEnable);
        open("device/available_pwle_segments", &mAvailablePwleSegments);
        open("device/pwle", &mPwle);
        open("device/pwle_ramp_down", &mPwleRampDown);
    }

    bool setF0(uint32_t value) override { return set(value, &mF0); }
    bool setF0Offset(uint32_t value) override { return set(value, &mF0Offset); }
    bool setRedc(uint32_t value) override { return set(value, &mRedc); }
    bool setQ(uint32_t value) override { return set(value, &mQ); }
    bool setActivate(bool value) override { return set(value, &mActivate); }
    bool setDuration(uint32_t value) override { return set(value, &mDuration); }
    bool getEffectCount(uint32_t *value) override { return get(value, &mEffectCount); }
    bool getEffectDuration(uint32_t *value) override { return get(value, &mEffectDuration); }
    bool setEffectIndex(uint32_t value) override { return set(value, &mEffectIndex); }
    bool setEffectQueue(std::string value) override { return set(value, &mEffectQueue); }
    bool hasEffectScale() override { return has(mEffectScale); }
    bool setEffectScale(uint32_t value) override { return set(value, &mEffectScale); }
    bool setGlobalScale(uint32_t value) override { return set(value, &mGlobalScale); }
    bool setState(bool value) override { return set(value, &mState); }
    bool hasAspEnable() override { return has(mAspEnable); }
    bool getAspEnable(bool *value) override { return get(value, &mAspEnable); }
    bool setAspEnable(bool value) override { return set(value, &mAspEnable); }
    bool setGpioFallIndex(uint32_t value) override { return set(value, &mGpioFallIndex); }
    bool setGpioFallScale(uint32_t value) override { return set(value, &mGpioFallScale); }
    bool setGpioRiseIndex(uint32_t value) override { return set(value, &mGpioRiseIndex); }
    bool setGpioRiseScale(uint32_t value) override { return set(value, &mGpioRiseScale); }
    bool pollVibeState(uint32_t value, int32_t timeoutMs) override {
        return poll(value, &mVibeState, timeoutMs);
    }
    bool setClabEnable(bool value) override { return set(value, &mClabEnable); }
    bool getAvailablePwleSegments(uint32_t *value) override {
        return get(value, &mAvailablePwleSegments);
    }
    bool hasPwle() override { return has(mPwle); }
    bool setPwle(std::string value) override { return set(value, &mPwle); }
    bool setPwleRampDown(uint32_t value) override { return set(value, &mPwleRampDown); }
    void debug(int fd) override { HwApiBase::debug(fd); }

  private:
    std::ofstream mF0;
    std::ofstream mF0Offset;
    std::ofstream mRedc;
    std::ofstream mQ;
    std::ofstream mActivate;
    std::ofstream mDuration;
    std::ifstream mEffectCount;
    std::ifstream mEffectDuration;
    std::ofstream mEffectIndex;
    std::ofstream mEffectQueue;
    std::ofstream mEffectScale;
    std::ofstream mGlobalScale;
    std::ofstream mState;
    std::fstream mAspEnable;
    std::ofstream mGpioFallIndex;
    std::ofstream mGpioFallScale;
    std::ofstream mGpioRiseIndex;
    std::ofstream mGpioRiseScale;
    std::ifstream mVibeState;
    std::ofstream mClabEnable;
    std::ifstream mAvailablePwleSegments;
    std::ofstream mPwle;
    std::ofstream mPwleRampDown;
};

class HwCal : public Vibrator::HwCal, private HwCalBase {
  private:
    static constexpr char VERSION[] = "version";
    static constexpr char F0_CONFIG[] = "f0_measured";
    static constexpr char REDC_CONFIG[] = "redc_measured";
    static constexpr char Q_CONFIG[] = "q_measured";
    static constexpr char Q_INDEX[] = "q_index";
    static constexpr char VOLTAGES_CONFIG[] = "v_levels";
    static constexpr char TICK_VOLTAGES_CONFIG[] = "v_tick";
    static constexpr char CLICK_VOLTAGES_CONFIG[] = "v_click";
    static constexpr char LONG_VOLTAGES_CONFIG[] = "v_long";

    static constexpr uint32_t Q_FLOAT_TO_FIXED = 1 << 16;
    static constexpr float Q_INDEX_TO_FLOAT = 1.5f;
    static constexpr uint32_t Q_INDEX_TO_FIXED = Q_INDEX_TO_FLOAT * Q_FLOAT_TO_FIXED;
    static constexpr uint32_t Q_INDEX_OFFSET = 2.0f * Q_FLOAT_TO_FIXED;

    static constexpr uint32_t VERSION_DEFAULT = 1;
    static constexpr int32_t DEFAULT_FREQUENCY_SHIFT = 0;
    static constexpr float DEFAULT_DEVICE_MASS = 0.21;
    static constexpr float DEFAULT_LOC_COEFF = 0.5;
    static constexpr uint32_t Q_DEFAULT = 15.5 * Q_FLOAT_TO_FIXED;
    static constexpr std::array<uint32_t, 6> V_LEVELS_DEFAULT = {60, 70, 80, 90, 100, 76};
    static constexpr std::array<uint32_t, 2> V_TICK_DEFAULT = {10, 70};
    static constexpr std::array<uint32_t, 2> V_CTICK_DEFAULT = {10, 70};
    static constexpr std::array<uint32_t, 2> V_LONG_DEFAULT = {10, 70};

  public:
    HwCal() {}

    bool getVersion(uint32_t *value) override {
        if (getPersist(VERSION, value)) {
            return true;
        }
        *value = VERSION_DEFAULT;
        return true;
    }
    bool getLongFrequencyShift(int32_t *value) override {
        return getProperty("long.frequency.shift", value, DEFAULT_FREQUENCY_SHIFT);
    }
    bool getDeviceMass(float *value) override {
        return getProperty("device.mass", value, DEFAULT_DEVICE_MASS);
    }
    bool getLocCoeff(float *value) override {
        return getProperty("loc.coeff", value, DEFAULT_LOC_COEFF);
    }
    bool getF0(uint32_t *value) override { return getPersist(F0_CONFIG, value); }
    bool getRedc(uint32_t *value) override { return getPersist(REDC_CONFIG, value); }
    bool getQ(uint32_t *value) override {
        if (getPersist(Q_CONFIG, value)) {
            return true;
        }
        if (getPersist(Q_INDEX, value)) {
            *value = *value * Q_INDEX_TO_FIXED + Q_INDEX_OFFSET;
            return true;
        }
        *value = Q_DEFAULT;
        return true;
    }
    bool getVolLevels(std::array<uint32_t, 6> *value) override {
        if (getPersist(VOLTAGES_CONFIG, value)) {
            return true;
        }
        *value = V_LEVELS_DEFAULT;
        return true;
    }
    bool getTickVolLevels(std::array<uint32_t, 2> *value) override {
        if (getPersist(TICK_VOLTAGES_CONFIG, value)) {
            return true;
        }
        *value = V_TICK_DEFAULT;
        return true;
    }
    bool getClickVolLevels(std::array<uint32_t, 2> *value) override {
        if (getPersist(CLICK_VOLTAGES_CONFIG, value)) {
            return true;
        }
        *value = V_CTICK_DEFAULT;
        return true;
    }
    bool getLongVolLevels(std::array<uint32_t, 2> *value) override {
        if (getPersist(LONG_VOLTAGES_CONFIG, value)) {
            return true;
        }
        *value = V_LONG_DEFAULT;
        return true;
    }
    bool isChirpEnabled() override {
        bool value;
        getProperty("chirp.enabled", &value, false);
        return value;
    }
    void debug(int fd) override { HwCalBase::debug(fd); }
};

}  // namespace vibrator
}  // namespace hardware
}  // namespace android
}  // namespace aidl