You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
190 lines
5.4 KiB
190 lines
5.4 KiB
6 years ago
|
/*
|
||
3 years ago
|
* Copyright (C) 2021 The LineageOS Project
|
||
6 years ago
|
*
|
||
3 years ago
|
* SPDX-License-Identifier: Apache-2.0
|
||
6 years ago
|
*/
|
||
3 years ago
|
|
||
|
#define LOG_TAG "android.hardware.lights-service.samsung"
|
||
6 years ago
|
|
||
5 years ago
|
#include <android-base/stringprintf.h>
|
||
3 years ago
|
#include <fstream>
|
||
6 years ago
|
|
||
3 years ago
|
#include "Lights.h"
|
||
6 years ago
|
|
||
|
#define COLOR_MASK 0x00ffffff
|
||
|
#define MAX_INPUT_BRIGHTNESS 255
|
||
|
|
||
3 years ago
|
namespace aidl {
|
||
6 years ago
|
namespace android {
|
||
|
namespace hardware {
|
||
|
namespace light {
|
||
|
|
||
|
/*
|
||
|
* Write value to path and close file.
|
||
|
*/
|
||
|
template <typename T>
|
||
|
static void set(const std::string& path, const T& value) {
|
||
|
std::ofstream file(path);
|
||
|
file << value << std::endl;
|
||
|
}
|
||
|
|
||
|
template <typename T>
|
||
|
static T get(const std::string& path, const T& def) {
|
||
|
std::ifstream file(path);
|
||
|
T result;
|
||
|
|
||
|
file >> result;
|
||
|
return file.fail() ? def : result;
|
||
|
}
|
||
|
|
||
3 years ago
|
Lights::Lights() {
|
||
|
mLights.emplace(LightType::BACKLIGHT,
|
||
|
std::bind(&Lights::handleBacklight, this, std::placeholders::_1));
|
||
6 years ago
|
#ifdef BUTTON_BRIGHTNESS_NODE
|
||
3 years ago
|
mLights.emplace(LightType::BUTTONS, std::bind(&Lights::handleButtons, this, std::placeholders::_1));
|
||
5 years ago
|
#endif /* BUTTON_BRIGHTNESS_NODE */
|
||
|
#ifdef LED_BLINK_NODE
|
||
3 years ago
|
mLights.emplace(LightType::BATTERY, std::bind(&Lights::handleBattery, this, std::placeholders::_1));
|
||
|
mLights.emplace(LightType::NOTIFICATIONS,
|
||
|
std::bind(&Lights::handleNotifications, this, std::placeholders::_1));
|
||
|
mLights.emplace(LightType::ATTENTION,
|
||
|
std::bind(&Lights::handleAttention, this, std::placeholders::_1));
|
||
5 years ago
|
#endif /* LED_BLINK_NODE */
|
||
6 years ago
|
}
|
||
|
|
||
3 years ago
|
ndk::ScopedAStatus Lights::setLightState(int32_t id, const HwLightState& state) {
|
||
|
LightType type = static_cast<LightType>(id);
|
||
6 years ago
|
auto it = mLights.find(type);
|
||
|
|
||
|
if (it == mLights.end()) {
|
||
3 years ago
|
return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
|
||
6 years ago
|
}
|
||
|
|
||
|
/*
|
||
|
* Lock global mutex until light state is updated.
|
||
|
*/
|
||
|
std::lock_guard<std::mutex> lock(mLock);
|
||
|
|
||
|
it->second(state);
|
||
|
|
||
3 years ago
|
return ndk::ScopedAStatus::ok();
|
||
6 years ago
|
}
|
||
|
|
||
3 years ago
|
void Lights::handleBacklight(const HwLightState& state) {
|
||
6 years ago
|
uint32_t max_brightness = get(PANEL_MAX_BRIGHTNESS_NODE, MAX_INPUT_BRIGHTNESS);
|
||
|
uint32_t brightness = rgbToBrightness(state);
|
||
|
|
||
|
if (max_brightness != MAX_INPUT_BRIGHTNESS) {
|
||
|
brightness = brightness * max_brightness / MAX_INPUT_BRIGHTNESS;
|
||
|
}
|
||
|
|
||
|
set(PANEL_BRIGHTNESS_NODE, brightness);
|
||
|
}
|
||
|
|
||
|
#ifdef BUTTON_BRIGHTNESS_NODE
|
||
3 years ago
|
void Lights::handleButtons(const HwLightState& state) {
|
||
6 years ago
|
#ifdef VAR_BUTTON_BRIGHTNESS
|
||
|
uint32_t brightness = rgbToBrightness(state);
|
||
|
#else
|
||
|
uint32_t brightness = (state.color & COLOR_MASK) ? 1 : 0;
|
||
|
#endif
|
||
|
|
||
|
set(BUTTON_BRIGHTNESS_NODE, brightness);
|
||
|
}
|
||
|
#endif
|
||
|
|
||
5 years ago
|
#ifdef LED_BLINK_NODE
|
||
3 years ago
|
void Lights::handleBattery(const HwLightState& state) {
|
||
6 years ago
|
mBatteryState = state;
|
||
|
setNotificationLED();
|
||
|
}
|
||
|
|
||
3 years ago
|
void Lights::handleNotifications(const HwLightState& state) {
|
||
6 years ago
|
mNotificationState = state;
|
||
|
setNotificationLED();
|
||
|
}
|
||
|
|
||
3 years ago
|
void Lights::handleAttention(const HwLightState& state) {
|
||
6 years ago
|
mAttentionState = state;
|
||
|
setNotificationLED();
|
||
|
}
|
||
|
|
||
3 years ago
|
void Lights::setNotificationLED() {
|
||
6 years ago
|
int32_t adjusted_brightness = MAX_INPUT_BRIGHTNESS;
|
||
3 years ago
|
HwLightState state;
|
||
6 years ago
|
#ifdef LED_BLN_NODE
|
||
|
bool bln = false;
|
||
5 years ago
|
#endif /* LED_BLN_NODE */
|
||
6 years ago
|
|
||
5 years ago
|
if (mNotificationState.color & COLOR_MASK) {
|
||
6 years ago
|
adjusted_brightness = LED_BRIGHTNESS_NOTIFICATION;
|
||
|
state = mNotificationState;
|
||
|
#ifdef LED_BLN_NODE
|
||
|
bln = true;
|
||
5 years ago
|
#endif /* LED_BLN_NODE */
|
||
6 years ago
|
} else if (mAttentionState.color & COLOR_MASK) {
|
||
|
adjusted_brightness = LED_BRIGHTNESS_ATTENTION;
|
||
|
state = mAttentionState;
|
||
3 years ago
|
if (state.flashMode == FlashMode::HARDWARE) {
|
||
|
if (state.flashOnMs > 0 && state.flashOffMs == 0) state.flashMode = FlashMode::NONE;
|
||
6 years ago
|
state.color = 0x000000ff;
|
||
|
}
|
||
3 years ago
|
if (state.flashMode == FlashMode::NONE) {
|
||
6 years ago
|
state.color = 0;
|
||
|
}
|
||
5 years ago
|
} else if (mBatteryState.color & COLOR_MASK) {
|
||
|
adjusted_brightness = LED_BRIGHTNESS_BATTERY;
|
||
|
state = mBatteryState;
|
||
6 years ago
|
} else {
|
||
|
set(LED_BLINK_NODE, "0x00000000 0 0");
|
||
|
return;
|
||
|
}
|
||
|
|
||
3 years ago
|
if (state.flashMode == FlashMode::NONE) {
|
||
6 years ago
|
state.flashOnMs = 0;
|
||
|
state.flashOffMs = 0;
|
||
|
}
|
||
|
|
||
|
state.color = calibrateColor(state.color & COLOR_MASK, adjusted_brightness);
|
||
3 years ago
|
set(LED_BLINK_NODE, ::android::base::StringPrintf("0x%08x %d %d", state.color, state.flashOnMs,
|
||
5 years ago
|
state.flashOffMs));
|
||
6 years ago
|
|
||
|
#ifdef LED_BLN_NODE
|
||
|
if (bln) {
|
||
|
set(LED_BLN_NODE, (state.color & COLOR_MASK) ? 1 : 0);
|
||
|
}
|
||
5 years ago
|
#endif /* LED_BLN_NODE */
|
||
|
}
|
||
|
|
||
3 years ago
|
uint32_t Lights::calibrateColor(uint32_t color, int32_t brightness) {
|
||
5 years ago
|
uint32_t red = ((color >> 16) & 0xFF) * LED_ADJUSTMENT_R;
|
||
|
uint32_t green = ((color >> 8) & 0xFF) * LED_ADJUSTMENT_G;
|
||
|
uint32_t blue = (color & 0xFF) * LED_ADJUSTMENT_B;
|
||
|
|
||
|
return (((red * brightness) / 255) << 16) + (((green * brightness) / 255) << 8) +
|
||
|
((blue * brightness) / 255);
|
||
6 years ago
|
}
|
||
5 years ago
|
#endif /* LED_BLINK_NODE */
|
||
6 years ago
|
|
||
3 years ago
|
#define AutoHwLight(light) {.id = (int32_t)light, .type = light, .ordinal = 0}
|
||
6 years ago
|
|
||
3 years ago
|
ndk::ScopedAStatus Lights::getLights(std::vector<HwLight> *_aidl_return) {
|
||
6 years ago
|
for (auto const& light : mLights) {
|
||
3 years ago
|
_aidl_return->push_back(AutoHwLight(light.first));
|
||
6 years ago
|
}
|
||
|
|
||
3 years ago
|
return ndk::ScopedAStatus::ok();
|
||
6 years ago
|
}
|
||
|
|
||
3 years ago
|
uint32_t Lights::rgbToBrightness(const HwLightState& state) {
|
||
6 years ago
|
uint32_t color = state.color & COLOR_MASK;
|
||
|
|
||
|
return ((77 * ((color >> 16) & 0xff)) + (150 * ((color >> 8) & 0xff)) + (29 * (color & 0xff))) >>
|
||
|
8;
|
||
|
}
|
||
|
|
||
3 years ago
|
} // namespace light
|
||
|
} // namespace hardware
|
||
|
} // namespace android
|
||
|
} // namespace aidl
|