aidl: light: Make RGB utils rgb_t methods

Change-Id: Iaaa34987c7670077214078169ddeb756a794403b
This commit is contained in:
Sebastiano Barezzi 2022-09-12 21:00:23 +02:00 committed by althafvly
parent 646127bbc9
commit 627e6ad184
3 changed files with 27 additions and 30 deletions

View File

@ -74,15 +74,18 @@ Lights::Lights() {
}
ndk::ScopedAStatus Lights::setLightState(int32_t id, const HwLightState& state) {
rgb_t color(state.color);
rgb_t batteryStateColor;
LightType type = static_cast<LightType>(id);
switch (type) {
case LightType::BACKLIGHT:
if (!mBacklightPath.empty())
writeToFile(mBacklightPath, colorToBrightness(state.color));
writeToFile(mBacklightPath, color.toBrightness());
break;
case LightType::BUTTONS:
for (auto& buttons : mButtonsPaths)
writeToFile(buttons, isLit(state.color));
writeToFile(buttons, color.isLit());
break;
case LightType::BATTERY:
case LightType::NOTIFICATIONS:
@ -91,7 +94,8 @@ ndk::ScopedAStatus Lights::setLightState(int32_t id, const HwLightState& state)
mLastBatteryState = state;
else
mLastNotificationState = state;
setLED(isLit(mLastBatteryState.color) ? mLastBatteryState : mLastNotificationState);
batteryStateColor = rgb_t(mLastBatteryState.color);
setLED(batteryStateColor.isLit() ? mLastBatteryState : mLastNotificationState);
mLEDMutex.unlock();
break;
default:
@ -111,7 +115,7 @@ ndk::ScopedAStatus Lights::getLights(std::vector<HwLight> *_aidl_return) {
void Lights::setLED(const HwLightState& state) {
bool rc = true;
rgb_t color = colorToRgb(state.color);
rgb_t color(state.color);
uint8_t blink = (state.flashOnMs != 0 && state.flashOffMs != 0);
switch (state.flashMode) {
@ -132,7 +136,7 @@ void Lights::setLED(const HwLightState& state) {
FALLTHROUGH_INTENDED;
default:
if (mWhiteLED) {
rc = kLEDs[WHITE].setBrightness(colorToBrightness(state.color));
rc = kLEDs[WHITE].setBrightness(color.toBrightness());
} else {
rc = kLEDs[RED].setBrightness(color.red);
rc &= kLEDs[GREEN].setBrightness(color.green);

View File

@ -45,39 +45,29 @@ bool writeToFile(const std::string& file, uint32_t content) {
return writeToFile(file, std::to_string(content));
}
bool isLit(uint32_t color) {
return color & 0x00ffffff;
}
rgb_t colorToRgb(uint32_t color) {
rgb_t r;
rgb::rgb(uint32_t color) {
// Extract brightness from AARRGGBB.
uint8_t alpha = (color >> 24) & 0xFF;
// Retrieve each of the RGB colors
r.red = (color >> 16) & 0xFF;
r.green = (color >> 8) & 0xFF;
r.blue = color & 0xFF;
red = (color >> 16) & 0xFF;
green = (color >> 8) & 0xFF;
blue = color & 0xFF;
// Scale RGB colors if a brightness has been applied by the user
if (alpha > 0 && alpha < 255) {
r.red = r.red * alpha / 0xFF;
r.green = r.green * alpha / 0xFF;
r.blue = r.blue * alpha / 0xFF;
red = red * alpha / 0xFF;
green = green * alpha / 0xFF;
blue = blue * alpha / 0xFF;
}
return r;
}
uint8_t rgbToBrightness(rgb_t c_rgb) {
return (77 * c_rgb.red + 150 * c_rgb.green + 29 * c_rgb.blue) >> 8;
bool rgb::isLit() {
return !!red || !!green || !!blue;
}
uint8_t colorToBrightness(uint32_t color) {
rgb_t c_rgb = colorToRgb(color);
return rgbToBrightness(c_rgb);
uint8_t rgb::toBrightness() {
return (77 * red + 150 * green + 29 * blue) >> 8;
}
} // namespace light

View File

@ -14,19 +14,22 @@ namespace hardware {
namespace light {
typedef struct rgb {
rgb(uint8_t r, uint8_t g, uint8_t b) : red(r), green(g), blue(b) {};
rgb(uint32_t color);
rgb() : red(0), green(0), blue(0) {};
uint8_t red;
uint8_t green;
uint8_t blue;
bool isLit();
uint8_t toBrightness();
} rgb_t;
bool fileWriteable(const std::string& file);
bool readFromFile(const std::string& file, std::string *content);
bool readFromFile(const std::string& file, uint32_t *content);
bool writeToFile(const std::string& file, uint32_t content);
bool isLit(uint32_t color);
rgb_t colorToRgb(uint32_t color);
uint8_t rgbToBrightness(rgb_t c_rgb);
uint8_t colorToBrightness(uint32_t color);
} // namespace light
} // namespace hardware