From 627e6ad184973b26051b5f00e1fcfee6eca28267 Mon Sep 17 00:00:00 2001 From: Sebastiano Barezzi Date: Mon, 12 Sep 2022 21:00:23 +0200 Subject: [PATCH] aidl: light: Make RGB utils rgb_t methods Change-Id: Iaaa34987c7670077214078169ddeb756a794403b --- aidl/light/Lights.cpp | 14 +++++++++----- aidl/light/Utils.cpp | 32 +++++++++++--------------------- aidl/light/Utils.h | 11 +++++++---- 3 files changed, 27 insertions(+), 30 deletions(-) diff --git a/aidl/light/Lights.cpp b/aidl/light/Lights.cpp index 4b014de..d2a6d3e 100644 --- a/aidl/light/Lights.cpp +++ b/aidl/light/Lights.cpp @@ -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(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 *_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); diff --git a/aidl/light/Utils.cpp b/aidl/light/Utils.cpp index 10e3e96..68fdff5 100644 --- a/aidl/light/Utils.cpp +++ b/aidl/light/Utils.cpp @@ -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 diff --git a/aidl/light/Utils.h b/aidl/light/Utils.h index 8d0011e..7f1dc0c 100644 --- a/aidl/light/Utils.h +++ b/aidl/light/Utils.h @@ -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