2018-05-02 07:42:53 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 The Android Open Source Project
|
2020-04-14 17:05:38 -04:00
|
|
|
* Copyright (C) 2020 The LineageOS Project
|
2018-05-02 07:42:53 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2019-08-06 14:05:02 -04:00
|
|
|
#define LOG_TAG "android.hardware.light@2.0-service.xiaomi_sdm660"
|
2018-05-02 07:42:53 -04:00
|
|
|
|
2020-04-14 03:29:09 -04:00
|
|
|
#include "Light.h"
|
2019-08-06 15:43:10 -04:00
|
|
|
|
2020-04-14 17:05:38 -04:00
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <android-base/logging.h>
|
2020-02-06 11:04:57 -05:00
|
|
|
#include <unistd.h>
|
2018-05-02 07:42:53 -04:00
|
|
|
|
2020-04-14 17:05:38 -04:00
|
|
|
#include <iomanip>
|
2019-08-06 15:43:10 -04:00
|
|
|
|
2020-04-14 17:05:38 -04:00
|
|
|
namespace {
|
2018-07-30 09:44:18 -04:00
|
|
|
|
2020-04-14 17:05:38 -04:00
|
|
|
#define LEDS "/sys/class/leds/"
|
|
|
|
#define LCD_LED LEDS "lcd-backlight/"
|
|
|
|
#define WHITE LEDS "white/"
|
2020-04-15 04:08:29 -04:00
|
|
|
#define RED LEDS "red/"
|
2020-02-06 11:04:57 -05:00
|
|
|
#define BUTTON LEDS "button-backlight/"
|
|
|
|
#define BUTTON1 LEDS "button-backlight1/"
|
2020-04-14 17:05:38 -04:00
|
|
|
#define BRIGHTNESS "brightness"
|
|
|
|
#define MAX_BRIGHTNESS "max_brightness"
|
|
|
|
#define BLINK "blink"
|
|
|
|
#define DUTY_PCTS "duty_pcts"
|
|
|
|
#define PAUSE_HI "pause_hi"
|
|
|
|
#define PAUSE_LO "pause_lo"
|
|
|
|
#define RAMP_STEP_MS "ramp_step_ms"
|
|
|
|
#define START_IDX "start_idx"
|
2018-05-02 07:42:53 -04:00
|
|
|
|
2020-04-14 17:05:38 -04:00
|
|
|
using ::android::base::ReadFileToString;
|
|
|
|
using ::android::base::WriteStringToFile;
|
|
|
|
|
|
|
|
// Default max brightness
|
|
|
|
constexpr auto kDefaultMaxLedBrightness = 255;
|
|
|
|
constexpr auto kDefaultMaxScreenBrightness = 4095;
|
|
|
|
|
2020-01-08 15:58:22 -05:00
|
|
|
// Each step will stay on for 150ms by default.
|
|
|
|
constexpr auto kRampStepDuration = 150;
|
2020-04-14 17:05:38 -04:00
|
|
|
|
|
|
|
// Each value represents a duty percent (0 - 100) for the led pwm.
|
|
|
|
constexpr std::array kBrightnessRamp = {0, 12, 25, 37, 50, 72, 85, 100, 85, 72, 50, 37, 25, 12, 0};
|
|
|
|
|
|
|
|
// Write value to path and close file.
|
|
|
|
bool WriteToFile(const std::string& path, uint32_t content) {
|
|
|
|
return WriteStringToFile(std::to_string(content), path);
|
2018-05-02 07:42:53 -04:00
|
|
|
}
|
|
|
|
|
2020-04-14 17:05:38 -04:00
|
|
|
bool WriteToFile(const std::string& path, const std::string& content) {
|
|
|
|
return WriteStringToFile(content, path);
|
2018-05-02 07:42:53 -04:00
|
|
|
}
|
|
|
|
|
2020-04-14 17:05:38 -04:00
|
|
|
uint32_t RgbaToBrightness(uint32_t color) {
|
|
|
|
// Extract brightness from AARRGGBB.
|
|
|
|
uint32_t alpha = (color >> 24) & 0xFF;
|
2019-08-06 15:43:10 -04:00
|
|
|
|
2020-04-14 17:05:38 -04:00
|
|
|
// Retrieve each of the RGB colors
|
|
|
|
uint32_t red = (color >> 16) & 0xFF;
|
|
|
|
uint32_t green = (color >> 8) & 0xFF;
|
|
|
|
uint32_t blue = color & 0xFF;
|
2019-08-06 15:43:10 -04:00
|
|
|
|
2020-04-14 17:05:38 -04:00
|
|
|
// Scale RGB colors if a brightness has been applied by the user
|
2019-08-06 15:43:10 -04:00
|
|
|
if (alpha != 0xFF) {
|
|
|
|
red = red * alpha / 0xFF;
|
|
|
|
green = green * alpha / 0xFF;
|
|
|
|
blue = blue * alpha / 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (77 * red + 150 * green + 29 * blue) >> 8;
|
|
|
|
}
|
|
|
|
|
2020-04-14 17:05:38 -04:00
|
|
|
inline uint32_t RgbaToBrightness(uint32_t color, uint32_t max_brightness) {
|
|
|
|
return RgbaToBrightness(color) * max_brightness / 0xFF;
|
|
|
|
}
|
|
|
|
|
2018-07-30 09:44:18 -04:00
|
|
|
/*
|
|
|
|
* Scale each value of the brightness ramp according to the
|
|
|
|
* brightness of the color.
|
|
|
|
*/
|
2020-04-14 17:05:38 -04:00
|
|
|
std::string GetScaledDutyPcts(uint32_t brightness) {
|
|
|
|
std::stringstream ramp;
|
2018-07-30 09:44:18 -04:00
|
|
|
|
2020-04-14 17:05:38 -04:00
|
|
|
for (size_t i = 0; i < kBrightnessRamp.size(); i++) {
|
|
|
|
if (i > 0) ramp << ",";
|
|
|
|
ramp << kBrightnessRamp[i] * brightness / 0xFF;
|
2018-07-30 09:44:18 -04:00
|
|
|
}
|
|
|
|
|
2020-04-14 17:05:38 -04:00
|
|
|
return ramp.str();
|
2018-07-30 09:44:18 -04:00
|
|
|
}
|
|
|
|
|
2020-04-14 17:05:38 -04:00
|
|
|
inline bool IsLit(uint32_t color) {
|
|
|
|
return color & 0x00ffffff;
|
2020-04-14 03:29:09 -04:00
|
|
|
}
|
|
|
|
|
2020-04-14 17:05:38 -04:00
|
|
|
} // anonymous namespace
|
2018-05-02 07:42:53 -04:00
|
|
|
|
2020-04-14 17:05:38 -04:00
|
|
|
namespace android {
|
|
|
|
namespace hardware {
|
|
|
|
namespace light {
|
|
|
|
namespace V2_0 {
|
|
|
|
namespace implementation {
|
2018-07-30 09:44:18 -04:00
|
|
|
|
2020-04-14 17:05:38 -04:00
|
|
|
Light::Light() {
|
|
|
|
std::string buf;
|
2018-05-02 07:42:53 -04:00
|
|
|
|
2020-04-14 17:05:38 -04:00
|
|
|
if (ReadFileToString(LCD_LED MAX_BRIGHTNESS, &buf)) {
|
|
|
|
max_screen_brightness_ = std::stoi(buf);
|
2018-05-02 07:42:53 -04:00
|
|
|
} else {
|
2020-04-14 17:05:38 -04:00
|
|
|
max_screen_brightness_ = kDefaultMaxScreenBrightness;
|
|
|
|
LOG(ERROR) << "Failed to read max screen brightness, fallback to "
|
|
|
|
<< kDefaultMaxLedBrightness;
|
2018-05-02 07:42:53 -04:00
|
|
|
}
|
2018-07-30 09:44:18 -04:00
|
|
|
|
2020-04-14 17:05:38 -04:00
|
|
|
if (ReadFileToString(WHITE MAX_BRIGHTNESS, &buf)) {
|
|
|
|
max_led_brightness_ = std::stoi(buf);
|
|
|
|
} else {
|
|
|
|
max_led_brightness_ = kDefaultMaxLedBrightness;
|
2020-04-15 04:08:29 -04:00
|
|
|
LOG(ERROR) << "Failed to read max white LED brightness, fallback to " << kDefaultMaxLedBrightness;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ReadFileToString(RED MAX_BRIGHTNESS, &buf)) {
|
|
|
|
max_red_led_brightness_= std::stoi(buf);
|
|
|
|
} else {
|
|
|
|
max_red_led_brightness_ = kDefaultMaxLedBrightness;
|
|
|
|
LOG(ERROR) << "Failed to read max red LED brightness, fallback to " << kDefaultMaxLedBrightness;
|
2020-04-14 03:29:09 -04:00
|
|
|
}
|
2020-02-06 11:04:57 -05:00
|
|
|
|
|
|
|
if (!access(BUTTON BRIGHTNESS, W_OK)) {
|
|
|
|
lights_.emplace(std::make_pair(Type::BUTTONS,
|
|
|
|
[this](auto&&... args) { setLightButtons(args...); }));
|
|
|
|
buttons_.emplace_back(BUTTON BRIGHTNESS);
|
|
|
|
|
|
|
|
if (!access(BUTTON1 BRIGHTNESS, W_OK)) {
|
|
|
|
buttons_.emplace_back(BUTTON1 BRIGHTNESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ReadFileToString(BUTTON MAX_BRIGHTNESS, &buf)) {
|
|
|
|
max_button_brightness_ = std::stoi(buf);
|
|
|
|
} else {
|
|
|
|
max_button_brightness_ = kDefaultMaxLedBrightness;
|
|
|
|
LOG(ERROR) << "Failed to read max button brightness, fallback to "
|
|
|
|
<< kDefaultMaxLedBrightness;
|
|
|
|
}
|
|
|
|
}
|
2020-04-14 03:29:09 -04:00
|
|
|
}
|
2018-05-02 07:42:53 -04:00
|
|
|
|
2020-04-14 03:29:09 -04:00
|
|
|
Return<Status> Light::setLight(Type type, const LightState& state) {
|
2020-04-14 17:05:38 -04:00
|
|
|
auto it = lights_.find(type);
|
2018-05-02 07:42:53 -04:00
|
|
|
|
2020-04-14 17:05:38 -04:00
|
|
|
if (it == lights_.end()) {
|
2020-04-14 03:29:09 -04:00
|
|
|
return Status::LIGHT_NOT_SUPPORTED;
|
2019-08-06 15:43:10 -04:00
|
|
|
}
|
|
|
|
|
2020-04-14 03:29:09 -04:00
|
|
|
it->second(type, state);
|
2020-04-14 17:05:38 -04:00
|
|
|
|
2018-05-02 07:42:53 -04:00
|
|
|
return Status::SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
Return<void> Light::getSupportedTypes(getSupportedTypes_cb _hidl_cb) {
|
|
|
|
std::vector<Type> types;
|
|
|
|
|
2020-04-14 17:05:38 -04:00
|
|
|
for (auto&& light : lights_) types.emplace_back(light.first);
|
2018-05-02 07:42:53 -04:00
|
|
|
|
|
|
|
_hidl_cb(types);
|
|
|
|
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2020-04-14 17:05:38 -04:00
|
|
|
void Light::setLightBacklight(Type /*type*/, const LightState& state) {
|
|
|
|
uint32_t brightness = RgbaToBrightness(state.color, max_screen_brightness_);
|
|
|
|
WriteToFile(LCD_LED BRIGHTNESS, brightness);
|
|
|
|
}
|
|
|
|
|
2020-02-06 11:04:57 -05:00
|
|
|
void Light::setLightButtons(Type /*type*/, const LightState& state) {
|
|
|
|
uint32_t brightness = RgbaToBrightness(state.color, max_button_brightness_);
|
|
|
|
for (auto&& button : buttons_) {
|
|
|
|
WriteToFile(button, brightness);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 17:05:38 -04:00
|
|
|
void Light::setLightNotification(Type type, const LightState& state) {
|
|
|
|
bool found = false;
|
|
|
|
for (auto&& [cur_type, cur_state] : notif_states_) {
|
|
|
|
if (cur_type == type) {
|
|
|
|
cur_state = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback to battery light
|
|
|
|
if (!found && (cur_type == Type::BATTERY || IsLit(state.color))) {
|
|
|
|
found = true;
|
|
|
|
LOG(DEBUG) << __func__ << ": type=" << toString(cur_type);
|
|
|
|
applyNotificationState(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Light::applyNotificationState(const LightState& state) {
|
|
|
|
uint32_t white_brightness = RgbaToBrightness(state.color, max_led_brightness_);
|
2020-04-15 04:08:29 -04:00
|
|
|
uint32_t red_brightness = RgbaToBrightness(state.color, max_red_led_brightness_);
|
2020-04-14 17:05:38 -04:00
|
|
|
|
|
|
|
// Turn off the leds (initially)
|
|
|
|
WriteToFile(WHITE BLINK, 0);
|
2020-04-15 04:08:29 -04:00
|
|
|
WriteToFile(RED BLINK, 0);
|
2020-04-14 17:05:38 -04:00
|
|
|
|
|
|
|
if (state.flashMode == Flash::TIMED && state.flashOnMs > 0 && state.flashOffMs > 0) {
|
|
|
|
/*
|
|
|
|
* If the flashOnMs duration is not long enough to fit ramping up
|
|
|
|
* and down at the default step duration, step duration is modified
|
|
|
|
* to fit.
|
|
|
|
*/
|
|
|
|
int32_t step_duration = kRampStepDuration;
|
|
|
|
int32_t pause_hi = state.flashOnMs - (step_duration * kBrightnessRamp.size() * 2);
|
|
|
|
if (pause_hi < 0) {
|
|
|
|
step_duration = state.flashOnMs / (kBrightnessRamp.size() * 2);
|
|
|
|
pause_hi = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(DEBUG) << __func__ << ": color=" << std::hex << state.color << std::dec
|
|
|
|
<< " onMs=" << state.flashOnMs << " offMs=" << state.flashOffMs;
|
|
|
|
|
|
|
|
// White
|
|
|
|
WriteToFile(WHITE START_IDX, 0);
|
|
|
|
WriteToFile(WHITE DUTY_PCTS, GetScaledDutyPcts(white_brightness));
|
|
|
|
WriteToFile(WHITE PAUSE_LO, static_cast<uint32_t>(state.flashOffMs));
|
|
|
|
WriteToFile(WHITE PAUSE_HI, static_cast<uint32_t>(pause_hi));
|
|
|
|
WriteToFile(WHITE RAMP_STEP_MS, static_cast<uint32_t>(step_duration));
|
|
|
|
WriteToFile(WHITE BLINK, 1);
|
2020-04-15 04:08:29 -04:00
|
|
|
|
|
|
|
// Red
|
|
|
|
WriteToFile(RED START_IDX, 0);
|
|
|
|
WriteToFile(RED DUTY_PCTS, GetScaledDutyPcts(red_brightness));
|
|
|
|
WriteToFile(RED PAUSE_LO, static_cast<uint32_t>(state.flashOffMs));
|
|
|
|
WriteToFile(RED PAUSE_HI, static_cast<uint32_t>(pause_hi));
|
|
|
|
WriteToFile(RED RAMP_STEP_MS, static_cast<uint32_t>(step_duration));
|
|
|
|
WriteToFile(RED BLINK, 1);
|
2020-04-14 17:05:38 -04:00
|
|
|
} else {
|
|
|
|
WriteToFile(WHITE BRIGHTNESS, white_brightness);
|
2020-04-15 04:08:29 -04:00
|
|
|
WriteToFile(RED BRIGHTNESS, red_brightness);
|
2020-04-14 17:05:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-02 07:42:53 -04:00
|
|
|
} // namespace implementation
|
|
|
|
} // namespace V2_0
|
|
|
|
} // namespace light
|
|
|
|
} // namespace hardware
|
|
|
|
} // namespace android
|