-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathLedDevice.cpp
183 lines (152 loc) · 5.72 KB
/
LedDevice.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
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Copyright (C) 2021-2025 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "LedDevice.h"
#define LOG_TAG "LedDevice"
#include <android-base/logging.h>
#include <fstream>
#include <thread>
#include "Utils.h"
namespace aidl {
namespace android {
namespace hardware {
namespace light {
static const uint32_t kDefaultMaxBrightness = 255;
static const std::string kBaseLedsPath = "/sys/class/leds/";
static const std::string kBrightnessNode = "brightness";
static const std::string kMaxBrightnessNode = "max_brightness";
static const std::string kBreathNodes[] = {
"breath",
"blink",
};
static const std::string kBlinkNode = "blink";
static const std::string kStartIdxNode = "start_idx";
static const std::string kDutyPctsNode = "duty_pcts";
static const std::string kPauseLoNode = "pause_lo";
static const std::string kPauseHiNode = "pause_hi";
static const std::string kRampStepMsNode = "ramp_step_ms";
static const std::string kTriggerNode = "trigger";
static const std::string kDelayOffNode = "delay_off";
static const std::string kDelayOnNode = "delay_on";
static constexpr int kRampSteps = 8;
static constexpr int kRampMaxStepDurationMs = 50;
LedDevice::LedDevice(std::string name)
: mName(name), mIdx(0), mBasePath(kBaseLedsPath + name + "/") {
if (!readFromFile(mBasePath + kMaxBrightnessNode, mMaxBrightness)) {
mMaxBrightness = kDefaultMaxBrightness;
}
for (const auto& node : kBreathNodes) {
if (std::ifstream(mBasePath + node).good()) {
mBreathNode = node;
break;
}
}
mSupportsTimed = std::ifstream(mBasePath + kBlinkNode).good() &&
std::ifstream(mBasePath + kStartIdxNode).good() &&
std::ifstream(mBasePath + kDutyPctsNode).good() &&
std::ifstream(mBasePath + kPauseLoNode).good() &&
std::ifstream(mBasePath + kPauseHiNode).good() &&
std::ifstream(mBasePath + kRampStepMsNode).good();
}
std::string LedDevice::getName() const {
return mName;
}
bool LedDevice::supportsBreath() const {
return !mBreathNode.empty();
}
bool LedDevice::supportsTimed() const {
return mSupportsTimed;
}
bool LedDevice::exists() const {
return std::ifstream(mBasePath + kBrightnessNode).good();
}
static std::string getScaledDutyPercent(uint8_t brightness) {
std::string output;
for (int i = 0; i < kRampSteps; i++) {
if (i != 0) {
output += ",";
}
output += std::to_string(i * 100 * brightness / (0xFF * kRampSteps));
}
return output;
}
bool LedDevice::setBrightness(uint8_t value, LightMode mode, uint32_t flashOnMs,
uint32_t flashOffMs) {
bool ok = false;
// Disable current blinking
if (mSupportsTimed) {
writeToFile(mBasePath + kBlinkNode, 0);
} else {
writeToFile(mBasePath + kTriggerNode, "none");
}
if (supportsBreath()) {
writeToFile(mBasePath + mBreathNode, 0);
}
switch (mode) {
case LightMode::TIMED:
if (mSupportsTimed) {
int32_t stepDuration = kRampMaxStepDurationMs;
int32_t pauseLo = flashOffMs;
int32_t pauseHi = flashOnMs - (stepDuration * kRampSteps * 2);
if (pauseHi < 0) {
stepDuration = flashOnMs / (kRampSteps * 2);
pauseHi = 0;
}
return writeToFile(mBasePath + kStartIdxNode, mIdx * kRampSteps) &&
writeToFile(mBasePath + kDutyPctsNode, getScaledDutyPercent(value)) &&
writeToFile(mBasePath + kPauseLoNode, pauseLo) &&
writeToFile(mBasePath + kPauseHiNode, pauseHi) &&
writeToFile(mBasePath + kRampStepMsNode, stepDuration) &&
writeToFile(mBasePath + kBlinkNode, 1);
} else {
ok = writeToFile(mBasePath + kTriggerNode, "timer");
if (ok) {
using namespace std::chrono_literals;
auto retries = 20;
while (retries--) {
std::this_thread::sleep_for(2ms);
ok = writeToFile(mBasePath + kDelayOffNode, flashOffMs);
if (!ok) continue;
ok = writeToFile(mBasePath + kDelayOnNode, flashOnMs);
if (ok) break;
}
}
if (ok) return true;
}
// Fallthrough to breath mode if timed is not supported
FALLTHROUGH_INTENDED;
case LightMode::BREATH:
if (supportsBreath()) {
return writeToFile(mBasePath + mBreathNode, value > 0 ? 1 : 0);
break;
}
// Fallthrough to static mode if breath is not supported
FALLTHROUGH_INTENDED;
case LightMode::STATIC:
return writeToFile(mBasePath + kBrightnessNode, scaleBrightness(value, mMaxBrightness));
break;
default:
LOG(ERROR) << "Unknown mode: " << mode;
return false;
break;
}
}
void LedDevice::setIdx(int idx) {
mIdx = idx;
}
void LedDevice::dump(int fd) const {
dprintf(fd, "Name: %s", mName.c_str());
dprintf(fd, ", index: %d", mIdx);
dprintf(fd, ", exists: %d", exists());
dprintf(fd, ", base path: %s", mBasePath.c_str());
dprintf(fd, ", max brightness: %u", mMaxBrightness);
dprintf(fd, ", supports breath: %d", supportsBreath());
dprintf(fd, ", supports timed: %d", supportsTimed());
dprintf(fd, ", breath node: %s", mBreathNode.c_str());
}
} // namespace light
} // namespace hardware
} // namespace android
} // namespace aidl